summaryrefslogtreecommitdiffstats
path: root/sigmodr/widgets/mapeditor/ShapeItem.cpp
blob: e514d3e7f8f8ee953d9c84bdd5bc1445dbdac2d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright 2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Header include
#include "ShapeItem.h"

// KDE includes
#include <KColorScheme>
#include <KDoubleNumInput>

// Qt includes
#include <QtCore/QFile>
#include <QtGui/QGridLayout>
#include <QtGui/QPainter>
#include <QtGui/QPainterPathStroker>
#include <QtGui/QStyleOptionGraphicsItem>
#include <QtUiTools/QUiLoader>

using namespace Sigmodr::Widgets;

ShapeItem::ShapeItem(const Shape shape) :
        m_shape(shape),
        m_rect(0, 0, 32, 32),
        m_thickness(1),
        m_rotation(0),
        m_xShear(1),
        m_yShear(1)
{
    if ((m_shape == PolyLine) || (m_shape == Polygon))
    {
        m_points.append(QPointF(0, 0));
        if (m_shape == Polygon)
            m_points.append(QPointF(0, 0));
    }
    recreatePath();
    setFlags(ItemIsMovable | ItemIsSelectable);
    setOpacity(.5);
}

ShapeItem::ShapeItem(const QPainterPath& path) :
        m_shape(Path),
        m_path(path),
        m_rect(0, 0, 32, 32),
        m_thickness(1),
        m_rotation(0),
        m_xShear(1),
        m_yShear(1)
{
    restroke();
}

ShapeItem::Shape ShapeItem::shapeType() const
{
    return m_shape;
}

QWidget* ShapeItem::widget()
{
    QString filename;
    switch (m_shape)
    {
        default:
            break;
    }
    QWidget *mainWidget = NULL;
    if (!filename.isEmpty())
    {
        QFile mainFile(filename);
        mainFile.open(QFile::ReadOnly);
        mainWidget = QUiLoader().load(&mainFile, NULL);
        mainFile.close();
    }
    QFile commonFile(":/gui/shapes/common.ui");
    commonFile.open(QFile::ReadOnly);
    QWidget *commonWidget = QUiLoader().load(&commonFile, NULL);
    commonFile.close();
    KDoubleNumInput* thickness = commonWidget->findChild<KDoubleNumInput*>("varThickness");
    KDoubleNumInput* rotation = commonWidget->findChild<KDoubleNumInput*>("varRotation");
    KDoubleNumInput* xShear = commonWidget->findChild<KDoubleNumInput*>("varXShear");
    KDoubleNumInput* yShear = commonWidget->findChild<KDoubleNumInput*>("varYShear");
    connect(thickness, SIGNAL(valueChanged(double)), this, SLOT(setThickness(double)));
    connect(rotation, SIGNAL(valueChanged(double)), this, SLOT(setRotation(double)));
    connect(xShear, SIGNAL(valueChanged(double)), this, SLOT(setXShear(double)));
    connect(yShear, SIGNAL(valueChanged(double)), this, SLOT(setYShear(double)));
    if (mainWidget)
    {
        QGridLayout* layout = commonWidget->findChild<QGridLayout*>("mainLayout");
        layout->addWidget(mainWidget, 0, 0);
        commonWidget->setTabOrder(yShear, mainWidget);
    }
    return commonWidget;
}

void ShapeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    Q_UNUSED(widget)
    if (option->state & QStyle::State_MouseOver)
    {
        painter->setPen(QPen(KStatefulBrush(KColorScheme::View, KColorScheme::HoverColor).brush(QPalette::Active), 3));
        painter->drawRect(boundingRect());
    }
    if (option->state & QStyle::State_Selected)
    {
        painter->setPen(QPen(KStatefulBrush(KColorScheme::Selection, KColorScheme::ActiveBackground).brush(QPalette::Active), 3));
        painter->drawRect(boundingRect());
    }
    painter->setPen(QPen());
    painter->setBrush(QBrush(Qt::red));
    painter->drawPath(m_strokePath);
}

QRectF ShapeItem::boundingRect() const
{
    return m_boundingRect;
}

