QTfrontend/drawmapscene.cpp
branchios-develop
changeset 13418 ba39a1d396c0
parent 13241 ef1393c6bd12
child 13915 6c8d4e140f27
equal deleted inserted replaced
13416:6e8b807bda4b 13418:ba39a1d396c0
    23 #include <QTransform>
    23 #include <QTransform>
    24 #include <math.h>
    24 #include <math.h>
    25 
    25 
    26 #include "drawmapscene.h"
    26 #include "drawmapscene.h"
    27 
    27 
       
    28 #define DRAWN_MAP_COLOR_LAND (Qt::yellow)
       
    29 #define DRAWN_MAP_COLOR_CURSOR_PEN (Qt::green)
       
    30 #define DRAWN_MAP_COLOR_CURSOR_ERASER (Qt::red)
       
    31 
    28 template <class T> T sqr(const T & x)
    32 template <class T> T sqr(const T & x)
    29 {
    33 {
    30     return x*x;
    34     return x*x;
    31 }
    35 }
    32 
    36 
    33 DrawMapScene::DrawMapScene(QObject *parent) :
    37 DrawMapScene::DrawMapScene(QObject *parent) :
    34     QGraphicsScene(parent),
    38     QGraphicsScene(parent),
    35     m_pen(Qt::yellow),
    39     m_pen(DRAWN_MAP_COLOR_LAND),
    36     m_brush(Qt::yellow),
    40     m_brush(DRAWN_MAP_COLOR_LAND),
    37     m_cursor(new QGraphicsEllipseItem(-0.5, -0.5, 1, 1))
    41     m_cursor(new QGraphicsEllipseItem(-5, -5, 5, 5))
    38 {
    42 {
    39     setSceneRect(0, 0, 4096, 2048);
    43     setSceneRect(0, 0, 4096, 2048);
    40 
    44 
    41     QLinearGradient gradient(0, 0, 0, 2048);
    45     QLinearGradient gradient(0, 0, 0, 2048);
    42     gradient.setColorAt(0, QColor(60, 60, 155));
    46     gradient.setColorAt(0, QColor(60, 60, 155));
    46     setBackgroundBrush(m_eraser);
    50     setBackgroundBrush(m_eraser);
    47     m_isErasing = false;
    51     m_isErasing = false;
    48 
    52 
    49     m_pathType = Polyline;
    53     m_pathType = Polyline;
    50 
    54 
    51     m_pen.setWidth(76);
    55     m_pen.setWidth(DRAWN_MAP_BRUSH_SIZE_START);
    52     m_pen.setJoinStyle(Qt::RoundJoin);
    56     m_pen.setJoinStyle(Qt::RoundJoin);
    53     m_pen.setCapStyle(Qt::RoundCap);
    57     m_pen.setCapStyle(Qt::RoundCap);
    54     m_currPath = 0;
    58     m_currPath = 0;
    55 
    59 
    56     m_isCursorShown = false;
    60     m_isCursorShown = false;
    57     m_cursor->setPen(QPen(Qt::green));
    61     QPen cursorPen = QPen(DRAWN_MAP_COLOR_CURSOR_PEN);
       
    62     cursorPen.setJoinStyle(Qt::RoundJoin);
       
    63     cursorPen.setCapStyle(Qt::RoundCap);
       
    64     cursorPen.setWidth(brushSize());
       
    65     m_cursor->setPen(cursorPen);
    58     m_cursor->setZValue(1);
    66     m_cursor->setZValue(1);
    59     m_cursor->setScale(m_pen.width());
       
    60 }
    67 }
    61 
    68 
    62 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    69 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
    63 {
    70 {
    64     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
    71     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
   122     p += QPointF(0.01, 0.01);
   129     p += QPointF(0.01, 0.01);
   123     path.moveTo(p);
   130     path.moveTo(p);
   124     path.lineTo(mouseEvent->scenePos());
   131     path.lineTo(mouseEvent->scenePos());
   125 
   132 
   126     PathParams params;
   133     PathParams params;
   127     params.width = serializePenWidth(m_pen.width());
   134     params.width = serializePenWidth(brushSize());
   128     params.erasing = m_isErasing;
   135     params.erasing = m_isErasing;
   129     params.initialPoint = mouseEvent->scenePos().toPoint();
   136     params.initialPoint = mouseEvent->scenePos().toPoint();
   130     params.points = QList<QPoint>() << params.initialPoint;
   137     params.points = QList<QPoint>() << params.initialPoint;
   131     paths.prepend(params);
   138     paths.prepend(params);
   132     m_currPath->setPath(path);
   139     m_currPath->setPath(path);
   176 
   183 
   177         emit pathChanged();
   184         emit pathChanged();
   178     }
   185     }
   179 }
   186 }
   180 
   187 
   181 void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
   188 void DrawMapScene::setBrushSize(int newBrushSize)
   182 {
   189 {
   183     if(wheelEvent->delta() > 0 && m_pen.width() < 516)
   190     if(newBrushSize > DRAWN_MAP_BRUSH_SIZE_MAX)
   184         m_pen.setWidth(m_pen.width() + 10);
   191         newBrushSize = DRAWN_MAP_BRUSH_SIZE_MAX;
   185     else if(wheelEvent->delta() < 0 && m_pen.width() >= 16)
   192     if(newBrushSize < DRAWN_MAP_BRUSH_SIZE_MIN)
   186         m_pen.setWidth(m_pen.width() - 10);
   193         newBrushSize = DRAWN_MAP_BRUSH_SIZE_MIN;
   187 
   194 
   188     m_cursor->setScale(m_pen.width());
   195     m_pen.setWidth(newBrushSize);
   189 
   196     QPen cursorPen = m_cursor->pen();
       
   197     cursorPen.setWidth(m_pen.width());
       
   198     m_cursor->setPen(cursorPen);
   190     if(m_currPath)
   199     if(m_currPath)
   191     {
   200     {
   192         m_currPath->setPen(m_pen);
   201         m_currPath->setPen(m_pen);
   193         paths.first().width = serializePenWidth(m_pen.width());
   202         paths.first().width = serializePenWidth(m_pen.width());
   194     }
   203     }
       
   204 
       
   205     emit brushSizeChanged(newBrushSize);
       
   206 }
       
   207 
       
   208 int DrawMapScene::brushSize()
       
   209 {
       
   210     return m_pen.width();
       
   211 }
       
   212 
       
   213 void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
       
   214 {
       
   215     int b = brushSize();
       
   216     if(wheelEvent->delta() > 0)
       
   217         setBrushSize(b + DRAWN_MAP_BRUSH_SIZE_STEP);
       
   218     else if(wheelEvent->delta() < 0 && b >= DRAWN_MAP_BRUSH_SIZE_MIN)
       
   219         setBrushSize(b - DRAWN_MAP_BRUSH_SIZE_STEP);
   195 }
   220 }
   196 
   221 
   197 void DrawMapScene::showCursor()
   222 void DrawMapScene::showCursor()
   198 {
   223 {
   199     if(!m_isCursorShown)
   224     if(!m_isCursorShown)
   264 
   289 
   265 
   290 
   266 void DrawMapScene::setErasing(bool erasing)
   291 void DrawMapScene::setErasing(bool erasing)
   267 {
   292 {
   268     m_isErasing = erasing;
   293     m_isErasing = erasing;
   269     if(erasing)
   294     QPen cursorPen = m_cursor->pen();
       
   295     if(erasing) {
   270         m_pen.setBrush(m_eraser);
   296         m_pen.setBrush(m_eraser);
   271     else
   297         cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_ERASER);
       
   298     } else {
   272         m_pen.setBrush(m_brush);
   299         m_pen.setBrush(m_brush);
       
   300         cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_PEN);
       
   301     }
       
   302     m_cursor->setPen(cursorPen);
   273 }
   303 }
   274 
   304 
   275 QByteArray DrawMapScene::encode()
   305 QByteArray DrawMapScene::encode()
   276 {
   306 {
   277     QByteArray b(m_specialPoints);
   307     QByteArray b(m_specialPoints);
   304 
   334 
   305 void DrawMapScene::decode(QByteArray data)
   335 void DrawMapScene::decode(QByteArray data)
   306 {
   336 {
   307     hideCursor();
   337     hideCursor();
   308 
   338 
       
   339     // Remember erasing mode
   309     bool erasing = m_isErasing;
   340     bool erasing = m_isErasing;
       
   341 
       
   342     // Use seperate for decoding the map, don't mess with the user pen
       
   343     QPen load_pen = QPen(m_pen);
   310 
   344 
   311     oldItems.clear();
   345     oldItems.clear();
   312     oldPaths.clear();
   346     oldPaths.clear();
   313     clear();
   347     clear();
   314     paths.clear();
   348     paths.clear();
   331         {
   365         {
   332             isSpecial = false;
   366             isSpecial = false;
   333 
   367 
   334             if(params.points.size())
   368             if(params.points.size())
   335             {
   369             {
   336                 addPath(pointsToPath(params.points), m_pen);
   370                 addPath(pointsToPath(params.points), load_pen);
   337 
   371 
   338                 paths.prepend(params);
   372                 paths.prepend(params);
   339 
   373 
   340                 params.points.clear();
   374                 params.points.clear();
   341             }
   375             }
   342 
   376 
   343             quint8 penWidth = flags & 0x3f;
   377             quint8 penWidth = flags & 0x3f;
   344             m_pen.setWidth(deserializePenWidth(penWidth));
   378             load_pen.setWidth(deserializePenWidth(penWidth));
   345             params.erasing = flags & 0x40;
   379             params.erasing = flags & 0x40;
   346             if(params.erasing)
   380             if(params.erasing)
   347                 m_pen.setBrush(m_eraser);
   381                 load_pen.setBrush(m_eraser);
   348             else
   382             else
   349                 m_pen.setBrush(m_brush);
   383                 load_pen.setBrush(m_brush);
   350             params.width = penWidth;
   384             params.width = penWidth;
   351         } else
   385         } else
   352             if(isSpecial)
   386             if(isSpecial)
   353             {
   387             {
   354                 QPainterPath path;
   388                 QPainterPath path;
   367             params.points.append(QPoint(px, py));
   401             params.points.append(QPoint(px, py));
   368     }
   402     }
   369 
   403 
   370     if(params.points.size())
   404     if(params.points.size())
   371     {
   405     {
   372         addPath(pointsToPath(params.points), m_pen);
   406         addPath(pointsToPath(params.points), load_pen);
   373         paths.prepend(params);
   407         paths.prepend(params);
   374     }
   408     }
   375 
   409 
   376     emit pathChanged();
   410     emit pathChanged();
   377 
   411 
       
   412     // Restore erasing mode
   378     setErasing(erasing);
   413     setErasing(erasing);
   379 }
   414 }
   380 
   415 
   381 void DrawMapScene::simplifyLast()
   416 void DrawMapScene::simplifyLast()
   382 {
   417 {