1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Jochen Becher
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <QGraphicsItem>
29 
QT_FORWARD_DECLARE_CLASS(QPainterPath)30 QT_FORWARD_DECLARE_CLASS(QPainterPath)
31 
32 namespace qmt {
33 
34 class IWindable;
35 
36 class PathSelectionItem : public QGraphicsItem
37 {
38     class GraphicsHandleItem;
39     friend class GraphicsHandleItem;
40 
41     enum HandleStatus {
42         Press,
43         Move,
44         Release,
45         Cancel
46     };
47 
48     enum HandleQualifier {
49         None,
50         DeleteHandle
51     };
52 
53 public:
54     explicit PathSelectionItem(IWindable *windable, QGraphicsItem *parent = nullptr);
55     ~PathSelectionItem() override;
56 
57     QRectF boundingRect() const override;
58     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
59     QPainterPath shape() const override;
60 
61     QSizeF pointSize() const { return m_pointSize; }
62     void setPointSize(const QSizeF &size);
63     QList<QPointF> points() const;
64     void setPoints(const QList<QPointF> &points);
65     void setSecondarySelected(bool secondarySelected);
66 
67 protected:
68     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
69 
70 private:
71     bool isEndHandle(int pointIndex) const;
72     void update();
73     void moveHandle(int pointIndex, const QPointF &deltaMove, HandleStatus handleStatus,
74                     HandleQualifier handleQualifier);
75     void keyPressed(int pointIndex, QKeyEvent *event, const QPointF &pos);
76 
77     IWindable *m_windable = nullptr;
78     QSizeF m_pointSize;
79     bool m_isSecondarySelected = false;
80     QList<GraphicsHandleItem *> m_handles;
81     GraphicsHandleItem *m_focusHandleItem = nullptr;
82     QPointF m_originalHandlePos;
83 };
84 
85 } // namespace qmt
86