QTfrontend/drawmapscene.cpp
author Wuzzy <Wuzzy2@mail.ru>
Thu, 22 Mar 2018 16:52:04 +0100
changeset 13267 48b23e6f26d9
parent 13236 ef1393c6bd12
child 13931 6c8d4e140f27
permissions -rw-r--r--
Add icons to background themes; allow to hide themes explicitly by adding “hidden” key to theme.cfg
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
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10235
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
4976
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 10002
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4976
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>
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    23
#include <QTransform>
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    24
#include <math.h>
4423
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
#include "drawmapscene.h"
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    27
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    28
#define DRAWN_MAP_COLOR_LAND (Qt::yellow)
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    29
#define DRAWN_MAP_COLOR_CURSOR_PEN (Qt::green)
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    30
#define DRAWN_MAP_COLOR_CURSOR_ERASER (Qt::red)
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    31
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    32
template <class T> T sqr(const T & x)
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    33
{
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    34
    return x*x;
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    35
}
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    36
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    37
DrawMapScene::DrawMapScene(QObject *parent) :
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    38
    QGraphicsScene(parent),
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    39
    m_pen(DRAWN_MAP_COLOR_LAND),
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    40
    m_brush(DRAWN_MAP_COLOR_LAND),
13203
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
    41
    m_cursor(new QGraphicsEllipseItem(-5, -5, 5, 5))
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    42
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    43
    setSceneRect(0, 0, 4096, 2048);
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    44
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    45
    QLinearGradient gradient(0, 0, 0, 2048);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    46
    gradient.setColorAt(0, QColor(60, 60, 155));
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    47
    gradient.setColorAt(1, QColor(155, 155, 60));
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
    48
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
    49
    m_eraser = QBrush(gradient);
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
    50
    setBackgroundBrush(m_eraser);
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
    51
    m_isErasing = false;
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    52
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    53
    m_pathType = Polyline;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    54
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
    55
    m_pen.setWidth(DRAWN_MAP_BRUSH_SIZE_START);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    56
    m_pen.setJoinStyle(Qt::RoundJoin);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    57
    m_pen.setCapStyle(Qt::RoundCap);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    58
    m_currPath = 0;
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
    59
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
    60
    m_isCursorShown = false;
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
    61
    QPen cursorPen = QPen(DRAWN_MAP_COLOR_CURSOR_PEN);
13203
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
    62
    cursorPen.setJoinStyle(Qt::RoundJoin);
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
    63
    cursorPen.setCapStyle(Qt::RoundCap);
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
    64
    cursorPen.setWidth(brushSize());
13203
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
    65
    m_cursor->setPen(cursorPen);
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
    66
    m_cursor->setZValue(1);
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    67
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    68
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    69
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    70
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    71
    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    72
    {
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    73
        QPainterPath path = m_currPath->path();
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    74
        QPointF currentPos = mouseEvent->scenePos();
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    75
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    76
        if(mouseEvent->modifiers() & Qt::ControlModifier)
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    77
            currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    78
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    79
        switch (m_pathType)
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    80
        {
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    81
        case Polyline:
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    82
            if(mouseEvent->modifiers() & Qt::ControlModifier)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    83
            {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    84
                int c = path.elementCount();
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    85
                path.setElementPositionAt(c - 1, currentPos.x(), currentPos.y());
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
    86
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    87
            }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    88
            else
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    89
            {
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    90
                path.lineTo(currentPos);
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    91
                paths.first().points.append(mouseEvent->scenePos().toPoint());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    92
            }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    93
            break;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    94
        case Rectangle: {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    95
            path = QPainterPath();
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    96
            QPointF p1 = paths.first().initialPoint;
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
    97
            QPointF p2 = currentPos;
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    98
            path.moveTo(p1);
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
    99
            path.lineTo(p1.x(), p2.y());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   100
            path.lineTo(p2);
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   101
            path.lineTo(p2.x(), p1.y());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   102
            path.lineTo(p1);
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   103
            break;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   104
            }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   105
        case Ellipse: {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   106
            path = QPainterPath();
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   107
            QList<QPointF> points = makeEllipse(paths.first().initialPoint, currentPos);
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   108
            path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points)));
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   109
            break;
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 5858
diff changeset
   110
        }
