1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2015 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but 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 #include <QMouseEvent> |
|
20 #include "selectMode.h" |
|
21 #include "../glRenderer.h" |
|
22 #include "../addObjectDialog.h" |
|
23 #include "../mainWindow.h" |
|
24 #include "../glRenderer.h" |
|
25 |
|
26 SelectMode::SelectMode (GLRenderer* renderer) : |
|
27 Super (renderer), |
|
28 m_rangepick (false) {} |
|
29 |
|
30 EditModeType SelectMode::type() const |
|
31 { |
|
32 return EditModeType::Select; |
|
33 } |
|
34 |
|
35 void SelectMode::render (QPainter& painter) const |
|
36 { |
|
37 // If we're range-picking, draw a rectangle encompassing the selection area. |
|
38 if (m_rangepick) |
|
39 { |
|
40 int x0 = m_rangeStart.x(), |
|
41 y0 = m_rangeStart.y(), |
|
42 x1 = renderer()->mousePosition().x(), |
|
43 y1 = renderer()->mousePosition().y(); |
|
44 |
|
45 QRect rect (x0, y0, x1 - x0, y1 - y0); |
|
46 QColor fillColor = (m_addpick ? "#40FF00" : "#00CCFF"); |
|
47 fillColor.setAlphaF (0.2f); |
|
48 painter.setPen (QPen (QColor (0, 0, 0, 208), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
|
49 painter.setBrush (QBrush (fillColor)); |
|
50 painter.drawRect (rect); |
|
51 } |
|
52 } |
|
53 |
|
54 bool SelectMode::mouseReleased (MouseEventData const& data) |
|
55 { |
|
56 if (Super::mouseReleased (data)) |
|
57 return true; |
|
58 |
|
59 if (data.releasedButtons & Qt::LeftButton) |
|
60 { |
|
61 if (not data.mouseMoved) |
|
62 m_rangepick = false; |
|
63 |
|
64 if (not m_rangepick) |
|
65 m_addpick = (data.keymods & Qt::ControlModifier); |
|
66 |
|
67 if (not data.mouseMoved or m_rangepick) |
|
68 { |
|
69 QRect area; |
|
70 int const mx = data.ev->x(); |
|
71 int const my = data.ev->y(); |
|
72 |
|
73 if (not m_rangepick) |
|
74 { |
|
75 area = QRect (mx, my, 1, 1); |
|
76 } |
|
77 else |
|
78 { |
|
79 int const x = Min (m_rangeStart.x(), mx); |
|
80 int const y = Min (m_rangeStart.y(), my); |
|
81 int const width = Abs (m_rangeStart.x() - mx); |
|
82 int const height = Abs (m_rangeStart.y() - my); |
|
83 area = QRect (x, y, width, height); |
|
84 } |
|
85 |
|
86 renderer()->pick (area, m_addpick); |
|
87 } |
|
88 |
|
89 m_rangepick = false; |
|
90 return true; |
|
91 } |
|
92 |
|
93 return false; |
|
94 } |
|
95 |
|
96 bool SelectMode::mousePressed (QMouseEvent* ev) |
|
97 { |
|
98 if (Super::mousePressed (ev)) |
|
99 return true; |
|
100 |
|
101 if (ev->modifiers() & Qt::ControlModifier) |
|
102 { |
|
103 m_rangepick = true; |
|
104 m_rangeStart.setX (ev->x()); |
|
105 m_rangeStart.setY (ev->y()); |
|
106 m_addpick = (ev->modifiers() & Qt::AltModifier); |
|
107 return true; |
|
108 } |
|
109 |
|
110 return false; |
|
111 } |
|
112 |
|
113 bool SelectMode::mouseDoubleClicked (QMouseEvent* ev) |
|
114 { |
|
115 if (Super::mouseDoubleClicked (ev)) |
|
116 return true; |
|
117 |
|
118 if (ev->buttons() & Qt::LeftButton) |
|
119 { |
|
120 renderer()->document()->clearSelection(); |
|
121 LDObjectPtr obj = renderer()->pickOneObject (ev->x(), ev->y()); |
|
122 |
|
123 if (obj != null) |
|
124 { |
|
125 AddObjectDialog::staticDialog (obj->type(), obj); |
|
126 g_win->endAction(); |
|
127 return true; |
|
128 } |
|
129 } |
|
130 |
|
131 return false; |
|
132 } |
|
133 |
|
134 bool SelectMode::mouseMoved (QMouseEvent*) |
|
135 { |
|
136 return m_rangepick; |
|
137 } |
|