00001
00002
00003
00004
00005
00006
00007 #include "Logger.h"
00008 #include "PdfCropping.h"
00009 #include "PdfFrameHandle.h"
00010 #include <QGraphicsRectItem>
00011 #include <QGraphicsScene>
00012 #include <QRect>
00013 #include "QtToString.h"
00014 #include "ViewPreview.h"
00015
00016 const int Z_BOX = 50;
00017 const int Z_HANDLE = 100;
00018
00019 PdfCropping::PdfCropping (QGraphicsScene &scene,
00020 ViewPreview &view) :
00021 m_view (view)
00022 {
00023 createWidgets (scene);
00024 }
00025
00026 void PdfCropping::createWidgets(QGraphicsScene &scene)
00027 {
00028 const double MARGIN_PERCENT = 5.0;
00029 const int ZERO_WIDTH_IS_ALWAYS_VISIBLE = 0;
00030
00031 int marginHor = scene.width() * MARGIN_PERCENT / 100.0;
00032 int marginVer = scene.height() * MARGIN_PERCENT / 100.0;
00033
00034 QRect box (scene.sceneRect().left() + marginHor,
00035 scene.sceneRect().top() + marginVer,
00036 scene.sceneRect().width() - 2 * marginHor,
00037 scene.sceneRect().height() - 2 * marginVer);
00038
00039 m_handleTL = new PdfFrameHandle (scene, m_view, box.topLeft() , PDF_CROPPING_LEFT | PDF_CROPPING_TOP , *this, Z_HANDLE);
00040 m_handleTR = new PdfFrameHandle (scene, m_view, box.topRight() , PDF_CROPPING_RIGHT | PDF_CROPPING_TOP , *this, Z_HANDLE);
00041 m_handleBR = new PdfFrameHandle (scene, m_view, box.bottomRight(), PDF_CROPPING_RIGHT | PDF_CROPPING_BOTTOM , *this, Z_HANDLE);
00042 m_handleBL = new PdfFrameHandle (scene, m_view, box.bottomLeft() , PDF_CROPPING_LEFT | PDF_CROPPING_BOTTOM , *this, Z_HANDLE);
00043
00044 m_box = new QGraphicsRectItem;
00045 m_box->setZValue (Z_BOX);
00046 m_box->setPen (QPen (QBrush (Qt::gray), ZERO_WIDTH_IS_ALWAYS_VISIBLE));
00047 scene.addItem (m_box);
00048
00049 updateBox ();
00050 }
00051
00052 void PdfCropping::disableEventsWhileMovingAutomatically ()
00053 {
00054 m_handleTL->setDisableEventsWhileMovingAutomatically (true);
00055 m_handleTR->setDisableEventsWhileMovingAutomatically (true);
00056 m_handleBR->setDisableEventsWhileMovingAutomatically (true);
00057 m_handleBL->setDisableEventsWhileMovingAutomatically (true);
00058 }
00059
00060 void PdfCropping::enableEventsWhileMovingAutomatically ()
00061 {
00062 m_handleTL->setDisableEventsWhileMovingAutomatically (false);
00063 m_handleTR->setDisableEventsWhileMovingAutomatically (false);
00064 m_handleBR->setDisableEventsWhileMovingAutomatically (false);
00065 m_handleBL->setDisableEventsWhileMovingAutomatically (false);
00066 }
00067
00068 QRectF PdfCropping::frameRect () const
00069 {
00070
00071
00072
00073
00074 QRectF rectTL = m_handleTL->mapRectToScene (m_handleTL->boundingRect());
00075 QRectF rectBR = m_handleBR->mapRectToScene (m_handleBR->boundingRect());
00076
00077 QRectF rectUnited = rectTL.united (rectBR);
00078
00079 return rectUnited;
00080 }
00081
00082 void PdfCropping::moveBL (const QPointF &newPos,
00083 const QPointF &oldPos)
00084 {
00085 disableEventsWhileMovingAutomatically();
00086
00087 double deltaX = newPos.x() - oldPos.x();
00088 double deltaY = newPos.y() - oldPos.y();
00089
00090 m_handleTL->moveBy (deltaX,
00091 0);
00092 m_handleBR->moveBy (0,
00093 deltaY);
00094
00095 enableEventsWhileMovingAutomatically();
00096
00097 updateBox();
00098 }
00099
00100 void PdfCropping::moveBR (const QPointF &newPos,
00101 const QPointF &oldPos)
00102 {
00103 disableEventsWhileMovingAutomatically();
00104
00105 double deltaX = newPos.x() - oldPos.x();
00106 double deltaY = newPos.y() - oldPos.y();
00107
00108 m_handleBL->moveBy (0,
00109 deltaY);
00110 m_handleTR->moveBy (deltaX,
00111 0);
00112
00113 enableEventsWhileMovingAutomatically();
00114
00115 updateBox();
00116 }
00117
00118 void PdfCropping::moveTL (const QPointF &newPos,
00119 const QPointF &oldPos)
00120 {
00121 disableEventsWhileMovingAutomatically();
00122
00123 double deltaX = newPos.x() - oldPos.x();
00124 double deltaY = newPos.y() - oldPos.y();
00125
00126 m_handleBL->moveBy (deltaX,
00127 0);
00128 m_handleTR->moveBy (0,
00129 deltaY);
00130
00131 enableEventsWhileMovingAutomatically();
00132
00133 updateBox();
00134 }
00135
00136 void PdfCropping::moveTR (const QPointF &newPos,
00137 const QPointF &oldPos)
00138 {
00139 disableEventsWhileMovingAutomatically();
00140
00141 double deltaX = newPos.x() - oldPos.x();
00142 double deltaY = newPos.y() - oldPos.y();
00143
00144 m_handleTL->moveBy (0,
00145 deltaY);
00146 m_handleBR->moveBy (deltaX,
00147 0);
00148
00149 enableEventsWhileMovingAutomatically();
00150
00151 updateBox();
00152 }
00153
00154 void PdfCropping::updateBox ()
00155 {
00156 QRectF rectUnited = frameRect ();
00157
00158
00159 rectUnited.setWidth (rectUnited.width () - 1);
00160 rectUnited.setHeight (rectUnited.height () - 1);
00161
00162 m_box->setRect (rectUnited);
00163 }
00164
00165 QSize PdfCropping::windowSize () const
00166 {
00167 return QSize (m_view.scene()->width(),
00168 m_view.scene()->height());
00169 }