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

Adam Treat treat at kde.org
Fri Sep 14 00:04:18 CEST 2007


SVN commit 712204 by treat:

* Move debug defines.
* Fix crash on exit.
* Bugfixes for reparenting when an item is in a layout
already in correct parent.  Simplify...
* Better reparenting debug.
* Fix crash when attempting to create a layout with no items.
* Add functions to the layout class for controlling plotitem's
margins as per spec.
* Make the plotitem and plotrenderitem respect the layout's
margins as per spec.


 M  +49 -14    plotitem.cpp  
 M  +7 -2      plotitem.h  
 M  +1 -1      plotrenderitem.cpp  
 M  +47 -12    viewgridlayout.cpp  
 M  +8 -0      viewgridlayout.h  
 M  +39 -18    viewitem.cpp  
 M  +0 -3      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #712203:712204
@@ -13,6 +13,8 @@
 
 #include "plotrenderitem.h"
 
+#include "viewgridlayout.h"
+
 #include "kstsvector.h"
 #include "kstvcurve.h"
 #include "kstdatacollection.h"
@@ -24,6 +26,8 @@
 static qreal MARGIN_WIDTH = 20.0;
 static qreal MARGIN_HEIGHT = 20.0;
 
+static uint COUNT = 0;
+
 namespace Kst {
 
 PlotItem::PlotItem(View *parent)
@@ -38,7 +42,12 @@
   yTest->setLabel("a nice y label");
 
   KstVectorPtr yTest2 = new KstSVector(-100.0, 100.0, 10000, KstObjectTag::fromString("Y vector 2"));
-  yTest2->setLabel("another nice y label");
+  if (COUNT % 2) {
+    yTest2->setLabel("another nice y label");
+  } else {
+    yTest2->setLabel("another really REALLY REALLY REALLY nice y label");
+  }
+  COUNT = COUNT + 1;
 
   KstVCurvePtr renderTest = new KstVCurve(QString("rendertest"), xTest, yTest, 0, 0, 0, 0, QColor(Qt::red));
   renderTest->writeLock();
@@ -179,24 +188,50 @@
 }
 
 
