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 <QWidget>
32 #include <QGraphicsView>
33 #include <QGraphicsScene>
34 #include <QMouseEvent>
35 #include <QResizeEvent>
36 
37 #include <QPointF>
38 #include <QPointer>
39 #include <QGraphicsEllipseItem>
40 
41 namespace armarx
42 {
43  /**
44  * @brief Provides the coordinates of mouse events through signals.
45  * Used in JoystickControlWidget.
46  */
47  class JoystickControlWidgetQGraphicsView: public QGraphicsView
48  {
49  Q_OBJECT
50  public:
51  JoystickControlWidgetQGraphicsView(QGraphicsScene* scene, QWidget* parent = nullptr);
53 
54  public:
55  /**
56  * @brief Passes the mapped mouse coordinates of the event through the signal positionChanged.
57  * @param event Contains the coordinates
58  */
59  void mousePressEvent(QMouseEvent* event) override;
60 
61  /**
62  * @brief Passes the mapped mouse coordinates of the event through the signal positionChanged.
63  * @param event Contains the coordinates
64  */
65  void mouseMoveEvent(QMouseEvent* event) override;
66 
67  /**
68  * @brief Passes (0;0) through the signal positionChanged.
69  */
70  void mouseReleaseEvent(QMouseEvent*) override;
71  signals:
72  /**
73  * @brief Sends the mouse position on press or move in the scene coordinates.
74  * On mouse release (0;0) is send.
75  */
76  void positionChanged(QPointF);
77 
78  /**
79  * @brief Emitted when the mouse was released.
80  */
81  void released();
82 
83  /**
84  * @brief Emitted when the mouse was pressed.
85  */
86  void pressed();
87  };//JoystickControlWidgetQGraphicsView
88 
89  /**
90  * @brief Provides a simple joystick control.
91  *
92  * The widget emits signals when the control is moved.
93  *
94  * The signal
95  * @code{.cpp}
96  * positionChanged(QPointF)
97  * @endcode
98  * passes the nibble's current position.
99  * The position is in the unit circle.
100  * The x-axis is horizontal and increases to the right.
101  * The y-axis is vertical and increases upwards.
102  *
103  * The signal
104  * @code{.cpp}
105  * rotationChanged(double)
106  * @endcode
107  * passes the position vectors rotation in polar coordinates (-pi,pi].
108  * The up position is 0.
109  * The down position is pi.
110  * The Quadrants 1 and 4 have positive value.
111  *
112  * If the constructor is called with useQuadrant3and4==true the control is the whole unit circle.
113  * If the constructor is called with useQuadrant3and4==false the control is the unit circle's upper semicircle. (y>=0)
114  *
115  * Possible positions can be influenced with
116  * \code{.cpp}
117  * setSteps(int steps);
118  * \endcode
119  * - 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.
120  * - steps<=0: all positions in the unit circle are valid.
121  *
122  * @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
123  */
124  class JoystickControlWidget : public QWidget
125  {
126  Q_OBJECT
127  public:
128  explicit JoystickControlWidget(bool useQuadrant3and4 = true, QWidget* parent = 0);
129 
130  /**f the control in polar coordinates (-pi,pi].
131  * The top position is 0. The bottom pos
132  * @brief Returns the angle oition is pi. The Quadrants 1 and 4 have positive value.
133  * @return The angle of the control in polar coordinates (-pi,pi].
134  */
135  double getRotation() const;
136 
137  /**
138  * @brief Returns the position of the nibble.
139  * The position is in a circle with the radius 1 around (0;0)
140  * @return The position of the nibble.
141  */
142  QPointF getPosition() const;
143 
144  public slots:
145  /**
146  * @brief Sets the steps of the control. (0=> unlimited steps)
147  * @param stepCount The new step count. (values <0 will be used as 0)
148  */
149  void setSteps(int stepCount);
150 
151  /**
152  * @brief Returns the steps of the control. (0=> unlimited steps)
153  * @return The steps of the control. (0=> unlimited steps)
154  */
155  int getSteps();
156 
157  /**
158  * @brief Sets the nibble to pos. (pos will be transformed to be a valid position)
159  * @param pos The new position.
160  */
161  void setNibble(QPointF pos);
162 
163 
164  signals:
165  /**
166  * @brief Passes the position of the control.
167  * The position is in a circle with the radius 1 around (0;0)
168  */
169  void positionChanged(QPointF);
170 
171  /**
172  * @brief Passes the angle of the control in polar coordinates (-pi,pi].
173  * The top position is 0. The bottom position is pi. The Quadrants 1 and 4 have positive value.
174  */
175  void rotationChanged(double);
176 
177  /**
178  * @brief Passes the position and angle of the control in polar coordinates (-pi,pi].
179  */
180  void changed(QPointF, double);
181 
182  /**
183  * @brief Emitted when the nibble was released.
184  */
185  void released();
186 
187  /**
188  * @brief Emitted when the nibble was pressed.
189  */
190  void pressed();
191 
192  protected:
193  void resizeEvent(QResizeEvent* event) override;
194 
195  private slots:
196 
197  /**
198  * @brief Called when the nibble is released.
199  * Emits released() and sets the Position to 0.
200  */
201  void mouseReleased();
202 
203  private:
204  /**
205  * @brief The view containing the scene.
206  */
207  QPointer<JoystickControlWidgetQGraphicsView> view;
208  /**
209  * @brief Whether only quadrant 1 and 2 are in use
210  */
211  bool onlyQuadrant2and1;
212 
213  //QGraphicsEllipseItem is no QObject so it cant be stored in a QPointer.
214  /**
215  * @brief The control item of this widget. (the red dot)
216  */
217  QGraphicsEllipseItem* nibble;
218 
219  /**
220  * @brief The steps of the control. (0=> unlimited steps)
221  */
222  int steps;
223  };// class JoystickControlWidget
224 
225  using JoystickControlWidgetPtr = std::shared_ptr<JoystickControlWidget>;
226 
227 }// namespace armarx
armarx::JoystickControlWidgetQGraphicsView::~JoystickControlWidgetQGraphicsView
~JoystickControlWidgetQGraphicsView() override
armarx::JoystickControlWidget::getRotation
double getRotation() const
f the control in polar coordinates (-pi,pi].
Definition: JoystickControlWidget.cpp:117
armarx::JoystickControlWidgetQGraphicsView::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event) override
Passes the mapped mouse coordinates of the event through the signal positionChanged.
Definition: JoystickControlWidget.cpp:58
armarx::JoystickControlWidget::getSteps
int getSteps()
Returns the steps of the control.
Definition: JoystickControlWidget.cpp:224
armarx::JoystickControlWidgetQGraphicsView::released
void released()
Emitted when the mouse was released.
armarx::JoystickControlWidgetQGraphicsView::JoystickControlWidgetQGraphicsView
JoystickControlWidgetQGraphicsView(QGraphicsScene *scene, QWidget *parent=nullptr)
Definition: JoystickControlWidget.cpp:44
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:225
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:148
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:68
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:124
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:219
armarx::JoystickControlWidget::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: JoystickControlWidget.cpp:194
armarx::JoystickControlWidgetQGraphicsView::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *) override
Passes (0;0) through the signal positionChanged.
Definition: JoystickControlWidget.cpp:62
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:47
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28