QTfrontend/drawmapscene.cpp
changeset 6873 30840365af0a
parent 6784 2c02ccb8f4ec
child 6934 14a230552c2e
equal deleted inserted replaced
6872:0f6eef4a07c8 6873:30840365af0a
    36     setSceneRect(0, 0, 4096, 2048);
    36     setSceneRect(0, 0, 4096, 2048);
    37 
    37 
    38     QLinearGradient gradient(0, 0, 0, 2048);
    38     QLinearGradient gradient(0, 0, 0, 2048);
    39     gradient.setColorAt(0, QColor(60, 60, 155));
    39     gradient.setColorAt(0, QColor(60, 60, 155));
    40     gradient.setColorAt(1, QColor(155, 155, 60));
    40     gradient.setColorAt(1, QColor(155, 155, 60));
    41     setBackgroundBrush(QBrush(gradient));
    41 
       
    42     m_eraser = QBrush(gradient);
       
    43     setBackgroundBrush(m_eraser);
       
    44     m_isErasing = false;
    42 
    45 
    43     m_pen.setWidth(76);
    46     m_pen.setWidth(76);
    44     m_pen.setJoinStyle(Qt::RoundJoin);
    47     m_pen.setJoinStyle(Qt::RoundJoin);
    45     m_pen.setCapStyle(Qt::RoundCap);
    48     m_pen.setCapStyle(Qt::RoundCap);
    46     m_currPath = 0;
    49     m_currPath = 0;
    60 
    63 
    61         }
    64         }
    62         else
    65         else
    63         {
    66         {
    64             path.lineTo(mouseEvent->scenePos());
    67             path.lineTo(mouseEvent->scenePos());
    65             paths.first().second.append(mouseEvent->scenePos().toPoint());
    68             paths.first().points.append(mouseEvent->scenePos().toPoint());
    66         }
    69         }
    67         m_currPath->setPath(path);
    70         m_currPath->setPath(path);
    68 
    71 
    69         emit pathChanged();
    72         emit pathChanged();
    70     }
    73     }
    77     QPainterPath path = m_currPath->path();
    80     QPainterPath path = m_currPath->path();
    78     QPointF p = mouseEvent->scenePos();
    81     QPointF p = mouseEvent->scenePos();
    79     p += QPointF(0.01, 0.01);
    82     p += QPointF(0.01, 0.01);
    80     path.moveTo(p);
    83     path.moveTo(p);
    81     path.lineTo(mouseEvent->scenePos());
    84     path.lineTo(mouseEvent->scenePos());
    82     paths.prepend(qMakePair(serializePenWidth(m_pen.width()), QList<QPoint>() << mouseEvent->scenePos().toPoint()));
    85 
       
    86     PathParams params;
       
    87     params.width = serializePenWidth(m_pen.width());
       
    88     params.erasing = m_isErasing;
       
    89     params.points = QList<QPoint>() << mouseEvent->scenePos().toPoint();
       
    90     paths.prepend(params);
    83     m_currPath->setPath(path);
    91     m_currPath->setPath(path);
    84 
    92 
    85     emit pathChanged();
    93     emit pathChanged();
    86 }
    94 }
    87 
    95 
    89 {
    97 {
    90     if (m_currPath)
    98     if (m_currPath)
    91     {
    99     {
    92         QPainterPath path = m_currPath->path();
   100         QPainterPath path = m_currPath->path();
    93         path.lineTo(mouseEvent->scenePos());
   101         path.lineTo(mouseEvent->scenePos());
    94         paths.first().second.append(mouseEvent->scenePos().toPoint());
   102         paths.first().points.append(mouseEvent->scenePos().toPoint());
    95         m_currPath->setPath(path);
   103         m_currPath->setPath(path);
    96 
   104 
    97         simplifyLast();
   105         simplifyLast();
    98 
   106 
    99         m_currPath = 0;
   107         m_currPath = 0;
   108         m_pen.setWidth(m_pen.width() - 10);
   116         m_pen.setWidth(m_pen.width() - 10);
   109 
   117 
   110     if(m_currPath)
   118     if(m_currPath)
   111     {
   119     {
   112         m_currPath->setPen(m_pen);
   120         m_currPath->setPen(m_pen);
   113         paths.first().first = serializePenWidth(m_pen.width());
   121         paths.first().width = serializePenWidth(m_pen.width());
   114     }
   122     }
   115 }
   123 }
   116 
   124 
   117 void DrawMapScene::undo()
   125 void DrawMapScene::undo()
   118 {
   126 {
   153     paths.clear();
   161     paths.clear();
   154 
   162 
   155     emit pathChanged();
   163     emit pathChanged();
   156 }
   164 }
   157 
   165 
       
   166 
       
   167 void DrawMapScene::setErasing(bool erasing)
       
   168 {
       
   169     m_isErasing = erasing;
       
   170     if(erasing)
       
   171         m_pen.setBrush(m_eraser);
       
   172     else
       
   173         m_pen.setBrush(m_brush);
       
   174 }
       
   175 
   158 QByteArray DrawMapScene::encode()
   176 QByteArray DrawMapScene::encode()
   159 {
   177 {
   160     QByteArray b;
   178     QByteArray b;
   161 
   179 
   162     for(int i = paths.size() - 1; i >= 0; --i)
   180     for(int i = paths.size() - 1; i >= 0; --i)
   163     {
   181     {
   164         int cnt = 0;
   182         int cnt = 0;
   165         QPair<quint8, QList<QPoint> > points = paths.at(i);
   183         PathParams params = paths.at(i);
   166         foreach(QPoint point, points.second)
   184         foreach(QPoint point, params.points)
   167         {
   185         {
   168             qint16 px = qToBigEndian((qint16)point.x());
   186             qint16 px = qToBigEndian((qint16)point.x());
   169             qint16 py = qToBigEndian((qint16)point.y());
   187             qint16 py = qToBigEndian((qint16)point.y());
   170             quint8 flags = 0;
   188             quint8 flags = 0;
   171             if(!cnt) flags = 0x80 + points.first;
   189             if(!cnt) flags = 0x80 + params.width;
       
   190             if(params.erasing) flags |= 0x40;
   172             b.append((const char *)&px, 2);
   191             b.append((const char *)&px, 2);
   173             b.append((const char *)&py, 2);
   192             b.append((const char *)&py, 2);
   174             b.append((const char *)&flags, 1);
   193             b.append((const char *)&flags, 1);
   175 
   194 
   176             ++cnt;
   195             ++cnt;
   181     return b;
   200     return b;
   182 }
   201 }
   183 
   202 
   184 void DrawMapScene::decode(QByteArray data)
   203 void DrawMapScene::decode(QByteArray data)
   185 {
   204 {
       
   205     bool erasing = m_isErasing;
       
   206 
   186     oldItems.clear();
   207     oldItems.clear();
   187     oldPaths.clear();
   208     oldPaths.clear();
   188     clear();
   209     clear();
   189     paths.clear();
   210     paths.clear();
   190 
   211 
   191     QPair<quint8, QList<QPoint> > points;
   212     PathParams params;
   192 
   213 
   193     while(data.size() >= 5)
   214     while(data.size() >= 5)
   194     {
   215     {
   195         qint16 px = qFromBigEndian(*(qint16 *)data.data());
   216         qint16 px = qFromBigEndian(*(qint16 *)data.data());
   196         data.remove(0, 2);
   217         data.remove(0, 2);
   199         quint8 flags = *(quint8 *)data.data();
   220         quint8 flags = *(quint8 *)data.data();
   200         data.remove(0, 1);
   221         data.remove(0, 1);
   201 
   222 
   202         if(flags & 0x80)
   223         if(flags & 0x80)
   203         {
   224         {
   204             if(points.second.size())
   225             if(params.points.size())
   205             {
   226             {
   206                 addPath(pointsToPath(points.second), m_pen);
   227                 addPath(pointsToPath(params.points), m_pen);
   207 
   228 
   208                 paths.prepend(points);
   229                 paths.prepend(params);
   209 
   230 
   210                 points.second.clear();
   231                 params.points.clear();
   211             }
   232             }
   212 
   233 
   213             quint8 penWidth = flags & 0x7f;
   234             quint8 penWidth = flags & 0x3f;
   214             m_pen.setWidth(deserializePenWidth(penWidth));
   235             m_pen.setWidth(deserializePenWidth(penWidth));
   215             points.first = penWidth;
   236             if(flags & 0x40)
   216         }
   237                 m_pen.setBrush(m_eraser);
   217 
   238             else
   218         points.second.append(QPoint(px, py));
   239                 m_pen.setBrush(m_brush);
   219     }
   240             params.width = penWidth;
   220 
   241         }
   221     if(points.second.size())
   242 
   222     {
   243         params.points.append(QPoint(px, py));
   223         addPath(pointsToPath(points.second), m_pen);
   244     }
   224         paths.prepend(points);
   245 
       
   246     if(params.points.size())
       
   247     {
       
   248         addPath(pointsToPath(params.points), m_pen);
       
   249         paths.prepend(params);
   225     }
   250     }
   226 
   251 
   227     emit pathChanged();
   252     emit pathChanged();
       
   253 
       
   254     setErasing(erasing);
   228 }
   255 }
   229 
   256 
   230 void DrawMapScene::simplifyLast()
   257 void DrawMapScene::simplifyLast()
   231 {
   258 {
   232     if(!paths.size()) return;
   259     if(!paths.size()) return;
   233 
   260 
   234     QList<QPoint> points = paths.at(0).second;
   261     QList<QPoint> points = paths.at(0).points;
   235 
   262 
   236     QPoint prevPoint = points.first();
   263     QPoint prevPoint = points.first();
   237     int i = 1;
   264     int i = 1;
   238     while(i < points.size())
   265     while(i < points.size())
   239     {
   266     {
   246             prevPoint = points[i];
   273             prevPoint = points[i];
   247             ++i;
   274             ++i;
   248         }
   275         }
   249     }
   276     }
   250 
   277 
   251     paths[0].second = points;
   278     paths[0].points = points;
   252 
   279 
   253 
   280 
   254     // redraw path
   281     // redraw path
   255     {
   282     {
   256         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
   283         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
   257         pathItem->setPath(pointsToPath(paths[0].second));
   284         pathItem->setPath(pointsToPath(paths[0].points));
   258     }
   285     }
   259 
   286 
   260     emit pathChanged();
   287     emit pathChanged();
   261 }
   288 }
   262 
   289