Engauge Digitizer 2
Loading...
Searching...
No Matches
GraphicsView Class Reference

QGraphicsView class with event handling added. Typically the events are sent to the active digitizing state. More...

#include <GraphicsView.h>

Inheritance diagram for GraphicsView:
Inheritance graph
Collaboration diagram for GraphicsView:
Collaboration graph

Public Slots

void slotDropRegression (QString)
 Receive drag and drop regression test url.

Signals

void signalContextMenuEventAxis (QString pointIdentifier)
 Send right click on axis point to MainWindow for editing.
void signalContextMenuEventGraph (QStringList pointIdentifiers)
 Send right click on graph point(s) to MainWindow for editing.
void signalDraggedDigFile (QString)
 Send dragged dig file to MainWindow for import. This comes from dragging an engauge dig file.
void signalDraggedImage (QImage)
 Send dragged image to MainWindow for import. This typically comes from dragging a file.
void signalDraggedImageUrl (QUrl)
 Send dragged url to MainWindow for import. This typically comes from dragging an image from a browser.
void signalKeyPress (Qt::Key, bool atLeastOneSelectedItem)
 Send keypress to MainWindow for eventual processing by DigitizeStateAbstractBase subclasses.
void signalMouseMove (QPointF)
 Send mouse move to MainWindow for eventual display of cursor coordinates in StatusBar.
void signalMousePress (QPointF)
 Send mouse press to MainWindow for creating one or more Points.
void signalMouseRelease (QPointF)
 Send mouse release to MainWindow for moving Points.
void signalViewZoomIn ()
 Send wheel event to MainWindow for zooming in.
void signalViewZoomOut ()
 Send wheel event to MainWindow for zooming out.

Public Member Functions

 GraphicsView (QGraphicsScene *scene, MainWindow &mainWindow)
 Single constructor.
virtual ~GraphicsView ()
virtual void contextMenuEvent (QContextMenuEvent *event)
 Intercept context event to support point editing.
virtual void dragEnterEvent (QDragEnterEvent *event)
 Intercept mouse drag event to support drag-and-drop.
virtual void dragMoveEvent (QDragMoveEvent *event)
 Intercept mouse move event to support drag-and-drop.
virtual void dropEvent (QDropEvent *event)
 Intercept mouse drop event to support drag-and-drop. This initiates asynchronous loading of the dragged image.
virtual void keyPressEvent (QKeyEvent *event)
 Intercept key press events to handle left/right/up/down moving.
virtual void mouseMoveEvent (QMouseEvent *event)
 Intercept mouse move events to populate the current cursor position in StatusBar.
virtual void mousePressEvent (QMouseEvent *event)
 Intercept mouse press events to create one or more Points.
virtual void mouseReleaseEvent (QMouseEvent *event)
 Intercept mouse release events to move one or more Points.
virtual void wheelEvent (QWheelEvent *event)
 Convert wheel events into zoom in/out.

Detailed Description

QGraphicsView class with event handling added. Typically the events are sent to the active digitizing state.

Definition at line 20 of file GraphicsView.h.

Constructor & Destructor Documentation

◆ GraphicsView()

GraphicsView::GraphicsView ( QGraphicsScene * scene,
MainWindow & mainWindow )

Single constructor.

Definition at line 31 of file GraphicsView.cpp.

32 :
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}
void signalDraggedImage(QImage)
Send dragged image to MainWindow for import. This typically comes from dragging a file.
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.
void signalViewZoomOut()
Send wheel event to MainWindow for zooming out.
void signalDraggedDigFile(QString)
Send dragged dig file to MainWindow for import. This comes from dragging an engauge dig file.
void signalContextMenuEventAxis(QString pointIdentifier)
Send right click on axis point to MainWindow for editing.
void signalViewZoomIn()
Send wheel event to MainWindow for zooming in.
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.

◆ ~GraphicsView()

GraphicsView::~GraphicsView ( )
virtual

Definition at line 70 of file GraphicsView.cpp.

71{
72}

Member Function Documentation

◆ contextMenuEvent()

void GraphicsView::contextMenuEvent ( QContextMenuEvent * event)
virtual

Intercept context event to support point editing.

Definition at line 74 of file GraphicsView.cpp.

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}
log4cpp::Category * mainCat
Definition Logger.cpp:14
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.
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18

