tools/drawMapTest/drawmapscene.cpp
author unc0rr
Sun, 28 Nov 2010 19:03:28 +0300
changeset 4427 c5193713055f
parent 4426 969e411c72aa
child 4434 34c305fbc63c
permissions -rw-r--r--
Basic encoding of drawn map
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
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     8
DrawMapScene::DrawMapScene(QObject *parent) :
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
     9
    QGraphicsScene(parent),
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    10
    m_pen(Qt::yellow),
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    11
    m_brush(Qt::yellow)
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    12
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    13
    setSceneRect(0, 0, 4096, 2048);
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    14
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    15
    QLinearGradient gradient(0, 0, 0, 2048);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    16
    gradient.setColorAt(0, QColor(60, 60, 155));
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    17
    gradient.setColorAt(1, QColor(155, 155, 60));
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    18
    setBackgroundBrush(QBrush(gradient));
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    19
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    20
    m_pen.setWidth(67);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    21
    m_pen.setJoinStyle(Qt::RoundJoin);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    22
    m_pen.setCapStyle(Qt::RoundCap);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    23
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    24
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    25
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    26
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    27
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    28
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    29
    qDebug() << "move" << mouseEvent->scenePos();
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    30
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    31
    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    32
    {
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    33
        QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    34
        path.lineTo(mouseEvent->scenePos());
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    35
        paths.last().append(mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    36
        m_currPath->setPath(path);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    37
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    38
        emit pathChanged();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    39
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    40
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    41
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    42
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    43
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    44
    qDebug() << "press" << mouseEvent->scenePos();
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    45
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    46
    m_currPath = addPath(QPainterPath(), m_pen);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    47
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    48
    QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    49
    QPointF p = mouseEvent->scenePos();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    50
    p += QPointF(0.01, 0.01);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    51
    path.moveTo(p);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    52
    path.lineTo(mouseEvent->scenePos());
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    53
    paths.append(QList<QPoint>() << mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    54
    m_currPath->setPath(path);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    55
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    56
    emit pathChanged();
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    57
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    58
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    59
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    60
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    61
    qDebug() << "release" << mouseEvent->scenePos();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    62
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    63
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    64
}
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    65
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    66
void DrawMapScene::undo()
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    67
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    68
    if(items().size())
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    69
    {
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    70
        removeItem(items().first());
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    71
        paths.removeLast();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    72
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    73
        emit pathChanged();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    74
    }
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    75
}
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    76
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    77
QByteArray DrawMapScene::encode()
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    78
{
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    79
    QByteArray b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    80
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    81
    foreach(QList<QPoint> points, paths)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    82
    {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    83
        int cnt = 0;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    84
        foreach(QPoint point, points)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    85
        {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    86
            qint16 px = qToBigEndian((qint16)point.x());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    87
            qint16 py = qToBigEndian((qint16)point.y());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    88
            quint8 flags = 2;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    89
            if(cnt) flags |= 0x80;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    90
            b.append((const char *)&flags, 1);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    91
            b.append((const char *)&px, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    92
            b.append((const char *)&py, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    93
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    94
            ++cnt;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    95
        }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    96
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    97
    }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    98
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    99
    return b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   100
}