[Kst] [Bug 116460] cleanup layout should be smart in regards to number of columns

Andrew Walker arwalker at sumusltd.com
Tue Nov 22 22:59:09 CET 2005


------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
         
http://bugs.kde.org/show_bug.cgi?id=116460         
arwalker sumusltd com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



------- Additional Comments From arwalker sumusltd com  2005-11-22 22:59 -------
SVN commit 482420 by arwalker:

BUG:116460 Attempt to preserve the inherent order of plots when regridding

 M  +81 -65    kstviewobject.cpp  
 M  +1 -0      kstviewobject.h  


--- trunk/extragear/graphics/kst/kst/kstviewobject.cpp #482419:482420
 @ -35,6 +35,7  @
 #include "kstaccessibility.h"
 #include "kstdoc.h"
 #include "ksteditviewobjectdialog_i.h"
+#include "kstmath.h"
 #include "kstobject.h"
 #include "kstplotgroup.h"
 #include "kstsettings.h"
 @ -609,7 +610,7  @
 
 void KstViewObject::cleanup(int cols) {
   KstViewObjectList childrenCopy;
-    
+
   for (KstViewObjectList::ConstIterator i = _children.begin(); i != _children.end(); ++i) {
     if ((*i)->followsFlow()) {
       childrenCopy.append(*i);
 @ -617,74 +618,85  @
   }
   
   int cnt = childrenCopy.count();
-  if (cnt < 1) {
-    return;
-  }
-
-  // FIXME: don't allow regrid to a number of columns that will result in
-  //        >= height() plots in a column
-  if (!_onGrid) {
-    if (cols <= 0) {
-      cols = int(sqrt(cnt));
+  if (cnt > 0) {
+    // FIXME: don't allow regrid to a number of columns that will result in
+    //        >= height() plots in a column
+    if (!_onGrid) {
+      if (cols <= 0) {
+        cols = int(sqrt(cnt));
+      }
+      _onGrid = true;
+      _columns = QMAX(1, cols);
+    } else {
+      if (cols > 0) {
+        _columns = cols;
+      } else if (cols <= 0){
+        _columns = QMAX(1, int(sqrt(cnt)));
+      }
     }
-    _onGrid = true;
-    _columns = QMAX(1, cols);
-  } else {
-    if (cols > 0) {
-      _columns = cols;
-    } else if (cols <= 0){
-      _columns = QMAX(1, int(sqrt(cnt)));
-    }
-  }  
-  int rows = ( cnt + _columns - 1 ) / _columns;
-
-  double minDistance = 0.0;
-  int pos = 0;
-  int x = 0;
-  int y = 0;
-  int w = _geom.width() / _columns;
-  int h = _geom.height() / rows;
+    int rows = ( cnt + _columns - 1 ) / _columns;
     
-  //kstdDebug() << "cleanup with w=" << w << " and h=" << h << endl;
-  //kstdDebug() << "columns=" << _columns  << endl;
-  for (int i = 0; i < cnt; ++i) {
-    KstViewObjectList::Iterator nearest = childrenCopy.end();
-    QPoint pt(x, y);
-    QSize sz(w, h);
-
-    // adjust the last column to be sure that we don't spill over
-    if (pos % _columns == _columns - 1) {
-      sz.setWidth(_geom.width() - x);
-    }
-
-    // adjust the last row to be sure that we don't spill over
-    if (pos / _columns == rows - 1) {
-      sz.setHeight(_geom.height() - y);
-    }
-
-    for (KstViewObjectList::Iterator it = childrenCopy.begin(); it != childrenCopy.end(); ++it) {
-      // find plot closest to the desired position, based on top-left corner...
-      double distance = double((x - (*it)->geometry().x()) * (x - (*it)->geometry().x())) + 
-                        double((y - (*it)->geometry().y()) * (y - (*it)->geometry().y()));
-      if (it == childrenCopy.begin() || distance < minDistance) {
-        minDistance = distance;
-        nearest = it;
+    //
+    // the following is an attempt to arrange objects on a grid.
+    //  This should behave as both a snap-to-grid when the objects
+    //  are already roughly aligned to the desired grid, but should
+    //  also act to retain the inherent order when this is not the
+    //  case. The inherent order is defined by identifying objects
+    //  from left-to-right and top-to-bottom based on the position
+    //  of their top-left corner.
+    //
+    double minDistance = 0.0;
+    double distance;
+    int pos = 0;
+    int x = 0;
+    int y = 0;
+    int w = _geom.width() / _columns;
+    int h = _geom.height() / rows;
+      
+    //kstdDebug() << "cleanup with w=" << w << " and h=" << h << endl;
+    //kstdDebug() << "columns=" << _columns  << endl;
+    for (int i = 0; i < cnt; ++i) {
+      KstViewObjectList::Iterator nearest = childrenCopy.end();
+      QPoint pt(x, y);
+      QSize sz(w, h);
+  
+      // adjust the last column to be sure that we don't spill over
+      if (pos % _columns == _columns - 1) {
+        sz.setWidth(_geom.width() - x);
       }
+  
+      // adjust the last row to be sure that we don't spill over
+      if (pos / _columns == rows - 1) {
+        sz.setHeight(_geom.height() - y);
+      }
+  
+      for (KstViewObjectList::Iterator it = childrenCopy.begin(); it != childrenCopy.end(); ++it) {
+        distance  = ceil( (double)rows * 2.0 * (*it)->aspectRatio().y );
+        distance  = rows * d2i( distance );
+        distance += (*it)->aspectRatio().x * rows;
+        distance += (*it)->aspectRatio().y;
+        
+        if (it == childrenCopy.begin() || distance < minDistance) {
+          minDistance = distance;
+          nearest = it;
+        }
+      }
+  
+      if (nearest != childrenCopy.end()) {
+        KstViewObjectPtr vop = *nearest;
+        vop->move(pt);
+        vop->resize(sz);
+        vop->lowerToBottom();
+        childrenCopy.remove(vop);
+      }
+  
+      if (++pos % _columns == 0) {
+        x = 0;
+        y += h;
+      } else {
+        x += w;
+      }
     }
-
-    if (nearest != childrenCopy.end()) {
-      KstViewObjectPtr vop = *nearest;
-      vop->move(pt);
-      vop->resize(sz);
-      childrenCopy.remove(vop);
-    }
-
-    if (++pos % _columns == 0) {
-      x = 0;
-      y += h;
-    } else {
-      x += w;
-    }
   }
 }
 
 @ -808,6 +820,10  @
 }
 
 
+const KstAspectRatio& KstViewObject::aspectRatio() const {
+  return _aspect;
+}
+
 const QRect& KstViewObject::geometry() const {
   return _geom;
 }
--- trunk/extragear/graphics/kst/kst/kstviewobject.h #482419:482420
 @ -94,6 +94,7  @
     virtual void internalAlignment(KstPaintType type, QPainter& p, QRect& plotRegion);
     virtual QPoint position() const;
     virtual const QRect& geometry() const;
+    virtual const KstAspectRatio& aspectRatio() const;
     virtual QRect surroundingGeometry() const;
     virtual QRect contentsRect() const;
     virtual void setContentsRect(const QRect& rect);


More information about the Kst mailing list