tools/drawMapTest/drawmapscene.cpp
author unC0Rr
Wed, 01 Dec 2010 20:45:05 +0300
changeset 4439 27a896207aae
parent 4434 34c305fbc63c
child 4442 f8424e1bc936
permissions -rw-r--r--
Some more improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     1
#include <QDebug>
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     2
#include <QGraphicsSceneMouseEvent>
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
     3
#include <QGraphicsPathItem>
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
     4
#include <QtEndian>
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     5
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     6
#include "drawmapscene.h"
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     7
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
     8
template <class T> T sqr(const T & x)
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
     9
{
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    10
    return x*x;
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    11
}
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    12
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    13
DrawMapScene::DrawMapScene(QObject *parent) :
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    14
    QGraphicsScene(parent),
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    15
    m_pen(Qt::yellow),
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    16
    m_brush(Qt::yellow)
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    17
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    18
    setSceneRect(0, 0, 4096, 2048);
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    19
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    20
    QLinearGradient gradient(0, 0, 0, 2048);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    21
    gradient.setColorAt(0, QColor(60, 60, 155));
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    22
    gradient.setColorAt(1, QColor(155, 155, 60));
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    23
    setBackgroundBrush(QBrush(gradient));
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    24
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    25
    m_pen.setWidth(67);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    26
    m_pen.setJoinStyle(Qt::RoundJoin);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    27
    m_pen.setCapStyle(Qt::RoundCap);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    28
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    29
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    30
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    31
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    32
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    33
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    34
    qDebug() << "move" << mouseEvent->scenePos();
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    35
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    36
    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    37
    {
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    38
        QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    39
        path.lineTo(mouseEvent->scenePos());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    40
        paths.first().append(mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    41
        m_currPath->setPath(path);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    42
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    43
        emit pathChanged();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    44
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    45
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    46
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    47
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    48
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    49
    qDebug() << "press" << mouseEvent->scenePos();
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    50
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    51
    m_currPath = addPath(QPainterPath(), m_pen);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    52
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    53
    QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    54
    QPointF p = mouseEvent->scenePos();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    55
    p += QPointF(0.01, 0.01);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    56
    path.moveTo(p);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    57
    path.lineTo(mouseEvent->scenePos());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    58
    paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    59
    m_currPath->setPath(path);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    60
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    61
    emit pathChanged();
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    62
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    63
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    64
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    65
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    66
    qDebug() << "release" << mouseEvent->scenePos();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    67
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    68
    simplifyLast();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    69
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    70
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    71
}
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    72
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    73
void DrawMapScene::undo()
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    74
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    75
    if(items().size())
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    76
    {
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    77
        removeItem(items().first());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    78
        paths.removeFirst();
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    79
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    80
        emit pathChanged();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    81
    }
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    82
}
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    83
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    84
QByteArray DrawMapScene::encode()
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    85
{
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    86
    QByteArray b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    87
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    88
    foreach(QList<QPoint> points, paths)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    89
    {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    90
        int cnt = 0;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    91
        foreach(QPoint point, points)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    92
        {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    93
            qint16 px = qToBigEndian((qint16)point.x());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    94
            qint16 py = qToBigEndian((qint16)point.y());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    95
            quint8 flags = 2;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    96
            if(cnt) flags |= 0x80;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    97
            b.append((const char *)&flags, 1);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    98
            b.append((const char *)&px, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    99
            b.append((const char *)&py, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   100
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   101
            ++cnt;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   102
        }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   103
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   104
    }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   105
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   106
    return b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   107
}
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   108
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   109
void DrawMapScene::simplifyLast()
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   110
{
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   111
    QList<QPoint> points = paths[0];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   112
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   113
    QPoint prevPoint = points.first();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   114
    int i = 1;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   115
    while(i < points.size())
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   116
    {
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   117
        if(sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   118
            points.removeAt(i);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   119
        else
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   120
        {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   121
            prevPoint = points[i];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   122
            ++i;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   123
        }
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   124
    }
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   125
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   126
    paths[0] = points;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   127
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   128
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   129
    // redraw path
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   130
    {
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   131
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   132
        QPainterPath path;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   133
        QPointF p = paths[0][0] + QPointF(0.01, 0.01);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   134
        path.moveTo(p);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   135
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   136
        foreach(QPoint p, paths[0])
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   137
            path.lineTo(p);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   138
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   139
        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   140
        pathItem->setPath(path);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   141
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   142
        ++i;
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   143
    }
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   144
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   145
    emit pathChanged();
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   146
}