Debian Codegraph @

src/Gui/MouseSelection.cpp

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 #include <QOpenGLFramebufferObject>
24 #include <QPixmap>
25 #include <QMenu>
26 #include <Inventor/SbBox.h>
27 #include <Inventor/events/SoEvent.h>
28 #include <Inventor/events/SoKeyboardEvent.h>
29 #include <Inventor/events/SoLocation2Event.h>
30 #include <Inventor/events/SoMouseButtonEvent.h>
31
32 #include <FCConfig.h>
33
34 #include "MouseSelection.h"
35 #include "View3DInventorViewer.h"
36
37
38 using namespace Gui;
39
40 AbstractMouseSelection::AbstractMouseSelection()
41 {
42 m_iXold = 0;
43 m_iYold = 0;
44 m_iXnew = 0;
45 m_iYnew = 0;
46 m_selectedRole = SelectionRole::None;
47 }
48
49 void AbstractMouseSelection::grabMouseModel(Gui::View3DInventorViewer* viewer)
50 {
51 _pcView3D = viewer;
52 m_cPrevCursor = _pcView3D->getWidget()->cursor();
53
54 // do initialization of your mousemodel
55 initialize();
56 }
57
58 void AbstractMouseSelection::releaseMouseModel(bool abort)
59 {
60 if (_pcView3D) {
61 // do termination of your mousemodel
62 terminate(abort);
63
64 _pcView3D->getWidget()->setCursor(m_cPrevCursor);
65 _pcView3D = nullptr;
66 }
67 }
68
69
70 int AbstractMouseSelection::handleEvent(const SoEvent* const ev, const SbViewportRegion& vp)
71 {
72 int ret = Continue;
73
74 const SbVec2s& sz = vp.getWindowSize();
75 short w, h;
76 sz.getValue(w, h);
77
78 SbVec2s loc = ev->getPosition();
79 short x, y;
80 loc.getValue(x, y);
81 y = h - y; // the origin is at the left bottom corner (instead of left top corner)
82
83 if (ev->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
84 const auto event = (const SoMouseButtonEvent*)ev;
85 const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
86
87 if (press) {
88 _clPoly.push_back(ev->getPosition());
89 ret = mouseButtonEvent(static_cast<const SoMouseButtonEvent*>(ev), QPoint(x, y));
90 }
91 else {
92 ret = mouseButtonEvent(static_cast<const SoMouseButtonEvent*>(ev), QPoint(x, y));
93 }
94 }
95 else if (ev->getTypeId().isDerivedFrom(SoLocation2Event::getClassTypeId())) {
96 ret = locationEvent(static_cast<const SoLocation2Event*>(ev), QPoint(x, y));
97 }
98 else if (ev->getTypeId().isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
99 ret = keyboardEvent(static_cast<const SoKeyboardEvent*>(ev));
100 }
101
102 if (ret == Restart) {
103 _clPoly.clear();
104 }
105
106 return ret;
107 }
108
109 // -----------------------------------------------------------------------------------
110
111 BaseMouseSelection::BaseMouseSelection()
112 : AbstractMouseSelection()
113 {}
114
115 // clang-format off
116 static const char* cursor_cut_scissors[]= {
117 "32 32 6 1",
118 "a c #800000",
119 "c c #808080",
120 "+ c #c0c0c0",
121 "b c #ff0000",
122 "# c #ffffff",
123 ". c None",
124 "....#...........................",
125 "....#...........................",
126 "....#...........................",
127 "................................",
128 "###.#.###.......................",
129 "................................",
130 "....#...........................",
131 "....#...................aaaaa...",
132 "....#.................aabbbbba..",
133 ".....................abbbbbbba..",
134 ".....ccc............abbaaaaabb..",
135 "....cc++cc.........babaa...aba..",
136 "...c+#++++cc.......abba...abba..",
137 "...cc+#+++++c......abba.aabbaa..",
138 ".....c+++++#+cc....abbaaabbaa...",
139 "......cc+#+++#+cc.aabbbbbbaa....",
140 "........cc+#+++#+cabbbaaaa......",
141 "..........c+++++++abbaa.........",
142 "...........cc+++#+aaaa..........",
143 "...........cc+#+++caa...........",
144 ".........cc+++++#+cbbaaaaa......",
145 "........cc+#+++#+cabbabbbaaa....",
146 "......cc+#+++#+cc.aaabbbbbbaa...",
147 "....cc+#+++#+cc....abbaaaabba...",
148 "...c++#++#+cc......abba..aabba..",
149 "...c+###++c........aabaa..aaba..",
150 "....cc++cc..........abbaa..aba..",
151 "......c.............aabbaaaaba..",
152 ".....................baabbbbba..",
153 ".......................aaaaaa...",
154 "................................",
155 "................................"
156 };
157 // clang-format on
158
159 PolyPickerSelection::PolyPickerSelection()
160 {
161 lastConfirmed = false;
162 }
163
164 void PolyPickerSelection::setColor(float r, float g, float b, float a)
165 {
166 polyline.setColor(r, g, b, a);
167 }
168
169 void PolyPickerSelection::setLineWidth(float l)
170 {
171 polyline.setLineWidth(l);
172 }
173
174 void PolyPickerSelection::initialize()
175 {
176 QPixmap p(cursor_cut_scissors);
177 QCursor cursor(p, 4, 4);
178 _pcView3D->getWidget()->setCursor(cursor);
179
180 polyline.setViewer(_pcView3D);
181
182 _pcView3D->addGraphicsItem(&polyline);
183 _pcView3D->redraw(); // needed to get an up-to-date image
184 _pcView3D->setRenderType(View3DInventorViewer::Image);
185 _pcView3D->redraw();
186
187 lastConfirmed = false;
188 }
189
190 void PolyPickerSelection::terminate(bool abort)
191 {
192 Q_UNUSED(abort)
193
194 _pcView3D->removeGraphicsItem(&polyline);
195 _pcView3D->setRenderType(View3DInventorViewer::Native);
196 _pcView3D->redraw();
197 }
198
199 void PolyPickerSelection::draw()
200 {
201 _pcView3D->redraw();
202 }
203
204 PolyPickerSelection::~PolyPickerSelection() = default;
205
206 int PolyPickerSelection::popupMenu()
207 {
208 QMenu menu;
209 QAction* fi = menu.addAction(QObject::tr("Finish"));
210 menu.addAction(QObject::tr("Clear"));
211 QAction* ca = menu.addAction(QObject::tr("Cancel"));
212
213 if (getPositions().size() < 3) {
214 fi->setEnabled(false);
215 }
216
217 QAction* id = menu.exec(QCursor::pos());
218
219 if (id == fi) {
220 return Finish;
221 }
222 else if (id == ca) {
223 return Cancel;
224 }
225 else {
226 return Restart;
227 }
228 }
229
230 int PolyPickerSelection::mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos)
231 {
232 const int button = e->getButton();
233 const SbBool press = e->getState() == SoButtonEvent::DOWN ? true : false;
234
235 if (press) {
236 switch (button) {
237 case SoMouseButtonEvent::BUTTON1: {
238 if (!polyline.isWorking()) {
239 polyline.setWorking(true);
240 polyline.clear();
241 };
242 polyline.addNode(pos);
243 lastConfirmed = true;
244 m_iXnew = pos.x();
245 m_iYnew = pos.y();
246 m_iXold = pos.x();
247 m_iYold = pos.y();
248 } break;
249
250 case SoMouseButtonEvent::BUTTON2: {
251 polyline.addNode(pos);
252 m_iXnew = pos.x();
253 m_iYnew = pos.y();
254 m_iXold = pos.x();
255 m_iYold = pos.y();
256 } break;
257
258 default: {
259 } break;
260 }
261 }
262 // release
263 else {
264 switch (button) {
265 case SoMouseButtonEvent::BUTTON2: {
266 QCursor cur = _pcView3D->getWidget()->cursor();
267 _pcView3D->getWidget()->setCursor(m_cPrevCursor);
268
269 // The pop-up menu should be shown when releasing mouse button because
270 // otherwise the navigation style doesn't get the UP event and gets into
271 // an inconsistent state.
272 int id = popupMenu();
273
274 if (id == Finish || id == Cancel) {
275 releaseMouseModel();
276 }
277 else if (id == Restart) {
278 _pcView3D->getWidget()->setCursor(cur);
279 }
280
281 polyline.setWorking(false);
282 return id;
283 } break;
284
285 default: {
286 } break;
287 }
288 }
289
290 return Continue;
291 }
292
293 int PolyPickerSelection::locationEvent(const SoLocation2Event* const, const QPoint& pos)
294 {
295 // do all the drawing stuff for us
296 QPoint clPoint = pos;
297
298 if (polyline.isWorking()) {
299 // check the position
300 qreal dpr = _pcView3D->getGLWidget()->devicePixelRatioF();
301 QRect r = _pcView3D->getGLWidget()->rect();
302 if (dpr != 1.0) {
303 r.setHeight(r.height() * dpr);
304 r.setWidth(r.width() * dpr);
305 }
306
307 if (!r.contains(clPoint)) {
308 if (clPoint.x() < r.left()) {
309 clPoint.setX(r.left());
310 }
311
312 if (clPoint.x() > r.right()) {
313 clPoint.setX(r.right());
314 }
315
316 if (clPoint.y() < r.top()) {
317 clPoint.setY(r.top());
318 }
319
320 if (clPoint.y() > r.bottom()) {
321 clPoint.setY(r.bottom());
322 }
323
324 #ifdef FC_OS_WINDOWS
325 QPoint newPos = _pcView3D->getGLWidget()->mapToGlobal(clPoint);
326 QCursor::setPos(newPos);
327 #endif
328 }
329
330 if (!lastConfirmed) {
331 polyline.popNode();
332 }
333 polyline.addNode(clPoint);
334 lastConfirmed = false;
335
336 draw();
337 }
338
339 m_iXnew = clPoint.x();
340 m_iYnew = clPoint.y();
341
342 return Continue;
343 }
344
345 int PolyPickerSelection::keyboardEvent(const SoKeyboardEvent* const)
346 {
347 return Continue;
348 }
349
350 // -----------------------------------------------------------------------------------
351
352 PolyClipSelection::PolyClipSelection()
353 {
354 selectionBits.set(1);
355 selectionBits.set(2);
356 }
357
358 PolyClipSelection::~PolyClipSelection() = default;
359
360 int PolyClipSelection::popupMenu()
361 {
362 QMenu menu;
363 QAction* ci = menu.addAction(QObject::tr("Inner"));
364 QAction* co = menu.addAction(QObject::tr("Outer"));
365 QAction* cs = menu.addAction(QObject::tr("Split"));
366 QAction* ca = menu.addAction(QObject::tr("Cancel"));
367
368 ci->setVisible(testRole(SelectionRole::Inner));
369 co->setVisible(testRole(SelectionRole::Outer));
370 cs->setVisible(testRole(SelectionRole::Split));
371
372 if (getPositions().size() < 3) {
373 ci->setEnabled(false);
374 co->setEnabled(false);
375 }
376
377 QAction* id = menu.exec(QCursor::pos());
378
379 if (id == ci) {
380 m_selectedRole = SelectionRole::Inner;
381 return Finish;
382 }
383 else if (id == co) {
384 m_selectedRole = SelectionRole::Outer;
385 return Finish;
386 }
387 else if (id == cs) {
388 m_selectedRole = SelectionRole::Split;
389 return Finish;
390 }
391 else if (id == ca) {
392 m_selectedRole = SelectionRole::None;
393 return Cancel;
394 }
395 else {
396 m_selectedRole = SelectionRole::None;
397 return Restart;
398 }
399 }
400
401 // -----------------------------------------------------------------------------------
402
403 FreehandSelection::FreehandSelection() = default;
404
405 FreehandSelection::~FreehandSelection() = default;
406
407 void FreehandSelection::setClosed(bool on)
408 {
409 polyline.setClosed(on);
410 polyline.setCloseStippled(true);
411 }
412
413 int FreehandSelection::popupMenu()
414 {
415 QMenu menu;
416 QAction* fi = menu.addAction(QObject::tr("Finish"));
417 menu.addAction(QObject::tr("Clear"));
418 QAction* ca = menu.addAction(QObject::tr("Cancel"));
419
420 if (getPositions().size() < 3) {
421 fi->setEnabled(false);
422 }
423
424 QAction* id = menu.exec(QCursor::pos());
425 if (id == fi) {
426 return Finish;
427 }
428 else if (id == ca) {
429 return Cancel;
430 }
431 else {
432 return Restart;
433 }
434 }
435
436 int FreehandSelection::mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos)
437 {
438 const int button = e->getButton();
439 const SbBool press = e->getState() == SoButtonEvent::DOWN ? true : false;
440
441 if (press) {
442 switch (button) {
443 case SoMouseButtonEvent::BUTTON1: {
444 if (!polyline.isWorking()) {
445 polyline.setWorking(true);
446 polyline.clear();
447 }
448
449 polyline.addNode(pos);
450 polyline.setCoords(pos.x(), pos.y());
451 m_iXnew = pos.x();
452 m_iYnew = pos.y();
453 m_iXold = pos.x();
454 m_iYold = pos.y();
455 } break;
456
457 case SoMouseButtonEvent::BUTTON2: {
458 polyline.addNode(pos);
459 m_iXnew = pos.x();
460 m_iYnew = pos.y();
461 m_iXold = pos.x();
462 m_iYold = pos.y();
463 } break;
464
465 default:
466 break;
467 }
468 }
469 // release
470 else {
471 switch (button) {
472 case SoMouseButtonEvent::BUTTON1:
473 if (polyline.isWorking()) {
474 releaseMouseModel();
475 return Finish;
476 }
477 break;
478 case SoMouseButtonEvent::BUTTON2: {
479 QCursor cur = _pcView3D->getWidget()->cursor();
480 _pcView3D->getWidget()->setCursor(m_cPrevCursor);
481
482 // The pop-up menu should be shown when releasing mouse button because
483 // otherwise the navigation style doesn't get the UP event and gets into
484 // an inconsistent state.
485 int id = popupMenu();
486
487 if (id == Finish || id == Cancel) {
488 releaseMouseModel();
489 }
490 else if (id == Restart) {
491 _pcView3D->getWidget()->setCursor(cur);
492 }
493
494 polyline.setWorking(false);
495 return id;
496 } break;
497
498 default:
499 break;
500 }
501 }
502
503 return Continue;
504 }
505
506 int FreehandSelection::locationEvent(const SoLocation2Event* const e, const QPoint& pos)
507 {
508 // do all the drawing stuff for us
509 QPoint clPoint = pos;
510
511 if (polyline.isWorking()) {
512 // check the position
513 qreal dpr = _pcView3D->getGLWidget()->devicePixelRatioF();
514 QRect r = _pcView3D->getGLWidget()->rect();
515 if (dpr != 1.0) {
516 r.setHeight(r.height() * dpr);
517 r.setWidth(r.width() * dpr);
518 }
519
520 if (!r.contains(clPoint)) {
521 if (clPoint.x() < r.left()) {
522 clPoint.setX(r.left());
523 }
524
525 if (clPoint.x() > r.right()) {
526 clPoint.setX(r.right());
527 }
528
529 if (clPoint.y() < r.top()) {
530 clPoint.setY(r.top());
531 }
532
533 if (clPoint.y() > r.bottom()) {
534 clPoint.setY(r.bottom());
535 }
536 }
537
538 SbVec2s last = _clPoly.back();
539 SbVec2s curr = e->getPosition();
540
541 if (abs(last[0] - curr[0]) > 20 || abs(last[1] - curr[1]) > 20) {
542 _clPoly.push_back(curr);
543 }
544
545 polyline.addNode(clPoint);
546 polyline.setCoords(clPoint.x(), clPoint.y());
547 }
548
549 m_iXnew = clPoint.x();
550 m_iYnew = clPoint.y();
551 draw();
552 m_iXold = clPoint.x();
553 m_iYold = clPoint.y();
554
555 return Continue;
556 }
557
558 // -----------------------------------------------------------------------------------
559
560 RubberbandSelection::RubberbandSelection()
561 {
562 rubberband.setColor(1.0, 1.0, 0.0, 0.5);
563 }
564
565 RubberbandSelection::~RubberbandSelection() = default;
566
567 void RubberbandSelection::setColor(float r, float g, float b, float a)
568 {
569 rubberband.setColor(r, g, b, a);
570 }
571
572 void RubberbandSelection::initialize()
573 {
574 rubberband.setViewer(_pcView3D);
575 rubberband.setWorking(false);
576 _pcView3D->addGraphicsItem(&rubberband);
577 if (QOpenGLFramebufferObject::hasOpenGLFramebufferObjects()) {
578 _pcView3D->setRenderType(View3DInventorViewer::Image);
579 }
580 _pcView3D->redraw();
581 }
582
583 void RubberbandSelection::terminate(bool abort)
584 {
585 Q_UNUSED(abort)
586
587 _pcView3D->removeGraphicsItem(&rubberband);
588 if (QOpenGLFramebufferObject::hasOpenGLFramebufferObjects()) {
589 _pcView3D->setRenderType(View3DInventorViewer::Native);
590 }
591 _pcView3D->redraw();
592 }
593
594 void RubberbandSelection::draw()
595 {
596 _pcView3D->redraw();
597 }
598
599 int RubberbandSelection::mouseButtonEvent(const SoMouseButtonEvent* const e, const QPoint& pos)
600 {
601 const int button = e->getButton();
602 const SbBool press = e->getState() == SoButtonEvent::DOWN ? true : false;
603
604 int ret = Continue;
605
606 if (press) {
607 switch (button) {
608 case SoMouseButtonEvent::BUTTON1: {
609 rubberband.setWorking(true);
610 m_iXold = m_iXnew = pos.x();
611 m_iYold = m_iYnew = pos.y();
612 } break;
613
614 default: {
615 } break;
616 }
617 }
618 else {
619 switch (button) {
620 case SoMouseButtonEvent::BUTTON1: {
621 rubberband.setWorking(false);
622 releaseMouseModel();
623 _clPoly.push_back(e->getPosition());
624 ret = Finish;
625 } break;
626
627 default: {
628 } break;
629 }
630 }
631
632 return ret;
633 }
634
635 int RubberbandSelection::locationEvent(const SoLocation2Event* const, const QPoint& pos)
636 {
637 m_iXnew = pos.x();
638 m_iYnew = pos.y();
639 rubberband.setCoords(m_iXold, m_iYold, m_iXnew, m_iYnew);
640 draw();
641 return Continue;
642 }
643
644 int RubberbandSelection::keyboardEvent(const SoKeyboardEvent* const)
645 {
646 return Continue;
647 }
648
649 // -----------------------------------------------------------------------------------
650
651 RectangleSelection::RectangleSelection()
652 : RubberbandSelection()
653 {
654 rubberband.setColor(0.0, 0.0, 1.0, 1.0);
655 }
656
657 RectangleSelection::~RectangleSelection() = default;
658
659 // -----------------------------------------------------------------------------------
660
661 BoxZoomSelection::BoxZoomSelection() = default;
662
663 BoxZoomSelection::~BoxZoomSelection() = default;
664
665 void BoxZoomSelection::terminate(bool abort)
666 {
667 RubberbandSelection::terminate(abort);
668 if (!abort) {
669 int xmin = std::min<int>(m_iXold, m_iXnew);
670 int xmax = std::max<int>(m_iXold, m_iXnew);
671 int ymin = std::min<int>(m_iYold, m_iYnew);
672 int ymax = std::max<int>(m_iYold, m_iYnew);
673 SbBox2s box(xmin, ymin, xmax, ymax);
674 _pcView3D->boxZoom(box);
675 }
676 }