QTfrontend/drawmapscene.cpp
changeset 6934 14a230552c2e
parent 6873 30840365af0a
child 6935 472ed92c4173
equal deleted inserted replaced
6933:78d194a30520 6934:14a230552c2e
    29 }
    29 }
    30 
    30 
    31 DrawMapScene::DrawMapScene(QObject *parent) :
    31 DrawMapScene::DrawMapScene(QObject *parent) :
    32     QGraphicsScene(parent),
    32     QGraphicsScene(parent),
    33     m_pen(Qt::yellow),
    33     m_pen(Qt::yellow),
    34     m_brush(Qt::yellow)
    34     m_brush(Qt::yellow),
       
    35     m_cursor(new QGraphicsEllipseItem(-0.5, -0.5, 1, 1))
    35 {
    36 {
    36     setSceneRect(0, 0, 4096, 2048);
    37     setSceneRect(0, 0, 4096, 2048);
    37 
    38 
    38     QLinearGradient gradient(0, 0, 0, 2048);
    39     QLinearGradient gradient(0, 0, 0, 2048);
    39     gradient.setColorAt(0, QColor(60, 60, 155));
    40     gradient.setColorAt(0, QColor(60, 60, 155));
    45 
    46 
    46     m_pen.setWidth(76);
    47     m_pen.setWidth(76);
    47     m_pen.setJoinStyle(Qt::RoundJoin);
    48     m_pen.setJoinStyle(Qt::RoundJoin);
    48     m_pen.setCapStyle(Qt::RoundCap);
    49     m_pen.setCapStyle(Qt::RoundCap);
    49     m_currPath = 0;
    50     m_currPath = 0;
       
    51 
       
    52     m_isCursorShown = false;
       
    53     m_cursor->setPen(QPen(Qt::green));
       
    54     m_cursor->setZValue(1);
       
    55     m_cursor->setScale(m_pen.width());
    50 }
    56 }
    51 
    57 
    52 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    58 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    53 {
    59 {
    54     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    60     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    69         }
    75         }
    70         m_currPath->setPath(path);
    76         m_currPath->setPath(path);
    71 
    77 
    72         emit pathChanged();
    78         emit pathChanged();
    73     }
    79     }
       
    80 
       
    81     if(!m_isCursorShown)
       
    82         showCursor();
       
    83     m_cursor->setPos(mouseEvent->scenePos());
    74 }
    84 }
    75 
    85 
    76 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
    86 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
    77 {
    87 {
    78     m_currPath = addPath(QPainterPath(), m_pen);
    88     m_currPath = addPath(QPainterPath(), m_pen);
   113     if(wheelEvent->delta() > 0 && m_pen.width() < 516)
   123     if(wheelEvent->delta() > 0 && m_pen.width() < 516)
   114         m_pen.setWidth(m_pen.width() + 10);
   124         m_pen.setWidth(m_pen.width() + 10);
   115     else if(wheelEvent->delta() < 0 && m_pen.width() >= 16)
   125     else if(wheelEvent->delta() < 0 && m_pen.width() >= 16)
   116         m_pen.setWidth(m_pen.width() - 10);
   126         m_pen.setWidth(m_pen.width() - 10);
   117 
   127 
       
   128     m_cursor->setScale(m_pen.width());
       
   129 
   118     if(m_currPath)
   130     if(m_currPath)
   119     {
   131     {
   120         m_currPath->setPen(m_pen);
   132         m_currPath->setPen(m_pen);
   121         paths.first().width = serializePenWidth(m_pen.width());
   133         paths.first().width = serializePenWidth(m_pen.width());
   122     }
   134     }
   123 }
   135 }
   124 
   136 
       
   137 void DrawMapScene::showCursor()
       
   138 {
       
   139     qDebug() << "show cursor";
       
   140     if(!m_isCursorShown)
       
   141         addItem(m_cursor);
       
   142 
       
   143     m_isCursorShown = true;
       
   144 }
       
   145 
       
   146 void DrawMapScene::hideCursor()
       
   147 {
       
   148     qDebug() << "hide cursor";
       
   149     if(m_isCursorShown)
       
   150         removeItem(m_cursor);
       
   151 
       
   152     m_isCursorShown = false;
       
   153 }
       
   154 
   125 void DrawMapScene::undo()
   155 void DrawMapScene::undo()
   126 {
   156 {
       
   157     // cursor is a part of items()
       
   158     if(m_isCursorShown)
       
   159         return;
       
   160 
   127     if(items().size())
   161     if(items().size())
   128     {
   162     {
   129         removeItem(items().first());
   163         removeItem(items().first());
   130         paths.removeFirst();
   164         paths.removeFirst();
   131 
   165 
   141     }
   175     }
   142 }
   176 }
   143 
   177 
   144 void DrawMapScene::clearMap()
   178 void DrawMapScene::clearMap()
   145 {
   179 {
       
   180     // cursor is a part of items()
       
   181     if(m_isCursorShown)
       
   182         return;
       
   183 
   146     // don't clear if already cleared
   184     // don't clear if already cleared
   147     if(!items().size())
   185     if(!items().size())
   148         return;
   186         return;
   149 
   187 
   150     oldItems.clear();
   188     oldItems.clear();
   278     paths[0].points = points;
   316     paths[0].points = points;
   279 
   317 
   280 
   318 
   281     // redraw path
   319     // redraw path
   282     {
   320     {
   283         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
   321         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]);
   284         pathItem->setPath(pointsToPath(paths[0].points));
   322         pathItem->setPath(pointsToPath(paths[0].points));
   285     }
   323     }
   286 
   324 
   287     emit pathChanged();
   325     emit pathChanged();
   288 }
   326 }