summaryrefslogtreecommitdiffstats
path: root/utils/themeeditor/graphics/rbmovable.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/themeeditor/graphics/rbmovable.cpp')
-rw-r--r--utils/themeeditor/graphics/rbmovable.cpp223
1 files changed, 219 insertions, 4 deletions
diff --git a/utils/themeeditor/graphics/rbmovable.cpp b/utils/themeeditor/graphics/rbmovable.cpp
index 3eca8bd18c..5d493c61e7 100644
--- a/utils/themeeditor/graphics/rbmovable.cpp
+++ b/utils/themeeditor/graphics/rbmovable.cpp
@@ -19,13 +19,16 @@
*
****************************************************************************/
+#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QDebug>
#include "rbmovable.h"
+const double RBMovable::handleSize = 7;
+
RBMovable::RBMovable(QGraphicsItem* parent)
- : QGraphicsItem(parent), geomChanged(false)
+ : QGraphicsItem(parent), dragMode(None)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges);
}
@@ -45,6 +48,11 @@ void RBMovable::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
pen.setColor(Qt::green);
painter->setPen(pen);
painter->drawRect(boundingRect());
+
+ painter->fillRect(topLeftHandle(), Qt::green);
+ painter->fillRect(topRightHandle(), Qt::green);
+ painter->fillRect(bottomLeftHandle(), Qt::green);
+ painter->fillRect(bottomRightHandle(), Qt::green);
}
}
@@ -61,7 +69,6 @@ QVariant RBMovable::itemChange(GraphicsItemChange change, const QVariant &value)
pos.setY(qMax(0., pos.y()));
pos.setY(qMin(pos.y(), bound.height() - boundingRect().height()));
- geomChanged = true;
return pos;
}
@@ -71,15 +78,223 @@ QVariant RBMovable::itemChange(GraphicsItemChange change, const QVariant &value)
void RBMovable::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
- QGraphicsItem::mousePressEvent(event);
+ if(!isSelected())
+ {
+ QGraphicsItem::mousePressEvent(event);
+ return;
+ }
+
+ if(topLeftHandle().contains(event->pos()))
+ {
+ dragStartClick = event->pos() + pos();
+ dragStartPos = pos();
+ dragStartSize = boundingRect();
+
+ dWidthMin = -1. * pos().x();
+ dWidthMax = boundingRect().width() - childrenBoundingRect().right();
+
+ dHeightMin = -1. * pos().y();
+ dHeightMax = boundingRect().height() - childrenBoundingRect().bottom();
+
+ dragMode = TopLeft;
+ }
+ else if(topRightHandle().contains(event->pos()))
+ {
+ dragStartClick = event->pos() + pos();
+ dragStartPos = pos();
+ dragStartSize = boundingRect();
+
+ dWidthMin = childrenBoundingRect().width() - boundingRect().width();
+ dWidthMin = qMax(dWidthMin, -1. * size.width());
+ dWidthMax = parentItem()->boundingRect().width()
+ - boundingRect().width() - pos().x();
+
+ dHeightMin = -1. * pos().y();
+ dHeightMax = boundingRect().height() - childrenBoundingRect().bottom();
+ dHeightMax = qMin(dHeightMax, boundingRect().height());
+
+ dragMode = TopRight;
+ }
+ else if(bottomLeftHandle().contains(event->pos()))
+ {
+ dragStartClick = event->pos() + pos();
+ dragStartPos = pos();
+ dragStartSize = boundingRect();
+
+ dWidthMin = -1. * pos().x();
+ dWidthMax = boundingRect().width() - childrenBoundingRect().right();
+ dWidthMax = qMin(dWidthMax, size.width());
+
+ dHeightMin = -1. * (boundingRect().height()
+ - childrenBoundingRect().bottom());
+ dHeightMin = qMax(dHeightMin, -1. * boundingRect().height());
+
+ dragMode = BottomLeft;
+ }
+ else if(bottomRightHandle().contains(event->pos()))
+ {
+ dragStartClick = event->pos() + pos();
+ dragStartPos = pos();
+ dragStartSize = boundingRect();
+
+ dWidthMin = -1. * (boundingRect().width()
+ - childrenBoundingRect().right());
+ dWidthMin = qMax(dWidthMin, -1. * boundingRect().width());
+ dWidthMax = parentItem()->boundingRect().width() -
+ boundingRect().width() - pos().x();
+
+ dHeightMin = -1. * (boundingRect().height()
+ - childrenBoundingRect().bottom());
+ dHeightMin = qMax(dHeightMin, -1. * boundingRect().height());
+ dHeightMax = parentItem()->boundingRect().height() -
+ boundingRect().height() - pos().y();
+
+ dragMode = BottomRight;
+ }
+ else
+ {
+ QGraphicsItem::mousePressEvent(event);
+ }
+}
+
+void RBMovable::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+
+ QPointF absPos;
+ QPointF dPos;
+ QPointF dMouse;
+ switch(dragMode)
+ {
+ case None:
+ QGraphicsItem::mouseMoveEvent(event);
+ break;
+
+ case TopLeft:
+ /* Dragging from the top left corner */
+ absPos = event->pos() + pos();
+ dMouse = absPos - dragStartClick;
+
+ dPos.setX(qMin(dMouse.x(), dWidthMax));
+ dPos.setX(qMax(dPos.x(), dWidthMin));
+
+ dPos.setY(qMin(dMouse.y(), dHeightMax));
+ dPos.setY(qMax(dPos.y(), dHeightMin));
+
+ prepareGeometryChange();
+
+ setPos(dragStartPos + dPos);
+
+ size.setWidth(dragStartSize.width() - dPos.x());
+ size.setHeight(dragStartSize.height() - dPos.y());
+
+ break;
+
+ case TopRight:
+ /* Dragging from the top right corner */
+ absPos = event->pos() + pos();
+ dMouse = absPos - dragStartClick;
+
+ dPos.setX(qMin(dMouse.x(), dWidthMax));
+ dPos.setX(qMax(dPos.x(), dWidthMin));
+
+ dPos.setY(qMin(dMouse.y(), dHeightMax));
+ dPos.setY(qMax(dPos.y(), dHeightMin));
+
+ prepareGeometryChange();
+
+ setPos(dragStartPos.x(), dragStartPos.y() + dPos.y());
+
+ size.setWidth(dragStartSize.width() + dPos.x());
+ size.setHeight(dragStartSize.height() - dPos.y());
+
+ break;
+
+ case BottomLeft:
+ /* Dragging from the bottom left corner */
+ absPos = event->pos() + pos();
+ dMouse = absPos - dragStartClick;
+
+ dPos.setX(qMin(dMouse.x(), dWidthMax));
+ dPos.setX(qMax(dPos.x(), dWidthMin));
+
+ dPos.setY(qMin(dMouse.y(), dHeightMax));
+ dPos.setY(qMax(dPos.y(), dHeightMin));
+
+ prepareGeometryChange();
+ setPos(dragStartPos.x() + dPos.x(), dragStartPos.y());
+ size.setHeight(dragStartSize.height() + dPos.y());
+ size.setWidth(dragStartSize.width() - dPos.x());
+
+ break;
+
+ case BottomRight:
+ /* Dragging from the bottom right corner */
+ absPos = event->pos() + pos();
+ dMouse = absPos - dragStartClick;
+
+ dPos.setX(qMin(dMouse.x(), dWidthMax));
+ dPos.setX(qMax(dPos.x(), dWidthMin));
+
+ dPos.setY(qMin(dMouse.y(), dHeightMax));
+ dPos.setY(qMax(dPos.y(), dHeightMin));
+
+ prepareGeometryChange();
+
+ size.setWidth(dragStartSize.width() + dPos.x());
+ size.setHeight(dragStartSize.height() + dPos.y());
+
+ break;
+ }
}
void RBMovable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseReleaseEvent(event);
+
+ dragMode = None;
+
if(isSelected())
{
saveGeometry();
- geomChanged = false;
}
}
+
+QRectF RBMovable::topLeftHandle()
+{
+ QRectF bounds = boundingRect();
+ double width = qMin(bounds.width() / 2. - .5, handleSize);
+ double height = qMin(bounds.height() / 2. - .5, handleSize);
+ double size = qMin(width, height);
+
+ return QRectF(0, 0, size, size);
+}
+
+QRectF RBMovable::topRightHandle()
+{
+ QRectF bounds = boundingRect();
+ double width = qMin(bounds.width() / 2. - .5, handleSize);
+ double height = qMin(bounds.height() / 2. - .5, handleSize);
+ double size = qMin(width, height);
+
+ return QRectF(bounds.width() - size, 0, size, size);
+}
+
+QRectF RBMovable::bottomLeftHandle()
+{
+ QRectF bounds = boundingRect();
+ double width = qMin(bounds.width() / 2. - .5, handleSize);
+ double height = qMin(bounds.height() / 2. - .5, handleSize);
+ double size = qMin(width, height);
+
+ return QRectF(0, bounds.height() - size, size, size);
+}
+
+QRectF RBMovable::bottomRightHandle()
+{
+ QRectF bounds = boundingRect();
+ double width = qMin(bounds.width() / 2. - .5, handleSize);
+ double height = qMin(bounds.height() / 2. - .5, handleSize);
+ double size = qMin(width, height);
+
+ return QRectF(bounds.width() - size, bounds.height() - size, size, size);
+}