00001
00002
00003
00004
00005
00006
00007 #include "PdfCropping.h"
00008 #include "PdfFrameHandle.h"
00009 #include <QBrush>
00010 #include <QGraphicsScene>
00011 #include <QGraphicsView>
00012 #include <QStyleOptionGraphicsItem>
00013
00014 const double HANDLE_SIZE_AS_FRACTION_OF_WINDOW_SIZE = 30;
00015
00016 PdfFrameHandle::PdfFrameHandle (QGraphicsScene &scene,
00017 QGraphicsView &view,
00018 const QPointF &pointReference,
00019 int orientationFlags,
00020 PdfCropping &pdfCropping,
00021 int zValue) :
00022 m_pdfCropping (pdfCropping),
00023 m_orientationFlags (orientationFlags),
00024 m_disableEventsWhileMovingAutomatically (false),
00025 m_scene (scene),
00026 m_view (view)
00027 {
00028 const double SUBTLE_OPACITY = 0.2;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 setFlags (QGraphicsItem::ItemIsMovable |
00043 QGraphicsItem::ItemIsSelectable |
00044 QGraphicsItem::ItemSendsScenePositionChanges);
00045
00046
00047 setBrush (QBrush (Qt::blue));
00048 setVisible (true);
00049 setZValue (zValue);
00050 setOpacity (SUBTLE_OPACITY);
00051 setPos (pointReference);
00052
00053
00054 scene.addItem (this);
00055
00056 QSize handleSize = m_pdfCropping.windowSize() / HANDLE_SIZE_AS_FRACTION_OF_WINDOW_SIZE;
00057
00058
00059 QPointF pointPos = pointReference;
00060 if ((orientationFlags && PdfCropping::PDF_CROPPING_LEFT) != 0) {
00061 pointPos.setX (pointPos.x() - handleSize.width() / 2.0);
00062 } else if ((orientationFlags && PdfCropping::PDF_CROPPING_RIGHT) != 0) {
00063 pointPos.setX (pointPos.x() + handleSize.width() / 2.0);
00064 }
00065 if ((orientationFlags && PdfCropping::PDF_CROPPING_TOP) != 0) {
00066 pointPos.setY (pointPos.y() - handleSize.height() / 2.0);
00067 } else if ((orientationFlags && PdfCropping::PDF_CROPPING_BOTTOM) != 0) {
00068 pointPos.setY (pointPos.y() + handleSize.height() / 2.0);
00069 }
00070
00071
00072
00073 QPointF topLeftLocal = mapFromScene (pointPos);
00074
00075 setRect (QRectF (topLeftLocal,
00076 handleSize));
00077 }
00078
00079 QVariant PdfFrameHandle::itemChange (GraphicsItemChange change,
00080 const QVariant &value)
00081 {
00082 QVariant valueFiltered = value;
00083
00084 if (change == ItemPositionChange && scene()) {
00085
00086 QPointF sizeAsPointF (boundingRect().size().width(),
00087 boundingRect().size().height());
00088
00089
00090 QPointF newPos = valueFiltered.toPointF();
00091 QPointF oldPos = pos ();
00092
00093
00094 QRectF newRectItem (newPos,
00095 QSize (boundingRect().size().width(),
00096 boundingRect().size().height()));
00097 QPolygonF newRectScene = mapToScene (newRectItem);
00098 QPolygon newRectView = m_view.mapFromScene (newRectScene.boundingRect());
00099
00100
00101 QRectF rectWindow = m_scene.sceneRect();
00102 if (!rectWindow.contains (newRectView.boundingRect())) {
00103
00104
00105 newPos.setX (qMin (rectWindow.right(), qMax (newPos.x(), rectWindow.left())));
00106 newPos.setY (qMin (rectWindow.bottom(), qMax (newPos.y(), rectWindow.top())));
00107
00108 valueFiltered = (newPos);
00109
00110 }
00111
00112
00113
00114 if (!m_disableEventsWhileMovingAutomatically) {
00115
00116 bool left = ((m_orientationFlags & PdfCropping::PDF_CROPPING_LEFT ) != 0);
00117 bool right = ((m_orientationFlags & PdfCropping::PDF_CROPPING_RIGHT ) != 0);
00118 bool top = ((m_orientationFlags & PdfCropping::PDF_CROPPING_TOP ) != 0);
00119 bool bottom = ((m_orientationFlags & PdfCropping::PDF_CROPPING_BOTTOM) != 0);
00120
00121 if (left && top) {
00122 m_pdfCropping.moveTL (newPos, oldPos);
00123 } else if (right && top) {
00124 m_pdfCropping.moveTR (newPos, oldPos);
00125 } else if (right && bottom) {
00126 m_pdfCropping.moveBR (newPos, oldPos);
00127 } else if (left && bottom) {
00128 m_pdfCropping.moveBL (newPos, oldPos);
00129 }
00130 }
00131 }
00132
00133 return QGraphicsItem::itemChange(change, valueFiltered);
00134 }
00135
00136 void PdfFrameHandle::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00137 {
00138
00139 QStyleOptionGraphicsItem scrubbed (*option);
00140 scrubbed.state &= ~QStyle::State_Selected;
00141 QGraphicsRectItem::paint (painter, &scrubbed, widget);
00142 }
00143
00144 void PdfFrameHandle::setDisableEventsWhileMovingAutomatically (bool disable)
00145 {
00146 m_disableEventsWhileMovingAutomatically = disable;
00147 }