[Kst] branches/work/kst/portto4/kst/src/libkstapp
Adam Treat
treat at kde.org
Fri Jun 8 16:56:00 CEST 2007
SVN commit 672956 by treat:
* Big refactor and fix for resizing undo/redo
commands.
M +16 -30 view.cpp
M +1 -1 view.h
M +48 -12 viewitem.cpp
M +8 -2 viewitem.h
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #672955:672956
@@ -102,50 +102,36 @@
bool View::eventFilter(QObject *obj, QEvent *event) {
- if (obj != scene())
+ if (obj != scene() || _mouseMode != Create)
return QGraphicsView::eventFilter(obj, event);
switch (event->type()) {
case QEvent::GraphicsSceneMousePress:
{
- if (_mouseMode == Create) {
- QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
- _creationPolygonPress << e->buttonDownScenePos(Qt::LeftButton);
- emit creationPolygonChanged(MousePress);
- return true; //filter this otherwise something can grab our mouse...
- } else {
- return false;
- }
+ QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
+ _creationPolygonPress << e->buttonDownScenePos(Qt::LeftButton);
+ emit creationPolygonChanged(MousePress);
+ return true; //filter this otherwise something can grab our mouse...
}
case QEvent::GraphicsSceneMouseRelease:
{
- if (_mouseMode == Create) {
- QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
- _creationPolygonRelease << e->scenePos();
- emit creationPolygonChanged(MouseRelease);
- } else if (_mouseMode == Move) {
- setMouseMode(Default);
- _undoStack->endMacro();
- }
- return false;
+ QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
+ _creationPolygonRelease << e->scenePos();
+ emit creationPolygonChanged(MouseRelease);
+ break;
}
case QEvent::GraphicsSceneMouseMove:
{
- if (_mouseMode == Create) {
- QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
- _creationPolygonMove << e->scenePos();
- emit creationPolygonChanged(MouseMove);
- } else if (_mouseMode == Default && scene()->mouseGrabberItem() &&
- /*FIXME not a good way to detect resize mode*/
- scene()->mouseGrabberItem()->cursor().shape() == Qt::ArrowCursor) {
- setMouseMode(Move);
- _undoStack->beginMacro(tr("Move"));
- }
- return false;
+ QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
+ _creationPolygonMove << e->scenePos();
+ emit creationPolygonChanged(MouseMove);
+ break;
}
default:
- return QGraphicsView::eventFilter(obj, event);
+ break;
}
+
+ return QGraphicsView::eventFilter(obj, event);
}
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #672955:672956
@@ -24,7 +24,7 @@
{
Q_OBJECT
public:
- enum MouseMode { Default, Move, Create };
+ enum MouseMode { Default, Move, Create, Resize, Rotate };
enum CreationEvent {
MousePress = 0x0,
MouseRelease =0x1,
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #672955:672956
@@ -24,7 +24,7 @@
: QObject(parent) {
setAcceptsHoverEvents(true);
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
- connect(parent, SIGNAL(mouseModeChanged()), this, SLOT(mouseModeChanged()));
+ connect(parent, SIGNAL(mouseModeChanged()), this, SLOT(viewMouseModeChanged()));
}
@@ -37,6 +37,16 @@
}
+ViewItem::MouseMode ViewItem::mouseMode() const {
+ return _mouseMode;
+}
+
+
+void ViewItem::setMouseMode(MouseMode mode) {
+ _mouseMode = mode;
+}
+
+
void ViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
#ifdef DEBUG_GEOMETRY
@@ -115,10 +125,20 @@
void ViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
- if (scene()->mouseGrabberItem() != this)
- return;
+ if (parentView()->mouseMode() == View::Default) {
+ if (mouseMode() == ViewItem::Default ||
+ mouseMode() == ViewItem::Move) {
+ parentView()->setMouseMode(View::Move);
+ parentView()->undoStack()->beginMacro(tr("Move"));
+ } else if (mouseMode() == ViewItem::Resize) {
+ parentView()->setMouseMode(View::Resize);
+ parentView()->undoStack()->beginMacro(tr("Resize"));
+ }
+ }
- //We have the mouse grab...
+ /*FIXME Need to compress the resize commands into one command like we do
+ * with the MoveCommand. Otherwise, this will eat memory... */
+
switch(cursor().shape()) {
case Qt::SizeFDiagCursor:
{
@@ -128,7 +148,6 @@
} else {
transformed.setBottomRight(event->pos());
}
-/* setRect(transformed);*/
ResizeCommand *resize = new ResizeCommand(this, rect(), transformed);
resize->redo();
return;
@@ -141,7 +160,6 @@
} else {
transformed.setTopRight(event->pos());
}
-/* setRect(transformed);*/
ResizeCommand *resize = new ResizeCommand(this, rect(), transformed);
resize->redo();
return;
@@ -154,7 +172,6 @@
} else {
transformed.setBottom(event->pos().y());
}
-/* setRect(transformed);*/
ResizeCommand *resize = new ResizeCommand(this, rect(), transformed);
resize->redo();
return;
@@ -167,7 +184,6 @@
} else {
transformed.setRight(event->pos().x());
}
-/* setRect(transformed);*/
ResizeCommand *resize = new ResizeCommand(this, rect(), transformed);
resize->redo();
return;
@@ -181,14 +197,17 @@
}
-bool ViewItem::transformToRect(const QRectF &newRect) {
+bool ViewItem::transformToRect(const QRectF &newRect, bool combine) {
+
+/* setRect(newRect);*/
+
QTransform t;
QPolygonF one(rect());
one.pop_back(); //get rid of last closed point
QPolygonF two(newRect);
two.pop_back(); //get rid of last closed point
bool success = QTransform::quadToQuad(one, two, t);
- if (success) setTransform(t, true);
+ if (success) setTransform(t, combine);
return success;
}
@@ -199,6 +218,12 @@
void ViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+
+ if (parentView()->mouseMode() != View::Default) {
+ parentView()->setMouseMode(View::Default);
+ parentView()->undoStack()->endMacro();
+ }
+
QGraphicsRectItem::mouseReleaseEvent(event);
}
@@ -212,24 +237,28 @@
//Look for corners
r.moveCenter(rect().bottomRight());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeFDiagCursor);
return;
}
r.moveCenter(rect().topLeft());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeFDiagCursor);
return;
}
r.moveCenter(rect().bottomLeft());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeBDiagCursor);
return;
}
r.moveCenter(rect().topRight());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeBDiagCursor);
return;
}
@@ -239,12 +268,14 @@
r.moveTopRight(rect().topRight());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeVerCursor);
return;
}
r.moveBottomRight(rect().bottomRight());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeVerCursor);
return;
}
@@ -254,21 +285,24 @@
r.moveTopLeft(rect().topLeft());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeHorCursor);
return;
}
r.moveTopRight(rect().topRight());
if (r.contains(event->pos())) {
+ setMouseMode(ViewItem::Resize);
setCursor(Qt::SizeHorCursor);
return;
}
+ setMouseMode(ViewItem::Default);
setCursor(Qt::ArrowCursor);
}
-void ViewItem::mouseModeChanged() {
+void ViewItem::viewMouseModeChanged() {
if (parentView()->mouseMode() == View::Move)
_originalPosition = pos();
else if (_originalPosition != pos())
@@ -385,12 +419,14 @@
void ResizeCommand::undo() {
+ /*FIXME Not combining this transform with previous ... undoes to much.
+ * OTOH, combining it means we don't really undo...*/
_item->transformToRect(_originalRect);
}
void ResizeCommand::redo() {
- _item->transformToRect(_newRect);
+ _item->transformToRect(_newRect, true);
}
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #672955:672956
@@ -27,21 +27,26 @@
{
Q_OBJECT
public:
+ enum MouseMode { Default, Move, Resize, Rotate };
ViewItem(View *parent);
virtual ~ViewItem();
View *parentView() const;
+ MouseMode mouseMode() const;
+ void setMouseMode(MouseMode mode);
+
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
Q_SIGNALS:
void creationComplete();
+/*FIXME these should be made private for only undo commands to access*/
public Q_SLOTS:
void remove();
void raise();
void lower();
- bool transformToRect(const QRectF &newRect);
+ bool transformToRect(const QRectF &newRect, bool combine = false);
protected Q_SLOTS:
virtual void creationPolygonChanged(View::CreationEvent event);
@@ -54,9 +59,10 @@
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
private Q_SLOTS:
- void mouseModeChanged();
+ void viewMouseModeChanged();
private:
+ MouseMode _mouseMode;
QPointF _originalPosition;
};
More information about the Kst
mailing list