JoystickControlWidget.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  *
19  * @package ArmarX::RobotAPI
20  * @author Raphael Grimm <raphael dot grimm at kit dot edu>
21  * @date 2014
22  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23  * GNU General Public License
24  */
25 
26 #pragma once
27 
28 #include <memory>
29 
30 //qt
31 #include <QGraphicsEllipseItem>
32 #include <QGraphicsScene>
33 #include <QGraphicsView>
34 #include <QMouseEvent>
35 #include <QPointF>
36 #include <QPointer>
37 #include <QResizeEvent>
38 #include <QWidget>
39 
40 namespace armarx
41 {
42  /**
43  * @brief Provides the coordinates of mouse events through signals.
44  * Used in JoystickControlWidget.
45  */
46  class JoystickControlWidgetQGraphicsView : public QGraphicsView
47  {
48  Q_OBJECT
49  public:
50  JoystickControlWidgetQGraphicsView(QGraphicsScene* scene, QWidget* parent = nullptr);
52 
53  public:
54  /**
55  * @brief Passes the mapped mouse coordinates of the event through the signal positionChanged.
56  * @param event Contains the coordinates
57  */
58  void mousePressEvent(QMouseEvent* event) override;
59 
60  /**
61  * @brief Passes the mapped mouse coordinates of the event through the signal positionChanged.
62  * @param event Contains the coordinates
63  */
64  void mouseMoveEvent(QMouseEvent* event) override;
65 
66  /**
67  * @brief Passes (0;0) through the signal positionChanged.
68  */
69  void mouseReleaseEvent(QMouseEvent*) override;
70  signals:
71  /**
72  * @brief Sends the mouse position on press or move in the scene coordinates.
73  * On mouse release (0;0) is send.
74  */
75  void positionChanged(QPointF);
76 
77  /**
78  * @brief Emitted when the mouse was released.
79  */
80  void released();
81 
82  /**
83  * @brief Emitted when the mouse was pressed.
84  */
85  void pressed();
86  }; //JoystickControlWidgetQGraphicsView
87 
88  /**
89  * @brief Provides a simple joystick control.
90  *
91  * The widget emits signals when the control is moved.
92  *
93  * The signal
94  * @code{.cpp}
95  * positionChanged(QPointF)
96  * @endcode
97  * passes the nibble's current position.
98  * The position is in the unit circle.
99  * The x-axis is horizontal and increases to the right.
100  * The y-axis is vertical and increases upwards.
101  *
102  * The signal
103  * @code{.cpp}
104  * rotationChanged(double)
105  * @endcode
106  * passes the position vectors rotation in polar coordinates (-pi,pi].
107  * The up position is 0.
108  * The down position is pi.
109  * The Quadrants 1 and 4 have positive value.
110  *
111  * If the constructor is called with useQuadrant3and4==true the control is the whole unit circle.
112  * If the constructor is called with useQuadrant3and4==false the control is the unit circle's upper semicircle. (y>=0)
113  *
114  * Possible positions can be influenced with
115  * \code{.cpp}
116  * setSteps(int steps);
117  * \endcode
118  * - steps>0 : only position vectors with a length from {n/steps | n in {0,1,...,steps} are valid. The nibble snaps to a valid position.
119  * - steps<=0: all positions in the unit circle are valid.
120  *
121  * @image html JoystickControlWidget_widget.png "Left: A widget with 2 steps using the whole unit circle. Right: A widget using the unit circle's upper semi circle." width=300px
122  */
123  class JoystickControlWidget : public QWidget
124  {
125  Q_OBJECT
126  public:
127  explicit JoystickControlWidget(bool useQuadrant3and4 = true, QWidget* parent = 0);
128 
129  /**f the control in polar coordinates (-pi,pi].
130  * The top position is 0. The bottom pos
131  * @brief Returns the angle oition is pi. The Quadrants 1 and 4 have positive value.
132  * @return The angle of the control in polar coordinates (-pi,pi].
133  */
134  double getRotation() const;
135 
136  /**
137  * @brief Returns the position of the nibble.
138  * The position is in a circle with the radius 1 around (0;0)
139  * @return The position of the nibble.
140  */
141  QPointF getPosition() const;
142 
143  public slots:
144  /**
145  * @brief Sets the steps of the control. (0=> unlimited steps)
146  * @param stepCount The new step count. (values <0 will be used as 0)
147  */
148  void setSteps(int stepCount);
149 
150  /**
151  * @brief Returns the steps of the control. (0=> unlimited steps)
152  * @return The steps of the control. (0=> unlimited steps)
153  */
154  int getSteps();
155 
156  /**
157  * @brief Sets the nibble to pos. (pos will be transformed to be a valid position)
158  * @param pos The new position.
159  */
160  void setNibble(QPointF pos);
161 
162 
163  signals:
164  /**
165  * @brief Passes the position of the control.
166  * The position is in a circle with the radius 1 around (0;0)
167  */
168  void positionChanged(QPointF);
169 
170  /**
171  * @brief Passes the angle of the control in polar coordinates (-pi,pi].
172  * The top position is 0. The bottom position is pi. The Quadrants 1 and 4 have positive value.
173  */
174  void rotationChanged(double);
175 
176  /**
177  * @brief Passes the position and angle of the control in polar coordinates (-pi,pi].
178  */
179  void changed(QPointF, double);
180 
181  /**
182  * @brief Emitted when the nibble was released.
183  */
184  void released();
185 
186  /**
187  * @brief Emitted when the nibble was pressed.
188  */
189  void pressed();
190 
191  protected:
192  void resizeEvent(QResizeEvent* event) override;
193 
194  private slots:
195 
196  /**
197  * @brief Called when the nibble is released.
198  * Emits released() and sets the Position to 0.
199  */
200  void mouseReleased();
201 
202  private:
203  /**
204  * @brief The view containing the scene.
205  */
206  QPointer<JoystickControlWidgetQGraphicsView> view;
207  /**
208  * @brief Whether only quadrant 1 and 2 are in use
209  */
210  bool onlyQuadrant2and1;
211 
212  //QGraphicsEllipseItem is no QObject so it cant be stored in a QPointer.
213  /**
214  * @brief The control item of this widget. (the red dot)
215  */
216  QGraphicsEllipseItem* nibble;
217 
218  /**
219  * @brief The steps of the control. (0=> unlimited steps)
220  */
221  int steps;
222  }; // class JoystickControlWidget
223 
224  using JoystickControlWidgetPtr = std::shared_ptr<JoystickControlWidget>;
225 
226 } // namespace armarx
armarx::JoystickControlWidgetQGraphicsView::~JoystickControlWidgetQGraphicsView
~JoystickControlWidgetQGraphicsView() override
armarx::JoystickControlWidget::getRotation
double getRotation() const
f the control in polar coordinates (-pi,pi].
Definition: JoystickControlWidget.cpp:118
armarx::JoystickControlWidgetQGraphicsView::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event) override
Passes the mapped mouse coordinates of the event through the signal positionChanged.
Definition: JoystickControlWidget.cpp:59
armarx::JoystickControlWidget::getSteps
int getSteps()
Returns the steps of the control.
Definition: JoystickControlWidget.cpp:229
armarx::JoystickControlWidgetQGraphicsView::released
void released()
Emitted when the mouse was released.
armarx::JoystickControlWidgetQGraphicsView::JoystickControlWidgetQGraphicsView
JoystickControlWidgetQGraphicsView(QGraphicsScene *scene, QWidget *parent=nullptr)
Definition: JoystickControlWidget.cpp:43
armarx::JoystickControlWidget::pressed
void pressed()
Emitted when the nibble was pressed.
armarx::JoystickControlWidgetQGraphicsView::pressed
void pressed()
Emitted when the mouse was pressed.
armarx::JoystickControlWidgetPtr
std::shared_ptr< JoystickControlWidget > JoystickControlWidgetPtr
Definition: JoystickControlWidget.h:224
armarx::JoystickControlWidget::released
void released()
Emitted when the nibble was released.
armarx::JoystickControlWidget::setNibble
void setNibble(QPointF pos)
Sets the nibble to pos.
Definition: JoystickControlWidget.cpp:151
armarx::JoystickControlWidget::changed
void changed(QPointF, double)
Passes the position and angle of the control in polar coordinates (-pi,pi].
armarx::JoystickControlWidget::JoystickControlWidget
JoystickControlWidget(bool useQuadrant3and4=true, QWidget *parent=0)
Definition: JoystickControlWidget.cpp:71
armarx::JoystickControlWidget::getPosition
QPointF getPosition() const
Returns the position of the nibble.
Definition: JoystickControlWidget.cpp:112
armarx::JoystickControlWidget
Provides a simple joystick control.
Definition: JoystickControlWidget.h:123
armarx::JoystickControlWidget::rotationChanged
void rotationChanged(double)
Passes the angle of the control in polar coordinates (-pi,pi].
armarx::JoystickControlWidget::setSteps
void setSteps(int stepCount)
Sets the steps of the control.
Definition: JoystickControlWidget.cpp:223
armarx::JoystickControlWidget::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: JoystickControlWidget.cpp:198
armarx::JoystickControlWidgetQGraphicsView::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *) override
Passes (0;0) through the signal positionChanged.
Definition: JoystickControlWidget.cpp:65
armarx::JoystickControlWidget::positionChanged
void positionChanged(QPointF)
Passes the position of the control.
armarx::JoystickControlWidgetQGraphicsView::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Passes the mapped mouse coordinates of the event through the signal positionChanged.
Definition: JoystickControlWidget.cpp:52
armarx::JoystickControlWidgetQGraphicsView::positionChanged
void positionChanged(QPointF)
Sends the mouse position on press or move in the scene coordinates.
armarx::JoystickControlWidgetQGraphicsView
Provides the coordinates of mouse events through signals.
Definition: JoystickControlWidget.h:46
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27