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 
28 void ViewPreview::resizeEvent(QResizeEvent *event)
29 {
30  if (m_viewAspectRatio == VIEW_ASPECT_RATIO_ONE_TO_ONE) {
31 
32  fitInView (scene()->sceneRect(),
33  Qt::KeepAspectRatio);
34 
35  } else {
36 
37  // Make image fit the new window size by using fitInView. This is needed since QGraphicsView ignores layout stretching.
38  // If there is an image then we use its extent, so DlgSettingsGridDisplay with polar coordinates (which can extend well
39  // outside of image) does not end up with tiny image with wasted space around it
40  bool foundImage = false;
41  for (int i = 0; i < scene()->items().count (); i++) {
42  const QGraphicsItem *item = scene()->items().at (i);
43  const QGraphicsPixmapItem *itemPixmap = dynamic_cast<const QGraphicsPixmapItem*> (item);
44  if (itemPixmap != 0) {
45  foundImage = true;
46  fitInView (itemPixmap->boundingRect());
47  }
48  }
49 
50  if (!foundImage) {
51  // Use the extent of everything
52  fitInView (scene()->itemsBoundingRect ());
53  }
54 
55  QGraphicsView::resizeEvent (event);
56  }
57 }
58 
59 void ViewPreview::wheelEvent (QWheelEvent *event)
60 {
61  event->accept ();
62 }
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:28
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:59
ViewPreview(QGraphicsScene *scene, ViewAspectRatio viewAspectRatio, QWidget *parent=0)
Single constructor.
Definition: ViewPreview.cpp:12