QPainterPath ShapeItem::shape() const
{
    return m_strokePath;
}

void ShapeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
    QGraphicsItem::contextMenuEvent(event);
}

void ShapeItem::focusInEvent(QFocusEvent* event)
{
    QGraphicsItem::focusInEvent(event);
}

void ShapeItem::focusOutEvent(QFocusEvent* event)
{
    QGraphicsItem::focusOutEvent(event);
}

void ShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
    QGraphicsItem::hoverEnterEvent(event);
}

void ShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
    QGraphicsItem::hoverLeaveEvent(event);
}

void ShapeItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    QGraphicsItem::mousePressEvent(event);
}

void ShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    QGraphicsItem::mouseMoveEvent(event);
}

void ShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
    QGraphicsItem::mouseReleaseEvent(event);
}

void ShapeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
    QGraphicsItem::mouseDoubleClickEvent(event);
}

void ShapeItem::setPosition(const QPointF& position)
{
    m_rect.moveTo(position);
}

void ShapeItem::setWidth(const int width)
{
    m_rect.setWidth(width);
    recreatePath();
}

void ShapeItem::setHeight(const int height)
{
    m_rect.setHeight(height);
    recreatePath();
}

void ShapeItem::addPoint()
{
    if (m_shape == PolyLine)
        m_points.append(QPointF(0, 0));
    else if (m_shape == Polygon)
        m_points.insert(m_points.size() - 1, QPointF(0, 0));
    else
        return;
}

void ShapeItem::removePoint()
{
    if (m_shape == PolyLine)
        m_points.remove(m_points.size() - 1);
    else if (m_shape == Polygon)
        m_points.remove(m_points.size() - 2);
    else
        return;
    recreatePath();
}

void ShapeItem::setStartAngle(const double startAngle)
{
    m_startAngle = startAngle;
    recreatePath();
}

void ShapeItem::setSpanAngle(const double spanAngle)
{
    m_spanAngle = spanAngle;
    recreatePath();
}

void ShapeItem::setThickness(const double thickness)
{
    m_thickness = thickness;
    restroke();
}

void ShapeItem::setRotation(const double rotation)
{
    m_rotation = rotation;
    restroke();
}

void ShapeItem::setXShear(const double xShear)
{
    m_xShear = xShear;
    restroke();
}

void ShapeItem::setYShear(const double yShear)
{
    m_yShear = yShear;
    restroke();
}

void ShapeItem::recreatePath()
{
    if (m_shape != Path)
        m_path = QPainterPath();
    switch (m_shape)
    {
        case Path:
            break;
        case Rectangle:
            m_path.addRect(m_rect);
            break;
        case PolyLine:
            for (QVectorIterator<QPointF> i(m_points); i.hasNext(); i.next())
            {
                if (i.hasPrevious())
                    m_path.lineTo(i.peekNext());
                else
                    m_path.moveTo(i.peekNext());
            }
            break;
        case Polygon:
            m_path.addPolygon(QPolygonF(m_points));
            break;
        case Ellipse:
            m_path.addEllipse(m_rect);
            break;
        case Arc:
            m_path.arcMoveTo(m_rect, m_startAngle);
            m_path.arcTo(m_rect, m_startAngle, m_spanAngle);
            break;
        case Chord:
        {
            m_path.arcMoveTo(m_rect, m_startAngle);
            QPointF pos = m_path.currentPosition();
            m_path.arcTo(m_rect, m_startAngle, m_spanAngle);
            m_path.lineTo(pos);
            break;
        }
        case Pie:
            m_path.moveTo(m_rect.center());
            m_path.arcTo(m_rect, m_startAngle, m_spanAngle);
            m_path.closeSubpath();
            break;
    }
    restroke();
}

void ShapeItem::restroke()
{
    QPainterPathStroker stroke;
    stroke.setWidth(m_thickness);
    m_strokePath = stroke.createStroke(m_path * QMatrix(m_xShear, m_yShear, 1, 1, -m_rect.left(), -m_rect.top()).rotate(m_rotation));
    m_boundingRect = m_strokePath.boundingRect().adjusted(-2, -2, 2, 2);
    update();
}