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

Mike Fenton mike at staikos.net
Tue Dec 9 18:25:58 CET 2008


SVN commit 894951 by fenton:

Add commands / functions to trigger view updates for sharing axis only.


 M  +22 -0     view.cpp  
 M  +1 -0      view.h  
 M  +91 -1     viewgridlayout.cpp  
 M  +4 -0      viewgridlayout.h  
 M  +74 -0     viewitem.cpp  
 M  +17 -0     viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #894950:894951
@@ -16,6 +16,7 @@
 #include "mainwindow.h"
 #include "application.h"
 #include "applicationsettings.h"
+#include "viewgridlayout.h"
 
 #include <math.h>
 
@@ -268,6 +269,27 @@
 }
 
 
+void View::sharePlots() {
+  qDebug() << "View::sharePlots";
+//   SharedAxisCommand *layout = new SharedAxisCommand(new LayoutBoxItem(this));
+//   layout->createLayout(0);
+// 
+//   if (_layoutBoxItem) {
+//     _layoutBoxItem->setEnabled(false);
+//     delete _layoutBoxItem;
+//     _layoutBoxItem = 0;
+//   }
+  new LayoutBoxItem(this);
+  ViewGridLayout::sharePlots(_layoutBoxItem);
+
+  if (_layoutBoxItem) {
+    _layoutBoxItem->setEnabled(false);
+    delete _layoutBoxItem;
+    _layoutBoxItem = 0;
+  }
+}
+
+
 void View::appendToLayout(CurvePlacement::Layout layout, ViewItem* item, int columns) {
   AppendLayoutCommand *appendlayout = new AppendLayoutCommand(new LayoutBoxItem(this));
   appendlayout->appendLayout(layout, item, columns);
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #894950:894951
@@ -99,6 +99,7 @@
   public Q_SLOTS:
     void createLayout(int columns = 0);
     void appendToLayout(CurvePlacement::Layout layout, ViewItem* item, int columns = 0);
+    void sharePlots();
 
   protected:
     bool event(QEvent *event);
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.cpp #894950:894951
@@ -11,8 +11,11 @@
 
 #include "viewgridlayout.h"
 
+#include "view.h"
 #include "viewitem.h"
 #include "plotitem.h"
+#include "layoutboxitem.h"
+#include "gridlayouthelper.h"
 
 #include <QDebug>
 
@@ -180,6 +183,53 @@
 }
 
 
