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