Engauge Digitizer  2
Public Slots | Signals | Public Member Functions | List of all members
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. More...
 

Signals

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

Public Member Functions

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

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 30 of file GraphicsView.cpp.

31  :
32  QGraphicsView (scene)
33 {
34  connect (this, SIGNAL (signalContextMenuEventAxis (QString)), &mainWindow, SLOT (slotContextMenuEventAxis (QString)));
35  connect (this, SIGNAL (signalContextMenuEventGraph (QStringList)), &mainWindow, SLOT (slotContextMenuEventGraph (QStringList)));
36  connect (this, SIGNAL (signalDraggedDigFile (QString)), &mainWindow, SLOT (slotFileOpenDraggedDigFile (QString)));
37  connect (this, SIGNAL (signalDraggedImage (QImage)), &mainWindow, SLOT (slotFileImportDraggedImage (QImage)));
38  connect (this, SIGNAL (signalDraggedImageUrl (QUrl)), &mainWindow, SLOT (slotFileImportDraggedImageUrl (QUrl)));
39  connect (this, SIGNAL (signalKeyPress (Qt::Key, bool)), &mainWindow, SLOT (slotKeyPress (Qt::Key, bool)));
40  connect (this, SIGNAL (signalMouseMove(QPointF)), &mainWindow, SLOT (slotMouseMove (QPointF)));
41  connect (this, SIGNAL (signalMousePress (QPointF)), &mainWindow, SLOT (slotMousePress (QPointF)));
42  connect (this, SIGNAL (signalMouseRelease (QPointF)), &mainWindow, SLOT (slotMouseRelease (QPointF)));
43  connect (this, SIGNAL (signalViewZoomIn ()), &mainWindow, SLOT (slotViewZoomInFromWheelEvent ()));
44  connect (this, SIGNAL (signalViewZoomOut ()), &mainWindow, SLOT (slotViewZoomOutFromWheelEvent ()));
45 
46  setMouseTracking (true);
47  setAcceptDrops (true);
48  setEnabled (true);
49  setRenderHints(QPainter::Antialiasing);
50  setBackgroundBrush (QBrush (QColor (Qt::gray)));
51  verticalScrollBar()->setCursor (QCursor (Qt::ArrowCursor));
52  horizontalScrollBar()->setCursor (QCursor (Qt::ArrowCursor));
53 
54  // Skip setStatusTip here since that will overwrite much more important messages, and trigger gratuitous showing of status bar
55  setWhatsThis (tr ("Main Window\n\n"
56  "After an image file is imported, or an Engauge Document opened, an image appears in this area. "
57  "Points are added to the image.\n\n"
58  "If the image is a graph with two axes and one or more curves, then three axis points must be "
59  "created along those axes. Just put two axis points on one axis and a third axis point on the other "
60  "axis, as far apart as possible for higher accuracy. Then curve points can be added along the curves.\n\n"
61  "If the image is a map with a scale to define length, then two axis points must be "
62  "created at either end of the scale. Then curve points can be added.\n\n"
63  "Zooming the image in or out is performed using any of several methods:\n"
64  "1) rotating the mouse wheel when the cursor is outside of the image\n"
65  "2) pressing the minus or plus keys\n"
66  "3) selecting a new zoom setting from the View/Zoom menu"));
67 }
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 signalKeyPress(Qt::Key, bool atLeastOneSelectedItem)
Send keypress to MainWindow for eventual processing by DigitizeStateAbstractBase subclasses.
void signalDraggedImage(QImage)
Send dragged image to MainWindow for import. This typically comes from dragging a file.
void signalViewZoomIn()
Send wheel event to MainWindow for zooming in.
void signalDraggedDigFile(QString)
Send dragged dig file to MainWindow for import. This comes from dragging an engauge dig file.
void signalMouseRelease(QPointF)
Send mouse release to MainWindow for moving Points.
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 signalDraggedImageUrl(QUrl)
Send dragged url to MainWindow for import. This typically comes from dragging an image from a browser...
void signalViewZoomOut()
Send wheel event to MainWindow for zooming out.

◆ ~GraphicsView()

GraphicsView::~GraphicsView ( )
virtual

Definition at line 69 of file GraphicsView.cpp.

70 {
71 }

Member Function Documentation

◆ contextMenuEvent()

void GraphicsView::contextMenuEvent ( QContextMenuEvent *  event)
virtual

Intercept context event to support point editing.

Definition at line 73 of file GraphicsView.cpp.

