QTfrontend/drawmapscene.cpp
changeset 9555 485b424be769
parent 9553 f92d43816186
child 9928 fe4b1ce9b6f8
child 9998 736015b847e3
equal deleted inserted replaced
9553:f92d43816186 9555:485b424be769
    18 
    18 
    19 #include <QGraphicsSceneMouseEvent>
    19 #include <QGraphicsSceneMouseEvent>
    20 #include <QGraphicsPathItem>
    20 #include <QGraphicsPathItem>
    21 #include <QtEndian>
    21 #include <QtEndian>
    22 #include <QDebug>
    22 #include <QDebug>
       
    23 #include <QTransform>
    23 #include <math.h>
    24 #include <math.h>
    24 
    25 
    25 #include "drawmapscene.h"
    26 #include "drawmapscene.h"
    26 
    27 
    27 template <class T> T sqr(const T & x)
    28 template <class T> T sqr(const T & x)
    61 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    62 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    62 {
    63 {
    63     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    64     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    64     {
    65     {
    65         QPainterPath path = m_currPath->path();
    66         QPainterPath path = m_currPath->path();
       
    67         QPointF currentPos = mouseEvent->scenePos();
       
    68 
       
    69         if(mouseEvent->modifiers() & Qt::ControlModifier)
       
    70             currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
    66 
    71 
    67         switch (m_pathType)
    72         switch (m_pathType)
    68         {
    73         {
    69         case Polyline:
    74         case Polyline:
    70             if(mouseEvent->modifiers() & Qt::ControlModifier)
    75             if(mouseEvent->modifiers() & Qt::ControlModifier)
    71             {
    76             {
    72                 int c = path.elementCount();
    77                 int c = path.elementCount();
    73                 QPointF pos = mouseEvent->scenePos();
    78                 path.setElementPositionAt(c - 1, currentPos.x(), currentPos.y());
    74                 path.setElementPositionAt(c - 1, pos.x(), pos.y());
       
    75 
    79 
    76             }
    80             }
    77             else
    81             else
    78             {
    82             {
    79                 path.lineTo(mouseEvent->scenePos());
    83                 path.lineTo(currentPos);
    80                 paths.first().points.append(mouseEvent->scenePos().toPoint());
    84                 paths.first().points.append(mouseEvent->scenePos().toPoint());
    81             }
    85             }
    82             break;
    86             break;
    83         case Rectangle: {
    87         case Rectangle: {
    84             path = QPainterPath();
    88             path = QPainterPath();
    85             QPointF p1 = paths.first().initialPoint;
    89             QPointF p1 = paths.first().initialPoint;
    86             QPointF p2 = mouseEvent->scenePos();
    90             QPointF p2 = currentPos;
    87             path.moveTo(p1);
    91             path.moveTo(p1);
    88             path.lineTo(p1.x(), p2.y());
    92             path.lineTo(p1.x(), p2.y());
    89             path.lineTo(p2);
    93             path.lineTo(p2);
    90             path.lineTo(p2.x(), p1.y());
    94             path.lineTo(p2.x(), p1.y());
    91             path.lineTo(p1);
    95             path.lineTo(p1);
    92             break;
    96             break;
    93             }
    97             }
    94         case Ellipse: {
    98         case Ellipse: {
    95             path = QPainterPath();
    99             path = QPainterPath();
    96             QList<QPointF> points = makeEllipse(paths.first().initialPoint, mouseEvent->scenePos());
   100             QList<QPointF> points = makeEllipse(paths.first().initialPoint, currentPos);
    97             path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points)));
   101             path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points)));
    98             break;
   102             break;
    99         }
   103         }
   100         }
   104         }
   101 
   105 
   132 
   136 
   133 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
   137 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
   134 {
   138 {
   135     if (m_currPath)
   139     if (m_currPath)
   136     {
   140     {
       
   141         QPointF currentPos = mouseEvent->scenePos();
       
   142 
       
   143         if(mouseEvent->modifiers() & Qt::ControlModifier)
       
   144             currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
       
   145 
   137         switch (m_pathType)
   146         switch (m_pathType)
   138         {
   147         {
   139         case Polyline: {
   148         case Polyline: {
   140             QPainterPath path = m_currPath->path();
   149             QPainterPath path = m_currPath->path();
   141             path.lineTo(mouseEvent->scenePos());
   150             path.lineTo(mouseEvent->scenePos());
   142             paths.first().points.append(mouseEvent->scenePos().toPoint());
   151             paths.first().points.append(currentPos.toPoint());
   143             m_currPath->setPath(path);
   152             m_currPath->setPath(path);
   144             simplifyLast();
   153             simplifyLast();
   145             break;
   154             break;
   146         }
   155         }
   147         case Rectangle: {
   156         case Rectangle: {
   148             QPoint p1 = paths.first().initialPoint;
   157             QPoint p1 = paths.first().initialPoint;
   149             QPoint p2 = mouseEvent->scenePos().toPoint();
   158             QPoint p2 = currentPos.toPoint();
   150             QList<QPoint> rpoints;
   159             QList<QPoint> rpoints;
   151             rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1;
   160             rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1;
   152             paths.first().points = rpoints;
   161             paths.first().points = rpoints;
   153             break;
   162             break;
   154         }
   163         }
   155         case Ellipse:
   164         case Ellipse:
   156             QPoint p1 = paths.first().initialPoint;
   165             QPoint p1 = paths.first().initialPoint;
   157             QPoint p2 = mouseEvent->scenePos().toPoint();
   166             QPoint p2 = currentPos.toPoint();
   158             QList<QPointF> points = makeEllipse(p1, p2);
   167             QList<QPointF> points = makeEllipse(p1, p2);
   159             QList<QPoint> epoints;
   168             QList<QPoint> epoints;
   160             foreach(const QPointF & p, points)
   169             foreach(const QPointF & p, points)
   161                 epoints.append(p.toPoint());
   170                 epoints.append(p.toPoint());
   162             paths.first().points = epoints;
   171             paths.first().points = epoints;
   456         l.append(l.first());
   465         l.append(l.first());
   457     }
   466     }
   458 
   467 
   459     return l;
   468     return l;
   460 }
   469 }
       
   470 
       
   471 QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point)
       
   472 {
       
   473     QPointF vector = point - initialPoint;
       
   474 
       
   475     for(int angle = 0; angle < 180; angle += 15)
       
   476     {
       
   477         QTransform transform;
       
   478         transform.rotate(angle);
       
   479 
       
   480         QPointF rotated = transform.map(vector);
       
   481 
       
   482         if(rotated.x() == 0) return point;
       
   483         if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0));
       
   484     }
       
   485 
       
   486     return point;
       
   487 }