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

Adam Treat treat at kde.org
Thu Aug 30 14:38:44 CEST 2007


SVN commit 706444 by treat:

* Floating point snap and more options


 M  +17 -15    view.cpp  
 M  +19 -6     view.h  


--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #706443:706444
@@ -14,6 +14,8 @@
 #include "kstapplication.h"
 #include "applicationsettings.h"
 
+#include <math.h>
+
 #include <QDebug>
 #include <QTimer>
 #include <QUndoStack>
@@ -26,7 +28,12 @@
 namespace Kst {
 
 View::View()
-  : QGraphicsView(kstApp->mainWindow()), _currentPlotItem(0), _mouseMode(Default), _snapToGrid(true) {
+  : QGraphicsView(kstApp->mainWindow()),
+    _currentPlotItem(0),
+    _mouseMode(Default),
+    _gridSpacing(QSizeF(20,20)),
+    _snapToGridHorizontal(true),
+    _snapToGridVertical(true) {
 
   _undoStack = new QUndoStack(this);
   setScene(new QGraphicsScene(this));
@@ -96,20 +103,15 @@
 
 
 QPointF View::snapPoint(const QPointF &point) {
+  qreal x = point.x();
+  qreal y = point.y();
+  if (_snapToGridHorizontal && gridSpacing().width() > 0)
+    x -= fmod(point.x(), gridSpacing().width());
 
-  if (!_snapToGrid)
-    return point;
+  if (_snapToGridVertical && gridSpacing().height() > 0)
+    y -= fmod(point.y(), gridSpacing().height());
 
-  //FIXME floating point?
-  QPointF p(point.x() - int(point.x()) % int(gridSpacing().width()),
-            point.y() - int(point.y()) % int(gridSpacing().height()));
-
-//   qDebug() << "snap "
-//             << "before:" << point
-//             << "after:" << p
-//             << endl;
-
-  return p;
+  return QPointF(x, y);
 }
 
 
@@ -190,7 +192,7 @@
 
   //vertical lines
   qreal x = r.left() + spacing;
-  while (x < r.right()) {
+  while (x < r.right() && spacing > 0) {
     QLineF line(QPointF(x, r.top()), QPointF(x, r.bottom()));
     painter->drawLine(line);
     x += spacing;
@@ -200,7 +202,7 @@
 
   //horizontal lines
   qreal y = r.top() + spacing;
-  while (y < r.bottom()) {
+  while (y < r.bottom() && spacing > 0) {
     QLineF line(QPointF(r.left(), y), QPointF(r.right(), y));
     painter->drawLine(line);
     y += spacing;
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #706443:706444
@@ -43,13 +43,24 @@
 
   QPolygonF creationPolygon(CreationEvents events) const;
 
-  //FIXME make configurable...
-  QSizeF gridSpacing() const { return QSizeF(20,20); }
+  QSizeF gridSpacing() const { return _gridSpacing; }
+  void setGridSpacing(const QSizeF &gridSpacing) { _gridSpacing = gridSpacing; }
 
-  //FIXME make configurable...
-  bool snapToGrid() const { return _snapToGrid; }
-  void setSnapToGrid(bool snapToGrid) { _snapToGrid = snapToGrid; }
+  bool snapToGridHorizontal() const
+  { return _snapToGridHorizontal; }
+  void setSnapToGridHorizontal(bool snapToGridHorizontal)
+  { _snapToGridHorizontal = snapToGridHorizontal; }
 
+  bool snapToGridVertical() const
+  { return _snapToGridVertical; }
+  void setSnapToGridVertical(bool snapToGridVertical)
+  { _snapToGridVertical = snapToGridVertical; }
+
+  bool snapToGrid() const
+  { return _snapToGridHorizontal && _snapToGridVertical; }
+  void setSnapToGrid(bool snapToGrid)
+  { _snapToGridHorizontal = snapToGrid; _snapToGridVertical = snapToGrid; }
+
   QPointF snapPoint(const QPointF &point);
 
 Q_SIGNALS:
@@ -72,7 +83,9 @@
   QPolygonF _creationPolygonPress;
   QPolygonF _creationPolygonMove;
   QPolygonF _creationPolygonRelease;
-  bool _snapToGrid;
+  QSizeF _gridSpacing;
+  bool _snapToGridHorizontal;
+  bool _snapToGridVertical;
 };
 
 }


More information about the Kst mailing list