tools/drawMapTest/drawmapscene.cpp
changeset 4439 27a896207aae
parent 4434 34c305fbc63c
child 4442 f8424e1bc936
equal deleted inserted replaced
4438:39ab70b37e01 4439:27a896207aae
    35 
    35 
    36     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    36     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    37     {
    37     {
    38         QPainterPath path = m_currPath->path();
    38         QPainterPath path = m_currPath->path();
    39         path.lineTo(mouseEvent->scenePos());
    39         path.lineTo(mouseEvent->scenePos());
    40         paths.last().append(mouseEvent->scenePos().toPoint());
    40         paths.first().append(mouseEvent->scenePos().toPoint());
    41         m_currPath->setPath(path);
    41         m_currPath->setPath(path);
    42 
    42 
    43         emit pathChanged();
    43         emit pathChanged();
    44     }
    44     }
    45 }
    45 }
    53     QPainterPath path = m_currPath->path();
    53     QPainterPath path = m_currPath->path();
    54     QPointF p = mouseEvent->scenePos();
    54     QPointF p = mouseEvent->scenePos();
    55     p += QPointF(0.01, 0.01);
    55     p += QPointF(0.01, 0.01);
    56     path.moveTo(p);
    56     path.moveTo(p);
    57     path.lineTo(mouseEvent->scenePos());
    57     path.lineTo(mouseEvent->scenePos());
    58     paths.append(QList<QPoint>() << mouseEvent->scenePos().toPoint());
    58     paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
    59     m_currPath->setPath(path);
    59     m_currPath->setPath(path);
    60 
    60 
    61     emit pathChanged();
    61     emit pathChanged();
    62 }
    62 }
    63 
    63 
    64 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
    64 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
    65 {
    65 {
    66     qDebug() << "release" << mouseEvent->scenePos();
    66     qDebug() << "release" << mouseEvent->scenePos();
    67 
    67 
       
    68     simplifyLast();
       
    69 
    68     m_currPath = 0;
    70     m_currPath = 0;
    69 }
    71 }
    70 
    72 
    71 void DrawMapScene::undo()
    73 void DrawMapScene::undo()
    72 {
    74 {
    73     if(items().size())
    75     if(items().size())
    74     {
    76     {
    75         removeItem(items().first());
    77         removeItem(items().first());
    76         paths.removeLast();
    78         paths.removeFirst();
    77 
    79 
    78         emit pathChanged();
    80         emit pathChanged();
    79     }
    81     }
    80 }
    82 }
    81 
    83 
   102     }
   104     }
   103 
   105 
   104     return b;
   106     return b;
   105 }
   107 }
   106 
   108 
   107 void DrawMapScene::simplify()
   109 void DrawMapScene::simplifyLast()
   108 {
   110 {
   109     for(int pit = 0; pit < paths.size(); ++pit)
   111     QList<QPoint> points = paths[0];
       
   112 
       
   113     QPoint prevPoint = points.first();
       
   114     int i = 1;
       
   115     while(i < points.size())
   110     {
   116     {
   111         QList<QPoint> points = paths[pit];
   117         if(sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
       
   118             points.removeAt(i);
       
   119         else
       
   120         {
       
   121             prevPoint = points[i];
       
   122             ++i;
       
   123         }
       
   124     }
   112 
   125 
   113         QPoint prevPoint = points.first();
   126     paths[0] = points;
   114         int i = 1;
       
   115         while(i < points.size())
       
   116         {
       
   117             if(sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
       
   118                 points.removeAt(i);
       
   119             else
       
   120                 ++i;
       
   121         }
       
   122 
   127 
   123         paths[pit] = points;
   128 
       
   129     // redraw path
       
   130     {
       
   131 
       
   132         QPainterPath path;
       
   133         QPointF p = paths[0][0] + QPointF(0.01, 0.01);
       
   134         path.moveTo(p);
       
   135 
       
   136         foreach(QPoint p, paths[0])
       
   137             path.lineTo(p);
       
   138 
       
   139         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
       
   140         pathItem->setPath(path);
       
   141 
       
   142         ++i;
   124     }
   143     }
   125 
   144 
   126     emit pathChanged();
   145     emit pathChanged();
   127 }
   146 }