@@ -40,12 +40,11 @@ namespace rviz_plugin_tutorials
40
40
41
41
DriveWidget::DriveWidget ( QWidget* parent )
42
42
: QWidget( parent )
43
+ , linear_velocity_( 0 )
44
+ , angular_velocity_( 0 )
43
45
, linear_max_( 10 )
44
46
, angular_max_( 2 )
45
47
{
46
- QSizePolicy policy = sizePolicy ();
47
- policy.setHeightForWidth ( true );
48
- setSizePolicy ( policy );
49
48
}
50
49
51
50
void DriveWidget::paintEvent ( QPaintEvent* event )
@@ -62,19 +61,21 @@ void DriveWidget::paintEvent( QPaintEvent* event )
62
61
background = Qt::lightGray;
63
62
crosshair = Qt::darkGray;
64
63
}
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
+
65
70
QPainter painter ( this );
66
71
painter.setBrush ( background );
67
- painter.drawRect ( rect () );
68
72
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 );
71
76
72
77
if ( isEnabled () && (angular_velocity_ != 0 || linear_velocity_ != 0 ))
73
78
{
74
- int w = width ();
75
- int h = height ();
76
- int size = ( w > h ) ? h : w;
77
-
78
79
QPen arrow;
79
80
arrow.setWidth ( size/20 );
80
81
arrow.setColor ( Qt::green );
@@ -168,8 +169,12 @@ void DriveWidget::leaveEvent( QEvent* event )
168
169
169
170
void DriveWidget::sendVelocitiesFromMouse ( int x, int y, int width, int height )
170
171
{
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_;
173
178
update ();
174
179
Q_EMIT outputVelocity ( linear_velocity_, angular_velocity_ );
175
180
}
0 commit comments