DtkWidget 5.6.0.2
DTK Widget module
danchors.h
1// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#ifndef DANCHORS_H
6#define DANCHORS_H
7
8
9#include <QObject>
10#include <QPointer>
11#include <QResizeEvent>
12#include <QMoveEvent>
13#include <QWidget>
14#include <QDebug>
15
16#include <dtkwidget_global.h>
17
18DWIDGET_BEGIN_NAMESPACE
19
20class DAnchorsBase;
23 base(b),
24 type(t)
25 {
26 }
27
28 DAnchorsBase *base;
29 Qt::AnchorPoint type;
30 const DAnchorInfo *targetInfo = NULL;
31
32 bool operator==(const DAnchorInfo *info) const
33 {
34 return info == targetInfo;
35 }
36
37 bool operator==(const DAnchorInfo &info) const
38 {
39 return &info == targetInfo;
40 }
41
42 bool operator!=(const DAnchorInfo *info) const
43 {
44 return info != targetInfo;
45 }
46
47 bool operator!=(const DAnchorInfo &info) const
48 {
49 return &info != targetInfo;
50 }
51
52 const DAnchorInfo &operator=(const DAnchorInfo *info)
53 {
54 targetInfo = info;
55
56 return *this;
57 }
58};
59
60class DAnchorsBasePrivate;
61class DEnhancedWidget;
62class DAnchorsBase : public QObject
63{
64 Q_OBJECT
65
66 Q_PROPERTY(QWidget *target READ target CONSTANT)
67 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
68 Q_PROPERTY(const DAnchorsBase *anchors READ anchors)
69 Q_PROPERTY(const DAnchorInfo *top READ top WRITE setTop NOTIFY topChanged)
70 Q_PROPERTY(const DAnchorInfo *bottom READ bottom WRITE setBottom NOTIFY bottomChanged)
71 Q_PROPERTY(const DAnchorInfo *left READ left WRITE setLeft NOTIFY leftChanged)
72 Q_PROPERTY(const DAnchorInfo *right READ right WRITE setRight NOTIFY rightChanged)
73 Q_PROPERTY(const DAnchorInfo *horizontalCenter READ horizontalCenter WRITE setHorizontalCenter NOTIFY horizontalCenterChanged)
74 Q_PROPERTY(const DAnchorInfo *verticalCenter READ verticalCenter WRITE setVerticalCenter NOTIFY verticalCenterChanged)
75 Q_PROPERTY(QWidget *fill READ fill WRITE setFill NOTIFY fillChanged)
76 Q_PROPERTY(QWidget *centerIn READ centerIn WRITE setCenterIn NOTIFY centerInChanged)
77 Q_PROPERTY(int margins READ margins WRITE setMargins NOTIFY marginsChanged)
78 Q_PROPERTY(int topMargin READ topMargin WRITE setTopMargin NOTIFY topMarginChanged)
79 Q_PROPERTY(int bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY bottomMarginChanged)
80 Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin NOTIFY leftMarginChanged)
81 Q_PROPERTY(int rightMargin READ rightMargin WRITE setRightMargin NOTIFY rightMarginChanged)
82 Q_PROPERTY(int horizontalCenterOffset READ horizontalCenterOffset WRITE setHorizontalCenterOffset NOTIFY horizontalCenterOffsetChanged)
83 Q_PROPERTY(int verticalCenterOffset READ verticalCenterOffset WRITE setVerticalCenterOffset NOTIFY verticalCenterOffsetChanged)
84 Q_PROPERTY(bool alignWhenCentered READ alignWhenCentered WRITE setAlignWhenCentered NOTIFY alignWhenCenteredChanged)
85
86public:
87 explicit DAnchorsBase(QWidget *w);
89
90 enum AnchorError {
91 NoError,
92 Conflict,
93 TargetInvalid,
94 PointInvalid,
95 LoopBind
96 };
97
98 QWidget *target() const;
99 DEnhancedWidget *enhancedWidget() const;
100 bool enabled() const;
101 const DAnchorsBase *anchors() const;
102 const DAnchorInfo *top() const;
103 const DAnchorInfo *bottom() const;
104 const DAnchorInfo *left() const;
105 const DAnchorInfo *right() const;
106 const DAnchorInfo *horizontalCenter() const;
107 const DAnchorInfo *verticalCenter() const;
108 QWidget *fill() const;
109 QWidget *centerIn() const;
110 int margins() const;
111 int topMargin() const;
112 int bottomMargin() const;
113 int leftMargin() const;
114 int rightMargin() const;
115 int horizontalCenterOffset() const;
116 int verticalCenterOffset() const;
117 int alignWhenCentered() const;
118 AnchorError errorCode() const;
119 QString errorString() const;
120 bool isBinding(const DAnchorInfo *info) const;
121
122 static bool setAnchor(QWidget *w, const Qt::AnchorPoint &p, QWidget *target, const Qt::AnchorPoint &point);
123 static void clearAnchors(const QWidget *w);
124 static DAnchorsBase *getAnchorBaseByWidget(const QWidget *w);
125
126public Q_SLOTS:
127 void setEnabled(bool enabled);
128 bool setAnchor(const Qt::AnchorPoint &p, QWidget *target, const Qt::AnchorPoint &point);
129 bool setTop(const DAnchorInfo *top);
130 bool setBottom(const DAnchorInfo *bottom);
131 bool setLeft(const DAnchorInfo *left);
132 bool setRight(const DAnchorInfo *right);
133 bool setHorizontalCenter(const DAnchorInfo *horizontalCenter);
134 bool setVerticalCenter(const DAnchorInfo *verticalCenter);
135 bool setFill(QWidget *fill);
136 bool setCenterIn(QWidget *centerIn);
137 bool setFill(DAnchorsBase *fill);
138 bool setCenterIn(DAnchorsBase *centerIn);
139 void setMargins(int margins);
140 void setTopMargin(int topMargin);
141 void setBottomMargin(int bottomMargin);
142 void setLeftMargin(int leftMargin);
143 void setRightMargin(int rightMargin);
144 void setHorizontalCenterOffset(int horizontalCenterOffset);
145 void setVerticalCenterOffset(int verticalCenterOffset);
146 void setAlignWhenCentered(bool alignWhenCentered);
147
148 void setTop(int arg, Qt::AnchorPoint point);
149 void setBottom(int arg, Qt::AnchorPoint point);
150 void setLeft(int arg, Qt::AnchorPoint point);
151 void setRight(int arg, Qt::AnchorPoint point);
152 void setHorizontalCenter(int arg, Qt::AnchorPoint point);
153 void setVerticalCenter(int arg, Qt::AnchorPoint point);
154
155 void moveTop(int arg);
156 void moveBottom(int arg);
157 void moveLeft(int arg);
158 void moveRight(int arg);
159 void moveHorizontalCenter(int arg);
160 void moveVerticalCenter(int arg);
161 void moveCenter(const QPoint &arg);
162
163private Q_SLOTS:
164 void updateVertical();
165 void updateHorizontal();
166 void updateFill();
167 void updateCenterIn();
168
169Q_SIGNALS:
170 void enabledChanged(bool enabled);
171 void topChanged(const DAnchorInfo *top);
172 void bottomChanged(const DAnchorInfo *bottom);
173 void leftChanged(const DAnchorInfo *left);
174 void rightChanged(const DAnchorInfo *right);
175 void horizontalCenterChanged(const DAnchorInfo *horizontalCenter);
176 void verticalCenterChanged(const DAnchorInfo *verticalCenter);
177 void fillChanged(QWidget *fill);
178 void centerInChanged(QWidget *centerIn);
179 void marginsChanged(int margins);
180 void topMarginChanged(int topMargin);
181 void bottomMarginChanged(int bottomMargin);
182 void leftMarginChanged(int leftMargin);
183 void rightMarginChanged(int rightMargin);
184 void horizontalCenterOffsetChanged(int horizontalCenterOffset);
185 void verticalCenterOffsetChanged(int verticalCenterOffset);
186 void alignWhenCenteredChanged(bool alignWhenCentered);
187
188protected:
189 void init(QWidget *w);
190
191private:
192 DAnchorsBase(QWidget *w, bool);
193
195
196 Q_DECLARE_PRIVATE(DAnchorsBase)
197};
198
199template<class T>
200class DAnchors : public DAnchorsBase
201{
202public:
203 inline DAnchors(): DAnchorsBase((QWidget*)NULL), m_widget(NULL) {}
204 inline DAnchors(T *w): DAnchorsBase(w), m_widget(w) {}
205 inline DAnchors(const DAnchors &me): DAnchorsBase(me.m_widget), m_widget(me.m_widget) {}
206
207 inline T &operator=(const DAnchors &me)
208 {
209 m_widget = me.m_widget;
210 init(m_widget);
211 return *m_widget;
212 }
213 inline T &operator=(T *w)
214 {
215 m_widget = w;
216 init(w);
217 return *m_widget;
218 }
219 inline T *widget() const
220 {
221 return m_widget;
222 }
223 inline T *operator ->() const
224 {
225 return m_widget;
226 }
227 inline T &operator *() const
228 {
229 return *m_widget;
230 }
231 inline operator T *() const
232 {
233 return m_widget;
234 }
235 inline operator T &() const
236 {
237 return *m_widget;
238 }
239
240private:
241 T *m_widget;
242};
243
244DWIDGET_END_NAMESPACE
245
246#endif // DANCHORS_H
DAnchorsBase 提供了一种指定 QWidget 与其它 QWidget 之间的关系来确定 其位置的方法.
Definition: danchors.h:63
void verticalCenterChanged(const DAnchorInfo *verticalCenter)
信号会在 verticalCenter 属性的值改变时被发送
void enabledChanged(bool enabled)
信号会在 enabled 属性的值改变时被发送
void horizontalCenterOffsetChanged(int horizontalCenterOffset)
信号会在 horizontalCenterOffset 属性的值改变时被发送
void rightChanged(const DAnchorInfo *right)
信号会在 right 属性的值改变时被发送
void bottomChanged(const DAnchorInfo *bottom)
信号会在 bottom 属性的值改变时被发送
void leftMarginChanged(int leftMargin)
信号会在 leftMargin 属性的值改变时被发送
void centerInChanged(QWidget *centerIn)
信号会在 centerIn 属性的值改变时被发送
void fillChanged(QWidget *fill)
信号会在 fill 属性的值改变时被发送
void topMarginChanged(int topMargin)
信号会在 topMargin 属性的值改变时被发送
void leftChanged(const DAnchorInfo *left)
信号会在 left 属性的值改变时被发送
void rightMarginChanged(int rightMargin)
信号会在 rightMargin 属性的值改变时被发送
void horizontalCenterChanged(const DAnchorInfo *horizontalCenter)
信号会在 horizontalCenter 属性的值改变时被发送
void marginsChanged(int margins)
信号会在 margins 属性的值改变时被发送
void verticalCenterOffsetChanged(int verticalCenterOffset)
信号会在 verticalCenterOffset 属性的值改变时被发送
void bottomMarginChanged(int bottomMargin)
信号会在 bottomMargin 属性的值改变时被发送
void topChanged(const DAnchorInfo *top)
信号会在 top 属性的值改变时被发送
DAnchors 是一个模板类,在 DAnchorsBase 的基础上保存了一个控件指针, 将控件和锚定绑定在一起使用,相当于把“锚线”属性附加到了控件本身.
Definition: danchors.h:201
Definition: denhancedwidget.h:16
AnchorPoint
DAnchorInfo 用于记录“锚线”的锚定信息:被锚定的 DAnchorsBase 对象、 锚定的类型、目标“锚线”的信息.
Definition: danchors.h:21