uhm... a feature
authorunc0rr
Sat, 19 Oct 2013 00:42:24 +0400
changeset 9555 485b424be769
parent 9553 f92d43816186
child 9557 a5005b7ca305
uhm... a feature
QTfrontend/drawmapscene.cpp
QTfrontend/drawmapscene.h
--- 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;
+}
--- a/QTfrontend/drawmapscene.h	Fri Oct 18 16:05:31 2013 +0400
+++ b/QTfrontend/drawmapscene.h	Sat Oct 19 00:42:24 2013 +0400
@@ -87,6 +87,7 @@
         quint8 serializePenWidth(int width);
         int deserializePenWidth(quint8 width);
         QList<QPointF> makeEllipse(const QPointF & center, const QPointF & corner);
+        QPointF putSomeConstraints(const QPointF & initialPoint, const QPointF & point);
 };
 
 #endif // DRAWMAPSCENE_H