QTfrontend/drawmapscene.cpp
branchexperimental3D
changeset 4812 f924be23ffb4
parent 4666 34551d8639cf
child 4937 55b9145fea94
equal deleted inserted replaced
4347:0ddb100fea61 4812:f924be23ffb4
       
     1 #include <QGraphicsSceneMouseEvent>
       
     2 #include <QGraphicsPathItem>
       
     3 #include <QtEndian>
       
     4 
       
     5 #include "drawmapscene.h"
       
     6 
       
     7 template <class T> T sqr(const T & x)
       
     8 {
       
     9     return x*x;
       
    10 }
       
    11 
       
    12 DrawMapScene::DrawMapScene(QObject *parent) :
       
    13     QGraphicsScene(parent),
       
    14     m_pen(Qt::yellow),
       
    15     m_brush(Qt::yellow)
       
    16 {
       
    17     setSceneRect(0, 0, 4096, 2048);
       
    18 
       
    19     QLinearGradient gradient(0, 0, 0, 2048);
       
    20     gradient.setColorAt(0, QColor(60, 60, 155));
       
    21     gradient.setColorAt(1, QColor(155, 155, 60));
       
    22     setBackgroundBrush(QBrush(gradient));
       
    23 
       
    24     m_pen.setWidth(67);
       
    25     m_pen.setJoinStyle(Qt::RoundJoin);
       
    26     m_pen.setCapStyle(Qt::RoundCap);
       
    27     m_currPath = 0;
       
    28 }
       
    29 
       
    30 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
       
    31 {
       
    32     if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
       
    33     {
       
    34         QPainterPath path = m_currPath->path();
       
    35         path.lineTo(mouseEvent->scenePos());
       
    36         paths.first().append(mouseEvent->scenePos().toPoint());
       
    37         m_currPath->setPath(path);
       
    38 
       
    39         emit pathChanged();
       
    40     }
       
    41 }
       
    42 
       
    43 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
       
    44 {
       
    45     m_currPath = addPath(QPainterPath(), m_pen);
       
    46 
       
    47     QPainterPath path = m_currPath->path();
       
    48     QPointF p = mouseEvent->scenePos();
       
    49     p += QPointF(0.01, 0.01);
       
    50     path.moveTo(p);
       
    51     path.lineTo(mouseEvent->scenePos());
       
    52     paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
       
    53     m_currPath->setPath(path);
       
    54 
       
    55     emit pathChanged();
       
    56 }
       
    57 
       
    58 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
       
    59 {
       
    60     Q_UNUSED(mouseEvent);
       
    61 
       
    62     simplifyLast();
       
    63 
       
    64     m_currPath = 0;
       
    65 }
       
    66 
       
    67 void DrawMapScene::undo()
       
    68 {
       
    69     if(items().size())
       
    70     {
       
    71         removeItem(items().first());
       
    72         paths.removeFirst();
       
    73 
       
    74         emit pathChanged();
       
    75     }
       
    76 }
       
    77 
       
    78 void DrawMapScene::clearMap()
       
    79 {
       
    80     clear();
       
    81     paths.clear();
       
    82 
       
    83     emit pathChanged();
       
    84 }
       
    85 
       
    86 QByteArray DrawMapScene::encode()
       
    87 {
       
    88     QByteArray b;
       
    89 
       
    90     for(int i = paths.size() - 1; i >= 0; --i)
       
    91     {
       
    92         int cnt = 0;
       
    93         QList<QPoint> points = paths.at(i);
       
    94         foreach(QPoint point, points)
       
    95         {
       
    96             qint16 px = qToBigEndian((qint16)point.x());
       
    97             qint16 py = qToBigEndian((qint16)point.y());
       
    98             quint8 flags = 2;
       
    99             if(!cnt) flags |= 0x80;
       
   100             b.append((const char *)&px, 2);
       
   101             b.append((const char *)&py, 2);
       
   102             b.append((const char *)&flags, 1);
       
   103 
       
   104             ++cnt;
       
   105         }
       
   106 
       
   107     }
       
   108 
       
   109     return b;
       
   110 }
       
   111 
       
   112 void DrawMapScene::decode(QByteArray data)
       
   113 {
       
   114     clear();
       
   115     paths.clear();
       
   116 
       
   117     QList<QPoint> points;
       
   118 
       
   119     while(data.size() >= 5)
       
   120     {
       
   121         qint16 px = qFromBigEndian(*(qint16 *)data.data());
       
   122         data.remove(0, 2);
       
   123         qint16 py = qFromBigEndian(*(qint16 *)data.data());
       
   124         data.remove(0, 2);
       
   125         quint8 flags = *(quint8 *)data.data();
       
   126         data.remove(0, 1);
       
   127 
       
   128         if((flags & 0x80) && points.size())
       
   129         {
       
   130             addPath(pointsToPath(points), m_pen);
       
   131             paths.prepend(points);
       
   132 
       
   133             points.clear();
       
   134         }
       
   135 
       
   136         points.append(QPoint(px, py));
       
   137     }
       
   138 
       
   139     if(points.size())
       
   140     {
       
   141         addPath(pointsToPath(points), m_pen);
       
   142         paths.prepend(points);
       
   143     }
       
   144 
       
   145     emit pathChanged();
       
   146 }
       
   147 
       
   148 void DrawMapScene::simplifyLast()
       
   149 {
       
   150     if(!paths.size()) return;
       
   151 
       
   152     QList<QPoint> points = paths.at(0);
       
   153 
       
   154     QPoint prevPoint = points.first();
       
   155     int i = 1;
       
   156     while(i < points.size())
       
   157     {
       
   158         if( (i != points.size() - 1)
       
   159             && (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
       
   160           )
       
   161             points.removeAt(i);
       
   162         else
       
   163         {
       
   164             prevPoint = points[i];
       
   165             ++i;
       
   166         }
       
   167     }
       
   168 
       
   169     paths[0] = points;
       
   170 
       
   171 
       
   172     // redraw path
       
   173     {
       
   174         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
       
   175         pathItem->setPath(pointsToPath(paths[0]));
       
   176     }
       
   177 
       
   178     emit pathChanged();
       
   179 }
       
   180 
       
   181 QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
       
   182 {
       
   183     QPainterPath path;
       
   184 
       
   185     if(points.size())
       
   186     {
       
   187         QPointF p = points[0] + QPointF(0.01, 0.01);
       
   188         path.moveTo(p);
       
   189 
       
   190         foreach(QPoint p, points)
       
   191             path.lineTo(p);
       
   192     }
       
   193 
       
   194     return path;
       
   195 }