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 *>(o: parent);
17 if (!m_buddyFor) {
18 qWarning(catFunc: KirigamiLog) << "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
36QString FormLayoutAttached::label() const
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
51int FormLayoutAttached::labelAlignment() const
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
66bool FormLayoutAttached::isSection() const
67{
68 return m_isSection;
69}
70
71QQuickItem *FormLayoutAttached::buddyFor() const
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 *>(o: 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(catFunc: KirigamiLog).nospace() << "FormData.buddyFor must be a direct child of the attachee. Attachee: " << attachee << ", buddyFor: " << aBuddyFor;
93 return;
94 }
95
96 if (m_buddyFor) {
97 disconnect(sender: m_buddyFor, signal: &QObject::destroyed, receiver: this, slot: &FormLayoutAttached::resetBuddyFor);
98 }
99
100 m_buddyFor = aBuddyFor;
101
102 if (m_buddyFor) {
103 connect(sender: m_buddyFor, signal: &QObject::destroyed, context: this, slot: &FormLayoutAttached::resetBuddyFor);
104 }
105
106 Q_EMIT buddyForChanged();
107}
108
109void FormLayoutAttached::resetBuddyFor()
110{
111 const auto attachee = qobject_cast<QQuickItem *>(o: parent());
112 setBuddyFor(attachee);
113}
114
115FormLayoutAttached *FormLayoutAttached::qmlAttachedProperties(QObject *object)
116{
117 return new FormLayoutAttached(object);
118}
119
120#include "moc_formlayoutattached.cpp"
121

source code of kirigami/src/formlayoutattached.cpp