Engauge Digitizer 2
Loading...
Searching...
No Matches
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
17const int Z_BOX = 50; // Under box and over background image
18const int Z_HANDLE = 100; // Over box and background image
19
20NonPdfCropping::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
39void 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
65void NonPdfCropping::disableEventsWhileMovingAutomatically ()
66{
71}
72
73void NonPdfCropping::enableEventsWhileMovingAutomatically ()
74{
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
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
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
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
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
167void NonPdfCropping::updateBox ()
168{
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}
const int INNER_RADIUS_MIN
const int Z_HANDLE
const int Z_BOX
void moveBL(const QPointF &newPos, const QPointF &oldPos)
Bottom left corner handle was moved.
static const int NON_PDF_CROPPING_BOTTOM
Bit flag when handle is aligned with bottom edge at reference point.
void moveBR(const QPointF &newPos, const QPointF &oldPos)
Bottom right corner handle was moved.
QSize windowSize() const
Size of window in scene coordinates.
static const int NON_PDF_CROPPING_RIGHT
Bit flag when handle is aligned with right edge at reference point.
QRectF frameRect() const
Frame rectangle selected by user.
void moveTR(const QPointF &newPos, const QPointF &oldPos)
Top right corner handle was moved.
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.
NonPdfCropping(QGraphicsScene &scene, ViewPreview &view)
Single constructor.
static const int NON_PDF_CROPPING_TOP
Bit flag when handle is aligned with top edge at reference point.
This class acts as a single handle for the NonPdfCropping class.
void setDisableEventsWhileMovingAutomatically(bool disable)
Temporarily disable event handling so code can move this object without triggering a cascade of event...
Class that modifies QGraphicsView to automatically expand/shrink the view to fit the window,...
Definition ViewPreview.h:15