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