-QRectF PlotItem::horizontalLabelRect() const {
-  return QRectF(0.0, 0.0, width() - 2.0 * marginWidth(), marginHeight());
+qreal PlotItem::layoutMarginWidth() const {
+  ViewItem *viewItem = dynamic_cast<ViewItem*>(parentItem());
+  if (viewItem && viewItem->layout()) {
+    return viewItem->layout()->plotMarginWidth();
+  } else {
+    return marginWidth();
+  }
 }
 
 
-QRectF PlotItem::verticalLabelRect() const {
-  return QRectF(0.0, 0.0, marginWidth(), height() - 2.0 * marginHeight());
+qreal PlotItem::layoutMarginHeight() const {
+  ViewItem *viewItem = dynamic_cast<ViewItem*>(parentItem());
+  if (viewItem && viewItem->layout()) {
+    return viewItem->layout()->plotMarginHeight();
+  } else {
+    return marginHeight();
+  }
 }
 
 
+QRectF PlotItem::horizontalLabelRect(bool calc) const {
+  if (calc)
+    return QRectF(0.0, 0.0, width() - 2.0 * marginWidth(), marginHeight());
+  else
+    return QRectF(0.0, 0.0, width() - 2.0 * layoutMarginWidth(), layoutMarginHeight());
+}
+
+
+QRectF PlotItem::verticalLabelRect(bool calc) const {
+  if (calc)
+    return QRectF(0.0, 0.0, marginWidth(), height() - 2.0 * marginHeight());
+  else
+    return QRectF(0.0, 0.0, layoutMarginWidth(), height() - 2.0 * layoutMarginHeight());
+}
+
+
 void PlotItem::paintLeftLabel(QPainter *painter) {
   painter->save();
   QTransform t;
   t.rotate(90.0);
   painter->rotate(-90.0);
 
-  QRectF leftLabelRect = verticalLabelRect();
-  leftLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
+  QRectF leftLabelRect = verticalLabelRect(false);
+  leftLabelRect.moveTopLeft(QPointF(0.0, layoutMarginHeight()));
   painter->drawText(t.mapRect(leftLabelRect), Qt::TextWordWrap | Qt::AlignCenter, leftLabel());
   painter->restore();
 }
@@ -220,8 +255,8 @@
 
 void PlotItem::paintBottomLabel(QPainter *painter) {
   painter->save();
-  QRectF bottomLabelRect = horizontalLabelRect();
-  bottomLabelRect.moveTopLeft(QPointF(marginWidth(), height() - marginHeight()));
+  QRectF bottomLabelRect = horizontalLabelRect(false);
+  bottomLabelRect.moveTopLeft(QPointF(layoutMarginWidth(), height() - layoutMarginHeight()));
   painter->drawText(bottomLabelRect, Qt::TextWordWrap | Qt::AlignCenter, bottomLabel());
   painter->restore();
 }
@@ -239,14 +274,14 @@
 
 void PlotItem::paintRightLabel(QPainter *painter) {
   painter->save();
-  painter->translate(width() - marginWidth(), 0.0);
+  painter->translate(width() - layoutMarginWidth(), 0.0);
   QTransform t;
   t.rotate(-90.0);
   painter->rotate(90.0);
 
   //same as left but painter is translated
-  QRectF rightLabelRect = verticalLabelRect();
-  rightLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
+  QRectF rightLabelRect = verticalLabelRect(false);
+  rightLabelRect.moveTopLeft(QPointF(0.0, layoutMarginHeight()));
   painter->drawText(t.mapRect(rightLabelRect), Qt::TextWordWrap | Qt::AlignCenter, rightLabel());
   painter->restore();
 }
@@ -269,8 +304,8 @@
 
 void PlotItem::paintTopLabel(QPainter *painter) {
   painter->save();
-  QRectF topLabelRect = horizontalLabelRect();
-  topLabelRect.moveTopLeft(QPointF(marginWidth(), 0.0));
+  QRectF topLabelRect = horizontalLabelRect(false);
+  topLabelRect.moveTopLeft(QPointF(layoutMarginWidth(), 0.0));
   painter->drawText(topLabelRect, Qt::TextWordWrap | Qt::AlignCenter, bottomLabel());
   painter->restore();
 }
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.h #712203:712204
@@ -42,14 +42,17 @@
     qreal marginHeight() const;
     void setMarginHeight(qreal marginHeight);
 
+    qreal layoutMarginWidth() const;
+    qreal layoutMarginHeight() const;
+
     QString leftLabel() const;
     QString bottomLabel() const;
     QString rightLabel() const;
     QString topLabel() const;
 
   private:
-    QRectF horizontalLabelRect() const;
-    QRectF verticalLabelRect() const;
+    QRectF horizontalLabelRect(bool calc = true) const;
+    QRectF verticalLabelRect(bool calc = true) const;
 
     void paintLeftLabel(QPainter *painter);
     QSizeF calculateLeftLabelBound(QPainter *painter);
@@ -64,6 +67,8 @@
     QList<PlotRenderItem*> _renderers;
     qreal _marginWidth;
     qreal _marginHeight;
+
+    friend class ViewGridLayout;
 };
 
 class KST_EXPORT CreatePlotCommand : public CreateCommand
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #712203:712204
@@ -35,7 +35,7 @@
 
 void PlotRenderItem::updateGeometry() {
   QRectF rect = plotItem()->rect().normalized();
-  QPointF margin(plotItem()->marginWidth(), plotItem()->marginHeight());
+  QPointF margin(plotItem()->layoutMarginWidth(), plotItem()->layoutMarginHeight());
   QPointF topLeft(rect.topLeft() + margin);
   QPointF bottomRight(rect.bottomRight() - margin);
   setRect(QRectF(topLeft, bottomRight));
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.cpp #712203:712204
@@ -12,9 +12,12 @@
 #include "viewgridlayout.h"
 
 #include "viewitem.h"
+#include "plotitem.h"
 
 #include <QDebug>
 
+// #define DEBUG_LAYOUT
+
 static qreal DEFAULT_STRUT = 20.0;
 
 namespace Kst {
@@ -24,7 +27,9 @@
     _rowCount(0),
     _columnCount(0),
     _spacing(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)),
-    _margin(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)) {
+    _margin(QSizeF(DEFAULT_STRUT,DEFAULT_STRUT)),
+    _plotMarginWidth(0.0),
+    _plotMarginHeight(0.0) {
 
   parent->setLayout(this);
 }
@@ -73,8 +78,20 @@
 }
 
 
+qreal ViewGridLayout::plotMarginWidth() const {
+  return _plotMarginWidth;
+}
+
+
+qreal ViewGridLayout::plotMarginHeight() const {
+  return _plotMarginHeight;
+}
+
+
 void ViewGridLayout::update() {
 
+  updatePlotMargins();
+
   //For now we divide up equally... can do stretch factors and such later...
 
   QSizeF layoutSize(parentItem()->width() - _margin.width() * 2,
@@ -88,10 +105,12 @@
   qreal itemWidth = layoutSize.width() / columnCount();
   qreal itemHeight = layoutSize.height() / rowCount();
 
-//   qDebug() << "layouting" << _items.count()
-//            << "itemWidth:" << itemWidth
-//            << "itemHeight:" << itemHeight
-//            << endl;
+#ifdef DEBUG_LAYOUT
+  qDebug() << "layouting" << _items.count()
+           << "itemWidth:" << itemWidth
+           << "itemHeight:" << itemHeight
+           << endl;
+#endif
 
   foreach (LayoutItem item, _items) {
     QPointF topLeft(itemWidth * item.column, itemHeight * item.row);
@@ -113,15 +132,31 @@
     item.viewItem->setPos(itemRect.topLeft());
     item.viewItem->setViewRect(QRectF(QPoint(0,0), itemRect.size()));
 
-//     qDebug() << "layout"
-//              << "row:" << item.row
-//              << "column:" << item.column
-//              << "rowSpan:" << item.rowSpan
-//              << "columnSpan:" << item.columnSpan
-//              << "itemRect:" << itemRect
-//              << endl;
+#ifdef DEBUG_LAYOUT
+    qDebug() << "layout"
+             << "row:" << item.row
+             << "column:" << item.column
+             << "rowSpan:" << item.rowSpan
+             << "columnSpan:" << item.columnSpan
+             << "itemRect:" << itemRect
+             << endl;
+#endif
   }
+}
 
+
+void ViewGridLayout::updatePlotMargins() {
+  _plotMarginWidth = 0.0;
+  _plotMarginHeight = 0.0;
+  foreach (LayoutItem item, _items) {
+    PlotItem *plotItem = dynamic_cast<PlotItem*>(item.viewItem);
+
+    if (!plotItem)
+      continue;
+
+    _plotMarginWidth = qMax(_plotMarginWidth, plotItem->marginWidth());
+    _plotMarginHeight = qMax(_plotMarginHeight, plotItem->marginHeight());
+  }
 }
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #712203:712204
@@ -40,9 +40,15 @@
     QSizeF margin() const { return _margin; }
     void setMargin(const QSizeF &margin) { _margin = margin; }
 
+    qreal plotMarginWidth() const;
+    qreal plotMarginHeight() const;
+
   public Q_SLOTS:
     void update();
 
+  private Q_SLOTS:
+    void updatePlotMargins();
+
   private:
     struct LayoutItem {
       ViewItem *viewItem;
@@ -59,6 +65,8 @@
     QSizeF _margin;
 
     QList<LayoutItem> _items;
+    qreal _plotMarginWidth;
+    qreal _plotMarginHeight;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #712203:712204
@@ -29,6 +29,8 @@
 static double TWO_PI = 2.0 * ONE_PI;
 static double RAD2DEG = 180.0 / ONE_PI;
 
+// #define DEBUG_GEOMETRY
+// #define DEBUG_REPARENT
 #define SELECT_BOUND 0
 #define SUPPRESS_SCALE 1
 
@@ -324,7 +326,10 @@
 
 
 QRectF ViewItem::boundingRect() const {
-  if (!isSelected() && !isHovering() || parentView()->mouseMode() == View::Create)
+  bool inCreation = false;
+  if (parentView()) /* false when exiting */
+    inCreation = parentView()->mouseMode() == View::Create;
+  if (!isSelected() && !isHovering() || inCreation)
     return QGraphicsRectItem::boundingRect();
 
 #if SELECT_BOUND
@@ -1096,21 +1101,28 @@
   foreach (QGraphicsItem *item, collisions) {
     ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
 
-    if (!viewItem || viewItem->layout() /*don't break existing layouts*/)
+    if (!viewItem) /*bah*/
       continue;
 
-    if (viewItem->collidesWithItem(this, Qt::ContainsItemShape)) {
+    if (!viewItem->collidesWithItem(this, Qt::ContainsItemShape)) /*doesn't contain*/
+      continue;
 
-      if (parentItem() == viewItem) /*already done*/
-        return false;
+    if (parentItem() == viewItem) { /*already done*/
+#ifdef DEBUG_REPARENT
+      qDebug() << "already in containing parent" << endl;
+#endif
+      return false;
+    }
 
+    if (viewItem->layout()) /*don't crash existing layout*/
+      continue;
+
 #ifdef DEBUG_REPARENT
-      qDebug() << "reparent to" << viewItem << endl;
+    qDebug() << "reparent to" << viewItem << endl;
 #endif
-      setParentItem(viewItem);
-      setPos(viewItem->mapFromScene(scenePos));
-      return true;
-    }
+    setParentItem(viewItem);
+    setPos(viewItem->mapFromScene(scenePos));
+    return true;
   }
 
   //No suitable collisions then reparent to top-level
@@ -1232,10 +1244,11 @@
 
 
 void LayoutCommand::redo() {
-  ViewGridLayout *layout = new ViewGridLayout(_item);
-
   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)
@@ -1243,10 +1256,14 @@
     viewItems.append(viewItem);
   }
 
+  if (viewItems.isEmpty())
+    return;
+
   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)) {
@@ -1264,11 +1281,11 @@
 }
 
 
-void BreakLayoutCommand::undo() {
-  ViewGridLayout *layout = new ViewGridLayout(_item);
+void BreakLayoutCommand::undo() {  QList<ViewItem*> viewItems;
+  QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
+  if (list.isEmpty())
+    return;
 
-  QList<ViewItem*> viewItems;
-  QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
   foreach (QGraphicsItem *item, list) {
     ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
     if (!viewItem)
@@ -1276,10 +1293,14 @@
     viewItems.append(viewItem);
   }
 
+  if (viewItems.isEmpty())
+    return;
+
   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)) {
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #712203:712204
@@ -19,9 +19,6 @@
 #include "viewcommand.h"
 #include "view.h" //forward declare, but enums??
 
-// #define DEBUG_GEOMETRY
-// #define DEBUG_REPARENT
-
 namespace Kst {
 
 class DialogPage;


More information about the Kst mailing list