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

Barth Netterfield netterfield at astro.utoronto.ca
Fri Dec 30 20:46:09 UTC 2011


SVN commit 1270940 by netterfield:

Fixes to 'columns' auto-layout:  It now lays out in columns, in the
order of creation, completely eradicating any previous formatting.
Use 'auto' to keep current formating.

This seems to work better...

Also delete some dead code.


 M  +3 -0      datawizard.cpp  
 M  +4 -86     layoutboxitem.cpp  
 M  +0 -2      layoutboxitem.h  
 M  +1 -1      mainwindow.cpp  
 M  +3 -3      view.cpp  
 M  +1 -1      view.h  
 M  +1 -0      viewgridlayout.h  
 M  +35 -30    viewitem.cpp  
 M  +1 -1      viewitem.h  
 A             viewitemmanager.cpp   [License: GPL (v2+)]
 A             viewitemmanager.h   [License: GPL (v2+)]


--- branches/work/kst/portto4/kst/src/libkstapp/datawizard.cpp #1270939:1270940
@@ -1161,6 +1161,9 @@
       plot->update();
       plot->view()->appendToLayout(layout_type, plot, num_columns);
     }
+    if (layout_type == CurvePlacement::Custom) {
+      plotList.at(0)->createCustomLayout(num_columns);
+    }
 
   }
   foreach (PlotItem* plot, plotList) {
--- branches/work/kst/portto4/kst/src/libkstapp/layoutboxitem.cpp #1270939:1270940
@@ -25,6 +25,8 @@
 
 LayoutBoxItem::LayoutBoxItem(View *parent)
     : ViewItem(parent) {
+  setAllowsLayout(false);
+
   setTypeName("Layout Box");
   setPen(Qt::NoPen);
   setBrush(Qt::NoBrush);
@@ -43,45 +45,17 @@
     return;
 
   item->setParentViewItem(this);
-//   layout()->addViewItem(item, layout()->rowCount(), 0, 1, layout()->columnCount());
   setPos(view()->sceneRect().topLeft());
   setViewRect(view()->sceneRect());
-  //singleshot to give plots a chance to set a projection rect
-//   QTimer::singleShot(0, layout(), SLOT(update()));
 }
 
 
-void LayoutBoxItem::save(QXmlStreamWriter &xml) {
-  if (isVisible()) {
-    xml.writeStartElement("layoutbox");
-    ViewItem::save(xml);
 
-    QList<QGraphicsItem*> list = QGraphicsItem::children();
-    foreach (QGraphicsItem *item, list) {
-      ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
-      if (!viewItem)
-        continue;
-
-      xml.writeStartElement("layoutitem");
-      xml.writeAttribute("name", viewItem->typeName());
-      xml.writeEndElement();
+void LayoutBoxItem::save(QXmlStreamWriter &xml) {
+  // layout box items are transient and are never saved..
     }
 
-    xml.writeEndElement();
-  }
-}
 
-
-void LayoutBoxItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
-  Q_UNUSED(option);
-  Q_UNUSED(widget);
-
-  painter->setPen(pen());
-  painter->setBrush(brush());
-  painter->drawRect(rect());
-}
-
-
 LayoutBoxItem::~LayoutBoxItem() {
 }
 
@@ -130,24 +104,7 @@
 }
 
 
-bool LayoutBoxItem::appendItemFromXml(QXmlStreamReader &xml) {
-  bool validTag = false;
-  if (xml.isStartElement() && xml.name().toString() == "layoutitem") {
-    QXmlStreamAttributes attrs = xml.attributes();
-    QStringRef av;
-    av = attrs.value("name");
-    if (!av.isNull()) {
-      //TODO Use ObjectTag to locate the correct item and add it to this layout.
-    }
-    xml.readNext();
-    if (xml.isEndElement() && xml.name().toString() == "layoutitem") {
-      validTag = true;
-    }
-  }
-  return validTag;
-}
 
