Debian Codegraph @

src/Gui/ToolHandler.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2024 Pierre-Louis Boyer <[email protected]> *
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 <cmath>
24
25 #include <QGuiApplication>
26 #include <QPainter>
27
28 #include <Inventor/events/SoKeyboardEvent.h>
29
30 #include <Base/Console.h>
31 #include <Base/Exception.h>
32
33 #include "Application.h"
34 #include "BitmapFactory.h"
35 #include "CommandT.h"
36 #include "MainWindow.h"
37 #include "View3DInventor.h"
38 #include "View3DInventorViewer.h"
39
40 #include "ToolHandler.h"
41 #include "InputHint.h"
42
43 using namespace Gui;
44
45 /**************************** ToolHandler *******************************************/
46
47 QString ToolHandler::getCrosshairCursorSVGName() const
48 {
49 return QStringLiteral("None");
50 }
51
52 bool ToolHandler::activate()
53 {
54 // save the cursor at the time the DSH is activated
55 QWidget* cw = getCursorWidget();
56 if (cw) {
57 oldCursor = cw->cursor();
58
59 updateCursor();
60 updateHint();
61
62 this->preActivated();
63 this->activated();
64 return true;
65 }
66
67 return false;
68 }
69
70 void ToolHandler::deactivate()
71 {
72 this->deactivated();
73 this->postDeactivated();
74
75 unsetCursor();
76
77 Gui::MainWindow::getInstance()->hideHints();
78 }
79
80 //**************************************************************************
81 // Helpers
82
83 unsigned long ToolHandler::getCrosshairColor()
84 {
85 unsigned long color = 0xFFFFFFFF; // white
86 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
87 "User parameter:BaseApp/Preferences/View"
88 );
89 color = hGrp->GetUnsigned("CursorCrosshairColor", color);
90 // from rgba to rgb
91 color = (color >> 8) & 0xFFFFFF;
92 return color;
93 }
94
95 void ToolHandler::setCrosshairCursor(const QString& svgName)
96 {
97 const unsigned long defaultCrosshairColor = 0xFFFFFF;
98 unsigned long color = getCrosshairColor();
99 auto colorMapping = std::map<unsigned long, unsigned long>();
100 colorMapping[defaultCrosshairColor] = color;
101 // hot spot of all SVG icons should be 8,8 for 32x32 size (16x16 for 64x64)
102 int hotX = 8;
103 int hotY = 8;
104 setSvgCursor(svgName, hotX, hotY, colorMapping);
105 }
106
107 void ToolHandler::setCrosshairCursor(const char* svgName)
108 {
109 QString cursorName = QString::fromLatin1(svgName);
110 setCrosshairCursor(cursorName);
111 }
112
113 void ToolHandler::setSvgCursor(
114 const QString& cursorName,
115 int x,
116 int y,
117 const std::map<unsigned long, unsigned long>& colorMapping
118 )
119 {
120 // The TechDraw_Pointer_*.svg icons have a default size of 64x64. When directly creating
121 // them with a size of 32x32 they look very bad.
122 // As a workaround the icons are created with 64x64 and afterwards the pixmap is scaled to
123 // 32x32. This workaround is only needed if pRatio is equal to 1.0
124 //
125 qreal pRatio = devicePixelRatio();
126 bool isRatioOne = (pRatio == 1.0);
127 qreal cursorSize = isRatioOne ? 64 : 32;
128 qreal hotX = x;
129 qreal hotY = y;
130 #if !defined(Q_OS_WIN32) && !defined(Q_OS_MACOS)
131 if (qGuiApp->platformName() == QLatin1String("xcb")) {
132 hotX *= pRatio;
133 hotY *= pRatio;
134 }
135 #endif
136
137 QPixmap pointer = Gui::BitmapFactory().pixmapFromSvg(
138 cursorName.toStdString().c_str(),
139 QSizeF {cursorSize, cursorSize},
140 colorMapping
141 );
142 if (isRatioOne) {
143 pointer = pointer.scaled(32, 32);
144 }
145 setCursor(pointer, hotX, hotY, false);
146 }
147
148 void ToolHandler::setCursor(const QPixmap& p, int x, int y, bool autoScale)
149 {
150
151 QWidget* cw = getCursorWidget();
152 if (cw) {
153 QCursor cursor;
154 QPixmap p1(p);
155 // TODO remove autoScale after all cursors are SVG-based
156 if (autoScale) {
157 qreal pRatio = devicePixelRatio();
158 int newWidth = p.width() * pRatio;
159 int newHeight = p.height() * pRatio;
160 p1 = p1.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
161 p1.setDevicePixelRatio(pRatio);
162 qreal hotX = x;
163 qreal hotY = y;
164 #if !defined(Q_OS_WIN32) && !defined(Q_OS_MACOS)
165 if (qGuiApp->platformName() == QLatin1String("xcb")) {
166 hotX *= pRatio;
167 hotY *= pRatio;
168 }
169 #endif
170 cursor = QCursor(p1, hotX, hotY);
171 }
172 else {
173 // already scaled
174 cursor = QCursor(p1, x, y);
175 }
176
177 actCursor = cursor;
178 actCursorPixmap = p1;
179
180 setWidgetCursor(cursor);
181 }
182 }
183
184 void ToolHandler::addCursorTail(std::vector<QPixmap>& pixmaps)
185 {
186 // Create a pixmap that will contain icon and each autoconstraint icon
187 QPixmap baseIcon = QPixmap(actCursorPixmap);
188 baseIcon.setDevicePixelRatio(actCursorPixmap.devicePixelRatio());
189 qreal pixelRatio = baseIcon.devicePixelRatio();
190 // cursor size in device independent pixels
191 qreal baseCursorWidth = baseIcon.width();
192 qreal baseCursorHeight = baseIcon.height();
193
194 int tailWidth = 0;
195 for (auto const& p : pixmaps) {
196 tailWidth += p.width();
197 }
198
199 int newIconWidth = baseCursorWidth + tailWidth;
200 int newIconHeight = baseCursorHeight;
201
202 QPixmap newIcon(newIconWidth * pixelRatio, newIconHeight * pixelRatio);
203 newIcon.setDevicePixelRatio(pixelRatio);
204 newIcon.fill(Qt::transparent);
205
206 QPainter qp;
207 qp.begin(&newIcon);
208
209 qp.drawPixmap(QPointF(0, 0), baseIcon);
210
211 // Iterate through pixmaps and them to the cursor pixmap
212 qreal currentIconX = baseCursorWidth;
213 qreal currentIconY;
214
215 for (auto& icon : pixmaps) {
216 currentIconY = baseCursorHeight - icon.height();
217 qp.drawPixmap(QPointF(currentIconX, currentIconY) / pixelRatio, icon);
218 currentIconX += icon.width();
219 }
220
221 qp.end(); // Finish painting
222
223 // Create the new cursor with the icon.
224 QPoint p = actCursor.hotSpot();
225 newIcon.setDevicePixelRatio(pixelRatio);
226 QCursor newCursor(newIcon, p.x(), p.y());
227 applyCursor(newCursor);
228 }
229
230 void ToolHandler::updateCursor()
231 {
232 auto cursorstring = getCrosshairCursorSVGName();
233
234 if (cursorstring != QStringLiteral("None")) {
235 setCrosshairCursor(cursorstring);
236 }
237 }
238
239 std::list<InputHint> ToolHandler::getToolHints() const
240 {
241 return {};
242 }
243
244 void ToolHandler::updateHint() const
245 {
246 Gui::getMainWindow()->showHints(getToolHints());
247 }
248
249 void ToolHandler::applyCursor()
250 {
251 applyCursor(actCursor);
252 }
253
254 void ToolHandler::applyCursor(QCursor& newCursor)
255 {
256 setWidgetCursor(newCursor);
257 }
258
259 void ToolHandler::unsetCursor()
260 {
261 setWidgetCursor(oldCursor);
262 }
263
264 qreal ToolHandler::devicePixelRatio()
265 {
266 qreal pixelRatio = 1;
267
268 QWidget* cw = getCursorWidget();
269 if (cw) {
270 pixelRatio = cw->devicePixelRatio();
271 }
272 return pixelRatio;
273 }
274
275 QWidget* ToolHandler::getCursorWidget()
276 {
277 Gui::View3DInventorViewer* viewer = getViewer();
278 if (viewer) {
279 return viewer->getWidget();
280 }
281 return nullptr;
282 }
283
284 void ToolHandler::setWidgetCursor(QCursor cursor)
285 {
286 QWidget* cw = getCursorWidget();
287 if (cw) {
288 cw->setCursor(cursor);
289 }
290 }
291
292 Gui::View3DInventorViewer* ToolHandler::getViewer()
293 {
294 Gui::MDIView* view = Gui::getMainWindow()->activeWindow();
295 if (view && view->isDerivedFrom<Gui::View3DInventor>()) {
296 return static_cast<Gui::View3DInventor*>(view)->getViewer();
297 }
298 return nullptr;
299 }