Debian Codegraph @

src/Gui/MouseSelection.h

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #ifndef MOUSESELECTION_H
24 #define MOUSESELECTION_H
25
26 #include <bitset>
27 #include <vector>
28 #include <QCursor>
29 #include <Gui/GLPainter.h>
30 #include <Gui/Namespace.h>
31
32
33 // forwards
34 class QMouseEvent;
35 class QWheelEvent;
36 class QKeyEvent;
37 class QPaintEvent;
38 class QResizeEvent;
39 class SbVec2s;
40 class SoEvent;
41 class SbViewportRegion;
42 class SoMouseButtonEvent;
43 class SoLocation2Event;
44 class SoKeyboardEvent;
45
46 namespace Gui
47 {
48 class View3DInventorViewer;
49
50 /**
51 * The mouse selection base class
52 * In derived classes you must implement the methods @ref initialize() and @ref terminate()
53 * For all drawing stuff you just have to reimplement the @ref draw() method.
54 * In general you need not to do anything else.
55 * \author Werner Mayer and Jürgen Riegel
56 */
57 class GuiExport AbstractMouseSelection
58 {
59 public:
60 enum
61 {
62 Continue = 0,
63 Restart = 1,
64 Finish = 2,
65 Cancel = 3,
66 Ignore = 4
67 };
68
69 AbstractMouseSelection();
70 virtual ~AbstractMouseSelection() = default;
71 /// implement this in derived classes
72 virtual void initialize() = 0;
73 /// implement this in derived classes
74 virtual void terminate(bool abort = false) = 0;
75 void grabMouseModel(Gui::View3DInventorViewer*);
76 void releaseMouseModel(bool abort = false);
77 const std::vector<SbVec2s>& getPositions() const
78 {
79 return _clPoly;
80 }
81 SelectionRole selectedRole() const
82 {
83 return m_selectedRole;
84 }
85
86 /** @name Mouse events*/
87 //@{
88 int handleEvent(const SoEvent* const ev, const SbViewportRegion& vp);
89 //@}
90
91 protected:
92 virtual int mouseButtonEvent(const SoMouseButtonEvent* const, const QPoint&)
93 {
94 return 0;
95 }
96 virtual int locationEvent(const SoLocation2Event* const, const QPoint&)
97 {
98 return 0;
99 }
100 virtual int keyboardEvent(const SoKeyboardEvent* const)
101 {
102 return 0;
103 }
104
105 /// drawing stuff
106 virtual void draw()
107 {}
108
109 protected:
110 Gui::View3DInventorViewer* _pcView3D {nullptr};
111 QCursor m_cPrevCursor;
112 int m_iXold, m_iYold;
113 int m_iXnew, m_iYnew;
114 SelectionRole m_selectedRole;
115 std::vector<SbVec2s> _clPoly;
116 };
117
118 // -----------------------------------------------------------------------------------
119
120 /**
121 * The standard mouse selection class
122 * \author Jürgen Riegel
123 */
124 class GuiExport BaseMouseSelection: public AbstractMouseSelection
125 {
126 public:
127 BaseMouseSelection();
128 ~BaseMouseSelection() override = default;
129 };
130
131 // -----------------------------------------------------------------------------------
132
133 /**
134 * The poly picker mouse selection class
135 * Create a polygon
136 * \author Werner Mayer
137 */
138 class GuiExport PolyPickerSelection: public BaseMouseSelection
139 {
140 public:
141 PolyPickerSelection();
142 ~PolyPickerSelection() override;
143
144 void setLineWidth(float l);
145 void setColor(float r, float g, float b, float a = 1.0);
146
147 void initialize() override;
148 void terminate(bool abort = false) override;
149
150 protected:
151 int mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos) override;
152 int locationEvent(const SoLocation2Event* const e, const QPoint& pos) override;
153 int keyboardEvent(const SoKeyboardEvent* const e) override;
154
155 /// draw the polygon
156 void draw() override;
157 virtual int popupMenu();
158
159 protected:
160 Gui::Polyline polyline;
161 bool lastConfirmed;
162 };
163
164 // -----------------------------------------------------------------------------------
165
166 /**
167 * The poly clip mouse model class
168 * Create a polygon
169 * \author Werner Mayer
170 */
171 class GuiExport PolyClipSelection: public PolyPickerSelection
172 {
173 public:
174 PolyClipSelection();
175 ~PolyClipSelection() override;
176
177 inline void setRole(SelectionRole pos, bool on)
178 {
179 selectionBits.set(static_cast<size_t>(pos), on);
180 }
181 inline bool testRole(SelectionRole pos) const
182 {
183 return selectionBits.test(static_cast<size_t>(pos));
184 }
185
186 protected:
187 int popupMenu() override;
188
189 private:
190 std::bitset<8> selectionBits;
191 };
192
193 // -----------------------------------------------------------------------------------
194
195 /**
196 * The freehand selection class
197 * \author Werner Mayer
198 */
199 class GuiExport FreehandSelection: public PolyPickerSelection
200 {
201 public:
202 FreehandSelection();
203 ~FreehandSelection() override;
204
205 void setClosed(bool c);
206
207 protected:
208 int popupMenu() override;
209 int mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos) override;
210 int locationEvent(const SoLocation2Event* const e, const QPoint& pos) override;
211 };
212
213 // -----------------------------------------------------------------------------------
214
215 /**
216 * The selection mouse model class
217 * Draws a rectangle for selection
218 * \author Werner Mayer
219 */
220 class GuiExport RubberbandSelection: public BaseMouseSelection
221 {
222 public:
223 RubberbandSelection();
224 ~RubberbandSelection() override;
225
226 void setColor(float r, float g, float b, float a = 1.0);
227
228 void initialize() override;
229 void terminate(bool abort = false) override;
230
231 protected:
232 int mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos) override;
233 int locationEvent(const SoLocation2Event* const e, const QPoint& pos) override;
234 int keyboardEvent(const SoKeyboardEvent* const e) override;
235
236 /// draw the rectangle
237 void draw() override;
238
239 protected:
240 Gui::Rubberband rubberband;
241 };
242
243 // -----------------------------------------------------------------------------------
244
245 /**
246 * The selection mouse model class
247 * Draws a rectangle for selection
248 * \author Werner Mayer
249 */
250 class GuiExport RectangleSelection: public RubberbandSelection
251 {
252 public:
253 RectangleSelection();
254 ~RectangleSelection() override;
255 };
256
257 // -----------------------------------------------------------------------------------
258
259 /**
260 * The box zoom mouse model class
261 * Draws a rectangle for box zooming
262 * \author Werner Mayer
263 */
264 class GuiExport BoxZoomSelection: public RubberbandSelection
265 {
266 public:
267 BoxZoomSelection();
268 ~BoxZoomSelection() override;
269 void terminate(bool abort = false) override;
270 };
271
272 } // namespace Gui
273
274 #endif // MOUSESELECTION_H