-
 LayoutBoxItemFactory::LayoutBoxItemFactory()
 : GraphicsFactory() {
   registerFactory("layoutbox", this);
@@ -159,47 +116,8 @@
 
 
 ViewItem* LayoutBoxItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) {
-  LayoutBoxItem *rc = 0;
-  while (!xml.atEnd()) {
-    bool validTag = true;
-    if (xml.isStartElement()) {
-      if (!rc && xml.name().toString() == "layoutbox") {
-        Q_ASSERT(!rc);
-        rc = new LayoutBoxItem(view);
-        if (parent) {
-          rc->setParentViewItem(parent);
-        }
-        // Add any specialized BoxItem Properties here.
-      } else if (xml.name().toString() == "layoutitem") {
-        Q_ASSERT(rc);
-        validTag = rc->appendItemFromXml(xml);
-      } else {
-        Q_ASSERT(rc);
-        if (!rc->parse(xml, validTag) && validTag) {
-          ViewItem *i = GraphicsFactory::parse(xml, store, view, rc);
-          if (!i) {
-          }
-        }
-      }
-    } else if (xml.isEndElement()) {
-      if (xml.name().toString() == "layoutbox") {
-        break;
-      } else {
-        validTag = false;
-      }
-    }
-    if (!validTag) {
-      qDebug("invalid Tag\n");
-      Debug::self()->log(QObject::tr("Error creating layoutbox object from Kst file."), Debug::Warning);
-      delete rc;
       return 0;
     }
-    xml.readNext();
-  }
-  // LayoutBoxItem automatically adds itself to the parent. Don't return the item here as it
-  // has already been added to the scene.  
-  return 0;
-}
 
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/layoutboxitem.h #1270939:1270940
@@ -28,10 +28,8 @@
     const QString defaultsGroupName() const {return QString("layoutbox");}
 
     void appendItem(ViewItem *item);
-    bool appendItemFromXml(QXmlStreamReader &xml);
 
     virtual void save(QXmlStreamWriter &xml);
-    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 
   public Q_SLOTS:
     void setEnabled(bool enabled);
