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

Adam Treat treat at kde.org
Sat May 26 00:22:19 CEST 2007


SVN commit 668308 by treat:

* Allow for creation interrupt/abort.

Still not happy with this as we have to allocate
all creation commands on the heap and there is
a somewhat confusing signal/slot tree going on...


 M  +38 -11    kstplotcommands.cpp  
 M  +14 -5     kstplotcommands.h  
 M  +8 -1      kstplotitems.cpp  
 M  +3 -0      kstplotitems.h  
 M  +2 -0      kstplotview.cpp  
 M  +1 -0      kstplotview.h  


--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.cpp #668307:668308
@@ -19,15 +19,19 @@
 #include <QInputDialog>
 #include <QGraphicsScene>
 
-KstPlotViewCommand::KstPlotViewCommand(const QString &text, QUndoCommand *parent)
+KstPlotViewCommand::KstPlotViewCommand(const QString &text,
+                                       bool addToStack, QUndoCommand *parent)
     : QUndoCommand(text, parent), _view(kstApp->mainWindow()->currentPlotView()) {
-  _view->undoStack()->push(this);
+  if (addToStack)
+    _view->undoStack()->push(this);
 }
 
 
-KstPlotViewCommand::KstPlotViewCommand(KstPlotView *view, const QString &text, QUndoCommand *parent)
+KstPlotViewCommand::KstPlotViewCommand(KstPlotView *view, const QString &text,
+                                       bool addToStack, QUndoCommand *parent)
     : QUndoCommand(text, parent), _view(view) {
-  _view->undoStack()->push(this);
+  if (addToStack)
+    _view->undoStack()->push(this);
 }
 
 
@@ -35,15 +39,19 @@
 }
 
 
-KstPlotItemCommand::KstPlotItemCommand(const QString &text, QUndoCommand *parent)
+KstPlotItemCommand::KstPlotItemCommand(const QString &text,
+                                       bool addToStack, QUndoCommand *parent)
     : QUndoCommand(text, parent), _item(kstApp->mainWindow()->currentPlotView()->currentPlotItem()) {
-  _item->parentView()->undoStack()->push(this);
+  if (addToStack)
+    _item->parentView()->undoStack()->push(this);
 }
 
 
-KstPlotItemCommand::KstPlotItemCommand(KstPlotItem *item, const QString &text, QUndoCommand *parent)
+KstPlotItemCommand::KstPlotItemCommand(KstPlotItem *item, const QString &text,
+                                       bool addToStack, QUndoCommand *parent)
     : QUndoCommand(text, parent), _item(item) {
-  _item->parentView()->undoStack()->push(this);
+  if (addToStack)
+    _item->parentView()->undoStack()->push(this);
 }
 
 
@@ -52,12 +60,12 @@
 
 
 CreateCommand::CreateCommand(const QString &text, QUndoCommand *parent)