4937
55b9145fea94 Draw straight lines while holding Alt key
unc0rr
parents: 4666
diff changeset
   111
        }
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   112
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   113
        m_currPath->setPath(path);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   114
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   115
        emit pathChanged();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   116
    }
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   117
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   118
    if(!m_isCursorShown)
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   119
        showCursor();
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   120
    m_cursor->setPos(mouseEvent->scenePos());
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   121
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   122
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   123
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   124
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   125
    m_currPath = addPath(QPainterPath(), m_pen);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   126
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   127
    QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   128
    QPointF p = mouseEvent->scenePos();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   129
    p += QPointF(0.01, 0.01);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   130
    path.moveTo(p);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   131
    path.lineTo(mouseEvent->scenePos());
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   132
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   133
    PathParams params;
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   134
    params.width = serializePenWidth(brushSize());
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   135
    params.erasing = m_isErasing;
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   136
    params.initialPoint = mouseEvent->scenePos().toPoint();
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   137
    params.points = QList<QPoint>() << params.initialPoint;
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   138
    paths.prepend(params);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   139
    m_currPath->setPath(path);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   140
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   141
    emit pathChanged();
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   142
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   143
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   144
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   145
{
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
   146
    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
   147
    {
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   148
        QPointF currentPos = mouseEvent->scenePos();
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   149
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   150
        if(mouseEvent->modifiers() & Qt::ControlModifier)
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   151
            currentPos = putSomeConstraints(paths.first().initialPoint, currentPos);
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   152
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   153
        switch (m_pathType)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   154
        {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   155
        case Polyline: {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   156
            QPainterPath path = m_currPath->path();
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   157
            path.lineTo(mouseEvent->scenePos());
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   158
            paths.first().points.append(currentPos.toPoint());
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   159
            m_currPath->setPath(path);
9553
f92d43816186 Some tweaks to ellipse code to produce less points
unc0rr
parents: 9551
diff changeset
   160
            simplifyLast();
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   161
            break;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   162
        }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   163
        case Rectangle: {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   164
            QPoint p1 = paths.first().initialPoint;
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   165
            QPoint p2 = currentPos.toPoint();
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   166
            QList<QPoint> rpoints;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   167
            rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   168
            paths.first().points = rpoints;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   169
            break;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   170
        }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   171
        case Ellipse:
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   172
            QPoint p1 = paths.first().initialPoint;
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   173
            QPoint p2 = currentPos.toPoint();
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   174
            QList<QPointF> points = makeEllipse(p1, p2);
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   175
            QList<QPoint> epoints;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   176
            foreach(const QPointF & p, points)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   177
                epoints.append(p.toPoint());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   178
            paths.first().points = epoints;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   179
            break;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   180
        }
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   181
9553
f92d43816186 Some tweaks to ellipse code to produce less points
unc0rr
parents: 9551
diff changeset
   182
        m_currPath = 0;
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   183
9553
f92d43816186 Some tweaks to ellipse code to produce less points
unc0rr
parents: 9551
diff changeset
   184
        emit pathChanged();
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
   185
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
   186
}
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   187
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   188
void DrawMapScene::setBrushSize(int newBrushSize)
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   189
{
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   190
    if(newBrushSize > DRAWN_MAP_BRUSH_SIZE_MAX)
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   191
        newBrushSize = DRAWN_MAP_BRUSH_SIZE_MAX;
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   192
    if(newBrushSize < DRAWN_MAP_BRUSH_SIZE_MIN)
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   193
        newBrushSize = DRAWN_MAP_BRUSH_SIZE_MIN;
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   194
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   195
    m_pen.setWidth(newBrushSize);
13203
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
   196
    QPen cursorPen = m_cursor->pen();
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
   197
    cursorPen.setWidth(m_pen.width());
eaa494f0b19e Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents: 11046
diff changeset
   198
    m_cursor->setPen(cursorPen);
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   199
    if(m_currPath)
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   200
    {
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   201
        m_currPath->setPen(m_pen);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   202
        paths.first().width = serializePenWidth(m_pen.width());
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   203
    }
13209
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   204
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   205
    emit brushSizeChanged(newBrushSize);
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   206
}
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   207
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   208
int DrawMapScene::brushSize()
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   209
{
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   210
    return m_pen.width();
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   211
}
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   212
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   213
void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   214
{
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   215
    int b = brushSize();
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   216
    if(wheelEvent->delta() > 0)
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   217
        setBrushSize(b + DRAWN_MAP_BRUSH_SIZE_STEP);
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   218
    else if(wheelEvent->delta() < 0 && b >= DRAWN_MAP_BRUSH_SIZE_MIN)
f5d36be88c61 Drawn map menu: Add spin box to change brush size by hand
Wuzzy <Wuzzy2@mail.ru>
parents: 13204
diff changeset
   219
        setBrushSize(b - DRAWN_MAP_BRUSH_SIZE_STEP);
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   220
}
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   221
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   222
void DrawMapScene::showCursor()
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   223
{
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   224
    if(!m_isCursorShown)
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   225
        addItem(m_cursor);
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   226
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   227
    m_isCursorShown = true;
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   228
}
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   229
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   230
void DrawMapScene::hideCursor()
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   231
{
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   232
    if(m_isCursorShown)
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   233
        removeItem(m_cursor);
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   234
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   235
    m_isCursorShown = false;
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   236
}
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   237
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   238
void DrawMapScene::undo()
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   239
{
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   240
    // cursor is a part of items()
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   241
    if(m_isCursorShown)
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   242
        return;
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   243
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   244
    if(paths.size())
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   245
    {
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
   246
        removeItem(items().first());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   247
        paths.removeFirst();
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   248
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   249
        emit pathChanged();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   250
    }
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   251
    else if(oldItems.size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   252
    {
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   253
        while(oldItems.size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   254
            addItem(oldItems.takeFirst());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   255
        paths = oldPaths;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   256
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   257
        emit pathChanged();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   258
    }
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
   259
}
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   260
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   261
void DrawMapScene::clearMap()
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   262
{
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   263
    // cursor is a part of items()
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   264
    if(m_isCursorShown)
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   265
        return;
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   266
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   267
    // don't clear if already cleared
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   268
    if(!items().size())
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   269
        return;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   270
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   271
    m_specialPoints.clear();
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   272
    oldItems.clear();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   273
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   274
    // do this since clear() would _destroy_ all items
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   275
    for(int i = paths.size() - 1; i >= 0; --i)
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 5858
diff changeset
   276
    {
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   277
        oldItems.push_front(items().first());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   278
        removeItem(items().first());
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   279
    }
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   280
9483
14ad1ac00ac9 Modify Racer script so it could work with special points
unc0rr
parents: 9472
diff changeset
   281
    items().clear();
14ad1ac00ac9 Modify Racer script so it could work with special points
unc0rr
parents: 9472
diff changeset
   282
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   283
    oldPaths = paths;
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   284
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   285
    paths.clear();
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   286
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   287
    emit pathChanged();
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   288
}
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   289
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   290
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   291
void DrawMapScene::setErasing(bool erasing)
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   292
{
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   293
    m_isErasing = erasing;
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   294
    QPen cursorPen = m_cursor->pen();
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   295
    if(erasing) {
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   296
        m_pen.setBrush(m_eraser);
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   297
        cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_ERASER);
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   298
    } else {
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   299
        m_pen.setBrush(m_brush);
13204
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   300
        cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_PEN);
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   301
    }
9565569e410c Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents: 13203
diff changeset
   302
    m_cursor->setPen(cursorPen);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   303
}
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   304
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   305
QByteArray DrawMapScene::encode()
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   306
{
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   307
    QByteArray b(m_specialPoints);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   308
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   309
    for(int i = paths.size() - 1; i >= 0; --i)
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   310
    {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   311
        int cnt = 0;
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   312
        PathParams params = paths.at(i);
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   313
        foreach(QPoint point, params.points)
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   314
        {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   315
            qint16 px = qToBigEndian((qint16)point.x());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   316
            qint16 py = qToBigEndian((qint16)point.y());
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   317
            quint8 flags = 0;
7298
a5f2fa95e711 Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents: 7146
diff changeset
   318
            if(!cnt)
a5f2fa95e711 Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents: 7146
diff changeset
   319
            {
a5f2fa95e711 Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents: 7146
diff changeset
   320
                flags = 0x80 + params.width;
a5f2fa95e711 Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents: 7146
diff changeset
   321
                if(params.erasing) flags |= 0x40;
a5f2fa95e711 Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents: 7146
diff changeset
   322
            }
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   323
            b.append((const char *)&px, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   324
            b.append((const char *)&py, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   325
            b.append((const char *)&flags, 1);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   326
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   327
            ++cnt;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   328
        }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   329
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   330
    }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   331
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   332
    return b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
   333
}
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   334
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   335
void DrawMapScene::decode(QByteArray data)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   336
{
10235
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   337
    hideCursor();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   338
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   339
    // Remember erasing mode
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   340
    bool erasing = m_isErasing;
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   341
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   342
    // Use seperate for decoding the map, don't mess with the user pen
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   343
    QPen load_pen = QPen(m_pen);
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   344
5858
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   345
    oldItems.clear();
1f4a8cf9efcb hand drawn map editor: allow "undo" after "clear"
sheepluva
parents: 5108
diff changeset
   346
    oldPaths.clear();
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   347
    clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   348
    paths.clear();
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   349
    m_specialPoints.clear();
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   350
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   351
    PathParams params;
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   352
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   353
    bool isSpecial = true;
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   354
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   355
    while(data.size() >= 5)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   356
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   357
        qint16 px = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   358
        data.remove(0, 2);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   359
        qint16 py = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   360
        data.remove(0, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   361
        quint8 flags = *(quint8 *)data.data();
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   362
        data.remove(0, 1);
10235
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   363
        //qDebug() << px << py;
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   364
        if(flags & 0x80)
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   365
        {
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   366
            isSpecial = false;
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   367
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   368
            if(params.points.size())
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   369
            {
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   370
                addPath(pointsToPath(params.points), load_pen);
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   371
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   372
                paths.prepend(params);
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   373
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   374
                params.points.clear();
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   375
            }
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   376
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   377
            quint8 penWidth = flags & 0x3f;
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   378
            load_pen.setWidth(deserializePenWidth(penWidth));
7146
a822413207c9 Oops, forgot to set 'erasing' flag on decoding
unc0rr
parents: 6952
diff changeset
   379
            params.erasing = flags & 0x40;
a822413207c9 Oops, forgot to set 'erasing' flag on decoding
unc0rr
parents: 6952
diff changeset
   380
            if(params.erasing)
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   381
                load_pen.setBrush(m_eraser);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   382
            else
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   383
                load_pen.setBrush(m_brush);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   384
            params.width = penWidth;
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   385
        } else
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   386
            if(isSpecial)
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   387
            {
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   388
                QPainterPath path;
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   389
                path.addEllipse(QPointF(px, py), 10, 10);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   390
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   391
                addPath(path);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   392
9472
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   393
                qint16 x = qToBigEndian(px);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   394
                qint16 y = qToBigEndian(py);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   395
                m_specialPoints.append((const char *)&x, 2);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   396
                m_specialPoints.append((const char *)&y, 2);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   397
                m_specialPoints.append((const char *)&flags, 1);
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   398
            }
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   399
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   400
        if(!isSpecial)
265e5997580e Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents: 9080
diff changeset
   401
            params.points.append(QPoint(px, py));
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   402
    }
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   403
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   404
    if(params.points.size())
4666
34551d8639cf - Fix encoding function to iterate reversed list
unc0rr
parents: 4658
diff changeset
   405
    {
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   406
        addPath(pointsToPath(params.points), load_pen);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   407
        paths.prepend(params);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   408
    }
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   409
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   410
    emit pathChanged();
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   411
13236
ef1393c6bd12 Draw map page: Fix pen size being changed after loading a map
Wuzzy <Wuzzy2@mail.ru>
parents: 13209
diff changeset
   412
    // Restore erasing mode
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   413
    setErasing(erasing);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   414
}
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   415
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   416
void DrawMapScene::simplifyLast()
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   417
{
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4520
diff changeset
   418
    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
   419
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   420
    QList<QPoint> points = paths.at(0).points;
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   421
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   422
    QPoint prevPoint = points.first();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   423
    int i = 1;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   424
    while(i < points.size())
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   425
    {
10223
b6b98dfa3807 Optimize out path closing point for one point paths
unc0rr
parents: 10108
diff changeset
   426
        if( ((i != points.size() - 1) || (prevPoint == points[i]))
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 5858
diff changeset
   427
                && (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
4471
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   428
          )
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   429
            points.removeAt(i);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   430
        else
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   431
        {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   432
            prevPoint = points[i];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   433
            ++i;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   434
        }
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   435
    }
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   436
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   437
    paths[0].points = points;
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   438
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   440
    // redraw path
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   441
    {
6934
14a230552c2e Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents: 6873
diff changeset
   442
        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]);
6873
30840365af0a Eraser tool
unc0rr
parents: 6784
diff changeset
   443
        pathItem->setPath(pointsToPath(paths[0].points));
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   444
    }
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   445
}
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   446
6935
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   447
int DrawMapScene::pointsCount()
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   448
{
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   449
    int cnt = 0;
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   450
    foreach(PathParams p, paths)
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   451
        cnt += p.points.size();
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   452
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   453
    return cnt;
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   454
}
472ed92c4173 Show number of points in drawn map
unc0rr
parents: 6934
diff changeset
   455
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   456
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   457
{
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   458
    QPainterPath path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   459
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   460
    if(points.size())
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   461
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   462
        QPointF p = points[0] + QPointF(0.01, 0.01);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   463
        path.moveTo(p);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   464
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   465
        foreach(QPoint p, points)
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 5858
diff changeset
   466
        path.lineTo(p);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   467
    }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   468
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   469
    return path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   470
}
6781
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   471
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   472
quint8 DrawMapScene::serializePenWidth(int width)
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   473
{
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   474
    return (width - 6) / 10;
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   475
}
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   476
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   477
int DrawMapScene::deserializePenWidth(quint8 width)
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   478
{
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   479
    return width * 10 + 6;
23f627ba8ee9 Variable pen width
unc0rr
parents: 6700
diff changeset
   480
}
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   481
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   482
void DrawMapScene::setPathType(PathType pathType)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   483
{
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   484
    m_pathType = pathType;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   485
}
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   486
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   487
QList<QPointF> DrawMapScene::makeEllipse(const QPointF &center, const QPointF &corner)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   488
{
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   489
    QList<QPointF> l;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   490
    qreal rx = qAbs(center.x() - corner.x());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   491
    qreal ry = qAbs(center.y() - corner.y());
9553
f92d43816186 Some tweaks to ellipse code to produce less points
unc0rr
parents: 9551
diff changeset
   492
    qreal r = qMax(rx, ry);
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   493
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   494
    if(r < 4)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   495
    {
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   496
        l.append(center);
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   497
    } else
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   498
    {
9949
2aa9cf5badfc Added cast to qreal, avoiding ftbfs due to issue #758
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9555
diff changeset
   499
        qreal angleDelta = qMax(static_cast<qreal> (0.1), qMin(static_cast<qreal> (0.7), 120 / r));
9551
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   500
        for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta)
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   501
            l.append(center + QPointF(rx * cos(angle), ry * sin(angle)));
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   502
        l.append(l.first());
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   503
    }
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   504
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   505
    return l;
61f160dfd0f1 Draw rectangles and ellipses!
unc0rr
parents: 9483
diff changeset
   506
}
9555
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   507
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   508
QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point)
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   509
{
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   510
    QPointF vector = point - initialPoint;
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   511
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   512
    for(int angle = 0; angle < 180; angle += 15)
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   513
    {
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   514
        QTransform transform;
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   515
        transform.rotate(angle);
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   516
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   517
        QPointF rotated = transform.map(vector);
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   518
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   519
        if(rotated.x() == 0) return point;
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   520
        if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0));
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   521
    }
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   522
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   523
    return point;
