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