QTfrontend/drawmapscene.cpp
author sheepluva
Wed, 19 Oct 2011 21:55:42 +0200
changeset 6153 3881126e06e8
parent 5858 1f4a8cf9efcb
child 6616 f77bb02b669f
permissions -rw-r--r--
allow changing number of hogs or weapons with mousewheel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     1
/*
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     2
 * Hedgewars, a free turn based strategy game
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     3
 * Copyright (c) 2011 Andrey Korotaev <unC0Rr@gmail.com>
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    17
 */
088d40d8aba2 Happy 2011 :)
koda
parents: 4938
diff changeset
    18
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    19
#include <QGraphicsSceneMouseEvent>
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    20
#include <QGraphicsPathItem>
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    21
#include <QtEndian>
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    22
#include <QDebug>
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    23
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    24
#include "drawmapscene.h"
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    25
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    26
template <class T> T sqr(const T & x)
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    27
{
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    28
    return x*x;
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    29
}
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    30
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    31
DrawMapScene::DrawMapScene(QObject *parent) :
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    32
    QGraphicsScene(parent),
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    33
    m_pen(Qt::yellow),
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    34
    m_brush(Qt::yellow)
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    35
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    36
    setSceneRect(0, 0, 4096, 2048);
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    37
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    38
    QLinearGradient gradient(0, 0, 0, 2048);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    39
    gradient.setColorAt(0, QColor(60, 60, 155));
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    40
    gradient.setColorAt(1, QColor(155, 155, 60));
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    41
    setBackgroundBrush(QBrush(gradient));
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    42
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    43
    m_pen.setWidth(67);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    44
    m_pen.setJoinStyle(Qt::RoundJoin);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    45
    m_pen.setCapStyle(Qt::RoundCap);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    46
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    47
}
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
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    50
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    51
    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
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();
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    54
4938
0985edac2ad7 use ctrl instead of alt to avoid colliding with linux window positioning
nemo
parents: 4937
diff changeset
    55
        if(mouseEvent->modifiers() & Qt::ControlModifier)
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    56
        {
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    57
            int c = path.elementCount();
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    58
            QPointF pos = mouseEvent->scenePos();
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    59
            path.setElementPositionAt(c - 1, pos.x(), pos.y());
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    60
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    61
        } else
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    62
        {
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    63
            path.lineTo(mouseEvent->scenePos());
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    64
            paths.first().append(mouseEvent->scenePos().toPoint());
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    65
        }
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    66
        m_currPath->setPath(path);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    67
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    68
        emit pathChanged();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    69
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    70
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    71
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    72
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    73
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    74
    m_currPath = addPath(QPainterPath(), m_pen);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    75
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    76
    QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    77
    QPointF p = mouseEvent->scenePos();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    78
    p += QPointF(0.01, 0.01);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    79
    path.moveTo(p);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    80
    path.lineTo(mouseEvent->scenePos());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    81
    paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    82
    m_currPath->setPath(path);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    83
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    84
    emit pathChanged();
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    85
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    86
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    87
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    88
{
5108
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    89
    if (m_currPath)
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    90
    {
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    91
        QPainterPath path = m_currPath->path();
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    92
        path.lineTo(mouseEvent->scenePos());
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    93
        paths.first().append(mouseEvent->scenePos().toPoint());
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    94
        m_currPath->setPath(path);
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
    95
5108
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    96
        simplifyLast();
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    97
5108
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    98
        m_currPath = 0;
b7483e29ea8c Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents: 5040
diff changeset
    99
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   100
}
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   101
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   102
void DrawMapScene::undo()
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   103
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   104
    if(items().size())
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   105
    {
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   106
        removeItem(items().first());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   107
        paths.removeFirst();
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   108
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   109
        emit pathChanged();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   110
    }
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   111
    else if(oldItems.size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   112
    {
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   113
        while(oldItems.size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   114
            addItem(oldItems.takeFirst());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   115
        paths = oldPaths;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   116
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   117
        emit pathChanged();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   118
    }
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   119
}
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   120
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   121
void DrawMapScene::clearMap()
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   122
{
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   123
    // don't clear if already cleared
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   124
    if(!items().size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   125
        return;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   126
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   127
    oldItems.clear();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   128
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   129
    // do this since clear() would _destroy_ all items
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   130
    while(items().size()) {
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   131
        oldItems.push_front(items().first());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   132
        removeItem(items().first());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   133
    }
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   134
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   135
    oldPaths = paths;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   136
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   137
    paths.clear();
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   138
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   139
    emit pathChanged();
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   140
}
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   141
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   142
QByteArray DrawMapScene::encode()
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   143
{
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   144
    QByteArray b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   145
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   146
    for(int i = paths.size() - 1; i >= 0; --i)
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   147
    {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   148
        int cnt = 0;
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   149
        QList<QPoint> points = paths.at(i);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   150
        foreach(QPoint point, points)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   151
        {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   152
            qint16 px = qToBigEndian((qint16)point.x());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   153
            qint16 py = qToBigEndian((qint16)point.y());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   154
            quint8 flags = 2;
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   155
            if(!cnt) flags |= 0x80;
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   156
            b.append((const char *)&px, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   157
            b.append((const char *)&py, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   158
            b.append((const char *)&flags, 1);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   159
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   160
            ++cnt;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   161
        }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   162
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   163
    }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   164
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   165
    return b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   166
}
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   167
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   168
void DrawMapScene::decode(QByteArray data)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   169
{
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   170
    oldItems.clear();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   171
    oldPaths.clear();
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   172
    clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   173
    paths.clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   174
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   175
    QList<QPoint> points;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   176
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   177
    while(data.size() >= 5)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   178
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   179
        qint16 px = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   180
        data.remove(0, 2);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   181
        qint16 py = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   182
        data.remove(0, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   183
        quint8 flags = *(quint8 *)data.data();
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   184
        data.remove(0, 1);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   185
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   186
        if((flags & 0x80) && points.size())
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   187
        {
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   188
            addPath(pointsToPath(points), m_pen);
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   189
            paths.prepend(points);
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   190
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   191
            points.clear();
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   192
        }
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   193
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   194
        points.append(QPoint(px, py));
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   195
    }
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   196
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   197
    if(points.size())
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   198
    {
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   199
        addPath(pointsToPath(points), m_pen);
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   200
        paths.prepend(points);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   201
    }
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   202
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   203
    emit pathChanged();
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   204
}
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   205
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   206
void DrawMapScene::simplifyLast()
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   207
{
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   208
    if(!paths.size()) return;
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   209
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   210
    QList<QPoint> points = paths.at(0);
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   211
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   212
    QPoint prevPoint = points.first();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   213
    int i = 1;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   214
    while(i < points.size())
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   215
    {
4471
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   216
        if( (i != points.size() - 1)
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   217
            && (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   218
          )
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   219
            points.removeAt(i);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   220
        else
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   221
        {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   222
            prevPoint = points[i];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   223
            ++i;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   224
        }
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   225
    }
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   226
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   227
    paths[0] = points;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   228
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   229
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   230
    // redraw path
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   231
    {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   232
        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   233
        pathItem->setPath(pointsToPath(paths[0]));
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   234
    }
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   235
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   236
    emit pathChanged();
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   237
}
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   238
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   239
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   240
{
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   241
    QPainterPath path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   242
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   243
    if(points.size())
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   244
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   245
        QPointF p = points[0] + QPointF(0.01, 0.01);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   246
        path.moveTo(p);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   247
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   248
        foreach(QPoint p, points)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   249
            path.lineTo(p);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   250
    }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   251
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   252
    return path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   253
}