[Kst] branches/work/kst/portto4/kst/src/libkstapp

Adam Treat treat at kde.org
Sat May 26 14:35:28 CEST 2007


SVN commit 668436 by treat:

* Undo/Redo for QGraphicsScene move interaction
at least for the label.  Right now it is too greedy
as it produces an undocommand for each slight movement.



 M  +3 -1      TODO  
 M  +2 -0      kstmainwindow.cpp  
 M  +11 -0     kstplotcommands.cpp  
 M  +23 -0     kstplotcommands.h  
 M  +15 -0     kstplotitems.cpp  
 M  +3 -0      kstplotitems.h  


--- branches/work/kst/portto4/kst/src/libkstapp/TODO #668435:668436
@@ -2,4 +2,6 @@
 - More shapes/items
 - Actual plot item
 - OpenGL?
-- SVG item?
\ No newline at end of file
+- SVG item?
+- Use some templates/factories for CreateCommands?
+- Undo/Redo for item select/focus/move and other ItemChange's.
\ No newline at end of file
--- branches/work/kst/portto4/kst/src/libkstapp/kstmainwindow.cpp #668435:668436
@@ -110,7 +110,9 @@
 
 void KstMainWindow::createActions() {
   _undoAct = _undoGroup->createUndoAction(this);
+  _undoAct->setShortcut(tr("Ctrl+Z"));
   _redoAct = _undoGroup->createRedoAction(this);
+  _redoAct->setShortcut(tr("Ctrl+Shift+Z"));
 
   _createLabelAct = new QAction(tr("&Create label"), this);
   _createLabelAct->setStatusTip(tr("Create a label for the current plot"));
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.cpp #668435:668436
@@ -111,6 +111,17 @@
   connect(_item, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
 }
 
+
+void MoveCommand::undo() {
+  _item->graphicsItem()->setPos(_originalPos);
+}
+
+
+void MoveCommand::redo() {
+  _item->graphicsItem()->setPos(_newPos);
+}
+
+
 #include "kstplotcommands.moc"
 
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.h #668435:668436
@@ -13,6 +13,7 @@
 #define KSTPLOTCOMMANDS_H
 
 #include <QObject>
+#include <QPointF>
 #include <QPointer>
 #include <QUndoCommand>
 
@@ -84,6 +85,28 @@
   virtual void createItem();
 };
 
+class KST_EXPORT MoveCommand : public KstPlotItemCommand
+{
+public:
+  MoveCommand(QPointF originalPos, QPointF newPos)
+      : KstPlotItemCommand(QObject::tr("Move Object")),
+        _originalPos(originalPos),
+        _newPos(newPos) {}
+  MoveCommand(KstPlotItem *item, QPointF originalPos, QPointF newPos)
+      : KstPlotItemCommand(item, QObject::tr("Move Object")),
+        _originalPos(originalPos),
+        _newPos(newPos) {}
+
+  virtual ~MoveCommand() {}
+
+  virtual void undo();
+  virtual void redo();
+
+private:
+  QPointF _originalPos;
+  QPointF _newPos;
+};
+
 /*
   LABEL
   BOX
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.cpp #668435:668436
@@ -11,6 +11,8 @@
 
 #include "kstplotitems.h"
 
+#include "kstplotcommands.h"
+
 #include <QDebug>
 #include <QInputDialog>
 #include <QGraphicsItem>
@@ -65,6 +67,19 @@
 }
 
 
+QVariant LabelItem::itemChange(GraphicsItemChange change, const QVariant &value) {
+  if (change == ItemPositionChange && scene()) {
+    QPointF originalPos = pos();
+    QPointF newPos = value.toPointF();
+    // FIXME this is too greedy as it produces too many undo commands.
+    // Ideally, we'd only record the move from right before the item
+    // becomes the mouse grabber to right after.
+    new MoveCommand(this, originalPos, newPos);
+  }
+  return QGraphicsItem::itemChange(change, value);
+}
+
+
 void LabelItem::creationPolygonChanged(KstPlotView::CreationEvent event) {
   if (event == KstPlotView::MousePress) {
 
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.h #668435:668436
@@ -56,6 +56,9 @@
 
   virtual QGraphicsItem *graphicsItem() { return this; }
 
+protected:
+  QVariant itemChange(GraphicsItemChange change, const QVariant &value);
+
 private Q_SLOTS:
   void creationPolygonChanged(KstPlotView::CreationEvent event);
 };


More information about the Kst mailing list