-    : KstPlotViewCommand(text, parent) {
+    : KstPlotViewCommand(text, false, parent) {
 }
 
 
 CreateCommand::CreateCommand(KstPlotView *view, const QString &text, QUndoCommand *parent)
-    : KstPlotViewCommand(view, text, parent) {
+    : KstPlotViewCommand(view, text, false, parent) {
 }
 
 
@@ -79,6 +87,11 @@
 }
 
 
+void CreateCommand::creationComplete() {
+  _view->undoStack()->push(this);
+}
+
+
 void CreateLabelCommand::createItem() {
   bool ok;
   QString text = QInputDialog::getText(_view, QObject::tr("label"),
@@ -88,12 +101,26 @@
     _item = new LabelItem(text, _view);
     _view->scene()->addItem(_item->graphicsItem());
     _item->graphicsItem()->setZValue(1);
+
+    //If the item is interrupted while creating itself it will destroy itself
+    //need to delete this too in response...
+    connect(_item, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
+
+    creationComplete();
   }
+
 }
 
 
 void CreateLineCommand::createItem() {
-    _item = new LineItem(_view);
+  _item = new LineItem(_view);
+  connect(_item, SIGNAL(creationComplete()), this, SLOT(creationComplete()));
+
+  //If the item is interrupted while creating itself it will destroy itself
+  //need to delete this too in response...
+  connect(_item, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
 }
 
+#include "kstplotcommands.moc"
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.h #668307:668308
@@ -12,6 +12,7 @@
 #ifndef KSTPLOTCOMMANDS_H
 #define KSTPLOTCOMMANDS_H
 
+#include <QObject>
 #include <QPointer>
 #include <QUndoCommand>
 
@@ -23,8 +24,10 @@
 class KST_EXPORT KstPlotViewCommand : public QUndoCommand
 {
 public:
-  KstPlotViewCommand(const QString &text, QUndoCommand *parent = 0);
-  KstPlotViewCommand(KstPlotView *view, const QString &text, QUndoCommand *parent = 0);
+  KstPlotViewCommand(const QString &text,
+                     bool addToStack = true, QUndoCommand *parent = 0);
+  KstPlotViewCommand(KstPlotView *view, const QString &text,
+                     bool addToStack = true, QUndoCommand *parent = 0);
   virtual ~KstPlotViewCommand();
 
 protected:
@@ -34,16 +37,19 @@
 class KST_EXPORT KstPlotItemCommand : public QUndoCommand
 {
 public:
-  KstPlotItemCommand(const QString &text, QUndoCommand *parent = 0);
-  KstPlotItemCommand(KstPlotItem *item, const QString &text, QUndoCommand *parent = 0);
+  KstPlotItemCommand(const QString &text,
+                     bool addToStack = true, QUndoCommand *parent = 0);
+  KstPlotItemCommand(KstPlotItem *item, const QString &text,
+                     bool addToStack = true, QUndoCommand *parent = 0);
   virtual ~KstPlotItemCommand();
 
 protected:
   QPointer<KstPlotItem> _item;
 };
 
-class KST_EXPORT CreateCommand : public KstPlotViewCommand
+class KST_EXPORT CreateCommand : public QObject, public KstPlotViewCommand
 {
+  Q_OBJECT
 public:
   CreateCommand(const QString &text, QUndoCommand *parent = 0);
   CreateCommand(KstPlotView *view, const QString &text, QUndoCommand *parent = 0);
@@ -53,6 +59,9 @@
   virtual void redo();
   virtual void createItem() = 0;
 
+public Q_SLOTS:
+  void creationComplete();
+
 protected:
   QPointer<KstPlotItem> _item;
 };
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.cpp #668307:668308
@@ -74,6 +74,11 @@
     : KstPlotItem(parent) {
   setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
   parent->setMouseMode(KstPlotView::Create);
+
+  //If the mouseMode is changed again before we're done with creation
+  //delete ourself.
+  connect(parent, SIGNAL(mouseModeChanged()), this, SLOT(deleteLater()));
+
   connect(parent, SIGNAL(creationPolygonChanged(KstPlotView::CreationEvent)),
           this, SLOT(creationPolygonChanged(KstPlotView::CreationEvent)));
 }
@@ -102,8 +107,10 @@
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(KstPlotView::MouseRelease));
     setLine(QLineF(line().p1(), poly.last())); //start and end
     updateAspectFromGeometry();
+    parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
+    parentView()->disconnect(this, SLOT(creationPolygonChanged(KstPlotView::CreationEvent)));
     parentView()->setMouseMode(KstPlotView::Default);
-    parentView()->disconnect(this, SLOT(creationPolygonChanged(KstPlotView::CreationEvent)));
+    emit creationComplete();
     return;
   }
 }
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.h #668307:668308
@@ -33,6 +33,9 @@
 
   virtual QGraphicsItem *graphicsItem() = 0;
 
+Q_SIGNALS:
+  void creationComplete();
+
 public Q_SLOTS:
   void updateAspectFromGeometry();
   void updateGeometry();
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.cpp #668307:668308
@@ -59,6 +59,8 @@
   }
 
   _mouseMode = mode;
+
+  emit mouseModeChanged();
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.h #668307:668308
@@ -43,6 +43,7 @@
   QPolygonF creationPolygon(CreationEvents events) const;
 
 Q_SIGNALS:
+  void mouseModeChanged();
   void creationPolygonChanged(KstPlotView::CreationEvent event);
   void resized();
 


More information about the Kst mailing list