Kirigami2

formlayoutattached.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "formlayoutattached.h"
8#include "loggingcategory.h"
9
10#include <QDebug>
11#include <QQuickItem>
12
13FormLayoutAttached::FormLayoutAttached(QObject *parent)
14 : QObject(parent)
15{
16 m_buddyFor = qobject_cast<QQuickItem *>(parent);
17 if (!m_buddyFor) {
18 qWarning(KirigamiLayoutsLog) << "FormData must be attached to an Item";
19 }
20}
21
22FormLayoutAttached::~FormLayoutAttached()
23{
24}
25
26void FormLayoutAttached::setLabel(const QString &text)
27{
28 if (m_label == text) {
29 return;
30 }
31
32 m_label = text;
33 Q_EMIT labelChanged();
34}
35
37{
38 return m_label;
39}
40
41void FormLayoutAttached::setLabelAlignment(int alignment)
42{
43 if (m_labelAlignment == alignment) {
44 return;
45 }
46
47 m_labelAlignment = alignment;
48 Q_EMIT labelAlignmentChanged();
49}
50
52{
53 return m_labelAlignment;
54}
55
56void FormLayoutAttached::setIsSection(bool section)
57{
58 if (m_isSection == section) {
59 return;
60 }
61
62 m_isSection = section;
63 Q_EMIT isSectionChanged();
64}
65
67{
68 return m_isSection;
69}
70
72{
73 return m_buddyFor;
74}
75
76void FormLayoutAttached::setBuddyFor(QQuickItem *aBuddyFor)
77{
78 if (m_buddyFor == aBuddyFor) {
79 return;
80 }
81
82 const auto attachee = qobject_cast<QQuickItem *>(parent());
83
84 if (!attachee) {
85 return;
86 }
87
88 // TODO: Use ScenePosition or introduce new type for optimized relative
89 // position calculation to support more nested buddy.
90
91 if (aBuddyFor && aBuddyFor != attachee && aBuddyFor->parentItem() != attachee) {
92 qWarning(KirigamiLayoutsLog).nospace() << "FormData.buddyFor must be a direct child of the attachee. Attachee: " << attachee
93 << ", buddyFor: " << aBuddyFor;
94 return;
95 }
96
97 if (m_buddyFor) {
98 disconnect(m_buddyFor, &QObject::destroyed, this, &FormLayoutAttached::resetBuddyFor);
99 }
100
101 m_buddyFor = aBuddyFor;
102
103 if (m_buddyFor) {
104 connect(m_buddyFor, &QObject::destroyed, this, &FormLayoutAttached::resetBuddyFor);
105 }
106
107 Q_EMIT buddyForChanged();
108}
109
110void FormLayoutAttached::resetBuddyFor()
111{
112 const auto attachee = qobject_cast<QQuickItem *>(parent());
113 setBuddyFor(attachee);
114}
115
116FormLayoutAttached *FormLayoutAttached::qmlAttachedProperties(QObject *object)
117{
118 return new FormLayoutAttached(object);
119}
120
121#include "moc_formlayoutattached.cpp"
This attached property contains the information for decorating a org::kde::kirigami::FormLayout:
QString label
The label for a org::kde::kirigami::FormLayout field.
QQuickItem * buddyFor
This property can only be used in conjunction with a Kirigami.FormData.label, often in a layout that ...
int labelAlignment
The alignment for the label of a org::kde::kirigami::FormLayout field.
bool isSection
If true, the child item of a org::kde::kirigami::FormLayout becomes a section separator,...
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
QQuickItem * parentItem() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 11:59:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.