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

Adam Treat treat at kde.org
Fri May 25 04:55:25 CEST 2007


SVN commit 668087 by treat:

* Setup for various item creation commands



 M  +2 -1      kstmainwindow.cpp  
 M  +13 -9     kstplotcommands.cpp  
 M  +23 -7     kstplotcommands.h  
 M  +7 -0      kstplotitems.cpp  
 M  +2 -0      kstplotitems.h  
 M  +46 -2     kstplotview.cpp  
 M  +14 -1     kstplotview.h  


--- branches/work/kst/portto4/kst/src/libkstapp/kstmainwindow.cpp #668086:668087
@@ -97,7 +97,8 @@
 
 
 void KstMainWindow::createLabel() {
-  new CreateLabelCommand;
+  CreateLabelCommand *cmd = new CreateLabelCommand;
+  cmd->createItem();
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.cpp #668086:668087
@@ -51,35 +51,34 @@
 }
 
 
-CreateLabelCommand::CreateLabelCommand()
-    : KstPlotViewCommand(QObject::tr("Create Label")) {
-  createItem();
+CreateCommand::CreateCommand(const QString &text, QUndoCommand *parent)
+    : KstPlotViewCommand(text, parent) {
 }
 
 
-CreateLabelCommand::CreateLabelCommand(KstPlotView *view)
-    : KstPlotViewCommand(view, QObject::tr("Create Label")) {
-  createItem();
+CreateCommand::CreateCommand(KstPlotView *view, const QString &text, QUndoCommand *parent)
+    : KstPlotViewCommand(view, text, parent) {
 }
 
 
-CreateLabelCommand::~CreateLabelCommand() {
+CreateCommand::~CreateCommand() {
 }
 
 
-void CreateLabelCommand::undo() {
+void CreateCommand::undo() {
   if (_item)
     _item->graphicsItem()->hide();
 }
 
 
-void CreateLabelCommand::redo() {
+void CreateCommand::redo() {
   if (!_item)
     createItem();
 
   _item->graphicsItem()->show();
 }
 
+
 void CreateLabelCommand::createItem() {
   bool ok;
   QString text = QInputDialog::getText(_view, QObject::tr("label"),
@@ -91,4 +90,9 @@
   }
 }
 
+
+void CreateLineCommand::createItem() {
+  /*nada yet*/
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotcommands.h #668086:668087
@@ -42,23 +42,39 @@
   QPointer<KstPlotItem> _item;
 };
 
-class KST_EXPORT CreateLabelCommand : public KstPlotViewCommand
+class KST_EXPORT CreateCommand : public KstPlotViewCommand
 {
 public:
-  CreateLabelCommand();
-  CreateLabelCommand(KstPlotView *view);
-  virtual ~CreateLabelCommand();
+  CreateCommand(const QString &text, QUndoCommand *parent = 0);
+  CreateCommand(KstPlotView *view, const QString &text, QUndoCommand *parent = 0);
+  virtual ~CreateCommand();
 
   virtual void undo();
   virtual void redo();
+  virtual void createItem() = 0;
 
-private:
-  void createItem();
-
 protected:
   QPointer<KstPlotItem> _item;
 };
 
+class KST_EXPORT CreateLabelCommand : public CreateCommand
+{
+public:
+  CreateLabelCommand() : CreateCommand(QObject::tr("Create Label")) {}
+  CreateLabelCommand(KstPlotView *view): CreateCommand(view, QObject::tr("Create Label")) {}
+  virtual ~CreateLabelCommand() {}
+  virtual void createItem();
+};
+
+class KST_EXPORT CreateLineCommand : public CreateCommand
+{
+public:
+  CreateLineCommand() : CreateCommand(QObject::tr("Create Line")) {}
+  CreateLineCommand(KstPlotView *view) : CreateCommand(view, QObject::tr("Create Line")) {}
+  virtual ~CreateLineCommand() {}
+  virtual void createItem();
+};
+
 /*
   LABEL
   BOX
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.cpp #668086:668087
@@ -12,6 +12,7 @@
 #include "kstplotitems.h"
 #include "kstplotview.h"
 
+#include <QDebug>
 #include <QGraphicsItem>
 
 KstPlotItem::KstPlotItem(KstPlotView *parent)
@@ -38,6 +39,12 @@
 }
 
 
+void LabelItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+  qDebug() << "LabelItem::mousePressEvent" << endl;
+  QGraphicsItem::mousePressEvent(event);
+}
+
+
 #include "kstplotitems.moc"
 
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.h #668086:668087
@@ -40,6 +40,8 @@
   virtual ~LabelItem();
 
   virtual QGraphicsItem *graphicsItem() { return this; }
+
+  virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
 };
 
 #endif
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.cpp #668086:668087
@@ -12,16 +12,19 @@
 #include "kstplotview.h"
 #include "kstmainwindow.h"
 #include "kstapplication.h"
+
 #include <QDebug>
 #include <QUndoStack>
 #include <QGraphicsScene>
+#include <QGraphicsSceneMouseEvent>
 
 KstPlotView::KstPlotView()
-    : QGraphicsView(kstApp->mainWindow()), _currentPlotItem(0) {
+    : QGraphicsView(kstApp->mainWindow()),
+      _currentPlotItem(0), _mouseMode(Default) {
 
   _undoStack = new QUndoStack(this);
   setScene(new QGraphicsScene(this));
-
+  scene()->installEventFilter(this);
 }
 
 
@@ -38,6 +41,47 @@
   return _currentPlotItem;
 }
 
+
+KstPlotView::MouseMode KstPlotView::mouseMode() const {
+  return _mouseMode;
+}
+
+
+void KstPlotView::setMouseMode(MouseMode mode) {
+
+  if (_mouseMode == Create)
+    _creationPolygon.clear();
+
+  _mouseMode = mode;
+}
+
+
+QPolygonF KstPlotView::creationPolygon() const {
+  return _creationPolygon;
+}
+
+
+bool KstPlotView::eventFilter(QObject *obj, QEvent *event) {
+  if (obj != scene())
+    return QGraphicsView::eventFilter(obj, event);
+
+  switch (event->type()) {
+  case QEvent::GraphicsSceneMousePress:
+    {
+      QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
+      if (_mouseMode == Create) {
+        _creationPolygon << e->buttonDownScreenPos(Qt::LeftButton);
+        emit creationPolygonChanged();
+        return false;
+      }
+    }
+  case QEvent::GraphicsSceneMouseRelease:
+  case QEvent::GraphicsSceneMouseMove:
+  default:
+    return QGraphicsView::eventFilter(obj, event);
+  }
+}
+
 #include "kstplotview.moc"
 
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.h #668086:668087
@@ -23,16 +23,29 @@
 {
   Q_OBJECT
 public:
+  enum MouseMode { Default, Move, Create };
   KstPlotView();
   virtual ~KstPlotView();
 
   QUndoStack *undoStack() const;
-
   KstPlotItem* currentPlotItem() const;
 
+  MouseMode mouseMode() const;
+  void setMouseMode(MouseMode mode);
+
+  QPolygonF creationPolygon() const;
+
+Q_SIGNALS:
+  void creationPolygonChanged();
+
+protected:
+  bool eventFilter(QObject *obj, QEvent *event);
+
 private:
   QUndoStack *_undoStack;
   KstPlotItem *_currentPlotItem;
+  MouseMode _mouseMode;
+  QPolygonF _creationPolygon;
 };
 
 #endif


More information about the Kst mailing list