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

Mike Fenton mike at staikos.net
Wed Dec 31 22:23:37 CET 2008


SVN commit 903958 by fenton:

Connect SharedAxisBoxItem updates to SharePlots and add support for custom updates.
Add calculation for share mode (x/y) based upon current projections.


 M  +29 -1     plotitem.cpp  
 M  +6 -0      plotitem.h  
 M  +16 -14    plotrenderitem.cpp  
 M  +3 -44     sharedaxisboxitem.cpp  
 M  +129 -6    viewgridlayout.cpp  
 M  +5 -0      viewgridlayout.h  
 M  +1 -8      viewitem.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #903957:903958
@@ -56,6 +56,7 @@
 PlotItem::PlotItem(View *parent)
   : ViewItem(parent),
   _isTiedZoom(false),
+  _isInSharedAxisBox(false),
   _isLeftLabelVisible(true),
   _isBottomLabelVisible(true),
   _isRightLabelVisible(true),
@@ -923,7 +924,7 @@
 
 
 void PlotItem::setTiedZoom(bool tiedZoom) {
-  if (_isTiedZoom == tiedZoom)
+  if ((_isInSharedAxisBox && !tiedZoom) || (_isTiedZoom == tiedZoom))
     return;
 
   _isTiedZoom = tiedZoom;
@@ -938,6 +939,33 @@
 }
 
 
+bool PlotItem::isInSharedAxisBox() const {
+  return _isInSharedAxisBox;
+}
+
+
+void PlotItem::setInSharedAxisBox(bool inSharedBox) {
+  _isInSharedAxisBox = inSharedBox;
+}
+
+
+void PlotItem::setSharedAxisBox(ViewItem* parent) {
+  if (parent) {
+    setInSharedAxisBox(true);
+    setTiedZoom(true);
+    setAllowedGripModes(0);
+    setFlags(0);
+    setParent(parent);
+  } else {
+    setInSharedAxisBox(false);
+    setTiedZoom(false);
+    setAllowedGripModes(Move | Resize | Rotate);
+    setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
+    setParent(0);
+  }
+}
+
+
 qreal PlotItem::leftLabelMargin() const {
   return calculatedLeftLabelMargin();
 }
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.h #903957:903958
@@ -80,6 +80,11 @@
     bool isTiedZoom() const;
     void setTiedZoom(bool tiedZoom);
 
+    bool isInSharedAxisBox() const;
+    void setInSharedAxisBox(bool inSharedBox);
+
+    void setSharedAxisBox(ViewItem* parent);
+
     qreal leftMarginSize() const;
     void setLeftPadding(const qreal);
     qreal bottomMarginSize() const;
@@ -336,6 +341,7 @@
   private:
     QHash<PlotRenderItem::RenderType, PlotRenderItem*> _renderers;
     bool _isTiedZoom;
+    bool _isInSharedAxisBox;
     bool _isLeftLabelVisible;
     bool _isBottomLabelVisible;
     bool _isRightLabelVisible;
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #903957:903958
@@ -215,21 +215,23 @@
     painter->save();
     painter->setRenderHint(QPainter::Antialiasing, true);
     painter->fillPath(checkBox(), Qt::white);
-    if (isHovering()) {
-      QRectF check = checkBox().controlPointRect();
-      check.setSize(QSizeF(check.width() / 1.8, check.height() / 1.8));
-      check.moveCenter(checkBox().controlPointRect().center());
-      QPainterPath p;
-      p.addEllipse(check);
-      painter->fillPath(p, Qt::black);
+    if (!plotItem()->isInSharedAxisBox()) {
+      if (isHovering()) {
+        QRectF check = checkBox().controlPointRect();
+        check.setSize(QSizeF(check.width() / 1.8, check.height() / 1.8));
+        check.moveCenter(checkBox().controlPointRect().center());
+        QPainterPath p;
+        p.addEllipse(check);
+        painter->fillPath(p, Qt::black);
+      }
+      if (plotItem()->isTiedZoom()) {
+        painter->save();
+        painter->setOpacity(0.5);
+        painter->fillPath(checkBox(), Qt::black);
+        painter->restore();
+      }
+      painter->drawPath(checkBox());
     }
-    if (plotItem()->isTiedZoom()) {
-      painter->save();
-      painter->setOpacity(0.5);
-      painter->fillPath(checkBox(), Qt::black);
-      painter->restore();
-    }
-    painter->drawPath(checkBox());
     painter->restore();
   }
 
