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

Adam Treat treat at kde.org
Wed Aug 29 19:59:15 CEST 2007


SVN commit 706220 by treat:

* Draw a grid and snap item creation and movement
to this grid.  All of this will be configurable
in the future of course.


 M  +57 -4     view.cpp  
 M  +11 -0     view.h  
 M  +3 -0      viewitem.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #706219:706220
@@ -26,7 +26,7 @@
 namespace Kst {
 
 View::View()
-  : QGraphicsView(kstApp->mainWindow()), _currentPlotItem(0), _mouseMode(Default) {
+  : QGraphicsView(kstApp->mainWindow()), _currentPlotItem(0), _mouseMode(Default), _snapToGrid(true) {
 
   _undoStack = new QUndoStack(this);
   setScene(new QGraphicsScene(this));
@@ -95,6 +95,24 @@
 }
 
 
+QPointF View::snapPoint(const QPointF &point) {
+
+  if (!_snapToGrid)
+    return point;
+
+  //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;
+}
+
+
 bool View::eventFilter(QObject *obj, QEvent *event) {
   if (obj != scene() || _mouseMode != Create)
     return QGraphicsView::eventFilter(obj, event);
@@ -103,21 +121,21 @@
   case QEvent::GraphicsSceneMousePress:
     {
       QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
-      _creationPolygonPress << e->buttonDownScenePos(Qt::LeftButton);
+      _creationPolygonPress << snapPoint(e->buttonDownScenePos(Qt::LeftButton));
       emit creationPolygonChanged(MousePress);
       return true; //filter this otherwise something can grab our mouse...
     }
   case QEvent::GraphicsSceneMouseRelease:
     {
       QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
-      _creationPolygonRelease << e->scenePos();
+      _creationPolygonRelease << snapPoint(e->scenePos());
       emit creationPolygonChanged(MouseRelease);
       break;
     }
   case QEvent::GraphicsSceneMouseMove:
     {
       QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event);
-      _creationPolygonMove << e->scenePos();
+      _creationPolygonMove << snapPoint(e->scenePos());
       emit creationPolygonChanged(MouseMove);
       break;
     }
@@ -156,6 +174,41 @@
   }
 }
 
+
+void View::drawBackground(QPainter *painter, const QRectF &rect) {
+
+  QGraphicsView::drawBackground(painter, rect);
+
+  painter->save();
+  painter->setPen(Qt::gray);
+  painter->setOpacity(0.2);
+
+  const QRectF r = sceneRect();
+  qreal spacing = gridSpacing().width();
+
+  //FIXME We should probably only draw those lines that intercept rect
+
+  //vertical lines
+  qreal x = r.left() + spacing;
+  while (x < r.right()) {
+    QLineF line(QPointF(x, r.top()), QPointF(x, r.bottom()));
+    painter->drawLine(line);
+    x += spacing;
+  }
+
+  spacing = gridSpacing().height();
+
+  //horizontal lines
+  qreal y = r.top() + spacing;
+  while (y < r.bottom()) {
+    QLineF line(QPointF(r.left(), y), QPointF(r.right(), y));
+    painter->drawLine(line);
+    y += spacing;
+  }
+
+  painter->restore();
 }
 
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #706219:706220
@@ -43,6 +43,15 @@
 
   QPolygonF creationPolygon(CreationEvents events) const;
 
+  //FIXME make configurable...
+  QSizeF gridSpacing() const { return QSizeF(20,20); }
+
+  //FIXME make configurable...
+  bool snapToGrid() const { return _snapToGrid; }
+  void setSnapToGrid(bool snapToGrid) { _snapToGrid = snapToGrid; }
+
+  QPointF snapPoint(const QPointF &point);
+
 Q_SIGNALS:
   void mouseModeChanged(View::MouseMode oldMode);
   void creationPolygonChanged(View::CreationEvent event);
@@ -51,6 +60,7 @@
   bool eventFilter(QObject *obj, QEvent *event);
   void setVisible(bool visible);
   void resizeEvent(QResizeEvent *event);
+  void drawBackground(QPainter *painter, const QRectF &rect);
 
 private Q_SLOTS:
   void initializeSceneRect();
@@ -62,6 +72,7 @@
   QPolygonF _creationPolygonPress;
   QPolygonF _creationPolygonMove;
   QPolygonF _creationPolygonRelease;
+  bool _snapToGrid;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #706219:706220
@@ -753,6 +753,9 @@
              parentView()->mouseMode() == View::Rotate) {
     _originalTransform = transform();
   } else if (oldMode == View::Move) {
+
+    setPos(parentView()->snapPoint(pos()));
+
     new MoveCommand(this, _originalPosition, pos());
   } else if (oldMode == View::Resize) {
     new ResizeCommand(this, _originalTransform, transform());


More information about the Kst mailing list