Skip to content

Commit d630f69

Browse files
committed
rviz_plugin_tutorials: fixed apparent drive_widget aspect ratio to 1:1.
1 parent 28f7098 commit d630f69

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

rviz_plugin_tutorials/src/drive_widget.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ namespace rviz_plugin_tutorials
4040

4141
DriveWidget::DriveWidget( QWidget* parent )
4242
: QWidget( parent )
43+
, linear_velocity_( 0 )
44+
, angular_velocity_( 0 )
4345
, linear_max_( 10 )
4446
, angular_max_( 2 )
4547
{
46-
QSizePolicy policy = sizePolicy();
47-
policy.setHeightForWidth( true );
48-
setSizePolicy( policy );
4948
}
5049

5150
void DriveWidget::paintEvent( QPaintEvent* event )
@@ -62,19 +61,21 @@ void DriveWidget::paintEvent( QPaintEvent* event )
6261
background = Qt::lightGray;
6362
crosshair = Qt::darkGray;
6463
}
64+
int w = width();
65+
int h = height();
66+
int size = (( w > h ) ? h : w) - 1;
67+
int hpad = ( w - size ) / 2;
68+
int vpad = ( h - size ) / 2;
69+
6570
QPainter painter( this );
6671
painter.setBrush( background );
67-
painter.drawRect( rect() );
6872
painter.setPen( crosshair );
69-
painter.drawLine( 0, height() / 2, width(), height() / 2 );
70-
painter.drawLine( width() / 2, 0, width() / 2, height() );
73+
painter.drawRect( QRect( hpad, vpad, size, size ));
74+
painter.drawLine( hpad, height() / 2, hpad + size, height() / 2 );
75+
painter.drawLine( width() / 2, vpad, width() / 2, vpad + size );
7176

7277
if( isEnabled() && (angular_velocity_ != 0 || linear_velocity_ != 0 ))
7378
{
74-
int w = width();
75-
int h = height();
76-
int size = ( w > h ) ? h : w;
77-
7879
QPen arrow;
7980
arrow.setWidth( size/20 );
8081
arrow.setColor( Qt::green );
@@ -168,8 +169,12 @@ void DriveWidget::leaveEvent( QEvent* event )
168169

169170
void DriveWidget::sendVelocitiesFromMouse( int x, int y, int width, int height )
170171
{
171-
linear_velocity_ = (1.0 - float( y ) / float( height / 2 )) * linear_max_;
172-
angular_velocity_ = (1.0 - float( x ) / float( width / 2 )) * angular_max_;
172+
int size = (( width > height ) ? height : width );
173+
int hpad = ( width - size ) / 2;
174+
int vpad = ( height - size ) / 2;
175+
176+
linear_velocity_ = (1.0 - float( y - vpad ) / float( size / 2 )) * linear_max_;
177+
angular_velocity_ = (1.0 - float( x - hpad ) / float( size / 2 )) * angular_max_;
173178
update();
174179
Q_EMIT outputVelocity( linear_velocity_, angular_velocity_ );
175180
}

rviz_plugin_tutorials/src/drive_widget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Q_OBJECT
4747
virtual void leaveEvent( QEvent* event );
4848

4949
virtual QSize sizeHint() const { return QSize( 150, 150 ); }
50-
virtual int heightForWidth( int width ) const { return width; } // enforce square widget
5150

5251
Q_SIGNALS:
5352
void outputVelocity( float linear, float angular );

0 commit comments

Comments
 (0)