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

Adam Treat treat at kde.org
Thu Sep 20 21:40:05 CEST 2007


SVN commit 714874 by treat:

* Refactor layout commands once again.  Now they
should maintain state and be completely reversible.


 M  +2 -1      view.cpp  
 M  +12 -0     viewgridlayout.cpp  
 M  +7 -1      viewgridlayout.h  
 M  +56 -56    viewitem.cpp  
 M  +8 -1      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #714873:714874
@@ -165,7 +165,8 @@
   if (_layoutBoxItem && _layoutBoxItem->isVisible() && _layoutBoxItem->layout() )
     return;
 
-  new LayoutBoxItem(this);
+  LayoutCommand *layout = new LayoutCommand(new LayoutBoxItem(this));
+  layout->createLayout();
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.cpp #714873:714874
@@ -24,6 +24,7 @@
 
 ViewGridLayout::ViewGridLayout(ViewItem *parent)
   : QObject(parent),
+    _enabled(false),
     _rowCount(0),
     _columnCount(0),
     _spacing(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)),
@@ -102,6 +103,17 @@
 }
 
 
+bool ViewGridLayout::isEnabled() const {
+  return _enabled;
+}
+
+
+void ViewGridLayout::setEnabled(bool enabled) {
+  _enabled = enabled;
+  emit enabledChanged(_enabled);
+}
+
+
 void ViewGridLayout::reset() {
   foreach (LayoutItem item, _items) {
     item.viewItem->setTransform(item.transform);
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #714873:714874
@@ -48,11 +48,16 @@
     qreal plotMarginWidth(const PlotItem *plotItem) const;
     qreal plotMarginHeight(const PlotItem *plotItem) const;
 
-    void reset();
+    bool isEnabled() const;
+    void setEnabled(bool enabled);
 
   public Q_SLOTS:
+    void reset();
     void update();
 
+  Q_SIGNALS:
+    void enabledChanged(bool enabled);
+
   private Q_SLOTS:
     void updatePlotMargins();
 
@@ -68,6 +73,7 @@
       QRectF rect;
     };
 
+    bool _enabled;
     int _rowCount;
     int _columnCount;
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #714873:714874
@@ -15,6 +15,8 @@
 #include "viewitemdialog.h"
 #include "viewgridlayout.h"
 
+#include "layoutboxitem.h"
+
 #include "gridlayouthelper.h"
 
 #include <math.h>
@@ -95,14 +97,19 @@
 
 
 void ViewItem::setLayout(ViewGridLayout *layout) {
+  if (_layout == layout)
+    return;
+
   //disconnect previous layout...
   if (_layout) {
+    _layout->setEnabled(false);
     disconnect(this, SIGNAL(geometryChanged()), _layout, SLOT(update()));
   }
 
   _layout = layout;
 
   if (_layout) {
+    _layout->setEnabled(true);
     connect(this, SIGNAL(geometryChanged()), _layout, SLOT(update()));
   }
 
@@ -470,11 +477,14 @@
 
 void ViewItem::createLayout() {
   LayoutCommand *layout = new LayoutCommand(this);
-  layout->redo();
+  layout->createLayout();
 }
 
 
 void ViewItem::breakLayout() {
+  if (!layout())
+    return;
+
   BreakLayoutCommand *layout = new BreakLayoutCommand(this);
   layout->redo();
 }
@@ -1427,21 +1437,20 @@
 
 
 void CreateCommand::undo() {
-  if (_item)
-    _item->hide();
+  Q_ASSERT(_item);
+  _item->hide();
 }
 
 
 void CreateCommand::redo() {
-  if (!_item)
-    createItem();
-
+  Q_ASSERT(_item);
   _item->show();
 }
 
 
 void CreateCommand::createItem() {
   Q_ASSERT(_item);
+  Q_ASSERT(_view);
 
   _view->setMouseMode(View::Create);
 
@@ -1463,21 +1472,27 @@
 
 
 void LayoutCommand::undo() {
-  ViewGridLayout *layout = _item->layout();
-  if (!layout)
-    return;
-
-  layout->reset();
+  Q_ASSERT(_layout);
+  _layout->reset();
   _item->setLayout(0);
-  delete layout;
 }
 
 
 void LayoutCommand::redo() {
+  Q_ASSERT(_layout);
+  _item->setLayout(_layout);
+  _layout->update();
+}
+
+
+void LayoutCommand::createLayout() {
+  Q_ASSERT(_item);
+  Q_ASSERT(_item->parentView());
+
   QList<ViewItem*> viewItems;
   QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
   if (list.isEmpty())
-    return;
+    return; //not added to undostack
 
   foreach (QGraphicsItem *item, list) {
     ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
@@ -1487,133 +1502,118 @@
   }
 
   if (viewItems.isEmpty())
-    return;
+    return; //not added to undostack
 
   Grid *grid = Grid::buildGrid(viewItems);
   Q_ASSERT(grid);
 
-  ViewGridLayout *layout = new ViewGridLayout(_item);
+  _layout = new ViewGridLayout(_item);
 
   foreach (ViewItem *v, viewItems) {
     int r = 0, c = 0, rs = 0, cs = 0;
     if (grid->locateWidget(v, r, c, rs, cs)) {
       if (rs * cs == 1) {
-        layout->addViewItem(v, r, c, 1, 1);
+        _layout->addViewItem(v, r, c, 1, 1);
       } else {
-        layout->addViewItem(v, r, c, rs, cs);
+        _layout->addViewItem(v, r, c, rs, cs);
       }
     } else {
       qDebug() << "ooops, viewItem does not fit in layout" << endl;
     }
   }
 
-  layout->update();
-}
-
-
-void BreakLayoutCommand::undo() {  QList<ViewItem*> viewItems;
-  QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
-  if (list.isEmpty())
-    return;
-
-  foreach (QGraphicsItem *item, list) {
-    ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
-    if (!viewItem || viewItem->parentItem() != _item)
-      continue;
-    viewItems.append(viewItem);
+  if (qobject_cast<LayoutBoxItem*>(_item)) {
+    QObject::connect(_layout, SIGNAL(enabledChanged(bool)),
+                     _item, SLOT(setEnabled(bool)));
   }
 
-  if (viewItems.isEmpty())
-    return;
+  _layout->update();
+  _item->parentView()->undoStack()->push(this);
+}
 
-  Grid *grid = Grid::buildGrid(viewItems);
-  Q_ASSERT(grid);
 
-  ViewGridLayout *layout = new ViewGridLayout(_item);
-
-  foreach (ViewItem *v, viewItems) {
-    int r = 0, c = 0, rs = 0, cs = 0;
-    if (grid->locateWidget(v, r, c, rs, cs)) {
-      if (rs * cs == 1) {
-        layout->addViewItem(v, r, c, 1, 1);
-      } else {
-        layout->addViewItem(v, r, c, rs, cs);
-      }
-    } else {
-      qDebug() << "ooops, viewItem does not fit in layout" << endl;
-    }
-  }
-
-  layout->update();
+void BreakLayoutCommand::undo() {
+  Q_ASSERT(_layout);
+  _item->setLayout(_layout);
+  _layout->update();
 }
 
 
 void BreakLayoutCommand::redo() {
-  ViewGridLayout *layout = _item->layout();
-  if (!layout)
-    return;
-
+  _layout = _item->layout();
+  Q_ASSERT(_layout);
   _item->setLayout(0);
-  delete layout;
 }
 
 
 void MoveCommand::undo() {
+  Q_ASSERT(_item);
   _item->setPos(_originalPos);
 }
 
 
 void MoveCommand::redo() {
+  Q_ASSERT(_item);
   _item->setPos(_newPos);
 }
 
 
 void ResizeCommand::undo() {
+  Q_ASSERT(_item);
   _item->setViewRect(_originalRect);
 }
 
 
 void ResizeCommand::redo() {
+  Q_ASSERT(_item);
   _item->setViewRect(_newRect);
 }
 
 
 void RemoveCommand::undo() {
+  Q_ASSERT(_item);
   _item->show();
 }
 
 
 void RemoveCommand::redo() {
+  Q_ASSERT(_item);
   _item->hide();
 }
 
 
 void RaiseCommand::undo() {
+  Q_ASSERT(_item);
   _item->setZValue(_item->zValue() - 1);
 }
 
 
 void RaiseCommand::redo() {
+  Q_ASSERT(_item);
   _item->setZValue(_item->zValue() + 1);
 }
 
 
 void LowerCommand::undo() {
+  Q_ASSERT(_item);
   _item->setZValue(_item->zValue() +1);
 }
 
 
 void LowerCommand::redo() {
+  Q_ASSERT(_item);
   _item->setZValue(_item->zValue() - 1);
 }
 
 
 void TransformCommand::undo() {
+  Q_ASSERT(_item);
   _item->setTransform(_originalTransform);
 }
 
 
 void TransformCommand::redo() {
+  Q_ASSERT(_item);
   _item->setTransform(_newTransform);
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #714873:714874
@@ -233,12 +233,16 @@
 {
   public:
     LayoutCommand(ViewItem *item)
-        : ViewItemCommand(item, QObject::tr("Layout")) {}
+        : ViewItemCommand(item, QObject::tr("Layout"), false) {}
 
     virtual ~LayoutCommand() {}
 
     virtual void undo();
     virtual void redo();
+    void createLayout();
+
+  private:
+    QPointer<ViewGridLayout> _layout;
 };
 
 class KST_EXPORT BreakLayoutCommand : public ViewItemCommand
@@ -251,6 +255,9 @@
 
     virtual void undo();
     virtual void redo();
+
+  private:
+    QPointer<ViewGridLayout> _layout;
 };
 
 class KST_EXPORT MoveCommand : public ViewItemCommand


More information about the Kst mailing list