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

Mike Fenton mike at staikos.net
Thu Jul 17 20:41:56 CEST 2008


SVN commit 833979 by fenton:

Create Custom Grid building options.


 M  +37 -0     gridlayouthelper.cpp  
 M  +1 -0      gridlayouthelper.h  
 M  +1 -1      mainwindow.cpp  
 M  +1 -1      plotrenderitem.cpp  
 M  +2 -2      view.cpp  
 M  +1 -1      view.h  
 M  +25 -5     viewitem.cpp  
 M  +3 -2      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/gridlayouthelper.cpp #833978:833979
@@ -52,6 +52,9 @@
 
 #include "gridlayouthelper.h"
 
+#include <math.h>
+#include <QDebug>
+
 namespace Kst {
 
 Grid *Grid::buildGrid(const QList<ViewItem*> &itemList)
@@ -144,6 +147,40 @@
     return grid;
 }
 
+Grid *Grid::buildGrid(const QList<ViewItem*> &itemList, int columns)
+{
+    if (!itemList.count())
+        return 0;
+
+    if (columns == 0) {
+      return buildGrid(itemList);
+    }
+
+    int rows = ceil((qreal)itemList.count() / columns);
+
+    QMap<int, ViewItem*> sortedItems;
+    foreach(ViewItem* item, itemList) {
+      int sortId = item->pos().x() + item->pos().y() * 10;
+      sortedItems.insert(sortId, item);
+    }
+
+    Grid *grid = new Grid(rows, columns);
+
+    // Mark the cells in the grid that contains a widget
+    int row = 0, col = 0;
+    foreach (ViewItem *v, sortedItems) {
+        grid->setCell(row, col, v);
+        col++;
+        if (col == columns) {
+          row++;
+          col = 0;
+        }
+    }
+
+    grid->simplify();
+    return grid;
+}
+
 Grid::Grid(int r, int c) :
     m_nrows(r),
     m_ncols(c),
--- branches/work/kst/portto4/kst/src/libkstapp/gridlayouthelper.h #833978:833979
@@ -61,6 +61,7 @@
 {
 public:
     static Grid *buildGrid(const QList<ViewItem*> &itemList);
+    static Grid *buildGrid(const QList<ViewItem*> &itemList, int columns);
 
     Grid(int rows, int cols);
     ~Grid();
--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.cpp #833978:833979
@@ -407,7 +407,7 @@
   if (!selectedItems.isEmpty()) {
     ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(selectedItems.first());
     Q_ASSERT(viewItem);
-    viewItem->createLayout();
+    viewItem->createAutoLayout();
   } else {
     view->createLayout();
   }
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #833978:833979
@@ -551,7 +551,7 @@
 
 
 void PlotRenderItem::createLayout() {
-  plotItem()->createLayout();
+  plotItem()->createAutoLayout();
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #833978:833979
@@ -253,9 +253,9 @@
 }
 
 
-void View::createLayout() {
+void View::createLayout(int columns) {
   LayoutCommand *layout = new LayoutCommand(new LayoutBoxItem(this));
-  layout->createLayout();
+  layout->createLayout(columns);
 
   if (_layoutBoxItem) {
     _layoutBoxItem->setEnabled(false);
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #833978:833979
@@ -91,7 +91,7 @@
     void creationPolygonChanged(View::CreationEvent event);
 
   public Q_SLOTS:
-    void createLayout();
+    void createLayout(int columns = 0);
 
   protected:
     bool event(QEvent *event);
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #833978:833979
@@ -28,6 +28,7 @@
 #include <QWidgetAction>
 #include <QGraphicsScene>
 #include <QGraphicsSceneContextMenuEvent>
+#include <QInputDialog>
 
 static const double ONE_PI = 3.14159265358979323846264338327950288419717;
 static double TWO_PI = 2.0 * ONE_PI;
@@ -652,7 +653,7 @@
 }
 
 
-void ViewItem::createLayout() {
+void ViewItem::createAutoLayout() {
   if (parentViewItem()) {
     LayoutCommand *layout = new LayoutCommand(parentViewItem());
     layout->createLayout();
@@ -662,6 +663,22 @@
 }
 
 
+void ViewItem::createCustomLayout() {
+  bool ok;
+  int columns = QInputDialog::getInteger(tr("Kst"),
+                                      tr("Select Number of Columns"),1, 0,
+                                      10, 1, &ok);
+  if (ok) {
+    if (parentViewItem()) {
+      LayoutCommand *layout = new LayoutCommand(parentViewItem());
+      layout->createLayout(columns);
+    } else if (parentView()) {
+      parentView()->createLayout(columns);
+    }
+  }
+}
+
+
 void ViewItem::raise() {
   RaiseCommand *up = new RaiseCommand(this);
   up->redo();
@@ -756,9 +773,12 @@
   QMenu layoutMenu;
   layoutMenu.setTitle(tr("Cleanup Layout"));
 
-  QAction *layoutAction = layoutMenu.addAction(tr("Automatic Cleanup"));
-  connect(layoutAction, SIGNAL(triggered()), this, SLOT(createLayout()));
+  QAction *autoLayoutAction = layoutMenu.addAction(tr("Automatic"));
+  connect(autoLayoutAction, SIGNAL(triggered()), this, SLOT(createAutoLayout()));
 
+  QAction *customLayoutAction = layoutMenu.addAction(tr("Custom"));
+  connect(customLayoutAction, SIGNAL(triggered()), this, SLOT(createCustomLayout()));
+
   menu.addMenu(&layoutMenu);
 
   QAction *removeAction = menu.addAction(tr("Remove"));
@@ -1801,7 +1821,7 @@
 }
 
 
-void LayoutCommand::createLayout() {
+void LayoutCommand::createLayout(int columns) {
   Q_ASSERT(_item);
   Q_ASSERT(_item->parentView());
 
@@ -1820,7 +1840,7 @@
   if (viewItems.isEmpty())
     return; //not added to undostack
 
-  Grid *grid = Grid::buildGrid(viewItems);
+  Grid *grid = Grid::buildGrid(viewItems, columns);
   Q_ASSERT(grid);
 
   _layout = new ViewGridLayout(_item);
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #833978:833979
@@ -143,7 +143,8 @@
     virtual void edit();
     virtual void raise();
     virtual void lower();
-    virtual void createLayout();
+    virtual void createAutoLayout();
+    virtual void createCustomLayout();
     virtual void remove();
     void resizeTopLeft(const QPointF &offset);
     void resizeTopRight(const QPointF &offset);
@@ -278,7 +279,7 @@
 
     virtual void undo();
     virtual void redo();
-    void createLayout();
+    void createLayout(int columns = 0);
 
   private:
     QPointer<ViewGridLayout> _layout;


More information about the Kst mailing list