1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Robert Bieber 2010-08-05 22:31:46 +00:00
parent 4b49ef2ade
commit 10b9e3b024
10 changed files with 249 additions and 38 deletions

View file

@ -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);
}