74 {
75  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::contextMenuEvent"
76  << " selectedCount=" << scene()->selectedItems().count();
77 
78  GraphicsItemsExtractor graphicsItemsExtractor;
79  const QList<QGraphicsItem*> &items = scene()->selectedItems();
80  QStringList pointIdentifiers = graphicsItemsExtractor.selectedPointIdentifiers(items);
81 
82  if (pointIdentifiers.count() > 0) {
83 
84  if (graphicsItemsExtractor.allSelectedItemsAreEitherAxisOrGraph (items,
85  GRAPH_POINTS)) {
86 
87  // One or more graph points are selected so edit their coordinates
88  emit signalContextMenuEventGraph (pointIdentifiers);
89 
90  } else if (graphicsItemsExtractor.allSelectedItemsAreEitherAxisOrGraph (items,
91  AXIS_POINTS) && pointIdentifiers.count() == 1) {
92 
93  // A single axis point is selected so edit it
94  emit signalContextMenuEventAxis (pointIdentifiers.first());
95 
96  }
97  }
98 
99  QGraphicsView::contextMenuEvent (event);
100 }
QStringList selectedPointIdentifiers(const QList< QGraphicsItem * > &items) const
Return list of selected point identifiers.
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
bool allSelectedItemsAreEitherAxisOrGraph(const QList< QGraphicsItem * > &items, AxisOrGraph axisOrGraph) const
Return true if all selected points are of the specified axis or graph type.
This class consolidates utility routines that deal with graphics items that are getting extracted fro...
log4cpp::Category * mainCat
Definition: Logger.cpp:14
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.

◆ dragEnterEvent()

void GraphicsView::dragEnterEvent ( QDragEnterEvent *  event)
virtual

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

Definition at line 102 of file GraphicsView.cpp.

103 {
104  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dragEnterEvent " << (event->mimeData ()->hasUrls () ? "urls" : "non-urls");
105 
106  if (event->mimeData ()->hasImage () ||
107  event->mimeData ()->hasUrls ()) {
108  event->acceptProposedAction();
109  }
110 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
log4cpp::Category * mainCat
Definition: Logger.cpp:14

◆ dragMoveEvent()

void GraphicsView::dragMoveEvent ( QDragMoveEvent *  event)
virtual

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

Definition at line 112 of file GraphicsView.cpp.

113 {
114  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dragMoveEvent";
115 
116  if (event->mimeData ()->hasImage () ||
117  event->mimeData ()->hasUrls ()) {
118  event->acceptProposedAction();
119  }
120 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
log4cpp::Category * mainCat
Definition: Logger.cpp:14

◆ 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 122 of file GraphicsView.cpp.

123 {
124  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dropEvent";
125 
126  // Urls from text/uri-list
127  QList<QUrl> urlList = event->mimeData ()->urls ();
128 
129  const QString MIME_FORMAT_TEXT_PLAIN ("text/plain");
130  QString textPlain (event->mimeData()->data (MIME_FORMAT_TEXT_PLAIN));
131 
132  QUrl urlFirst;
133  if (event->mimeData ()->hasUrls () &&
134  urlList.count () > 0) {
135  urlFirst = urlList.at (0);
136  }
137 
138  QImage image;
139  if (event->mimeData()->hasImage()) {
140  image = qvariant_cast<QImage> (event->mimeData ()->imageData ());
141  }
142 
143  if (handleDropEvent (textPlain,
144  event->mimeData ()->hasUrls (),
145  urlFirst,
146  event->mimeData ()->hasImage (),
147  image)) {
148 
149  event->acceptProposedAction();
150 
151  } else {
152 
153  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsView::dropEvent dropped";
154  QGraphicsView::dropEvent (event);
155  }
156 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
log4cpp::Category * mainCat
Definition: Logger.cpp:14

◆ keyPressEvent()

void GraphicsView::keyPressEvent ( QKeyEvent *  event)
virtual

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

Definition at line 203 of file GraphicsView.cpp.

204 {
205  LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::keyPressEvent";
206 
207  // Intercept up/down/left/right if any items are selected
208  Qt::Key key = static_cast<Qt::Key> (event->key());
209 
210  bool atLeastOneSelectedItem = (scene ()->selectedItems ().count () > 0);
211 
212  if (key == Qt::Key_Down ||
213  key == Qt::Key_Left ||
214  key == Qt::Key_Right ||
215  key == Qt::Key_Up) {
216 
217  emit signalKeyPress (key, atLeastOneSelectedItem);
218  event->accept();
219 
220  } else {
221 
222  QGraphicsView::keyPressEvent (event);
223 
224  }
225 }
void signalKeyPress(Qt::Key, bool atLeastOneSelectedItem)
Send keypress to MainWindow for eventual processing by DigitizeStateAbstractBase subclasses.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
#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 227 of file GraphicsView.cpp.

228 {
229 // LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mouseMoveEvent cursor="
230 // << QtCursorToString (cursor().shape()).toLatin1 ().data ();
231 
232  QPointF posScreen = mapToScene (event->pos ());
233 
234  if (!inBounds (posScreen)) {
235 
236  // Set to out-of-bounds value
237  posScreen = QPointF (-1.0, -1.0);
238  }
239 
240  emit signalMouseMove (posScreen);
241 
242  QGraphicsView::mouseMoveEvent (event);
243 }
void signalMouseMove(QPointF)
Send mouse move to MainWindow for eventual display of cursor coordinates in StatusBar.

◆ mousePressEvent()

void GraphicsView::mousePressEvent ( QMouseEvent *  event)
virtual

Intercept mouse press events to create one or more Points.

Definition at line 245 of file GraphicsView.cpp.

246 {
247  LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mousePressEvent";
248 
249  QPointF posScreen = mapToScene (event->pos ());
250 
251  if (!inBounds (posScreen)) {
252 
253  // Set to out-of-bounds value
254  posScreen = QPointF (-1.0, -1.0);
255  }
256 
257  emit signalMousePress (posScreen);
258 
259  QGraphicsView::mousePressEvent (event);
260 }
void signalMousePress(QPointF)
Send mouse press to MainWindow for creating one or more Points.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
#define LOG4CPP_DEBUG_S(logger)
Definition: convenience.h:20

◆ mouseReleaseEvent()

void GraphicsView::mouseReleaseEvent ( QMouseEvent *  event)
virtual

Intercept mouse release events to move one or more Points.

Definition at line 262 of file GraphicsView.cpp.

263 {
264  LOG4CPP_DEBUG_S ((*mainCat)) << "GraphicsView::mouseReleaseEvent signalMouseRelease";
265 
266  QPointF posScreen = mapToScene (event->pos ());
267 
268  if (!inBounds (posScreen)) {
269 
270  // Set to out-of-bounds value
271  posScreen = QPointF (-1.0, -1.0);
272  }
273 
274  // Send a signal, unless this is a right click. We still send if out of bounds since
275  // a click-and-drag often ends out of bounds (and user is unlikely to expect different
276  // behavior when endpoint is outside, versus inside, the image boundary)
277  int bitFlag = (unsigned (event->buttons ()) & Qt::RightButton);
278  bool isRightClick = (bitFlag != 0);
279 
280  if (!isRightClick) {
281 
282  emit signalMouseRelease (posScreen);
283 
284  }
285 
286  QGraphicsView::mouseReleaseEvent (event);
287 }
log4cpp::Category * mainCat
Definition: Logger.cpp:14
void signalMouseRelease(QPointF)
Send mouse release to MainWindow for moving Points.
#define LOG4CPP_DEBUG_S(logger)
Definition: convenience.h:20

◆ 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 309 of file GraphicsView.cpp.

310 {
311  // Regression test with local file or internet url specified
312  QString emptyDigFileName;
313  bool hasUrl = true;
314  QUrl url (urlText); // Works as is for internet url
315  if (!urlText.contains ("http")) {
316  url = QUrl::fromLocalFile (urlText);
317  }
318  bool hasImage = false;
319  QImage emptyImage;
320 
321  handleDropEvent (emptyDigFileName,
322  hasUrl,
323  url,
324  hasImage,
325  emptyImage);
326 }

◆ wheelEvent()

void GraphicsView::wheelEvent ( QWheelEvent *  event)
virtual

Convert wheel events into zoom in/out.

Definition at line 328 of file GraphicsView.cpp.

329 {
330  const int ANGLE_THRESHOLD = 15; // From QWheelEvent documentation
331  const int DELTAS_PER_DEGREE = 8; // From QWheelEvent documentation
332 
333  QPoint numDegrees = event->angleDelta() / DELTAS_PER_DEGREE;
334 
335  LOG4CPP_INFO_S ((*mainCat)) << "MainWindow::wheelEvent"
336  << " degrees=" << numDegrees.y()
337  << " phase=" << event->phase();
338 
339  // Criteria:
340  // 1) User has enabled wheel zoom control (but that is not known here so MainWindow will handle that part)
341  // in slotViewZoomInFromWheelEvent and slotViewZoomOutFromWheelEvent
342  // 2) Angle is over a threshold to eliminate false events from just touching wheel
343  if ((event->modifiers() & Qt::ControlModifier) != 0) {
344 
345  if (numDegrees.y() >= ANGLE_THRESHOLD) {
346 
347  // Rotated backwards towards the user, which means zoom in
348  emit signalViewZoomIn();
349 
350  } else if (numDegrees.y() <= -ANGLE_THRESHOLD) {
351 
352  // Rotated forwards away from the user, which means zoom out
353  emit signalViewZoomOut();
354 
355  }
356 
357  // Accept the event as long as Control key was used and we are capturing wheel event
358  event->accept();
359 
360  } else {
361 
362  // Let non-Control events manage scrolling
363  QGraphicsView::wheelEvent (event);
364 
365  }
366 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
log4cpp::Category * mainCat
Definition: Logger.cpp:14
void signalViewZoomIn()
Send wheel event to MainWindow for zooming in.
void signalViewZoomOut()
Send wheel event to MainWindow for zooming out.

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