| 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 | |
| 13 | FormLayoutAttached::FormLayoutAttached(QObject *parent) |
| 14 | : QObject(parent) |
| 15 | { |
| 16 | m_buddyFor = qobject_cast<QQuickItem *>(o: parent); |
| 17 | if (!m_buddyFor) { |
| 18 | qWarning(catFunc: KirigamiLayoutsLog) << "FormData must be attached to an Item"; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | FormLayoutAttached::~FormLayoutAttached() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void 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 | |
| 36 | QString FormLayoutAttached::label() const |
| 37 | { |
| 38 | return m_label; |
| 39 | } |
| 40 | |
| 41 | void FormLayoutAttached::setLabelAlignment(int alignment) |
| 42 | { |
| 43 | if (m_labelAlignment == alignment) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | m_labelAlignment = alignment; |
| 48 | Q_EMIT labelAlignmentChanged(); |
| 49 | } |
| 50 | |
| 51 | int FormLayoutAttached::labelAlignment() const |
| 52 | { |
| 53 | return m_labelAlignment; |
| 54 | } |
| 55 | |
| 56 | void FormLayoutAttached::setIsSection(bool section) |
| 57 | { |
| 58 | if (m_isSection == section) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | m_isSection = section; |
| 63 | Q_EMIT isSectionChanged(); |
| 64 | } |
| 65 | |
| 66 | bool FormLayoutAttached::isSection() const |
| 67 | { |
| 68 | return m_isSection; |
| 69 | } |
| 70 | |
| 71 | QQuickItem *FormLayoutAttached::buddyFor() const |
| 72 | { |
| 73 | return m_buddyFor; |
| 74 | } |
| 75 | |
| 76 | void 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: 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(sender: m_buddyFor, signal: &QObject::destroyed, receiver: this, slot: &FormLayoutAttached::resetBuddyFor); |
| 99 | } |
| 100 | |
| 101 | m_buddyFor = aBuddyFor; |
| 102 | |
| 103 | if (m_buddyFor) { |
| 104 | connect(sender: m_buddyFor, signal: &QObject::destroyed, context: this, slot: &FormLayoutAttached::resetBuddyFor); |
| 105 | } |
| 106 | |
| 107 | Q_EMIT buddyForChanged(); |
| 108 | } |
| 109 | |
| 110 | void FormLayoutAttached::resetBuddyFor() |
| 111 | { |
| 112 | const auto attachee = qobject_cast<QQuickItem *>(o: parent()); |
| 113 | setBuddyFor(attachee); |
| 114 | } |
| 115 | |
| 116 | FormLayoutAttached *FormLayoutAttached::qmlAttachedProperties(QObject *object) |
| 117 | { |
| 118 | return new FormLayoutAttached(object); |
| 119 | } |
| 120 | |
| 121 | #include "moc_formlayoutattached.cpp" |
| 122 |