--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.cpp #1270939:1270940
@@ -195,7 +195,7 @@
 
 
 bool MainWindow::promptSave() {
-  int rc = QMessageBox::warning(this, tr("Kst"), tr("Your document has been modified.\nSave changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
+  int rc = QMessageBox::warning(this, tr("Kst: Save Promp"), tr("Your document has been modified.\nSave changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
   if (rc == QMessageBox::Save) {
     save();
   } else if (rc == QMessageBox::Cancel) {
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #1270939:1270940
@@ -372,9 +372,9 @@
 void View::createCustomLayout() {
   bool ok;
   int default_cols = qMax(1,int(sqrt((double)Data::self()->plotList().count())));
-  int columns = QInputDialog::getInteger(this, tr("Kst"),
-                                      tr("Select Number of Columns"),default_cols, 0,
-                                      10, 1, &ok);
+  int columns = QInputDialog::getInt(this, tr("Kst: Column Layout"),
+                                      tr("Layout in columns in order of creation.\nSelect number of columns:"),default_cols, 1,
+                                      15, 1, &ok);
   if (ok) {
     createLayout(false, columns);
   }
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #1270939:1270940
@@ -69,7 +69,7 @@
 
     QPolygonF creationPolygon(CreationEvents events) const;
 
-    LayoutBoxItem* layoutBoxItem() const { return _layoutBoxItem; }
+    //LayoutBoxItem* layoutBoxItem() const { return _layoutBoxItem; }
     void setLayoutBoxItem(LayoutBoxItem *layoutBoxItem) { _layoutBoxItem = layoutBoxItem; }
 
     bool showGrid() const { return _showGrid; }
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #1270939:1270940
@@ -40,6 +40,7 @@
 
     int rowCount() const;
     int columnCount() const;
+    void setColumnCount(int count) {_columnCount = (count>_columnCount)? count : _columnCount ;}
 
     QSizeF spacing() const { return _spacing; }
     void setSpacing(const QSizeF &spacing) { _spacing = spacing; }
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #1270939:1270940
@@ -20,6 +20,7 @@
 #include "datacollection.h"
 #include "formatgridhelper.h"
 #include "dialogdefaults.h"
+#include "viewitemmanager.h"
 
 #include "layoutboxitem.h"
 
@@ -129,10 +130,13 @@
   // only drop plots onto TabBar
   setAcceptDrops(false);
 
+  ViewItemManager::self()->addViewItem(this);
+
 }
 
 
 ViewItem::~ViewItem() {
+  ViewItemManager::self()->removeViewItem(this);
 }
 
 void ViewItem::_initializeShortName() {
@@ -850,15 +854,18 @@
 }
 
 
-void ViewItem::createCustomLayout() {
-  bool ok;
+void ViewItem::createCustomLayout(int columns) {
+  bool ok = true;
   int default_cols = qMax(1,int(sqrt((double)Data::self()->plotList().count())));
-  int columns = QInputDialog::getInteger(view(), tr("Kst"),
-                                      tr("Select Number of Columns"),default_cols, 0,
-                                      10, 1, &ok);
 
+  if (columns<1) {
+    columns = QInputDialog::getInt(view(), tr("Kst: Column Layout"),
+                                       tr("Layout in columns in order of creation.\nSelect number of columns:"),default_cols, 1,
+                                       15, 1, &ok);
+  }
+
   if (ok) {
-    if (parentViewItem()) {
+    if (parentViewItem() && false) {
       LayoutCommand *layout = new LayoutCommand(parentViewItem());
       layout->createLayout(false, columns);
     } else if (view()) {
@@ -2240,13 +2247,16 @@
   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);
-  }
 
+  //foreach (QGraphicsItem *item, list) {
+  //  ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
+  //  if (!viewItem || viewItem->hasStaticGeometry() || !viewItem->allowsLayout() || viewItem->parentItem() != _item)
+  //    continue;
+    //viewItems.append(viewItem);
+  //}
+
+  viewItems = ViewItemManager::layoutableViewItemsForView(_item->view());
+
   if (viewItems.isEmpty()) {
     return; //not added to undostack
   }
@@ -2257,25 +2267,25 @@
 
   if (grid.n_cols == columns) {
     if (grid.numHoles()<columns) {
-      columns = 0; // already in correct columns - just line stuff up
+      //columns = 0; // already in correct columns - just line stuff up
     }
   }
 
   if (columns == 0) {
-    int n_views = viewItems.size();
-    for (int i_view = 0; i_view<n_views; i_view++) {
-      ViewItem *v = viewItems.at(i_view);
-      struct AutoFormatRC rc = grid.rcList.at(i_view);
+    int n_view_items = viewItems.size();
+    for (int i_view_item = 0; i_view_item<n_view_items; i_view_item++) {
+      ViewItem *v = viewItems.at(i_view_item);
+      struct AutoFormatRC rc = grid.rcList.at(i_view_item);
       _layout->addViewItem(v, rc.row, rc.col, rc.row_span, rc.col_span);
     }
 
   } else {
     int row = 0;
     int col = 0;
-    int n_views = viewItems.size();
+    int n_view_items = viewItems.size();
 
-    for (int i_view = 0; i_view<n_views; i_view++) {
-      ViewItem *v = viewItems.at(i_view);
+    for (int i_view_item = 0; i_view_item<n_view_items; i_view_item++) {
+      ViewItem *v = viewItems.at(i_view_item);
       _layout->addViewItem(v, row, col, 1, 1);
       col++;
       if (col>=columns) {
@@ -2316,6 +2326,7 @@
     columns = 0;
   }
 
+  if (layout == CurvePlacement::Custom) layout = CurvePlacement::Protect;
   if (layout == CurvePlacement::Protect) {
     _layout = new ViewGridLayout(_item);
 
@@ -2325,21 +2336,14 @@
     item->setPos(center);
     item->setViewRect(0.0, 0.0, 200.0, 200.0);
     _item->view()->scene()->addItem(item);
-    _item->view()->undoStack()->push(this);
+    //_item->view()->undoStack()->push(this);
     return;
   }
 
 
   QList<ViewItem*> viewItems;
-  QList<QGraphicsItem*> list = _item->QGraphicsItem::children();
+  viewItems = ViewItemManager::layoutableViewItemsForView(_item->view());
 
-  foreach (QGraphicsItem *item, list) {
-    ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
-    if (!viewItem || viewItem->hasStaticGeometry() || !viewItem->allowsLayout() || viewItem->parentItem() != _item)
-      continue;
-    viewItems.append(viewItem);
-  }
-
   _layout = new ViewGridLayout(_item);
 
   FormatGridHelper grid(viewItems);
@@ -2399,14 +2403,15 @@
     }
     _item->view()->scene()->addItem(item);
     _layout->addViewItem(item, row, col, 1,1);
+    _layout->setColumnCount(columns);
   }
+
   if (qobject_cast<LayoutBoxItem*>(_item)) {
     QObject::connect(_layout, SIGNAL(enabledChanged(bool)),
                     _item, SLOT(setEnabled(bool)));
   }
 
   _layout->apply();
-  _item->view()->undoStack()->push(this);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #1270939:1270940
@@ -244,7 +244,7 @@
     virtual void lower();
     virtual void createAutoLayout();
     virtual void createProtectedLayout();
-    virtual void createCustomLayout();
+    virtual void createCustomLayout(int columns = 0);
     virtual void sharePlots(QPainter *painter, bool creation);
     virtual void remove();
     void resizeTopLeft(const QPointF &offset);


More information about the Kst mailing list