[Kst] kdeextragear-2/kst/kst

Andrew Walker arwalker at sumusltd.com
Wed Dec 15 20:52:10 CET 2004


CVS commit by arwalker: 

Use a smarter algorithm to do the re-gridding which moves plots to the grid based on the closest plot relative to their top-left corner.

CCMAIL: 95029-done at bugs.kde.org


  M +69 -44    kstviewobject.cpp   1.103


--- kdeextragear-2/kst/kst/kstviewobject.cpp  #1.102:1.103
@@ -21,4 +21,5 @@
 
 // include files for Qt
+#include <qdeepcopy.h>
 #include <qstylesheet.h>
 
@@ -30,4 +31,5 @@
 #include "kst.h"
 #include "kstdoc.h"
+#include "kstobject.h"
 #include "kstplotgroup.h"
 #include "kstsettings.h"
@@ -459,8 +461,5 @@ void KstViewObject::setOnGrid(bool on_gr
 
 void KstViewObject::cleanup(int cols) {
-  if (_children.count() < 1) {
-    return;
-  }
-
+  if (_children.count() > 0) {
   if (!_onGrid) {
     if (cols <= 0) {
@@ -477,4 +476,7 @@ void KstViewObject::cleanup(int cols) {
   }
 
+    KstViewObjectList childrenCopy;
+    double distance;
+    double minDistance = 0.0;
   int pos = 0;
   int x = 0;
@@ -484,21 +486,43 @@ void KstViewObject::cleanup(int cols) {
   int h = _geom.height() / (lastRow + (_children.count() % _columns > 0 ? 1 : 0));
 
+    childrenCopy = QDeepCopy<KstViewObjectList>(_children);
+
   //kdDebug() << "cleanup with w=" << w << " and h=" << h << endl;
   //kdDebug() << "columns=" << _columns  << endl;
-  for (KstViewObjectList::Iterator i = _children.begin(); i != _children.end(); ++i) {
-    QSize sz(w, h);
+    for (unsigned i = 0; i < _children.count(); ++i) {
+      KstViewObjectList::Iterator nearest = childrenCopy.end();
     QPoint pt(x, y);
-    // Adjust the last column to be sure that we don't spill over
+      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
+      //
+      // adjust the last row to be sure that we don't spill over
+      //
     if ((pos + 1) / _columns > lastRow) {
       sz.setHeight(_geom.height() - y);
     }
 
-    (*i)->move(pt);
-    (*i)->resize(sz);
+      for (KstViewObjectList::Iterator it = childrenCopy.begin(); it != childrenCopy.end(); ++it) {
+        //
+        // find the plot closest to the desired position, based on the top-left corner...
+        //
+        distance  = (double)(( x - (*it)->geometry().x() ) * ( x - (*it)->geometry().x() ));
+        distance += (double)(( y - (*it)->geometry().y() ) * ( y - (*it)->geometry().y() ));
+        if( it == childrenCopy.begin() || distance < minDistance ) {
+          minDistance = distance;
+          nearest = it;
+        }
+      }
+      if (nearest != childrenCopy.end()) {
+        (*nearest)->move(pt);
+        (*nearest)->resize(sz);
+        childrenCopy.remove(*nearest);
+      }
 
     if (++pos % _columns == 0) {
@@ -509,4 +533,5 @@ void KstViewObject::cleanup(int cols) {
     }
   }
+  }
 }
 





More information about the Kst mailing list