--- branches/work/kst/portto4/kst/src/libkstapp/sharedaxisboxitem.cpp #903957:903958
@@ -76,52 +76,14 @@
             continue;
           }
         }
-        plotItem->setParent(this);
-        plotItem->setAllowedGripModes(0);
-        plotItem->setFlags(0);
-        plotItem->setTiedZoom(true);
+        plotItem->setSharedAxisBox(this);
         child = plotItem;
       }
     }
     if (child) {
       setBrush(Qt::white);
 
-      QList<ViewItem*> viewItems;
-      QList<QGraphicsItem*> list = 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() != this)
-          continue;
-        viewItems.append(viewItem);
-      }
-
-      if (viewItems.isEmpty())
-        return; //not added to undostack
-
-      Grid *grid = Grid::buildGrid(viewItems, 0);
-      Q_ASSERT(grid);
-
-      _layout = new ViewGridLayout(this);
-
-      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->apply();
-      _layout->apply();
+      sharePlots();
     } else {
       delete this;
     }
@@ -137,10 +99,7 @@
       continue;
 
     if (PlotItem *plotItem = qobject_cast<PlotItem*>(viewItem)) {
-      plotItem->setParent(0);
-      plotItem->setAllowedGripModes(Move | Resize | Rotate);
-      plotItem->setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
-      plotItem->setTiedZoom(false);
+       plotItem->setSharedAxisBox(0);
     }
   }
   if (_layout) {
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.cpp #903957:903958
@@ -32,6 +32,8 @@
     _enabled(false),
     _rowCount(0),
     _columnCount(0),
+    _shareX(false),
+    _shareY(false),
     _spacing(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)),
     _margin(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)) {
 }
@@ -227,6 +229,7 @@
     }
   }
   layout->applyAxis();
+  layout->applyAxis();
 }
 
 
@@ -292,7 +295,6 @@
 
 void ViewGridLayout::apply() {
   updatePlotMargins();
-  updateSharedAxis();
 
   //For now we divide up equally... can do stretch factors and such later...
 
@@ -366,11 +368,85 @@
              << endl;
 #endif
   }
-  updateSharedAxis();
 }
 
 
 void ViewGridLayout::applyAxis() {
+  updatePlotMargins();
+
+  //For now we divide up equally... can do stretch factors and such later...
+
+  QSizeF layoutSize(parentItem()->width() - _margin.width() * 2,
+                    parentItem()->height() - _margin.height() * 2);
+
+  QPointF layoutTopLeft = parentItem()->rect().topLeft();
+  layoutTopLeft += QPointF(_margin.width(), _margin.height());
+
+  QRectF layoutRect(layoutTopLeft, layoutSize);
+
+  qreal itemWidth = layoutSize.width() / columnCount();
+  qreal itemHeight = layoutSize.height() / rowCount();
+
+#if DEBUG_LAYOUT
+  qDebug() << "layouting" << _items.count()
+           << "itemWidth:" << itemWidth
+           << "itemHeight:" << itemHeight
+           << endl;
+#endif
+
+  foreach (LayoutItem item, _items) {
+    QPointF topLeft(itemWidth * item.column, itemHeight * item.row);
+    QSizeF size(itemWidth * item.columnSpan, itemHeight * item.rowSpan);
+    topLeft += layoutTopLeft;
+
+    QRectF itemRect(topLeft, size);
+
+    if (itemRect.top() != layoutRect.top())
+      itemRect.setTop(itemRect.top() + _spacing.height() / 2);
+    if (itemRect.left() != layoutRect.left())
+      itemRect.setLeft(itemRect.left() + _spacing.width() / 2);
+    if (itemRect.bottom() != layoutRect.bottom())
+      itemRect.setBottom(itemRect.bottom() - _spacing.height() / 2);
+    if (itemRect.right() != layoutRect.right())
+      itemRect.setRight(itemRect.right() - _spacing.width() / 2);
+
+    item.viewItem->resetTransform();
+    item.viewItem->setPos(itemRect.topLeft());
+
+    if (item.viewItem->fixedSize()) {
+      itemRect.setBottom(itemRect.top() + item.viewItem->rect().height());
+      itemRect.setRight(itemRect.left() + item.viewItem->rect().width());
+    } else if (item.viewItem->lockAspectRatio()) {
+      qreal newHeight = itemRect.height();
+      qreal newWidth = itemRect.width();
+
+      qreal aspectRatio = item.viewItem->rect().width() / item.viewItem->rect().height();
+      if ((newWidth / newHeight) > aspectRatio) {
+        // newWidth is too large.  Use newHeight as key.
+        newWidth = newHeight * aspectRatio;
+      } else {
+        // newHeight is either too large, or perfect.  use newWidth as key.
+        newHeight = newWidth / aspectRatio;
+      }
+      itemRect.setBottom(itemRect.top() + newHeight);
+      itemRect.setRight(itemRect.left() + newWidth);
+    }
+    item.viewItem->setViewRect(QRectF(QPoint(0,0), itemRect.size()));
+
+    if (PlotItem *plotItem = qobject_cast<PlotItem*>(item.viewItem))
+      emit plotItem->updatePlotRect();
+
+#if DEBUG_LAYOUT
+    qDebug() << "layout"
+             << "row:" << item.row
+             << "column:" << item.column
+             << "rowSpan:" << item.rowSpan
+             << "columnSpan:" << item.columnSpan
+             << "itemRect:" << itemRect
+             << endl;
+#endif
+  }
+  calculateSharing();
   updateSharedAxis();
 }
 
