Debian Codegraph @

src/gui/snippingArea/SnippingAreaSelector.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 "SnippingAreaSelector.h"
21
22 SnippingAreaSelector::SnippingAreaSelector(const QSharedPointer<IConfig> &config, QObject *parent) :
23 QObject(parent),
24 mIsActive(false),
25 mConfig(config),
26 mIsMouseDown(false),
27 mMouseCursor()
28 {
29
30 }
31
32 void SnippingAreaSelector::activate(const QRectF &snippingAreaGeometry, const QPointF &pos)
33 {
34 setupAdorner();
35 mIsActive = true;
36 mSnippingAreaGeometry = snippingAreaGeometry;
37 mAdornerColor = mConfig->snippingAdornerColor();
38 mCursorColor = mConfig->snippingCursorColor();
39 updateCurrentRect({}, pos);
40 updateAdorner(pos);
41 emit cursorChanged(CustomCursor(mConfig->snippingCursorColor(), mConfig->snippingCursorSize()));
42 }
43
44 void SnippingAreaSelector::deactivate()
45 {
46 mIsActive = false;
47 mIsMouseDown = false;
48 mMouseDownPos = {};
49 }
50
51 void SnippingAreaSelector::paint(QPainter *painter)
52 {
53 if(mIsActive) {
54 mAdorner.paint(painter, mAdornerColor, mCursorColor);
55
56 painter->setClipping(false);
57 painter->setRenderHint(QPainter::Antialiasing, false);
58
59 painter->setPen(mAdornerColor);
60 painter->drawRect(mCurrentRect);
61 }
62 }
63
64 void SnippingAreaSelector::setBackgroundImage(const QPixmap *background)
65 {
66 mAdorner.setBackgroundImage(background);
67 }
68
69 bool SnippingAreaSelector::isActive() const
70 {
71 return mIsActive;
72 }
73
74 void SnippingAreaSelector::handleMousePress(const QPointF &pos)
75 {
76 if(mIsActive) {
77 mMouseDownPos = pos;
78 setIsMouseDown(true);
79 rectChanged(QRectF(mMouseDownPos, mMouseDownPos));
80 }
81 }
82
83 void SnippingAreaSelector::handleMouseRelease()
84 {
85 if(mIsActive) {
86 setIsMouseDown(false);
87 }
88 }
89
90 void SnippingAreaSelector::handleMouseMove(const QPointF &pos)
91 {
92 if(mIsActive) {
93 if(mIsMouseDown) {
94 const auto rect = QRectF(mMouseDownPos, pos).normalized();
95 updateCurrentRect(rect, pos);
96 }
97
98 updateAdorner(pos);
99 }
100 }
101
102 void SnippingAreaSelector::handleKeyPress(QKeyEvent *event)
103 {
104 if (mIsActive) {
105 // Get the current mouse cursor position and move it.
106 // This triggers the mouse move event
107 const QPoint mouseCursorPosition = mMouseCursor.pos();
108 switch (event->key()) {
109 case Qt::Key_Up:
110 mMouseCursor.setPos(mouseCursorPosition + QPoint(0, -1));
111 break;
112 case Qt::Key_Down:
113 mMouseCursor.setPos(mouseCursorPosition + QPoint(0, 1));
114 break;
115 case Qt::Key_Left:
116 mMouseCursor.setPos(mouseCursorPosition + QPoint(-1, 0));
117 break;
118 case Qt::Key_Right:
119 mMouseCursor.setPos(mouseCursorPosition + QPoint(1, 0));
120 break;
121 }
122 }
123 }
124
125 void SnippingAreaSelector::updateAdorner(const QPointF &pos)
126 {
127 mAdorner.update(pos.toPoint(), mSnippingAreaGeometry.toRect(), mCurrentRect.toRect());
128 }
129
130 void SnippingAreaSelector::updateCurrentRect(const QRectF &rect, const QPointF &pos)
131 {
132 mCurrentRect = rect;
133 emit rectChanged(rect);
134 }
135
136 void SnippingAreaSelector::setupAdorner()
137 {
138 auto magnifyingGlassEnabled = mConfig->snippingAreaMagnifyingGlassEnabled();
139 auto freezeImageWhileSnippingEnabled = mConfig->freezeImageWhileSnippingEnabled();
140 mAdorner.setRulersEnabled(mConfig->snippingAreaRulersEnabled());
141 mAdorner.setPositionAndSizeInfoEnabled(mConfig->snippingAreaPositionAndSizeInfoEnabled());
142 mAdorner.setMagnifyingGlassEnabled(magnifyingGlassEnabled && freezeImageWhileSnippingEnabled);
143 }
144
145 void SnippingAreaSelector::setIsMouseDown(bool isMouseDown)
146 {
147 mIsMouseDown = isMouseDown;
148 mAdorner.setIsMouseDown(isMouseDown);
149 }