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