00001 #include "rectangle.h"
00002 #include <QPainter>
00003 #include <QPixmap>
00004
00005 #define HANDLE_MARGIN 10
00006 #define HANDLE_VIRTUAL_SIZE 50
00007
00008 QPixmap* Rectangle::resizeHandle(NULL);
00009
00010 Rectangle::Rectangle(qreal x, qreal y, qreal w, qreal h, QGraphicsItem * parent) :
00011 QGraphicsRectItem(x, y, w, h, parent)
00012 {
00013 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
00014 }
00015
00016 QRectF Rectangle::boundingRect() const
00017 {
00018 QRectF ret = QGraphicsRectItem::boundingRect();
00019 ret.setRect(ret.x() - HANDLE_MARGIN - 1, ret.y() - HANDLE_MARGIN - 1,
00020 ret.width() + HANDLE_MARGIN + HANDLE_MARGIN + 1,
00021 ret.height() + HANDLE_MARGIN + HANDLE_MARGIN + 1);
00022 return ret.normalized();
00023 }
00024
00025
00026 QRectF Rectangle::innerBoundingRect () const
00027 {
00028 return QGraphicsRectItem::boundingRect();
00029 }
00030
00031 void Rectangle::resize(ResizeHandleIdx hidx, QPointF pos)
00032 {
00033 QRectF bounds = QGraphicsRectItem::boundingRect();
00034 switch(hidx)
00035 {
00036 case RH_TOP_LEFT:
00037 bounds.setTopLeft(pos);
00038 break;
00039 case RH_TOP_RIGHT:
00040 bounds.setTopRight(pos);
00041 break;
00042 case RH_BOTTOM_LEFT:
00043 bounds.setBottomLeft(pos);
00044 break;
00045 case RH_BOTTOM_RIGHT:
00046 bounds.setBottomRight(pos);
00047 break;
00048 default:
00049 break;
00050 }
00051 prepareGeometryChange();
00052 setRect(bounds);
00053 }
00054
00055 void Rectangle::resizeDone()
00056 {
00057 setRect(rect().normalized());
00058 }
00059
00060 Rectangle::ResizeHandleIdx Rectangle::handleAt(QPointF pos)
00061 {
00062 QRectF bounds = innerBoundingRect();
00063 QRectF handleBounds(0, 0, HANDLE_VIRTUAL_SIZE, HANDLE_VIRTUAL_SIZE);
00064 handleBounds.moveCenter(mapToScene(bounds.topLeft()));
00065
00066
00067 if (handleBounds.contains(pos))
00068 return RH_TOP_LEFT;
00069 handleBounds.moveCenter(mapToScene(bounds.topRight()));
00070 if (handleBounds.contains(pos))
00071 return RH_TOP_RIGHT;
00072 handleBounds.moveCenter(mapToScene(bounds.bottomLeft()));
00073 if (handleBounds.contains(pos))
00074 return RH_BOTTOM_LEFT;
00075 handleBounds.moveCenter(mapToScene(bounds.bottomRight()));
00076 if (handleBounds.contains(pos))
00077 return RH_BOTTOM_RIGHT;
00078 return RH_NONE;
00079 }
00080
00081 int Rectangle::handleWidth()
00082 {
00083 if (!resizeHandle)
00084 resizeHandle = new QPixmap(":/images/resize-handle.png");
00085 return resizeHandle->width();
00086 }
00087
00088 void Rectangle::paint ( QPainter * painter,
00089 const QStyleOptionGraphicsItem * option, QWidget * widget )
00090 {
00091
00092 QGraphicsRectItem::paint(painter, option, widget);
00093 QRectF bounds = boundingRect();
00094 if (isSelected())
00095 {
00096 int halfHandleW = handleWidth() / -2;
00097 QRectF outerBounds = QGraphicsRectItem::boundingRect()
00098 .adjusted(halfHandleW, halfHandleW, halfHandleW, halfHandleW);
00099 painter->drawPixmap(outerBounds.topLeft(), *resizeHandle);
00100 painter->drawPixmap(outerBounds.topRight(), *resizeHandle);
00101 painter->drawPixmap(outerBounds.bottomLeft(), *resizeHandle);
00102 painter->drawPixmap(outerBounds.bottomRight(), *resizeHandle);
00103 }
00104
00105
00106 }
00107
00108 void Rectangle::buttonDown(QPointF scenePos)
00109 {
00110 m_buttonDownPos = mapFromScene(scenePos);
00111 }