+void ViewGridLayout::sharePlots(View *view) {
+  qDebug() << "ViewGridLayout::sharePlots";
+  sharePlots(new LayoutBoxItem(view));
+}
+
+
+void ViewGridLayout::sharePlots(ViewItem *item) {
+  Q_ASSERT(item);
+  Q_ASSERT(item->parentView());
+
+  QList<ViewItem*> viewItems;
+  QList<QGraphicsItem*> list = item->QGraphicsItem::children();
+  if (list.isEmpty())
+    return; //not added to undostack
+
+  foreach (QGraphicsItem *graphicsItem, list) {
+    ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(graphicsItem);
+    if (!viewItem || viewItem->hasStaticGeometry() || !viewItem->allowsLayout() || viewItem->parentItem() != item)
+      continue;
+    viewItems.append(viewItem);
+  }
+
+  if (viewItems.isEmpty())
+    return; //not added to undostack
+
+  Grid *grid = Grid::buildGrid(viewItems, 0);
+  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)) {
+      layout->addViewItem(v, r, c, rs, cs);
+    } else {
+      grid->appendItem(v);
+      if (grid->locateWidget(v, r, c, rs, cs)) {
+        layout->addViewItem(v, r, c, rs, cs);
+      } else {
+        qDebug() << "ooops, viewItem does not fit in layout" << endl;
+      }
+    }
+  }
+  layout->applyAxis();
+}
+
+
 void ViewGridLayout::standardizePlotMargins(ViewItem *item) {
 
   QList<QGraphicsItem*> list;
@@ -241,7 +291,6 @@
 
 
 void ViewGridLayout::apply() {
-
   updatePlotMargins();
   updateSharedAxis();
 
@@ -321,6 +370,11 @@
 }
 
 
+void ViewGridLayout::applyAxis() {
+  updateSharedAxis();
+}
+
+
 void ViewGridLayout::updatePlotMargins() {
   _plotMarginWidth.clear();
   _plotMarginHeight.clear();
@@ -407,6 +461,15 @@
     return;
   }
 
+  //location check
+  if (((leftItem->mapToParent(leftItem->rect().topRight()).y() != plotItem->mapToParent(plotItem->rect().topLeft()).y()) || 
+        (plotItem->mapToParent(plotItem->rect().topLeft()).x() - leftItem->mapToParent(leftItem->rect().topRight()).x() > spacing().width()))) {
+    plotItem->setLeftSuppressed(false);
+    leftItem->setRightSuppressed(false);
+    setSpacing(QSizeF(spacing().width(), spacing().height()));
+    return;
+  }
+
   if (item.rowSpan == left.rowSpan && item.columnSpan == left.columnSpan) {
     plotItem->setLeftSuppressed(true);
     leftItem->setRightSuppressed(true);
@@ -443,6 +506,15 @@
     return;
   }
 
+  //location check
+  if (((plotItem->mapToParent(plotItem->rect().topRight()).y() != rightItem->mapToParent(rightItem->rect().topLeft()).y()) || 
+        (rightItem->mapToParent(rightItem->rect().topLeft()).x() - plotItem->mapToParent(plotItem->rect().topRight()).x() > spacing().width()))) {
+    plotItem->setRightSuppressed(false);
+    rightItem->setLeftSuppressed(false);
+    setSpacing(QSizeF(spacing().width(), spacing().height()));
+    return;
+  }
+
   if (item.rowSpan == right.rowSpan && item.columnSpan == right.columnSpan) {
     plotItem->setRightSuppressed(true);
     rightItem->setLeftSuppressed(true);
@@ -478,6 +550,15 @@
     return;
   }
 
+  //location check
+  if (((topItem->mapToParent(topItem->rect().bottomLeft()).x() != plotItem->mapToParent(plotItem->rect().topLeft()).x()) || 
+        (plotItem->mapToParent(plotItem->rect().topLeft()).y() - topItem->mapToParent(topItem->rect().bottomLeft()).y() > spacing().height()))) {
+    plotItem->setTopSuppressed(false);
+    topItem->setBottomSuppressed(false);
+    setSpacing(QSizeF(spacing().width(), spacing().height()));
+    return;
+  }
+
   if (item.rowSpan == top.rowSpan && item.columnSpan == top.columnSpan) {
     plotItem->setTopSuppressed(true);
     topItem->setBottomSuppressed(true);
@@ -513,6 +594,15 @@
     return;
   }
 
+  //location check
+  if (((plotItem->mapToParent(plotItem->rect().bottomLeft()).x() != bottomItem->mapToParent(bottomItem->rect().topLeft()).x()) || 
+        (bottomItem->mapToParent(bottomItem->rect().topLeft()).y() - plotItem->mapToParent(plotItem->rect().bottomLeft()).y() > spacing().height()))) {
+    plotItem->setBottomSuppressed(false);
+    bottomItem->setTopSuppressed(false);
+    setSpacing(QSizeF(spacing().width(), spacing().height()));
+    return;
+  }
+
   if (item.rowSpan == bottom.rowSpan && item.columnSpan == bottom.columnSpan) {
     plotItem->setBottomSuppressed(true);
     bottomItem->setTopSuppressed(true);
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #894950:894951
@@ -21,6 +21,7 @@
 
 namespace Kst {
 
+class View;
 class ViewItem;
 class PlotItem;
 
@@ -56,11 +57,14 @@
 
     static void resetSharedPlots(ViewItem *item);
     static void standardizePlotMargins(ViewItem *item);
+    static void sharePlots(ViewItem *item);
+    static void sharePlots(View *view);
 
   public Q_SLOTS:
     void reset();
     void resetSharedAxis();
     void apply();
+    void applyAxis();
 
   Q_SIGNALS:
     void enabledChanged(bool enabled);
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #894950:894951
@@ -627,6 +627,18 @@
 }
 
 
+void ViewItem::sharePlots() {
+  qDebug() << "ViewItem::sharePlots";
+  if (parentViewItem()) {
+    ViewGridLayout::sharePlots(parentViewItem());
+/*    LayoutCommand *layout = new LayoutCommand(parentViewItem());
+    layout->createLayout();*/
+  } else if (parentView()) {
+    parentView()->sharePlots();
+  }
+}
+
+
 void ViewItem::createAutoLayout() {
   if (parentViewItem()) {
     LayoutCommand *layout = new LayoutCommand(parentViewItem());
@@ -1840,6 +1852,68 @@
 }
 
 
+
+void SharedAxisCommand::undo() {
+  Q_ASSERT(_layout);
+  _layout->reset();
+}
+
+
+void SharedAxisCommand::redo() {
+  Q_ASSERT(_layout);
+  _layout->applyAxis();
+}
+
+
+void SharedAxisCommand::createLayout(int columns) {
+  Q_ASSERT(_item);
+  Q_ASSERT(_item->parentView());
+
+  QList<ViewItem*> viewItems;
+  QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
+  if (list.isEmpty())
+    return; //not added to undostack
+
+  foreach (QGraphicsItem *item, list) {
+    ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
+    if (!viewItem || viewItem->hasStaticGeometry() || !viewItem->allowsLayout() || viewItem->parentItem() != _item)
+      continue;
+    viewItems.append(viewItem);
+  }
+
+  if (viewItems.isEmpty())
+    return; //not added to undostack
+
+  Grid *grid = Grid::buildGrid(viewItems, columns);
+  Q_ASSERT(grid);
+
+  _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)) {
+      _layout->addViewItem(v, r, c, rs, cs);
+    } else {
+      grid->appendItem(v);
+      if (grid->locateWidget(v, r, c, rs, cs)) {
+        _layout->addViewItem(v, r, c, rs, cs);
+      } else {
+        qDebug() << "ooops, viewItem does not fit in layout" << endl;
+      }
+    }
+  }
+
+  if (qobject_cast<LayoutBoxItem*>(_item)) {
+    _layout->setMargin((_item->sizeOfGrip() / 2.0));
+    _layout->setSpacing((_item->sizeOfGrip() / 2.0));
+    QObject::connect(_layout, SIGNAL(enabledChanged(bool)),
+                     _item, SLOT(setEnabled(bool)));
+  }
+
+  _layout->applyAxis();
+  _item->parentView()->undoStack()->push(this);
+}
+
 void AppendLayoutCommand::undo() {
   Q_ASSERT(_layout);
   _layout->reset();
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #894950:894951
@@ -146,6 +146,7 @@
     virtual void lower();
     virtual void createAutoLayout();
     virtual void createCustomLayout();
+    virtual void sharePlots();
     virtual void remove();
     void resizeTopLeft(const QPointF &offset);
     void resizeTopRight(const QPointF &offset);
@@ -286,6 +287,22 @@
     QPointer<ViewGridLayout> _layout;
 };
 
+class KST_EXPORT SharedAxisCommand : public ViewItemCommand
+{
+  public:
+    SharedAxisCommand(ViewItem *item)
+        : ViewItemCommand(item, QObject::tr("Share Axis"), false) {}
+
+    virtual ~SharedAxisCommand() {}
+
+    virtual void undo();
+    virtual void redo();
+    void createLayout(int columns = 0);
+
+  private:
+    QPointer<ViewGridLayout> _layout;
+};
+
 class KST_EXPORT AppendLayoutCommand : public ViewItemCommand
 {
   public:


More information about the Kst mailing list