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

Barth Netterfield netterfield at astro.utoronto.ca
Wed Aug 10 16:48:48 UTC 2011


SVN commit 1246235 by netterfield:

Auto-format now deletes empty rows and columns, and moves plots that would be
covered up by other plots so they aren't covered.

The old behavior is preserved with 'protect layout'.



 M  +0 -8      devel-docs/Kst2Specs/Wishlist  
 M  +92 -4     src/libkstapp/formatgridhelper.cpp  
 M  +3 -1      src/libkstapp/formatgridhelper.h  
 M  +8 -4      src/libkstapp/view.cpp  
 M  +3 -1      src/libkstapp/view.h  
 M  +20 -8     src/libkstapp/viewitem.cpp  
 M  +3 -1      src/libkstapp/viewitem.h  


--- branches/work/kst/portto4/kst/devel-docs/Kst2Specs/Wishlist #1246234:1246235
@@ -13,10 +13,6 @@
 Fixup line/arrow dimensions tab:
   x,y instead of length/angle
 
-----------
-
-Scientific notation (instead of C's %e notation) for extreme axis numbers
-
 --------
 
 Edit multiple for other view objects (arrow, line, label...)
@@ -49,10 +45,6 @@
 
 Legends can get larger than the plot if there are enought things in them.  This should not be. 
 
---------
-
-vector selectors in curve dialog can't grow when you extend the window size.
-
 -------
 
 Some automagic way of deciding if plugin dialogs can make plots.
--- branches/work/kst/portto4/kst/src/libkstapp/formatgridhelper.cpp #1246234:1246235
@@ -110,7 +110,7 @@
 
 /*****************************************************************************/
 
-FormatGridHelper::FormatGridHelper(const QList<ViewItem*> &viewItems) {
+FormatGridHelper::FormatGridHelper(const QList<ViewItem*> &viewItems, bool protectLayout) {
   const double min_size_limit = 0.02;
 
   double min_height = 100000.0;
@@ -201,15 +201,35 @@
       a[i_row][i_col] = 0;
     }
   }
-  foreach (const struct AutoFormatRC &rc, rcList) {
-    for (int i_row = rc.row; i_row<rc.row+rc.row_span; i_row++) {
-      for (int i_col = rc.col; i_col<rc.col+rc.col_span; i_col++) {
+  QList<int> overlapedEntryIndices;
+  int n_rc = rcList.length();
+  for (int i_rc = 0; i_rc < n_rc; i_rc++) {
+  //foreach (const struct AutoFormatRC &rc, rcList) {
+    for (int i_row = rcList.at(i_rc).row; i_row<rcList.at(i_rc).row+rcList.at(i_rc).row_span; i_row++) {
+      for (int i_col = rcList.at(i_rc).col; i_col<rcList.at(i_rc).col+rcList.at(i_rc).col_span; i_col++) {
+        if (a[i_row][i_col]>0) {
+          overlapedEntryIndices.append(i_rc);
+        }
         a[i_row][i_col]++;
       }
     }
   }
+  if (!protectLayout) { // remove overlaps and delete empty rows
+    // move overlaps
+    if (overlapedEntryIndices.length()>0) {
+      foreach (const int &i_ol, overlapedEntryIndices) {
+        int row, col;
+        getHole(row,col);
+        rcList[i_ol].row = row;
+        rcList[i_ol].col = col;
 }
+    }
+    // remove empty rows
+    condense();
+  }
 
+}
+
 int FormatGridHelper::numHoles() {
   int n_holes = 0;
   for (int i_row = 0; i_row<n_rows; i_row++) {
@@ -222,4 +242,72 @@
 
   return n_holes;
 }
+
+// find an unused location in the grid.  Create a new row if needed.
+void FormatGridHelper::getHole(int &row, int &col) {
+  for (int i_row = 0; i_row<n_rows; i_row++) {
+    for (int i_col = 0; i_col<n_cols; i_col++) {
+      if (a[i_row][i_col] == 0) {
+        row = i_row;
+        col = i_col;
+        a[i_row][i_col]++; // mark it used
+        return;
 }
+    }
+  }
+  // add a new row
+  n_rows++;
+  a.resize(n_rows);
+  a[n_rows-1].resize(n_cols);
+  row = n_rows-1;
+  col = 0;
+  a[row][col]++; // mark it used
+  return;
+}
+
+// remove empty rows and empty columns
+void FormatGridHelper::condense() {
+  // delete empty rows;
+  for (int i_row = 0; i_row < n_rows; i_row++) {
+    bool row_used = false;
+    for (int i_col = 0; i_col < n_cols; i_col++) {
+      if (a[i_row][i_col]>0) {
+        row_used = true;
+      }
+    }
+    if (!row_used) {
+      a.remove(i_row);
+      int n_rc = rcList.length();
+      for (int i_rc=0; i_rc < n_rc; i_rc++) {
+        if (rcList[i_rc].row > i_row) {
+          rcList[i_rc].row--;
+        }
+      }
+      i_row--;
+      n_rows--;
+    }
+  }
+  // delete empty columns
+  for (int i_col = 0; i_col < n_cols; i_col++) {
+    bool col_used = false;
+    for (int i_row = 0; i_row < n_rows; i_row++) {
+      if (a[i_row][i_col]>0) {
+        col_used = true;
+      }
+    }
+    if (!col_used) {
+      for (int j_row = 0; j_row<n_rows; j_row++) {
+        a[j_row].remove(i_col);
+      }
+      int n_rc = rcList.length();
+      for (int i_rc=0; i_rc < n_rc; i_rc++) {
+        if (rcList[i_rc].col > i_col) {
+          rcList[i_rc].col--;
+        }
+      }
+      i_col--;
+      n_cols--;
+    }
+  }
+}
+}
--- branches/work/kst/portto4/kst/src/libkstapp/formatgridhelper.h #1246234:1246235
@@ -38,13 +38,15 @@
 class FormatGridHelper
 {
 public:
-  FormatGridHelper(const QList<ViewItem*> &viewItems);
+  FormatGridHelper(const QList<ViewItem*> &viewItems, bool protectLayout=true);
 
   QList<AutoFormatRC> rcList;
   QVector< QVector <int> > a;
   int n_rows;
   int n_cols;
   int numHoles();
+  void getHole(int &row, int &col);
+  void condense();
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #1246234:1246235
@@ -98,8 +98,11 @@
   connect(_editAction, SIGNAL(triggered()), this, SLOT(edit()));
 
   _autoLayoutAction = new QAction(tr("Automatic"), this);
-  connect(_autoLayoutAction, SIGNAL(triggered()), this, SLOT(createLayout()));
+  connect(_autoLayoutAction, SIGNAL(triggered()), this, SLOT(createUnprotectedLayout()));
 
+  _protectedLayoutAction = new QAction(tr("Protect Layout"), this);
+  connect(_protectedLayoutAction, SIGNAL(triggered()), this, SLOT(createLayout()));
+
   _customLayoutAction = new QAction(tr("Custom"), this);
   connect(_customLayoutAction, SIGNAL(triggered()), this, SLOT(createCustomLayout()));
 
@@ -372,16 +375,16 @@
                                       tr("Select Number of Columns"),default_cols, 0,
                                       10, 1, &ok);
   if (ok) {
-    createLayout(columns);
+    createLayout(false, columns);
   }
 }
 
 
-void View::createLayout(int columns) {
+void View::createLayout(bool preserve, int columns) {
   PlotItemManager::self()->clearFocusedPlots();
 
   LayoutCommand *layout = new LayoutCommand(new LayoutBoxItem(this));
-  layout->createLayout(columns);
+  layout->createLayout(preserve, columns);
 
   if (_layoutBoxItem) {
     _layoutBoxItem->setEnabled(false);
@@ -664,6 +667,7 @@
   QMenu layoutMenu;
   layoutMenu.setTitle(tr("Cleanup Layout"));
   layoutMenu.addAction(_autoLayoutAction);
+  layoutMenu.addAction(_protectedLayoutAction);
   layoutMenu.addAction(_customLayoutAction);
   menu.addMenu(&layoutMenu);
 
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #1246234:1246235
@@ -127,7 +127,8 @@
     void creationPolygonChanged(View::CreationEvent event);
 
   public Q_SLOTS:
-    void createLayout(int columns = 0);
+    void createLayout(bool preserve = true, int columns = 0);
+    void createUnprotectedLayout(bool preserve = true, int columns = 0) {createLayout(false);}
     void appendToLayout(CurvePlacement::Layout layout, ViewItem* item, int columns = 0);
     void createCustomLayout();
     void viewChanged();
@@ -146,6 +147,7 @@
 
     QAction *_editAction;
     QAction *_autoLayoutAction;
+    QAction *_protectedLayoutAction;
     QAction *_customLayoutAction;
 
   private Q_SLOTS:
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #1246234:1246235
@@ -120,7 +120,10 @@
   _autoLayoutAction = new QAction(tr("Automatic"), this);
   connect(_autoLayoutAction, SIGNAL(triggered()), this, SLOT(createAutoLayout()));
 
-  _customLayoutAction = new QAction(tr("Custom"), this);
+  _protectedLayoutAction = new QAction(tr("Protect Layout"), this);
+  connect(_protectedLayoutAction, SIGNAL(triggered()), this, SLOT(createProtectedLayout()));
+
+  _customLayoutAction = new QAction(tr("Columns"), this);
   connect(_customLayoutAction, SIGNAL(triggered()), this, SLOT(createCustomLayout()));
 
   // only drop plots onto TabBar
@@ -823,13 +826,22 @@
 void ViewItem::createAutoLayout() {
   if (parentViewItem()) {
     LayoutCommand *layout = new LayoutCommand(parentViewItem());
-    layout->createLayout();
+    layout->createLayout(false);
   } else if (view()) {
-    view()->createLayout();
+    view()->createLayout(false);
   }
 }
 
+void ViewItem::createProtectedLayout() {
+  if (parentViewItem()) {
+    LayoutCommand *layout = new LayoutCommand(parentViewItem());
+    layout->createLayout(true);
+  } else if (view()) {
+    view()->createLayout(true);
+  }
+}
 
+
 void ViewItem::createCustomLayout() {
   bool ok;
   int default_cols = qMax(1,int(sqrt((double)Data::self()->plotList().count())));
@@ -840,9 +852,9 @@
   if (ok) {
     if (parentViewItem()) {
       LayoutCommand *layout = new LayoutCommand(parentViewItem());
-      layout->createLayout(columns);
+      layout->createLayout(false, columns);
     } else if (view()) {
-      view()->createLayout(columns);
+      view()->createLayout(false, columns);
     }
   }
 }
@@ -955,6 +967,7 @@
 
     layoutMenu.setTitle(tr("Cleanup Layout"));
     layoutMenu.addAction(_autoLayoutAction);
+    layoutMenu.addAction(_protectedLayoutAction);
     layoutMenu.addAction(_customLayoutAction);
     menu.addMenu(&layoutMenu);
 
@@ -2167,7 +2180,7 @@
 }
 
 
-void LayoutCommand::createLayout(int columns) {
+void LayoutCommand::createLayout(bool preserve, int columns) {
   Q_ASSERT(_item);
   Q_ASSERT(_item->view());
 
@@ -2176,7 +2189,6 @@
   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)
@@ -2190,7 +2202,7 @@
 
   _layout = new ViewGridLayout(_item);
 
-  FormatGridHelper grid(viewItems);
+  FormatGridHelper grid(viewItems, preserve);
 
   if (grid.n_cols == columns) {
     if (grid.numHoles()<columns) {
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #1246234:1246235
@@ -240,6 +240,7 @@
     virtual void raise();
     virtual void lower();
     virtual void createAutoLayout();
+    virtual void createProtectedLayout();
     virtual void createCustomLayout();
     virtual void sharePlots(QPainter *painter, bool creation);
     virtual void remove();
@@ -303,6 +304,7 @@
     QAction *_raiseAction;
     QAction *_lowerAction;
     QAction *_autoLayoutAction;
+    QAction *_protectedLayoutAction;
     QAction *_customLayoutAction;
 
     bool _isXTiedZoom;
@@ -413,7 +415,7 @@
 
     virtual void undo();
     virtual void redo();
-    void createLayout(int columns = 0);
+    void createLayout(bool preserve = true, int columns = 0);
   private:
     QPointer<ViewGridLayout> _layout;
 };


More information about the Kst mailing list