QTfrontend/drawmapscene.cpp
changeset 6781 23f627ba8ee9
parent 6700 e04da46ee43c
child 6784 2c02ccb8f4ec
equal deleted inserted replaced
6780:166ee5275915 6781:23f627ba8ee9
    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     setBackgroundBrush(QBrush(gradient));
    42 
    42 
    43     m_pen.setWidth(67);
    43     m_pen.setWidth(72);
    44     m_pen.setJoinStyle(Qt::RoundJoin);
    44     m_pen.setJoinStyle(Qt::RoundJoin);
    45     m_pen.setCapStyle(Qt::RoundCap);
    45     m_pen.setCapStyle(Qt::RoundCap);
    46     m_currPath = 0;
    46     m_currPath = 0;
    47 }
    47 }
    48 
    48 
    60 
    60 
    61         }
    61         }
    62         else
    62         else
    63         {
    63         {
    64             path.lineTo(mouseEvent->scenePos());
    64             path.lineTo(mouseEvent->scenePos());
    65             paths.first().append(mouseEvent->scenePos().toPoint());
    65             paths.first().second.append(mouseEvent->scenePos().toPoint());
    66         }
    66         }
    67         m_currPath->setPath(path);
    67         m_currPath->setPath(path);
    68 
    68 
    69         emit pathChanged();
    69         emit pathChanged();
    70     }
    70     }
    77     QPainterPath path = m_currPath->path();
    77     QPainterPath path = m_currPath->path();
    78     QPointF p = mouseEvent->scenePos();
    78     QPointF p = mouseEvent->scenePos();
    79     p += QPointF(0.01, 0.01);
    79     p += QPointF(0.01, 0.01);
    80     path.moveTo(p);
    80     path.moveTo(p);
    81     path.lineTo(mouseEvent->scenePos());
    81     path.lineTo(mouseEvent->scenePos());
    82     paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
    82     paths.prepend(qMakePair(serializePenWidth(m_pen.width()), QList<QPoint>() << mouseEvent->scenePos().toPoint()));
    83     m_currPath->setPath(path);
    83     m_currPath->setPath(path);
    84 
    84 
    85     emit pathChanged();
    85     emit pathChanged();
    86 }
    86 }
    87 
    87 
    89 {
    89 {
    90     if (m_currPath)
    90     if (m_currPath)
    91     {
    91     {
    92         QPainterPath path = m_currPath->path();
    92         QPainterPath path = m_currPath->path();
    93         path.lineTo(mouseEvent->scenePos());
    93         path.lineTo(mouseEvent->scenePos());
    94         paths.first().append(mouseEvent->scenePos().toPoint());
    94         paths.first().second.append(mouseEvent->scenePos().toPoint());
    95         m_currPath->setPath(path);
    95         m_currPath->setPath(path);
    96 
    96 
    97         simplifyLast();
    97         simplifyLast();
    98 
    98 
    99         m_currPath = 0;
    99         m_currPath = 0;
       
   100     }
       
   101 }
       
   102 
       
   103 void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
       
   104 {
       
   105     if(wheelEvent->delta() > 0 && m_pen.width() < 512)
       
   106         m_pen.setWidth(m_pen.width() + 10);
       
   107     else if(wheelEvent->delta() < 0 && m_pen.width() >= 12)
       
   108         m_pen.setWidth(m_pen.width() - 10);
       
   109 
       
   110     if(m_currPath)
       
   111     {
       
   112         m_currPath->setPen(m_pen);
       
   113         paths.first().first = serializePenWidth(m_pen.width());
   100     }
   114     }
   101 }
   115 }
   102 
   116 
   103 void DrawMapScene::undo()
   117 void DrawMapScene::undo()
   104 {
   118 {
   146     QByteArray b;
   160     QByteArray b;
   147 
   161 
   148     for(int i = paths.size() - 1; i >= 0; --i)
   162     for(int i = paths.size() - 1; i >= 0; --i)
   149     {
   163     {
   150         int cnt = 0;
   164         int cnt = 0;
   151         QList<QPoint> points = paths.at(i);
   165         QPair<quint8, QList<QPoint> > points = paths.at(i);
   152         foreach(QPoint point, points)
   166         foreach(QPoint point, points.second)
   153         {
   167         {
   154             qint16 px = qToBigEndian((qint16)point.x());
   168             qint16 px = qToBigEndian((qint16)point.x());
   155             qint16 py = qToBigEndian((qint16)point.y());
   169             qint16 py = qToBigEndian((qint16)point.y());
   156             quint8 flags = 2;
   170             quint8 flags = 0;
   157             if(!cnt) flags |= 0x80;
   171             if(!cnt) flags = 0x80 + points.first;
   158             b.append((const char *)&px, 2);
   172             b.append((const char *)&px, 2);
   159             b.append((const char *)&py, 2);
   173             b.append((const char *)&py, 2);
   160             b.append((const char *)&flags, 1);
   174             b.append((const char *)&flags, 1);
   161 
   175 
   162             ++cnt;
   176             ++cnt;
   172     oldItems.clear();
   186     oldItems.clear();
   173     oldPaths.clear();
   187     oldPaths.clear();
   174     clear();
   188     clear();
   175     paths.clear();
   189     paths.clear();
   176 
   190 
   177     QList<QPoint> points;
   191     QPair<quint8, QList<QPoint> > points;
   178 
   192 
   179     while(data.size() >= 5)
   193     while(data.size() >= 5)
   180     {
   194     {
   181         qint16 px = qFromBigEndian(*(qint16 *)data.data());
   195         qint16 px = qFromBigEndian(*(qint16 *)data.data());
   182         data.remove(0, 2);
   196         data.remove(0, 2);
   183         qint16 py = qFromBigEndian(*(qint16 *)data.data());
   197         qint16 py = qFromBigEndian(*(qint16 *)data.data());
   184         data.remove(0, 2);
   198         data.remove(0, 2);
   185         quint8 flags = *(quint8 *)data.data();
   199         quint8 flags = *(quint8 *)data.data();
   186         data.remove(0, 1);
   200         data.remove(0, 1);
   187 
   201 
   188         if((flags & 0x80) && points.size())
   202         if(flags & 0x80)
   189         {
   203         {
   190             addPath(pointsToPath(points), m_pen);
   204             if(points.second.size())
   191             paths.prepend(points);
   205             {
   192 
   206                 addPath(pointsToPath(points.second), m_pen);
   193             points.clear();
   207 
   194         }
   208                 paths.prepend(points);
   195 
   209 
   196         points.append(QPoint(px, py));
   210                 points.second.clear();
   197     }
   211             }
   198 
   212 
   199     if(points.size())
   213             quint8 penWidth = flags & 0x7f;
   200     {
   214             m_pen.setWidth(deserializePenWidth(penWidth));
   201         addPath(pointsToPath(points), m_pen);
   215             points.first = penWidth;
       
   216         }
       
   217 
       
   218         points.second.append(QPoint(px, py));
       
   219     }
       
   220 
       
   221     if(points.second.size())
       
   222     {
       
   223         addPath(pointsToPath(points.second), m_pen);
   202         paths.prepend(points);
   224         paths.prepend(points);
   203     }
   225     }
   204 
   226 
   205     emit pathChanged();
   227     emit pathChanged();
   206 }
   228 }
   207 
   229 
   208 void DrawMapScene::simplifyLast()
   230 void DrawMapScene::simplifyLast()
   209 {
   231 {
   210     if(!paths.size()) return;
   232     if(!paths.size()) return;
   211 
   233 
   212     QList<QPoint> points = paths.at(0);
   234     QList<QPoint> points = paths.at(0).second;
   213 
   235 
   214     QPoint prevPoint = points.first();
   236     QPoint prevPoint = points.first();
   215     int i = 1;
   237     int i = 1;
   216     while(i < points.size())
   238     while(i < points.size())
   217     {
   239     {
   224             prevPoint = points[i];
   246             prevPoint = points[i];
   225             ++i;
   247             ++i;
   226         }
   248         }
   227     }
   249     }
   228 
   250 
   229     paths[0] = points;
   251     paths[0].second = points;
   230 
   252 
   231 
   253 
   232     // redraw path
   254     // redraw path
   233     {
   255     {
   234         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
   256         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
   235         pathItem->setPath(pointsToPath(paths[0]));
   257         pathItem->setPath(pointsToPath(paths[0].second));
   236     }
   258     }
   237 
   259 
   238     emit pathChanged();
   260     emit pathChanged();
   239 }
   261 }
   240 
   262 
   251         path.lineTo(p);
   273         path.lineTo(p);
   252     }
   274     }
   253 
   275 
   254     return path;
   276     return path;
   255 }
   277 }
       
   278 
       
   279 quint8 DrawMapScene::serializePenWidth(int width)
       
   280 {
       
   281     return (width - 6) / 10;
       
   282 }
       
   283 
       
   284 int DrawMapScene::deserializePenWidth(quint8 width)
       
   285 {
       
   286     return width * 10 + 6;
       
   287 }