Debian Codegraph @

src/gui/snippingArea/SnippingAreaResizer.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "SnippingAreaResizer.h"
21
22 SnippingAreaResizer::SnippingAreaResizer(const QSharedPointer<IConfig> &config, QObject *parent) :
23 QObject(parent),
24 mConfig(config),
25 mIsActive(false),
26 mIsGrabbed(false),
27 mGrabbedHandleIndex(-1),
28 mControlPressed(false),
29 mAltPressed(false)
30 {
31
32 const auto width = 15;
33 mHandles.append(QRectF(0, 0, width, width));
34 mHandles.append(QRectF(0, 0, width, width));
35 mHandles.append(QRectF(0, 0, width, width));
36 mHandles.append(QRectF(0, 0, width, width));
37 mHandles.append(QRectF(0, 0, width, width));
38 mHandles.append(QRectF(0, 0, width, width));
39 mHandles.append(QRectF(0, 0, width, width));
40 mHandles.append(QRectF(0, 0, width, width));
41 }
42
43 void SnippingAreaResizer::activate(const QRectF &rect, const QPointF &pos)
44 {
45 mIsActive = true;
46 mCurrentRect = rect;
47 mColor = mConfig->snippingAdornerColor();
48 updateHandlePositions();
49 updateCursor(pos.toPoint());
50 }
51
52 void SnippingAreaResizer::deactivate()
53 {
54 mIsActive = false;
55 mIsGrabbed = false;
56 mGrabbedHandleIndex = -1;
57 }
58
59 void SnippingAreaResizer::paint(QPainter *painter)
60 {
61 if(mIsActive) {
62 painter->setRenderHint(QPainter::Antialiasing);
63 painter->setBrush(Qt::NoBrush);
64 painter->setPen(QPen(mColor, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
65 painter->drawRect(mCurrentRect);
66 painter->setBrush(mColor);
67 for(const auto handle : mHandles) {
68 painter->drawEllipse(handle);
69 }
70 }
71 }
72
73 bool SnippingAreaResizer::isActive() const
74 {
75 return mIsActive;
76 }
77
78 void SnippingAreaResizer::handleMousePress(const QPointF &pos)
79 {
80 if(mIsActive) {
81 for(const auto handle : mHandles) {
82 if(handle.contains(pos)) {
83 mIsGrabbed = true;
84 mGrabOffset = pos - handle.center();
85 mGrabbedHandleIndex = mHandles.indexOf(handle);
86 break;
87 }
88 }
89
90 if(!mIsGrabbed && mCurrentRect.contains(pos)) {
91 mIsGrabbed = true;
92 mGrabOffset = pos - mCurrentRect.topLeft();
93 }
94 }
95 }
96
97 void SnippingAreaResizer::handleMouseRelease()
98 {
99 if(mIsActive && mIsGrabbed) {
100 mIsGrabbed = false;
101 mGrabOffset = {};
102 mGrabbedHandleIndex = -1;
103 }
104 }
105
106 void SnippingAreaResizer::handleMouseMove(const QPointF &pos)
107 {
108 if (mIsActive) {
109 if (mIsGrabbed) {
110 updateCurrentRect(pos);
111 } else {
112 updateCursor(pos);
113 }
114 }
115 }
116
117 void SnippingAreaResizer::handleKeyPress(QKeyEvent *event)
118 {
119 if (event->key() == Qt::Key_Control) {
120 mControlPressed = true;
121 } else if (event->key() == Qt::Key_Alt) {
122 mAltPressed = true;
123 }
124
125 if (mIsActive) {
126 arrowKeyPressed(event);
127 notifyRectChanged();
128 }
129 }
130
131 void SnippingAreaResizer::arrowKeyPressed(const QKeyEvent *event)
132 {
133 if (event->key() == Qt::Key_Up) {
134 arrowUpPressed();
135 } else if (event->key() == Qt::Key_Down) {
136 arrowDownPressed();
137 } else if (event->key() == Qt::Key_Left) {
138 arrowLeftPressed();
139 } else if (event->key() == Qt::Key_Right) {
140 arrowRightPressed();
141 }
142 }
143
144 void SnippingAreaResizer::handleKeyRelease(QKeyEvent *event)
145 {
146 if(event->key() == Qt::Key_Control) {
147 mControlPressed = false;
148 } else if(event->key() == Qt::Key_Alt) {
149 mAltPressed = false;
150 }
151 }
152
153 void SnippingAreaResizer::updateCursor(const QPointF &pos)
154 {
155 if (mHandles[1].contains(pos) || mHandles[5].contains(pos)) {
156 emit cursorChanged(Qt::SizeVerCursor);
157 } else if (mHandles[3].contains(pos) || mHandles[7].contains(pos)) {
158 emit cursorChanged(Qt::SizeHorCursor);
159 } else if (mHandles[0].contains(pos) || mHandles[2].contains(pos) || mHandles[4].contains(pos) || mHandles[6].contains(pos) || mCurrentRect.contains(pos)) {
160 emit cursorChanged(Qt::SizeAllCursor);
161 } else {
162 emit cursorChanged(Qt::ArrowCursor);
163 }
164 }
165
166 void SnippingAreaResizer::updateCurrentRect(const QPointF &point)
167 {
168 if(mGrabbedHandleIndex == -1){
169 mCurrentRect.moveTo(point - mGrabOffset);
170 } else if(mGrabbedHandleIndex == 0){
171 mCurrentRect.setTopLeft(point - mGrabOffset);
172 } else if(mGrabbedHandleIndex == 1){
173 mCurrentRect.setTop((point - mGrabOffset).y());
174 } else if(mGrabbedHandleIndex == 2){
175 mCurrentRect.setTopRight(point - mGrabOffset);
176 } else if(mGrabbedHandleIndex == 3){
177 mCurrentRect.setRight((point - mGrabOffset).x());
178 } else if(mGrabbedHandleIndex == 4){
179 mCurrentRect.setBottomRight(point - mGrabOffset);
180 } else if(mGrabbedHandleIndex == 5){
181 mCurrentRect.setBottom((point - mGrabOffset).y());
182 } else if(mGrabbedHandleIndex == 6){
183 mCurrentRect.setBottomLeft(point - mGrabOffset);
184 } else if(mGrabbedHandleIndex == 7){
185 mCurrentRect.setLeft((point - mGrabOffset).x());
186 }
187
188 notifyRectChanged();
189 }
190
191 void SnippingAreaResizer::notifyRectChanged()
192 {
193 updateHandlePositions();
194 emit rectChanged(mCurrentRect.normalized());
195 }
196
197 void SnippingAreaResizer::updateHandlePositions()
198 {
199 mHandles[0].moveCenter(RectHelper::topLeft(mCurrentRect));
200 mHandles[1].moveCenter(RectHelper::top(mCurrentRect));
201 mHandles[2].moveCenter(RectHelper::topRight(mCurrentRect));
202 mHandles[3].moveCenter(RectHelper::right(mCurrentRect));
203 mHandles[4].moveCenter(RectHelper::bottomRight(mCurrentRect));
204 mHandles[5].moveCenter(RectHelper::bottom(mCurrentRect));
205 mHandles[6].moveCenter(RectHelper::bottomLeft(mCurrentRect));
206 mHandles[7].moveCenter(RectHelper::left(mCurrentRect));
207 }
208
209 void SnippingAreaResizer::arrowRightPressed()
210 {
211 if (mControlPressed) {
212 mCurrentRect.setLeft(mCurrentRect.left() + 1);
213 } else if (mAltPressed) {
214 mCurrentRect.setRight(mCurrentRect.right() + 1);
215 } else {
216 mCurrentRect.moveRight(mCurrentRect.right() + 1);
217 }
218 }
219
220 void SnippingAreaResizer::arrowLeftPressed()
221 {
222 if (mControlPressed) {
223 mCurrentRect.setLeft(mCurrentRect.left() - 1);
224 } else if (mAltPressed) {
225 mCurrentRect.setRight(mCurrentRect.right() - 1);
226 } else {
227 mCurrentRect.moveLeft(mCurrentRect.left() - 1);
228 }
229 }
230
231 void SnippingAreaResizer::arrowDownPressed()
232 {
233 if (mControlPressed) {
234 mCurrentRect.setTop(mCurrentRect.top() + 1);
235 } else if (mAltPressed) {
236 mCurrentRect.setBottom(mCurrentRect.bottom() + 1);
237 } else {
238 mCurrentRect.moveBottom(mCurrentRect.bottom() + 1);
239 }
240 }
241
242 void SnippingAreaResizer::arrowUpPressed()
243 {
244 if (mControlPressed) {
245 mCurrentRect.setTop(mCurrentRect.top() - 1);
246 } else if (mAltPressed) {
247 mCurrentRect.setBottom(mCurrentRect.bottom() - 1);
248 } else {
249 mCurrentRect.moveTop(mCurrentRect.top() - 1);
250 }
251 }