Engauge Digitizer 2
Loading...
Searching...
No Matches
GraphicsView.cpp
Go to the documentation of this file.
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7#include "DataKey.h"
8#include "EngaugeAssert.h"
10#include "GraphicsItemType.h"
11#include "GraphicsView.h"
12#include "LoadFileInfo.h"
13#include "Logger.h"
14#include "MainWindow.h"
15#include "Point.h"
16#include <QApplication>
17#include <QContextMenuEvent>
18#include <QDebug>
19#include <QDropEvent>
20#include <QGraphicsPixmapItem>
21#include <QGraphicsPolygonItem>
22#include <QGraphicsScene>
23#include <QMimeData>
24#include <QMouseEvent>
25#include <QScrollBar>
26#include "QtToString.h"
27#include "UrlDirty.h"
28
29extern const QString AXIS_CURVE_NAME;
30
31GraphicsView::GraphicsView(QGraphicsScene *scene,
32 MainWindow &mainWindow) :
33 QGraphicsView (scene)
34{
35 connect (this, SIGNAL (signalContextMenuEventAxis (QString)), &mainWindow, SLOT (slotContextMenuEventAxis (QString)));
36 connect (this, SIGNAL (signalContextMenuEventGraph (QStringList)), &mainWindow, SLOT (slotContextMenuEventGraph (QStringList)));
37 connect (this, SIGNAL (signalDraggedDigFile (QString)), &mainWindow, SLOT (slotFileOpenDraggedDigFile (QString)));
38 connect (this, SIGNAL (signalDraggedImage (QImage)), &mainWindow, SLOT (slotFileImportDraggedImage (QImage)));
39 connect (this, SIGNAL (signalDraggedImageUrl (QUrl)), &mainWindow, SLOT (slotFileImportDraggedImageUrl (QUrl)));
40 connect (this, SIGNAL (signalKeyPress (Qt::Key, bool)), &mainWindow, SLOT (slotKeyPress (Qt::Key, bool)));
41 connect (this, SIGNAL (signalMouseMove(QPointF)), &mainWindow, SLOT (slotMouseMove (QPointF)));
42 connect (this, SIGNAL (signalMousePress (QPointF)), &mainWindow, SLOT (slotMousePress (QPointF)));
43 connect (this, SIGNAL (signalMouseRelease (QPointF)), &mainWindow, SLOT (slotMouseRelease (QPointF)));
44 connect (this, SIGNAL (signalViewZoomIn ()), &mainWindow, SLOT (slotViewZoomInFromWheelEvent ()));
45 connect (this, SIGNAL (signalViewZoomOut ()), &mainWindow, SLOT (slotViewZoomOutFromWheelEvent ()));
46
47 setMouseTracking (true);
48 setAcceptDrops (true);
49 setEnabled (true);
50 setRenderHints(QPainter::Antialiasing);
51 setBackgroundBrush (QBrush (QColor (Qt::gray)));
52 verticalScrollBar()->setCursor (QCursor (Qt::ArrowCursor));
53 horizontalScrollBar()->setCursor (QCursor (Qt::ArrowCursor));
54
55 // Skip setStatusTip here since that will overwrite much more important messages, and trigger gratuitous showing of status bar
56 setWhatsThis (tr ("Main Window\n\n"
57 "After an image file is imported, or an Engauge Document opened, an image appears in this area. "
58 "Points are added to the image.\n\n"
59 "If the image is a graph with two axes and one or more curves, then three axis points must be "
60 "created along those axes. Just put two axis points on one axis and a third axis point on the other "
61 "axis, as far apart as possible for higher accuracy. Then curve points can be added along the curves.\n\n"
62 "If the image is a map with a scale to define length, then two axis points must be "
63 "created at either end of the scale. Then curve points can be added.\n\n"
64 "Zooming the image in or out is performed using any of several methods:\n"
65 "1) rotating the mouse wheel when the cursor is outside of the image\n"
66 "2) pressing the minus or plus keys\n"
67 "3) selecting a new zoom setting from the View/Zoom menu"));
68}
69
73
74void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
75{
76 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::contextMenuEvent"
77 << " selectedCount=" << scene()->selectedItems().count();
78
79 GraphicsItemsExtractor graphicsItemsExtractor;
80 const QList<QGraphicsItem*> &items = scene()->selectedItems();
81 QStringList pointIdentifiers = graphicsItemsExtractor.selectedPointIdentifiers(items);
82
83 if (pointIdentifiers.count() > 0) {
84
85 if (graphicsItemsExtractor.allSelectedItemsAreEitherAxisOrGraph (items,
86 GRAPH_POINTS)) {
87
88 // One or more graph points are selected so edit their coordinates
89 emit signalContextMenuEventGraph (pointIdentifiers);
90
91 } else if (graphicsItemsExtractor.allSelectedItemsAreEitherAxisOrGraph (items,
92 AXIS_POINTS) && pointIdentifiers.count() == 1) {
93
94 // A single axis point is selected so edit it
95 emit signalContextMenuEventAxis (pointIdentifiers.first());
96
97 }
98 }
99
100 QGraphicsView::contextMenuEvent (event);
101}
102
103void GraphicsView::dragEnterEvent (QDragEnterEvent *event)
104{
105 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dragEnterEvent " << (event->mimeData ()->hasUrls () ? "urls" : "non-urls");
106
107 if (event->mimeData ()->hasImage () ||
108 event->mimeData ()->hasUrls ()) {
109 event->acceptProposedAction();
110 }
111}
112
113void GraphicsView::dragMoveEvent (QDragMoveEvent *event)
114{
115 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dragMoveEvent";
116
117 if (event->mimeData ()->hasImage () ||
118 event->mimeData ()->hasUrls ()) {
119 event->acceptProposedAction();
120 }
121}
122
123void GraphicsView::dropEvent (QDropEvent *event)
124{
125 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dropEvent";
126
127 // Urls from text/uri-list
128 QList<QUrl> urlList = event->mimeData ()->urls ();
129
130 const QString MIME_FORMAT_TEXT_PLAIN ("text/plain");
131 QString textPlain (event->mimeData()->data (MIME_FORMAT_TEXT_PLAIN));
132
133 QUrl urlFirst;
134 if (event->mimeData ()->hasUrls () &&
135 urlList.count () > 0) {
136 urlFirst = urlList.at (0);
137 }
138
139 QImage image;
140 if (event->mimeData()->hasImage()) {
141 image = qvariant_cast<QImage> (event->mimeData ()->imageData ());
142 }
143
144 if (handleDropEvent (textPlain,
145 event->mimeData ()->hasUrls (),
146 urlFirst,
147 event->mimeData ()->hasImage (),
148 image)) {
149
150 event->acceptProposedAction();
151
152 } else {
153
154 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dropEvent dropped";
155 QGraphicsView::dropEvent (event);
156 }
157}
158
159bool GraphicsView::handleDropEvent (const QString &possibleDigFileName,
160 bool hasUrl,
161 const QUrl &urlFirst,
162 bool hasImage,
163 const QImage &image)
164{
165 bool willAccept = false;
166
167 LoadFileInfo loadFileInfo;
168 if (loadFileInfo.loadsAsDigFile (possibleDigFileName)) {
169
170 // Branch that applies when a dig file name has been dropped
171 LOG4CPP_INFO_S ((*mainCat)) << "QGraphicsView::handleDropEvent dig file";
172 UrlDirty url (possibleDigFileName);
173 emit signalDraggedDigFile (url.toLocalFile());
174 willAccept = true;
175
176 } else if (hasImage) {
177
178 // Branch that applies when an image selected within another application (e.g. LibreOffice Draw) has been dropped
179 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::handleDropEvent image";
180 emit signalDraggedImage (image);
181 willAccept = true;
182
183 } else if (hasUrl) {
184
185 // Branch that applies when a local file name or internet url has been dropped
186 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::handleDropEvent url=" << urlFirst.toString ().toLatin1 ().data ();
187 emit signalDraggedImageUrl (urlFirst);
188 willAccept = true;
189 }
190
191 return willAccept;
192}
193
194bool GraphicsView::inBounds (const QPointF &posScreen)
195{
196 QRectF boundingRect = scene()->sceneRect();
197
198 return 0 <= posScreen.x () &&
199 0 <= posScreen.y () &&
200 posScreen.x () < boundingRect.width() &&
201 posScreen.y () < boundingRect.height();
202}
203
204void GraphicsView::keyPressEvent (QKeyEvent *event)
205{
206 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::keyPressEvent";
207
208 // Intercept up/down/left/right if any items are selected
209 Qt::Key key = static_cast<Qt::Key> (event->key());
210
211 bool atLeastOneSelectedItem = (scene ()->selectedItems ().count () > 0);
212
213 // Arrow keys are for several states to move newly created points. Escape key is
214 // for DigitizeStateGuideline
215 if (key == Qt::Key_Down ||
216 key == Qt::Key_Left ||
217 key == Qt::Key_Right ||
218 key == Qt::Key_Up ||
219 key == Qt::Key_Escape ||
220 ((event->modifiers() & Qt::ControlModifier) && (key == Qt::Key_Z))) {
221
222 emit signalKeyPress (key, atLeastOneSelectedItem);
223 event->accept();
224
225 } else {
226
227 QGraphicsView::keyPressEvent (event);
228
229 }
230}
231
232void GraphicsView::mouseMoveEvent (QMouseEvent *event)
233{
234// LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mouseMoveEvent cursor="
235// << QtCursorToString (cursor().shape()).toLatin1 ().data ();
236
237 QPointF posScreen = mapToScene (event->pos ());
238
239 if (!inBounds (posScreen)) {
240
241 // Set to out-of-bounds value
242 posScreen = QPointF (-1.0, -1.0);
243 }
244
245 emit signalMouseMove (posScreen);
246
247 QGraphicsView::mouseMoveEvent (event);
248}
249
250void GraphicsView::mousePressEvent (QMouseEvent *event)
251{
252 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mousePressEvent";
253
254 QPointF posScreen = mapToScene (event->pos ());
255
256 if (!inBounds (posScreen)) {
257
258 // Set to out-of-bounds value
259 posScreen = QPointF (-1.0, -1.0);
260 }
261
262 emit signalMousePress (posScreen);
263
264 QGraphicsView::mousePressEvent (event);
265}
266
267void GraphicsView::mouseReleaseEvent (QMouseEvent *event)
268{
269 LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mouseReleaseEvent signalMouseRelease";
270
271 QPointF posScreen = mapToScene (event->pos ());
272
273 if (!inBounds (posScreen)) {
274
275 // Set to out-of-bounds value
276 posScreen = QPointF (-1.0, -1.0);
277 }
278
279 // Send a signal, unless this is a right click. We still send if out of bounds since
280 // a click-and-drag often ends out of bounds (and user is unlikely to expect different
281 // behavior when endpoint is outside, versus inside, the image boundary)
282 int bitFlag = (unsigned (event->buttons ()) & Qt::RightButton);
283 bool isRightClick = (bitFlag != 0);
284
285 if (!isRightClick) {
286
287 emit signalMouseRelease (posScreen);
288
289 }
290
291 QGraphicsView::mouseReleaseEvent (event);
292}
293
294QStringList GraphicsView::pointIdentifiersFromSelection (const QList<QGraphicsItem*> &items) const
295{
296 // This method assumes that all specified items are points
297
298 QStringList pointIdentifiers;
299
300 QList<QGraphicsItem*>::const_iterator itr;
301 for (itr = items.begin(); itr != items.end(); itr++) {
302
303 QGraphicsItem *item = *itr;
304 GraphicsItemType type = static_cast<GraphicsItemType> (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt ());
306
307 QString pointIdentifier = item->data (DATA_KEY_IDENTIFIER).toString ();
308 pointIdentifiers << pointIdentifier;
309 }
310
311 return pointIdentifiers;
312}
313
315{
316 // Regression test with local file or internet url specified
317 QString emptyDigFileName;
318 bool hasUrl = true;
319 QUrl url (urlText); // Works as is for internet url
320 if (!urlText.contains ("http")) {
321 url = QUrl::fromLocalFile (urlText);
322 }
323 bool hasImage = false;
324 QImage emptyImage;
325
326 handleDropEvent (emptyDigFileName,
327 hasUrl,
328 url,
329 hasImage,
330 emptyImage);
331}
332
333void GraphicsView::wheelEvent(QWheelEvent *event)
334{
335 const int ANGLE_THRESHOLD = 15; // From QWheelEvent documentation
336 const int DELTAS_PER_DEGREE = 8; // From QWheelEvent documentation
337
338 QPoint numDegrees = event->angleDelta() / DELTAS_PER_DEGREE;
339
340 LOG4CPP_INFO_S ((*mainCat)) << "MainWindow::wheelEvent"
341 << " degrees=" << numDegrees.y()
342 << " phase=" << event->phase();
343
344 // Criteria:
345 // 1) User has enabled wheel zoom control (but that is not known here so MainWindow will handle that part)
346 // in slotViewZoomInFromWheelEvent and slotViewZoomOutFromWheelEvent
347 // 2) Angle is over a threshold to eliminate false events from just touching wheel
348 if ((event->modifiers() & Qt::ControlModifier) != 0) {
349
350 if (numDegrees.y() >= ANGLE_THRESHOLD) {
351
352 // Rotated backwards towards the user, which means zoom in
353 emit signalViewZoomIn();
354
355 } else if (numDegrees.y() <= -ANGLE_THRESHOLD) {
356
357 // Rotated forwards away from the user, which means zoom out
358 emit signalViewZoomOut();
359
360 }
361
362 // Accept the event as long as Control key was used and we are capturing wheel event
363 event->accept();
364
365 } else {
366
367 // Let non-Control events manage scrolling
368 QGraphicsView::wheelEvent (event);
369
370 }
371}
const QString AXIS_CURVE_NAME
@ DATA_KEY_GRAPHICS_ITEM_TYPE
Definition DataKey.h:15
@ DATA_KEY_IDENTIFIER
Definition DataKey.h:14
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
GraphicsItemType
Runtime type identification (RTTI) for QGraphicsItem objects.
@ GRAPHICS_ITEM_TYPE_POINT
log4cpp::Category * mainCat
Definition Logger.cpp:14
This class consolidates utility routines that deal with graphics items that are getting extracted fro...
bool allSelectedItemsAreEitherAxisOrGraph(const QList< QGraphicsItem * > &items, AxisOrGraph axisOrGraph) const
Return true if all selected points are of the specified axis or graph type.
QStringList selectedPointIdentifiers(const QList< QGraphicsItem * > &items) const
Return list of selected point identifiers.
virtual void mouseReleaseEvent(QMouseEvent *event)
Intercept mouse release events to move one or more Points.
void signalDraggedImage(QImage)
Send dragged image to MainWindow for import. This typically comes from dragging a file.
virtual void wheelEvent(QWheelEvent *event)
Convert wheel events into zoom in/out.
virtual void dropEvent(QDropEvent *event)
Intercept mouse drop event to support drag-and-drop. This initiates asynchronous loading of the dragg...
void slotDropRegression(QString)
Receive drag and drop regression test url.
virtual ~GraphicsView()
void signalKeyPress(Qt::Key, bool atLeastOneSelectedItem)
Send keypress to MainWindow for eventual processing by DigitizeStateAbstractBase subclasses.
void signalMouseRelease(QPointF)
Send mouse release to MainWindow for moving Points.
virtual void dragEnterEvent(QDragEnterEvent *event)
Intercept mouse drag event to support drag-and-drop.
virtual void keyPressEvent(QKeyEvent *event)
Intercept key press events to handle left/right/up/down moving.
void signalViewZoomOut()
Send wheel event to MainWindow for zooming out.
virtual void dragMoveEvent(QDragMoveEvent *event)
Intercept mouse move event to support drag-and-drop.
void signalDraggedDigFile(QString)
Send dragged dig file to MainWindow for import. This comes from dragging an engauge dig file.
virtual void mousePressEvent(QMouseEvent *event)
Intercept mouse press events to create one or more Points.
void signalContextMenuEventAxis(QString pointIdentifier)
Send right click on axis point to MainWindow for editing.
void signalViewZoomIn()
Send wheel event to MainWindow for zooming in.
GraphicsView(QGraphicsScene *scene, MainWindow &mainWindow)
Single constructor.
virtual void mouseMoveEvent(QMouseEvent *event)
Intercept mouse move events to populate the current cursor position in StatusBar.
void signalMousePress(QPointF)
Send mouse press to MainWindow for creating one or more Points.
void signalDraggedImageUrl(QUrl)
Send dragged url to MainWindow for import. This typically comes from dragging an image from a browser...
void signalContextMenuEventGraph(QStringList pointIdentifiers)
Send right click on graph point(s) to MainWindow for editing.
void signalMouseMove(QPointF)
Send mouse move to MainWindow for eventual display of cursor coordinates in StatusBar.
virtual void contextMenuEvent(QContextMenuEvent *event)
Intercept context event to support point editing.
Returns information about files.
bool loadsAsDigFile(const QString &urlString) const
Returns true if specified file name can be loaded as a DIG file.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition MainWindow.h:95
Adds ability to QUrl to cleanup url path.
Definition UrlDirty.h:16
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18
#define LOG4CPP_DEBUG_S(logger)
Definition convenience.h:20