@@ -418,6 +494,9 @@
 
 
 void ViewGridLayout::updateSharedAxis() {
+  if (!_shareX && !_shareY) {
+    return;
+  }
   foreach (LayoutItem item, _items) {
     PlotItem *plotItem = qobject_cast<PlotItem*>(item.viewItem);
 
@@ -426,14 +505,58 @@
 
     //same horizontal range and same row/rowspan
     //same vertical range and same col/colspan
-    shareAxisWithPlotToLeft(item);
-    shareAxisWithPlotToRight(item);
-    shareAxisWithPlotAbove(item);
-    shareAxisWithPlotBelow(item);
+    if (_shareX) {
+      shareAxisWithPlotAbove(item);
+      shareAxisWithPlotBelow(item);
+    }
+    if (_shareY) {
+      shareAxisWithPlotToLeft(item);
+      shareAxisWithPlotToRight(item);
+    }
   }
 }
 
 
+void ViewGridLayout::calculateSharing() {
+  bool xMatch = true;
+  bool yMatch = true;
+
+  bool first = true;
+
+  qreal xStart = 0.0, xStop = 0.0;
+  qreal yStart = 0.0, yStop = 0.0;
+
+  foreach (LayoutItem item, _items) {
+    PlotItem *plotItem = qobject_cast<PlotItem*>(item.viewItem);
+
+    if (!plotItem)
+      continue;
+
+    if (first) {
+      xStart = plotItem->projectionRect().left();
+      xStop = plotItem->projectionRect().right();
+      yStart = plotItem->projectionRect().top();
+      yStop = plotItem->projectionRect().bottom();
+      first = false;
+    } else {
+      if (xMatch && (plotItem->projectionRect().left() != xStart || plotItem->projectionRect().right() != xStop)) {
+        xMatch = false;
+      }
+      if (yMatch && (plotItem->projectionRect().top() != yStart || plotItem->projectionRect().bottom() != yStop)) {
+        yMatch = false;
+      }
+    }
+  }
+  if (xMatch || yMatch) {
+    _shareX = xMatch;
+    _shareY = yMatch;
+  } else {
+    _shareX = true;
+    _shareY = true;
+  }
+}
+
+
 void ViewGridLayout::shareAxisWithPlotToLeft(LayoutItem item) {
   PlotItem *plotItem = qobject_cast<PlotItem*>(item.viewItem);
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #903957:903958
@@ -55,6 +55,8 @@
     bool isEnabled() const;
     void setEnabled(bool enabled);
 
+    void calculateSharing();
+
     static void resetSharedPlots(ViewItem *item);
     static void standardizePlotMargins(ViewItem *item);
     static void sharePlots(ViewItem *item);
@@ -101,6 +103,9 @@
     int _rowCount;
     int _columnCount;
 
+    bool _shareX;
+    bool _shareY;
+
     QSizeF _spacing;
     QSizeF _margin;
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #903957:903958
@@ -677,14 +677,7 @@
 
 
 void ViewItem::sharePlots() {
-  qDebug() << "ViewItem::sharePlots";
-  if (parentViewItem()) {
-    ViewGridLayout::sharePlots(parentViewItem());
-/*    LayoutCommand *layout = new LayoutCommand(parentViewItem());
-    layout->createLayout();*/
-  } else if (parentView()) {
-    parentView()->sharePlots();
-  }
+  ViewGridLayout::sharePlots(this);
 }
 
 


More information about the Kst mailing list