[Kst] extragear/graphics/kst/src/libkstapp

George Staikos staikos at kde.org
Tue Aug 1 19:08:30 CEST 2006


Quoting Andrew Walker <arwalker at sumusltd.com>:

> Calling normalize on a QRect that (for example) has a left value of 399 and
> a width of -399 results in a QRect with a left value of -1 and a width of
> 401.
> Clearly incorrect, and this is what the code fixes.

  Interesting.  I investigated it and found that the problem was that it was 
dividing an odd number by two and then adding the two halfs (integers) 
together again, getting a number that was off-by-one.

  I didn't see any issues with normalization.  Also based on the code in 
normalize(), I can't see how it would possibly produce the result you 
describe:

QRect QRect::normalize() const
{
    QRect r;
    if ( x2 < x1 ) {                            // swap bad x values
        r.x1 = x2;
        r.x2 = x1;
    } else {
        r.x1 = x1;
        r.x2 = x2;
    }
    if ( y2 < y1 ) {                            // swap bad y values
        r.y1 = y2;
        r.y2 = y1;
    } else {
        r.y1 = y1;
        r.y2 = y2;
    }
    return r;
}




Index: libkstapp/kstgfxmousehandlerutils.cpp
===================================================================
--- libkstapp/kstgfxmousehandlerutils.cpp       (revision 568353)
+++ libkstapp/kstgfxmousehandlerutils.cpp       (working copy)
@@ -16,7 +16,6 @@
  ***************************************************************************/

 #include <stdlib.h>
-#include <stdio.h>
 #include <math.h>

 #include <qrect.h>
@@ -24,6 +23,7 @@

 #include <kglobal.h>

+#include "ksdebug.h"
 #include "kstgfxmousehandlerutils.h"

 QPoint KstGfxMouseHandlerUtils::findNearestPtOnLine(const QPoint& fromPoint, 
const QPoint& toPoint, const QPoint& pos, const QRect &bounds) {
@@ -106,35 +106,35 @@


 QRect KstGfxMouseHandlerUtils::resizeRectFromEdge(const QRect& originalSize, 
const QPoint& anchorPoint, const QPoint& movePoint, const QPoint& pos, const 
QRect &bounds, bool maintainAspect) {
-  QRect newSize;
+  QRect newSize(originalSize);

-  if (movePoint.y() == anchorPoint.y() ) {
+  if (movePoint.y() == anchorPoint.y()) {
       int newWidth = pos.x() - anchorPoint.x() + 1; // +1 for the way widths 
are defined in QRect.
-      double newHalfHeight = originalSize.height()/2.0;

       if (maintainAspect) {
-        newHalfHeight = 
originalSize.height()*abs(newWidth)/originalSize.width()/2.0;
+        double newHalfHeight = originalSize.height() * abs(newWidth) / 
originalSize.width() / 2.0;

         newHalfHeight = kMin(double(movePoint.y() - bounds.top()), 
newHalfHeight); // ensure we are still within the bounds.
         newHalfHeight = kMin(double(bounds.bottom() - movePoint.y()), 
newHalfHeight);

-        if (newWidth == 0) { newWidth = 1; } // anything better to be done?
+        if (newWidth == 0) { // anything better to be done?
+          newWidth = 1;
+        }

-        newWidth = 
int(originalSize.width()*newHalfHeight*2.0/originalSize.height()*newWidth/abs(newWidth)); // 
consistency of width w/ the newly calculated height.
+        newWidth = int(originalSize.width() * newHalfHeight * 2.0 / 
originalSize.height() * newWidth / abs(newWidth)); // consistency of width w/ 
the newly calculated height.
+        newSize.setTop(anchorPoint.y() + int(newHalfHeight));
+        newSize.setBottom(anchorPoint.y() - int(newHalfHeight));
       }

       newSize.setLeft(anchorPoint.x());
       newSize.setWidth(newWidth);
-      newSize.setTop(anchorPoint.y() + int(newHalfHeight));
-      newSize.setBottom(anchorPoint.y() - int(newHalfHeight));

-    } else if (movePoint.x() == anchorPoint.x() ) {
+    } else if (movePoint.x() == anchorPoint.x()) {
       // mimic the case for (movePoint.y() == anchorPoint.y()). comments are 
there.
       int newHeight = pos.y() - anchorPoint.y() + 1;
-      double newHalfWidth = originalSize.width()/2.0;

       if (maintainAspect) {
-        newHalfWidth = 
originalSize.width()*abs(newHeight)/originalSize.height()/2.0;
+        double newHalfWidth = originalSize.width() * abs(newHeight) / 
originalSize.height() / 2.0;

         newHalfWidth = kMin(double(movePoint.x() - bounds.left()), 
newHalfWidth);
         newHalfWidth = kMin(double(bounds.right() - movePoint.x()), 
newHalfWidth);
@@ -143,13 +143,13 @@
           newHeight = 1;
         }

-        newHeight = 
int(originalSize.height()*newHalfWidth*2.0/originalSize.width()*newHeight/abs(newHeight));
+        newHeight = int(originalSize.height() * newHalfWidth * 2.0 / 
originalSize.width() * newHeight / abs(newHeight));
+        newSize.setLeft(anchorPoint.x() + int(newHalfWidth));
+        newSize.setRight(anchorPoint.x() - int(newHalfWidth));
       }

       newSize.setTop(anchorPoint.y());
       newSize.setHeight(newHeight);
-      newSize.setLeft(anchorPoint.x() + int(newHalfWidth));
-      newSize.setRight(anchorPoint.x() - int(newHalfWidth));
     }

     newSize = newSize.normalize();


-- 
George Staikos 



More information about the Kst mailing list