QTfrontend/drawmapscene.cpp
changeset 9555 485b424be769
parent 9553 f92d43816186
child 9928 fe4b1ce9b6f8
child 9998 736015b847e3
--- a/QTfrontend/drawmapscene.cpp	Fri Oct 18 16:05:31 2013 +0400
+++ b/QTfrontend/drawmapscene.cpp	Sat Oct 19 00:42:24 2013 +0400
@@ -20,6 +20,7 @@
 #include <QGraphicsPathItem>
 #include <QtEndian>
 #include <QDebug>
+#include <QTransform>
 #include <math.h>
 
 #include "drawmapscene.h"
@@ -63,6 +64,10 @@
     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
     {
         QPainterPath path = m_currPath->path();
+        QPointF currentPos = mouseEvent->scenePos();
+
+        if(mouseEvent->modifiers() & Qt::ControlModifier)
+            currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
 
         switch (m_pathType)
         {
@@ -70,20 +75,19 @@
             if(mouseEvent->modifiers() & Qt::ControlModifier)
             {
                 int c = path.elementCount();
-                QPointF pos = mouseEvent->scenePos();
-                path.setElementPositionAt(c - 1, pos.x(), pos.y());
+                path.setElementPositionAt(c - 1, currentPos.x(), currentPos.y());
 
             }
             else
             {
-                path.lineTo(mouseEvent->scenePos());
+                path.lineTo(currentPos);
                 paths.first().points.append(mouseEvent->scenePos().toPoint());
             }
             break;
         case Rectangle: {
             path = QPainterPath();
             QPointF p1 = paths.first().initialPoint;
-            QPointF p2 = mouseEvent->scenePos();
+            QPointF p2 = currentPos;
             path.moveTo(p1);
             path.lineTo(p1.x(), p2.y());
             path.lineTo(p2);
@@ -93,7 +97,7 @@
             }
         case Ellipse: {
             path = QPainterPath();
-            QList<QPointF> points = makeEllipse(paths.first().initialPoint, mouseEvent->scenePos());
+            QList<QPointF> points = makeEllipse(paths.first().initialPoint, currentPos);
             path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points)));
             break;
         }
@@ -134,19 +138,24 @@
 {
     if (m_currPath)
     {
+        QPointF currentPos = mouseEvent->scenePos();
+
+        if(mouseEvent->modifiers() & Qt::ControlModifier)
+            currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
+
         switch (m_pathType)
         {
         case Polyline: {
             QPainterPath path = m_currPath->path();
             path.lineTo(mouseEvent->scenePos());
-            paths.first().points.append(mouseEvent->scenePos().toPoint());
+            paths.first().points.append(currentPos.toPoint());
             m_currPath->setPath(path);
             simplifyLast();
             break;
         }
         case Rectangle: {
             QPoint p1 = paths.first().initialPoint;
-            QPoint p2 = mouseEvent->scenePos().toPoint();
+            QPoint p2 = currentPos.toPoint();
             QList<QPoint> rpoints;
             rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1;
             paths.first().points = rpoints;
@@ -154,7 +163,7 @@
         }
         case Ellipse:
             QPoint p1 = paths.first().initialPoint;
-            QPoint p2 = mouseEvent->scenePos().toPoint();
+            QPoint p2 = currentPos.toPoint();
             QList<QPointF> points = makeEllipse(p1, p2);
             QList<QPoint> epoints;
             foreach(const QPointF & p, points)
@@ -458,3 +467,21 @@
 
     return l;
 }
+
+QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point)
+{
+    QPointF vector = point - initialPoint;
+
+    for(int angle = 0; angle < 180; angle += 15)
+    {
+        QTransform transform;
+        transform.rotate(angle);
+
+        QPointF rotated = transform.map(vector);
+
+        if(rotated.x() == 0) return point;
+        if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0));
+    }
+
+    return point;
+}