QTfrontend/drawmapscene.cpp
changeset 9551 61f160dfd0f1
parent 9483 14ad1ac00ac9
child 9553 f92d43816186
equal deleted inserted replaced
9549:ac5c1f691ce2 9551:61f160dfd0f1
    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 <math.h>
    23 
    24 
    24 #include "drawmapscene.h"
    25 #include "drawmapscene.h"
    25 
    26 
    26 template <class T> T sqr(const T & x)
    27 template <class T> T sqr(const T & x)
    27 {
    28 {
    42 
    43 
    43     m_eraser = QBrush(gradient);
    44     m_eraser = QBrush(gradient);
    44     setBackgroundBrush(m_eraser);
    45     setBackgroundBrush(m_eraser);
    45     m_isErasing = false;
    46     m_isErasing = false;
    46 
    47 
       
    48     m_pathType = Polyline;
       
    49 
    47     m_pen.setWidth(76);
    50     m_pen.setWidth(76);
    48     m_pen.setJoinStyle(Qt::RoundJoin);
    51     m_pen.setJoinStyle(Qt::RoundJoin);
    49     m_pen.setCapStyle(Qt::RoundCap);
    52     m_pen.setCapStyle(Qt::RoundCap);
    50     m_currPath = 0;
    53     m_currPath = 0;
    51 
    54 
    59 {
    62 {
    60     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    63     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    61     {
    64     {
    62         QPainterPath path = m_currPath->path();
    65         QPainterPath path = m_currPath->path();
    63 
    66 
    64         if(mouseEvent->modifiers() & Qt::ControlModifier)
    67         switch (m_pathType)
    65         {
    68         {
    66             int c = path.elementCount();
    69         case Polyline:
    67             QPointF pos = mouseEvent->scenePos();
    70             if(mouseEvent->modifiers() & Qt::ControlModifier)
    68             path.setElementPositionAt(c - 1, pos.x(), pos.y());
    71             {
    69 
    72                 int c = path.elementCount();
    70         }
    73                 QPointF pos = mouseEvent->scenePos();
    71         else
    74                 path.setElementPositionAt(c - 1, pos.x(), pos.y());
    72         {
    75 
    73             path.lineTo(mouseEvent->scenePos());
    76             }
    74             paths.first().points.append(mouseEvent->scenePos().toPoint());
    77             else
    75         }
    78             {
       
    79                 path.lineTo(mouseEvent->scenePos());
       
    80                 paths.first().points.append(mouseEvent->scenePos().toPoint());
       
    81             }
       
    82             break;
       
    83         case Rectangle: {
       
    84             path = QPainterPath();
       
    85             QPointF p1 = paths.first().initialPoint;
       
    86             QPointF p2 = mouseEvent->scenePos();
       
    87             path.moveTo(p1);
       
    88             path.lineTo(p1.x(), p2.y());
       
    89             path.lineTo(p2);
       
    90             path.lineTo(p2.x(), p1.y());
       
    91             path.lineTo(p1);
       
    92             break;
       
    93             }
       
    94         case Ellipse: {
       
    95             path = QPainterPath();
       
    96             QList<QPointF> points = makeEllipse(paths.first().initialPoint, mouseEvent->scenePos());
       
    97             path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points)));
       
    98             break;
       
    99         }
       
   100         }
       
   101 
    76         m_currPath->setPath(path);
   102         m_currPath->setPath(path);
    77 
   103 
    78         emit pathChanged();
   104         emit pathChanged();
    79     }
   105     }
    80 
   106 
    94     path.lineTo(mouseEvent->scenePos());
   120     path.lineTo(mouseEvent->scenePos());
    95 
   121 
    96     PathParams params;
   122     PathParams params;
    97     params.width = serializePenWidth(m_pen.width());
   123     params.width = serializePenWidth(m_pen.width());
    98     params.erasing = m_isErasing;
   124     params.erasing = m_isErasing;
    99     params.points = QList<QPoint>() << mouseEvent->scenePos().toPoint();
   125     params.initialPoint = mouseEvent->scenePos().toPoint();
       
   126     params.points = QList<QPoint>() << params.initialPoint;
   100     paths.prepend(params);
   127     paths.prepend(params);
   101     m_currPath->setPath(path);
   128     m_currPath->setPath(path);
   102 
   129 
   103     emit pathChanged();
   130     emit pathChanged();
   104 }
   131 }
   105 
   132 
   106 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
   133 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
   107 {
   134 {
   108     if (m_currPath)
   135     if (m_currPath)
   109     {
   136     {
   110         QPainterPath path = m_currPath->path();
   137         switch (m_pathType)
   111         path.lineTo(mouseEvent->scenePos());
   138         {
   112         paths.first().points.append(mouseEvent->scenePos().toPoint());
   139         case Polyline: {
   113         m_currPath->setPath(path);
   140             QPainterPath path = m_currPath->path();
       
   141             path.lineTo(mouseEvent->scenePos());
       
   142             paths.first().points.append(mouseEvent->scenePos().toPoint());
       
   143             m_currPath->setPath(path);
       
   144             break;
       
   145         }
       
   146         case Rectangle: {
       
   147             QPoint p1 = paths.first().initialPoint;
       
   148             QPoint p2 = mouseEvent->scenePos().toPoint();
       
   149             QList<QPoint> rpoints;
       
   150             rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1;
       
   151             paths.first().points = rpoints;
       
   152             break;
       
   153         }
       
   154         case Ellipse:
       
   155             QPoint p1 = paths.first().initialPoint;
       
   156             QPoint p2 = mouseEvent->scenePos().toPoint();
       
   157             QList<QPointF> points = makeEllipse(p1, p2);
       
   158             QList<QPoint> epoints;
       
   159             foreach(const QPointF & p, points)
       
   160                 epoints.append(p.toPoint());
       
   161             paths.first().points = epoints;
       
   162             break;
       
   163         }
   114 
   164 
   115         simplifyLast();
   165         simplifyLast();
   116 
   166 
   117         m_currPath = 0;
   167         m_currPath = 0;
   118     }
   168     }
   381 
   431 
   382 int DrawMapScene::deserializePenWidth(quint8 width)
   432 int DrawMapScene::deserializePenWidth(quint8 width)
   383 {
   433 {
   384     return width * 10 + 6;
   434     return width * 10 + 6;
   385 }
   435 }
       
   436 
       
   437 void DrawMapScene::setPathType(PathType pathType)
       
   438 {
       
   439     m_pathType = pathType;
       
   440 }
       
   441 
       
   442 QList<QPointF> DrawMapScene::makeEllipse(const QPointF &center, const QPointF &corner)
       
   443 {
       
   444     QList<QPointF> l;
       
   445     qreal r = (center - corner).manhattanLength();
       
   446     qreal rx = qAbs(center.x() - corner.x());
       
   447     qreal ry = qAbs(center.y() - corner.y());
       
   448 
       
   449     if(r < 4)
       
   450     {
       
   451         l.append(center);
       
   452     } else
       
   453     {
       
   454         qreal angleDelta = 12 / r;
       
   455         for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta)
       
   456             l.append(center + QPointF(rx * cos(angle), ry * sin(angle)));
       
   457         l.append(l.first());
       
   458     }
       
   459 
       
   460     return l;
       
   461 }