Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
NonPdfCropping.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 "Logger.h"
8 #include "NonPdfCropping.h"
9 #include "NonPdfFrameHandle.h"
10 #include <QGraphicsRectItem>
11 #include <QGraphicsScene>
12 #include <QRect>
13 #include "QtToString.h"
14 #include "ViewPreview.h"
15 
16 const int Z_BOX = 50; // Under box and over background image
17 const int Z_HANDLE = 100; // Over box and background image
18 
19 NonPdfCropping::NonPdfCropping (QGraphicsScene &scene,
20  ViewPreview &view) :
21  m_view (view),
22  m_handleTL (0),
23  m_handleTR (0),
24  m_handleBR (0),
25  m_handleBL (0)
26 {
27  createWidgets (scene);
28 }
29 
30 NonPdfCropping::~NonPdfCropping ()
31 {
32  delete m_handleTL;
33  delete m_handleTR;
34  delete m_handleBR;
35  delete m_handleBL;
36 }
37 
38 void NonPdfCropping::createWidgets(QGraphicsScene &scene)
39 {
40  const double MARGIN_PERCENT = 5.0;
41  const int ZERO_WIDTH_IS_ALWAYS_VISIBLE = 0;
42 
43  int marginHor = scene.width() * MARGIN_PERCENT / 100.0;
44  int marginVer = scene.height() * MARGIN_PERCENT / 100.0;
45 
46  QRect box (scene.sceneRect().left() + marginHor,
47  scene.sceneRect().top() + marginVer,
48  scene.sceneRect().width() - 2 * marginHor,
49  scene.sceneRect().height() - 2 * marginVer);
50 
51  m_handleTL = new NonPdfFrameHandle (scene, m_view, box.topLeft() , NON_PDF_CROPPING_LEFT | NON_PDF_CROPPING_TOP , *this, Z_HANDLE);
52  m_handleTR = new NonPdfFrameHandle (scene, m_view, box.topRight() , NON_PDF_CROPPING_RIGHT | NON_PDF_CROPPING_TOP , *this, Z_HANDLE);
53  m_handleBR = new NonPdfFrameHandle (scene, m_view, box.bottomRight(), NON_PDF_CROPPING_RIGHT | NON_PDF_CROPPING_BOTTOM , *this, Z_HANDLE);
54  m_handleBL = new NonPdfFrameHandle (scene, m_view, box.bottomLeft() , NON_PDF_CROPPING_LEFT | NON_PDF_CROPPING_BOTTOM , *this, Z_HANDLE);
55 
56  m_box = new QGraphicsRectItem;
57  m_box->setZValue (Z_BOX);
58  m_box->setPen (QPen (QBrush (Qt::gray), ZERO_WIDTH_IS_ALWAYS_VISIBLE));
59  scene.addItem (m_box);
60 
61  updateBox ();
62 }
63 
64 void NonPdfCropping::disableEventsWhileMovingAutomatically ()
65 {
66  m_handleTL->setDisableEventsWhileMovingAutomatically (true);
70 }
71 
72 void NonPdfCropping::enableEventsWhileMovingAutomatically ()
73 {
74  m_handleTL->setDisableEventsWhileMovingAutomatically (false);
75  m_handleTR->setDisableEventsWhileMovingAutomatically (false);
76  m_handleBR->setDisableEventsWhileMovingAutomatically (false);
77  m_handleBL->setDisableEventsWhileMovingAutomatically (false);
78 }
79 
81 {
82  // The x(), y(), pos(), rect() and boundingRect() will return coordinates assuming origin at the initial position of
83  // each handle. So to get the coordinates in the window reference frame it takes a two step process like
84  // QGraphicsRectItem::mapRectToScene (QGraphicsRectItem::rect())
85 
86  QRectF rectTL = m_handleTL->mapRectToScene (m_handleTL->boundingRect());
87  QRectF rectBR = m_handleBR->mapRectToScene (m_handleBR->boundingRect());
88 
89  QRectF rectUnited = rectTL.united (rectBR);
90 
91  return rectUnited;
92 }
93 
94 void NonPdfCropping::moveBL (const QPointF &newPos,
95  const QPointF &oldPos)
96 {
97  disableEventsWhileMovingAutomatically();
98 
99  double deltaX = newPos.x() - oldPos.x();
100  double deltaY = newPos.y() - oldPos.y();
101 
102  m_handleTL->moveBy (deltaX,
103  0);
104  m_handleBR->moveBy (0,
105  deltaY);
106 
107  enableEventsWhileMovingAutomatically();
108 
109  updateBox();
110 }
111 
112 void NonPdfCropping::moveBR (const QPointF &newPos,
113  const QPointF &oldPos)
114 {
115  disableEventsWhileMovingAutomatically();
116 
117  double deltaX = newPos.x() - oldPos.x();
118  double deltaY = newPos.y() - oldPos.y();
119 
120  m_handleBL->moveBy (0,
121  deltaY);
122  m_handleTR->moveBy (deltaX,
123  0);
124 
125  enableEventsWhileMovingAutomatically();
126 
127  updateBox();
128 }
129 
130 void NonPdfCropping::moveTL (const QPointF &newPos,
131  const QPointF &oldPos)
132 {
133  disableEventsWhileMovingAutomatically();
134 
135  double deltaX = newPos.x() - oldPos.x();
136  double deltaY = newPos.y() - oldPos.y();
137 
138  m_handleBL->moveBy (deltaX,
139  0);
140  m_handleTR->moveBy (0,
141  deltaY);
142 
143  enableEventsWhileMovingAutomatically();
144 
145  updateBox();
146 }
147 
148 void NonPdfCropping::moveTR (const QPointF &newPos,
149  const QPointF &oldPos)
150 {
151  disableEventsWhileMovingAutomatically();
152 
153  double deltaX = newPos.x() - oldPos.x();
154  double deltaY = newPos.y() - oldPos.y();
155 
156  m_handleTL->moveBy (0,
157  deltaY);
158  m_handleBR->moveBy (deltaX,
159  0);
160 
161  enableEventsWhileMovingAutomatically();
162 
163  updateBox();
164 }
165 
166 void NonPdfCropping::updateBox ()
167 {
168  QRectF rectUnited = frameRect ();
169 
170  // Adjust by one pixel in both horizontal and vertical directions so bottom/right handles end on the box
171  rectUnited.setWidth (rectUnited.width () - 1);
172  rectUnited.setHeight (rectUnited.height () - 1);
173 
174  m_box->setRect (rectUnited);
175 }
176 
178 {
179  return QSize (m_view.scene()->width(),
180  m_view.scene()->height());
181 }
static const int NON_PDF_CROPPING_LEFT
Bit flag when handle is aligned with left edge at reference point.
void moveTL(const QPointF &newPos, const QPointF &oldPos)
Top left corner handle was moved.
static const int NON_PDF_CROPPING_TOP
Bit flag when handle is aligned with top edge at reference point.
static const int NON_PDF_CROPPING_BOTTOM
Bit flag when handle is aligned with bottom edge at reference point.
void moveTR(const QPointF &newPos, const QPointF &oldPos)
Top right corner handle was moved.
Class that modifies QGraphicsView to automatically expand/shrink the view to fit the window...
Definition: ViewPreview.h:14
This class acts as a single handle for the NonPdfCropping class.
QSize windowSize() const
Size of window in scene coordinates.
void moveBR(const QPointF &newPos, const QPointF &oldPos)
Bottom right corner handle was moved.
NonPdfCropping(QGraphicsScene &scene, ViewPreview &view)
Single constructor.
void moveBL(const QPointF &newPos, const QPointF &oldPos)
Bottom left corner handle was moved.
QRectF frameRect() const
Frame rectangle selected by user.
void setDisableEventsWhileMovingAutomatically(bool disable)
Temporarily disable event handling so code can move this object without triggering a cascade of event...
static const int NON_PDF_CROPPING_RIGHT
Bit flag when handle is aligned with right edge at reference point.