aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qmt/diagram_scene/parts/pathselectionitem.cpp
blob: 937541f8969b147c1710e23f63626123adef4095 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
/****************************************************************************
**
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "pathselectionitem.h"

#include "qmt/diagram_scene/capabilities/windable.h"
#include "qmt/diagram_scene/diagramsceneconstants.h"
#include "qmt/infrastructure/geometryutilities.h"
#include "qmt/infrastructure/qmtassert.h"

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QBrush>
#include <QLineF>
#include <QPainter>
#include <QPainterPath>
#include <QKeyEvent>

namespace qmt {

static const double MAX_SELECTION_DISTANCE_FROM_PATH = 4.0;

class PathSelectionItem::GraphicsHandleItem : public QGraphicsRectItem
{
public:
    enum Selection {
        NotSelected,
        Selected,
        SecondarySelected
    };

    GraphicsHandleItem(int pointIndex, PathSelectionItem *parent)
        : QGraphicsRectItem(parent),
          m_owner(parent),
          m_pointIndex(pointIndex)
    {
        setFlag(QGraphicsItem::ItemIsFocusable);
    }

    void setPointIndex(int pointIndex)
    {
        m_pointIndex = pointIndex;
    }

    void setPointSize(const QSizeF &pointSize)
    {
        if (m_pointSize != pointSize) {
            m_pointSize = pointSize;
            update();
        }
    }

    void setSelection(Selection selection)
    {
        if (m_selection != selection) {
            m_selection = selection;
            update();
        }
    }

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        m_startPos = event->scenePos();
        m_lastPos = m_startPos;
        m_qualifier = event->modifiers() & Qt::ControlModifier ? DeleteHandle : None;
        m_owner->moveHandle(m_pointIndex, QPointF(0.0, 0.0), Press, m_qualifier);
        setFocus();
    }

    void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        m_lastPos = event->scenePos();
        QPointF delta = m_lastPos - m_startPos;
        m_owner->moveHandle(m_pointIndex, delta, Move, m_qualifier);
    }

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        m_lastPos = event->scenePos();
        QPointF delta = m_lastPos - m_startPos;
        m_owner->moveHandle(m_pointIndex, delta, Release, m_qualifier);
        clearFocus();
    }

    void keyPressEvent(QKeyEvent *event)
    {
        m_owner->keyPressed(m_pointIndex, event, m_lastPos);
    }

private:
    void update()
    {
        prepareGeometryChange();
        setRect(-m_pointSize.width() / 2.0, -m_pointSize.height() / 2.0, m_pointSize.width(), m_pointSize.height());
        switch (m_selection) {
        case NotSelected:
            setPen(Qt::NoPen);
            setBrush(Qt::NoBrush);
            break;
        case Selected:
            setPen(QPen(Qt::black));
            setBrush(QBrush(Qt::black));
            break;
        case SecondarySelected:
            setPen(QPen(Qt::lightGray));
            setBrush(Qt::NoBrush);
            break;
        }
    }

    PathSelectionItem *m_owner = nullptr;
    int m_pointIndex = -1;
    QSizeF m_pointSize;
    Selection m_selection = NotSelected;
    QPointF m_startPos;
    QPointF m_lastPos;
    PathSelectionItem::HandleQualifier m_qualifier = PathSelectionItem::None;
};

PathSelectionItem::PathSelectionItem(IWindable *windable, QGraphicsItem *parent)
    : QGraphicsItem(parent),
      m_windable(windable),
      m_pointSize(QSizeF(8.0, 8.0))
{
}

PathSelectionItem::~PathSelectionItem()
{
}

QRectF PathSelectionItem::boundingRect() const
{
    return childrenBoundingRect();
}

void PathSelectionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(painter)
    Q_UNUSED(option)
    Q_UNUSED(widget)

#ifdef DEBUG_PAINT_SHAPE
    painter->save();
    painter->setPen(Qt::red);
    painter->setBrush(Qt::blue);
    painter->drawPath(shape());
    painter->restore();
#endif
}

QPainterPath PathSelectionItem::shape() const
{
    QPainterPath shape;
    shape.setFillRule(Qt::WindingFill);
    foreach (const GraphicsHandleItem *handle, m_handles)
        shape.addPath(handle->shape());
    // TODO duplicate of ArrowItem::GraphicsShaftItem's shape
    QPolygonF polygon;
    for (int i = 0; i < m_handles.size(); ++i)
        polygon.append(m_handles.at(i)->pos());
    QPainterPath polygonPath;
    polygonPath.addPolygon(polygon);
    QPainterPathStroker ps;
    ps.setWidth(16.0);
    polygonPath = ps.createStroke(polygonPath);
    shape.addPath(polygonPath);
    return shape;
}

void PathSelectionItem::setPointSize(const QSizeF &size)
{
    if (size != m_pointSize) {
        m_pointSize = size;
        update();
    }
}

QList<QPointF> PathSelectionItem::points() const
{
    QList<QPointF> points;
    foreach (GraphicsHandleItem *handle, m_handles)
        points.append(handle->pos());
    return points;
}

void PathSelectionItem::setPoints(const QList<QPointF> &points)
{
    QMT_ASSERT(points.size() >= 2, return);
    prepareGeometryChange();

    GraphicsHandleItem *focusEndBItem = nullptr;
    if (!m_handles.isEmpty() && m_focusHandleItem == m_handles.last()) {
        focusEndBItem = m_focusHandleItem;
        m_handles.removeLast();
    }
    int pointIndex = 0;
    foreach (const QPointF &point, points) {
        GraphicsHandleItem *handle;
        if (focusEndBItem && pointIndex == points.size() - 1) {
            handle = focusEndBItem;
            handle->setPointIndex(pointIndex);
            m_handles.insert(pointIndex, handle);
            focusEndBItem = nullptr;
        } else if (pointIndex >= m_handles.size()) {
            handle = new GraphicsHandleItem(pointIndex, this);
            handle->setPointSize(m_pointSize);
            m_handles.append(handle);
        } else {
            handle = m_handles.at(pointIndex);
        }
        handle->setPos(point);
        ++pointIndex;
    }
    QMT_CHECK(!focusEndBItem);
    while (m_handles.size() > pointIndex) {
        m_handles.last()->scene()->removeItem(m_handles.last());
        delete m_handles.last();
        m_handles.removeLast();
    }
    update();
}

void PathSelectionItem::setSecondarySelected(bool secondarySelected)
{
    if (m_isSecondarySelected != secondarySelected) {
        m_isSecondarySelected = secondarySelected;
        update();
    }
}

void PathSelectionItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier) {
        for (int i = 0; i < m_handles.size() - 1; ++i) {
            qreal distance = GeometryUtilities::calcDistancePointToLine(event->pos(), QLineF(m_handles.at(i)->pos(), m_handles.at(i+1)->pos()));
            if (distance < MAX_SELECTION_DISTANCE_FROM_PATH) {
                m_windable->insertHandle(i + 1, event->scenePos(), RASTER_WIDTH, RASTER_HEIGHT);
                event->accept();
                return;
            }
        }
    } else {
        QGraphicsItem::mousePressEvent(event);
    }
}

bool PathSelectionItem::isEndHandle(int pointIndex) const
{
    return pointIndex == 0 || pointIndex == m_handles.size() - 1;
}

void PathSelectionItem::update()
{
    prepareGeometryChange();
    int i = 0;
    foreach (GraphicsHandleItem *handle, m_handles) {
        handle->setPointSize(m_pointSize);
        handle->setSelection(m_isSecondarySelected
                             ? (isEndHandle(i) ? GraphicsHandleItem::NotSelected : GraphicsHandleItem::SecondarySelected)
                             : GraphicsHandleItem::Selected);
        ++i;
    }
}

void PathSelectionItem::moveHandle(int pointIndex, const QPointF &deltaMove, HandleStatus handleStatus, HandleQualifier handleQualifier)
{
    switch (handleQualifier) {
    case None:
    {
        if (handleStatus == Press) {
            m_focusHandleItem = m_handles.at(pointIndex);
            m_originalHandlePos = m_windable->grabHandle(pointIndex);
        }
        QPointF newPos = m_originalHandlePos + deltaMove;
        m_windable->setHandlePos(pointIndex, newPos);
        if (handleStatus == Release) {
            m_windable->dropHandle(pointIndex, RASTER_WIDTH, RASTER_HEIGHT);
            m_focusHandleItem = nullptr;
        }
        break;
    }
    case DeleteHandle:
        if (handleStatus == Press)
            m_windable->deleteHandle(pointIndex);
        break;
    }
}

void PathSelectionItem::keyPressed(int pointIndex, QKeyEvent *event, const QPointF &pos)
{
    if (isEndHandle(pointIndex)) {
        if (event->key() == Qt::Key_Shift)
            m_windable->insertHandle(pointIndex, pos, RASTER_WIDTH, RASTER_HEIGHT);
        else if (event->key() == Qt::Key_Control)
            m_windable->deleteHandle(pointIndex);
    }
}

} // namespace qmt