From 10b9e3b024c3a1f3796b6c3f764a5dc3d19b84cb Mon Sep 17 00:00:00 2001 From: Robert Bieber Date: Thu, 5 Aug 2010 22:31:46 +0000 Subject: Theme Editor: Implemented resizing in RBMovable subclasses. Implementation is still somewhat crash-prone, but mostly works at this point git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27729 a1c6a512-1295-4272-9138-f99709370657 --- utils/themeeditor/graphics/rbalbumart.cpp | 12 +- utils/themeeditor/graphics/rbalbumart.h | 2 - utils/themeeditor/graphics/rbimage.cpp | 5 - utils/themeeditor/graphics/rbimage.h | 3 - utils/themeeditor/graphics/rbmovable.cpp | 223 ++++++++++++++++++++++++++- utils/themeeditor/graphics/rbmovable.h | 28 +++- utils/themeeditor/graphics/rbprogressbar.cpp | 5 - utils/themeeditor/graphics/rbprogressbar.h | 2 - utils/themeeditor/graphics/rbviewport.cpp | 5 - utils/themeeditor/graphics/rbviewport.h | 2 - 10 files changed, 249 insertions(+), 38 deletions(-) diff --git a/utils/themeeditor/graphics/rbalbumart.cpp b/utils/themeeditor/graphics/rbalbumart.cpp index 40c6f05605..bb9bd12369 100644 --- a/utils/themeeditor/graphics/rbalbumart.cpp +++ b/utils/themeeditor/graphics/rbalbumart.cpp @@ -28,23 +28,17 @@ RBAlbumArt::RBAlbumArt(QGraphicsItem *parent, int x, int y, int maxWidth, int maxHeight, int artWidth, int artHeight, ParseTreeNode* node, char hAlign, char vAlign) - : RBMovable(parent), size(0, 0, maxWidth, - maxHeight), - artWidth(artWidth), artHeight(artHeight), - hAlign(hAlign), vAlign(vAlign), + : RBMovable(parent),artWidth(artWidth), + artHeight(artHeight), hAlign(hAlign), vAlign(vAlign), texture(":/render/albumart.png"), node(node) { + size = QRectF(0, 0, maxWidth, maxHeight); setFlag(ItemSendsGeometryChanges, false); setPos(x, y); hide(); } -QRectF RBAlbumArt::boundingRect() const -{ - return size; -} - void RBAlbumArt::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { diff --git a/utils/themeeditor/graphics/rbalbumart.h b/utils/themeeditor/graphics/rbalbumart.h index 2c8090f9c3..d862f25659 100644 --- a/utils/themeeditor/graphics/rbalbumart.h +++ b/utils/themeeditor/graphics/rbalbumart.h @@ -35,7 +35,6 @@ public: int artWidth, int artHeight, ParseTreeNode* node, char hAlign = 'c', char vAlign = 'c'); - QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -48,7 +47,6 @@ protected: void saveGeometry(); private: - QRectF size; int artWidth; int artHeight; char hAlign; diff --git a/utils/themeeditor/graphics/rbimage.cpp b/utils/themeeditor/graphics/rbimage.cpp index 83a564c465..9d82fb110d 100644 --- a/utils/themeeditor/graphics/rbimage.cpp +++ b/utils/themeeditor/graphics/rbimage.cpp @@ -79,11 +79,6 @@ RBImage::~RBImage() delete image; } -QRectF RBImage::boundingRect() const -{ - return size; -} - void RBImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { diff --git a/utils/themeeditor/graphics/rbimage.h b/utils/themeeditor/graphics/rbimage.h index ba028de7e2..f89213bb33 100644 --- a/utils/themeeditor/graphics/rbimage.h +++ b/utils/themeeditor/graphics/rbimage.h @@ -37,7 +37,6 @@ public: RBImage(const RBImage& other, QGraphicsItem* parent); virtual ~RBImage(); - QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -62,8 +61,6 @@ private: int tiles; int currentTile; - QRectF size; - ParseTreeNode* node; }; 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 #include #include #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); +} diff --git a/utils/themeeditor/graphics/rbmovable.h b/utils/themeeditor/graphics/rbmovable.h index 5b6330228d..b4ade98f0c 100644 --- a/utils/themeeditor/graphics/rbmovable.h +++ b/utils/themeeditor/graphics/rbmovable.h @@ -42,15 +42,41 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual QRectF boundingRect() const{ return size; } + protected: virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); /* Responsible for updating the parse tree */ virtual void saveGeometry() = 0; - bool geomChanged; + QRectF size; + +private: + static const double handleSize; + + QRectF topLeftHandle(); + QRectF topRightHandle(); + QRectF bottomLeftHandle(); + QRectF bottomRightHandle(); + + enum{ + None, + TopLeft, + TopRight, + BottomLeft, + BottomRight + } dragMode; + QPointF dragStartPos; + QRectF dragStartSize; + QPointF dragStartClick; + double dHeightMin; + double dHeightMax; + double dWidthMin; + double dWidthMax; }; diff --git a/utils/themeeditor/graphics/rbprogressbar.cpp b/utils/themeeditor/graphics/rbprogressbar.cpp index 15515f8a18..292318cf60 100644 --- a/utils/themeeditor/graphics/rbprogressbar.cpp +++ b/utils/themeeditor/graphics/rbprogressbar.cpp @@ -103,11 +103,6 @@ RBProgressBar::~RBProgressBar() delete bitmap; } -QRectF RBProgressBar::boundingRect() const -{ - return size; -} - void RBProgressBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) diff --git a/utils/themeeditor/graphics/rbprogressbar.h b/utils/themeeditor/graphics/rbprogressbar.h index 782d08e4f9..69686098ed 100644 --- a/utils/themeeditor/graphics/rbprogressbar.h +++ b/utils/themeeditor/graphics/rbprogressbar.h @@ -40,7 +40,6 @@ public: ParseTreeNode* node, bool pv = 0); virtual ~RBProgressBar(); - QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -50,7 +49,6 @@ protected: private: QPixmap* bitmap; QColor color; - QRectF size; QRectF renderSize; ParseTreeNode* node; diff --git a/utils/themeeditor/graphics/rbviewport.cpp b/utils/themeeditor/graphics/rbviewport.cpp index 274edd67fd..015ed729d1 100644 --- a/utils/themeeditor/graphics/rbviewport.cpp +++ b/utils/themeeditor/graphics/rbviewport.cpp @@ -161,11 +161,6 @@ QPainterPath RBViewport::shape() const return retval; } -QRectF RBViewport::boundingRect() const -{ - return size; -} - void RBViewport::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { diff --git a/utils/themeeditor/graphics/rbviewport.h b/utils/themeeditor/graphics/rbviewport.h index 691133beeb..9bbceb4065 100644 --- a/utils/themeeditor/graphics/rbviewport.h +++ b/utils/themeeditor/graphics/rbviewport.h @@ -51,7 +51,6 @@ public: virtual ~RBViewport(); QPainterPath shape() const; - QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -91,7 +90,6 @@ private: void alignCenter(); void alignRight(); - QRectF size; RBFont* font; QColor foreground; QColor background; -- cgit v1.2.3