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 #include <QVector>
30 
31 QT_BEGIN_NAMESPACE
32 class QGraphicsRectItem;
33 QT_END_NAMESPACE
34 
35 namespace qmt {
36 
37 class IResizable;
38 
39 class RectangularSelectionItem : public QGraphicsItem
40 {
41     class GraphicsHandleItem;
42     friend class GraphicsHandleItem;
43 
44     enum HandleStatus {
45         Press,
46         Move,
47         Release,
48         Cancel
49     };
50 
51     enum HandleQualifier {
52         None,
53         KeepPos
54     };
55 
56 public:
57     enum Handle {
58         HandleNone = -1,
59         HandleFirst = 0,
60         HandleTopLeft = HandleFirst,
61         HandleTop,
62         HandleTopRight,
63         HandleLeft,
64         HandleRight,
65         HandleBottomLeft,
66         HandleBottom,
67         HandleBottomRight,
68         HandleLast = HandleBottomRight
69     };
70 
71     enum Freedom {
72         FreedomAny,
73         FreedomVerticalOnly,
74         FreedomHorizontalOnly,
75         FreedomKeepRatio
76     };
77 
78     explicit RectangularSelectionItem(IResizable *itemResizer, QGraphicsItem *parent = nullptr);
79     ~RectangularSelectionItem() override;
80 
81     QRectF boundingRect() const override;
82     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
83 
rect()84     QRectF rect() const { return m_rect; }
85     void setRect(const QRectF &rectangle);
86     void setRect(qreal x, qreal y, qreal width, qreal height);
pointSize()87     QSizeF pointSize() const { return m_pointSize; }
88     void setPointSize(const QSizeF &size);
showBorder()89     bool showBorder() const { return m_showBorder; }
90     void setShowBorder(bool showBorder);
freedom()91     Freedom freedom() const { return m_freedom; }
92     void setFreedom(Freedom freedom);
isSecondarySelected()93     bool isSecondarySelected() const { return m_isSecondarySelected; }
94     void setSecondarySelected(bool secondarySelected);
activeHandle()95     Handle activeHandle() const { return m_activeHandle; }
96 
97 private:
98     void update();
99     void moveHandle(Handle handle, const QPointF &deltaMove, HandleStatus handleStatus,
100                     HandleQualifier handleQualifier);
101 
102     IResizable *m_itemResizer = nullptr;
103     QRectF m_rect;
104     QSizeF m_pointSize;
105     QVector<GraphicsHandleItem *> m_points;
106     QPointF m_originalResizePos;
107     QRectF m_originalResizeRect;
108     bool m_showBorder = false;
109     QGraphicsRectItem *m_borderItem = nullptr;
110     Freedom m_freedom = FreedomAny;
111     bool m_isSecondarySelected = false;
112     Handle m_activeHandle = HandleNone;
113 };
114 
115 } // namespace qmt
116