◆ dragEnterEvent()

void GraphicsView::dragEnterEvent ( QDragEnterEvent * event)
virtual

Intercept mouse drag event to support drag-and-drop.

Definition at line 103 of file GraphicsView.cpp.

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}

◆ dragMoveEvent()

void GraphicsView::dragMoveEvent ( QDragMoveEvent * event)
virtual

Intercept mouse move event to support drag-and-drop.

Definition at line 113 of file GraphicsView.cpp.

114{
115 LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dragMoveEvent";
116
117 if (event->mimeData ()->hasImage () ||
118 event->mimeData ()->hasUrls ()) {
119 event->acceptProposedAction();
120 }
121}

◆ dropEvent()

void GraphicsView::dropEvent ( QDropEvent * event)
virtual

Intercept mouse drop event to support drag-and-drop. This initiates asynchronous loading of the dragged image.

Definition at line 123 of file GraphicsView.cpp.

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}

◆ keyPressEvent()

void GraphicsView::keyPressEvent ( QKeyEvent * event)
virtual

Intercept key press events to handle left/right/up/down moving.

Definition at line 204 of file GraphicsView.cpp.

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}
#define LOG4CPP_DEBUG_S(logger)
Definition convenience.h:20

◆ mouseMoveEvent()

void GraphicsView::mouseMoveEvent ( QMouseEvent * event)
virtual

Intercept mouse move events to populate the current cursor position in StatusBar.

Definition at line 232 of file GraphicsView.cpp.

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}

◆ mousePressEvent()

void GraphicsView::mousePressEvent ( QMouseEvent * event)
virtual

Intercept mouse press events to create one or more Points.

Definition at line 250 of file GraphicsView.cpp.

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}

◆ mouseReleaseEvent()

void GraphicsView::mouseReleaseEvent ( QMouseEvent * event)
virtual

Intercept mouse release events to move one or more Points.

Definition at line 267 of file GraphicsView.cpp.

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}

◆ signalContextMenuEventAxis

void GraphicsView::signalContextMenuEventAxis ( QString pointIdentifier)
signal

Send right click on axis point to MainWindow for editing.

◆ signalContextMenuEventGraph

void GraphicsView::signalContextMenuEventGraph ( QStringList pointIdentifiers)
signal

Send right click on graph point(s) to MainWindow for editing.

◆ signalDraggedDigFile

void GraphicsView::signalDraggedDigFile ( QString )
signal

Send dragged dig file to MainWindow for import. This comes from dragging an engauge dig file.

◆ signalDraggedImage

void GraphicsView::signalDraggedImage ( QImage )
signal

Send dragged image to MainWindow for import. This typically comes from dragging a file.

◆ signalDraggedImageUrl

void GraphicsView::signalDraggedImageUrl ( QUrl )
signal

Send dragged url to MainWindow for import. This typically comes from dragging an image from a browser.

◆ signalKeyPress

void GraphicsView::signalKeyPress ( Qt::Key ,
bool atLeastOneSelectedItem )
signal

Send keypress to MainWindow for eventual processing by DigitizeStateAbstractBase subclasses.

◆ signalMouseMove

void GraphicsView::signalMouseMove ( QPointF )
signal

Send mouse move to MainWindow for eventual display of cursor coordinates in StatusBar.

◆ signalMousePress

void GraphicsView::signalMousePress ( QPointF )
signal

Send mouse press to MainWindow for creating one or more Points.

◆ signalMouseRelease

void GraphicsView::signalMouseRelease ( QPointF )
signal

Send mouse release to MainWindow for moving Points.

◆ signalViewZoomIn

void GraphicsView::signalViewZoomIn ( )
signal

Send wheel event to MainWindow for zooming in.

◆ signalViewZoomOut

void GraphicsView::signalViewZoomOut ( )
signal

Send wheel event to MainWindow for zooming out.

◆ slotDropRegression

void GraphicsView::slotDropRegression ( QString urlText)
slot

Receive drag and drop regression test url.

Definition at line 314 of file GraphicsView.cpp.

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}

◆ wheelEvent()

void GraphicsView::wheelEvent ( QWheelEvent * event)
virtual

Convert wheel events into zoom in/out.

Definition at line 333 of file GraphicsView.cpp.

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}

The documentation for this class was generated from the following files: