Engauge Digitizer  2
ViewPreview.cpp
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 <QGraphicsPixmapItem>
8 #include <QGraphicsScene>
9 #include <QMouseEvent>
10 #include "ViewPreview.h"
11 
12 ViewPreview::ViewPreview(QGraphicsScene *scene,
13  ViewAspectRatio viewAspectRatio,
14  QWidget *parent) :
15  QGraphicsView (scene, parent),
16  m_viewAspectRatio (viewAspectRatio)
17 {
18  setMouseTracking(true);
19 }
20 
21 void ViewPreview::mouseMoveEvent(QMouseEvent *event)
22 {
23  QPointF pos = mapToScene (event->pos ());
24 
25  emit signalMouseMove (pos);
26 
27  // Normally we would need to call QGraphicsView::mouseMoveEvent at this point so that the mouse move event could be handled,
28  // but this is unwanted since:
29  // 1) Everywhere, except the pdf import preview, there is nothing to drag
30  // 2) Dragging of PdfFrameHandle objects in the pdf import preview is handled indirectly by PdfCropping
31  QGraphicsView::mouseMoveEvent (event);
32 }
33 
34 void ViewPreview::resizeEvent(QResizeEvent *event)
35 {
36  if (m_viewAspectRatio == VIEW_ASPECT_RATIO_ONE_TO_ONE) {
37 
38  fitInView (scene()->sceneRect(),
39  Qt::KeepAspectRatio);
40 
41  } else {
42 
43  // Make image fit the new window size by using fitInView. This is needed since QGraphicsView ignores layout stretching.
44  // If there is an image then we use its extent, so DlgSettingsGridDisplay with polar coordinates (which can extend well
45  // outside of image) does not end up with tiny image with wasted space around it
46  bool foundImage = false;
47  for (int i = 0; i < scene()->items().count (); i++) {
48  const QGraphicsItem *item = scene()->items().at (i);
49  const QGraphicsPixmapItem *itemPixmap = dynamic_cast<const QGraphicsPixmapItem*> (item);
50  if (itemPixmap != 0) {
51  foundImage = true;
52  fitInView (itemPixmap->boundingRect());
53  }
54  }
55 
56  if (!foundImage) {
57  // Use the extent of everything
58  fitInView (scene()->itemsBoundingRect ());
59  }
60 
61  QGraphicsView::resizeEvent (event);
62  }
63 }
64 
65 void ViewPreview::wheelEvent (QWheelEvent *event)
66 {
67  event->accept ();
68 }
void signalMouseMove(QPointF pos)
Forward the mouse move events.
ViewAspectRatio
Prevent aspect ratio distortion in certain previews by providing fixed 1:1 aspect ratio option...
Definition: ViewPreview.h:21
virtual void mouseMoveEvent(QMouseEvent *event)
Intercept cursor move events and forward them.
Definition: ViewPreview.cpp:21
virtual void resizeEvent(QResizeEvent *event)
Intercept resize events so we can rescale to the graphics items just fit into the resized window...
Definition: ViewPreview.cpp:34
virtual void wheelEvent(QWheelEvent *event)
Intercept wheel event and discard it so accidentally moving the wheel does not move drawn items out o...
Definition: ViewPreview.cpp:65
ViewPreview(QGraphicsScene *scene, ViewAspectRatio viewAspectRatio, QWidget *parent=0)
Single constructor.
Definition: ViewPreview.cpp:12