485b424be769 uhm... a feature
unc0rr
parents: 9553
diff changeset
   524
}
10235
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   525
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   526
void DrawMapScene::optimize()
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   527
{
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   528
    if(!paths.size()) return;
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   529
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   530
    // break paths into segments
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   531
    Paths pth;
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   532
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   533
    foreach(const PathParams & pp, paths)
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   534
    {
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   535
        int l = pp.points.size();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   536
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   537
        if(l == 1)
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   538
        {
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   539
            pth.prepend(pp);
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   540
        } else
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   541
        {
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   542
            for(int i = l - 2; i >= 0; --i)
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   543
            {
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   544
                PathParams p = pp;
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   545
                p.points = QList<QPoint>() << p.points[i] << p.points[i + 1];
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   546
                pth.prepend(pp);
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   547
            }
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   548
        }
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   549
    }
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   550
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   551
    // clear the scene
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   552
    oldItems.clear();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   553
    oldPaths.clear();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   554
    clear();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   555
    paths.clear();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   556
    m_specialPoints.clear();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   557
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   558
    // render the result
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   559
    foreach(const PathParams & p, pth)
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   560
    {
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   561
        if(p.erasing)
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   562
            m_pen.setBrush(m_eraser);
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   563
        else
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   564
            m_pen.setBrush(m_brush);
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   565
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   566
        m_pen.setWidth(deserializePenWidth(p.width));
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   567
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   568
        addPath(pointsToPath(p.points), m_pen);
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   569
    }
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   570
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   571
    emit pathChanged();
bafca1686867 Drawn maps optimizer - first steps:
unc0rr
parents: 10223
diff changeset
   572
}