1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
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 #pragma once
26 
27 #include "formeditoritem.h"
28 
29 QT_BEGIN_NAMESPACE
30 class QLineF;
31 QT_END_NAMESPACE
32 
33 namespace QmlDesigner {
34 
35 class Snapper
36 {
37 public:
38     enum Snapping {
39         UseSnapping,
40         UseSnappingAndAnchoring,
41         NoSnapping
42     };
43 
44     Snapper();
45 
46     void setContainerFormEditorItem(FormEditorItem *formEditorItem);
47     FormEditorItem *containerFormEditorItem() const;
48 
49     void setTransformtionSpaceFormEditorItem(FormEditorItem *formEditorItem);
50     FormEditorItem *transformtionSpaceFormEditorItem() const;
51 
52     double snappedVerticalOffset(const QRectF &boundingRect) const;
53     double snappedHorizontalOffset(const QRectF &boundingRect) const;
54 
55     double snapTopOffset(const QRectF &boundingRect) const;
56     double snapRightOffset(const QRectF &boundingRect) const;
57     double snapLeftOffset(const QRectF &boundingRect) const;
58     double snapBottomOffset(const QRectF &boundingRect) const;
59 
60     QList<QLineF> horizontalSnappedLines(const QRectF &boundingRect, QList<QRectF> *boundingRects = nullptr) const;
61     QList<QLineF> verticalSnappedLines(const QRectF &boundingRect, QList<QRectF> *boundingRects = nullptr) const;
62 
63     void setSnappingDistance(double snappingDistance);
64     double snappingDistance() const;
65 
66     void updateSnappingLines(const QList<FormEditorItem*> &exceptionList);
67     void updateSnappingLines(FormEditorItem* exceptionItem);
68 
69     QList<QGraphicsItem*> generateSnappingLines(const QList<QRectF> &boundingRectList,
70                                                 QGraphicsItem *layerItem,
71                                                 const QTransform &transform);
72     QList<QGraphicsItem*> generateSnappingLines(const QRectF &boundingRect,
73                                                 QGraphicsItem *layerItem,
74                                                 const QTransform &transform);
75 
76     void adjustAnchoringOfItem(FormEditorItem *formEditorItem);
77 
78 protected:
79     double snappedOffsetForLines(const SnapLineMap &snappingLineMap,
80                          double value) const;
81 
82     double snappedOffsetForOffsetLines(const SnapLineMap &snappingOffsetMap,
83                          Qt::Orientation orientation,
84                          double value,
85                          double lowerLimit,
86                          double upperLimit) const;
87 
88     QList<QLineF> findSnappingLines(const SnapLineMap &snappingLineMap,
89                          Qt::Orientation orientation,
90                          double snapLine,
91                          double lowerLimit,
92                          double upperLimit,
93                          QList<QRectF> *boundingRects = nullptr) const;
94 
95     QList<QLineF> findSnappingOffsetLines(const SnapLineMap &snappingOffsetMap,
96                                     Qt::Orientation orientation,
97                                     double snapLine,
98                                     double lowerLimit,
99                                     double upperLimit,
100                                     QList<QRectF> *boundingRects = nullptr) const;
101 
102     QLineF createSnapLine(Qt::Orientation orientation,
103                          double snapLine,
104                          double lowerEdge,
105                          double upperEdge,
106                          const QRectF &itemRect) const;
107 
108 //    bool canSnap(QList<double> snappingLineList, double value) const;
109 private:
110     FormEditorItem *m_containerFormEditorItem;
111     FormEditorItem *m_transformtionSpaceFormEditorItem;
112     double m_snappingDistance;
113 };
114 
115 } // namespace QmlDesigner
116