File indexing completed on 2024-04-14 03:53:46

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "formlayoutattached.h"
0008 #include "loggingcategory.h"
0009 
0010 #include <QDebug>
0011 #include <QQuickItem>
0012 
0013 FormLayoutAttached::FormLayoutAttached(QObject *parent)
0014     : QObject(parent)
0015 {
0016     m_buddyFor = qobject_cast<QQuickItem *>(parent);
0017     if (!m_buddyFor) {
0018         qWarning(KirigamiLog) << "FormData must be attached to an Item";
0019     }
0020 }
0021 
0022 FormLayoutAttached::~FormLayoutAttached()
0023 {
0024 }
0025 
0026 void FormLayoutAttached::setLabel(const QString &text)
0027 {
0028     if (m_label == text) {
0029         return;
0030     }
0031 
0032     m_label = text;
0033     Q_EMIT labelChanged();
0034 }
0035 
0036 QString FormLayoutAttached::label() const
0037 {
0038     return m_label;
0039 }
0040 
0041 void FormLayoutAttached::setLabelAlignment(int alignment)
0042 {
0043     if (m_labelAlignment == alignment) {
0044         return;
0045     }
0046 
0047     m_labelAlignment = alignment;
0048     Q_EMIT labelAlignmentChanged();
0049 }
0050 
0051 int FormLayoutAttached::labelAlignment() const
0052 {
0053     return m_labelAlignment;
0054 }
0055 
0056 void FormLayoutAttached::setIsSection(bool section)
0057 {
0058     if (m_isSection == section) {
0059         return;
0060     }
0061 
0062     m_isSection = section;
0063     Q_EMIT isSectionChanged();
0064 }
0065 
0066 bool FormLayoutAttached::isSection() const
0067 {
0068     return m_isSection;
0069 }
0070 
0071 QQuickItem *FormLayoutAttached::buddyFor() const
0072 {
0073     return m_buddyFor;
0074 }
0075 
0076 void FormLayoutAttached::setBuddyFor(QQuickItem *aBuddyFor)
0077 {
0078     if (m_buddyFor == aBuddyFor) {
0079         return;
0080     }
0081 
0082     const auto attachee = qobject_cast<QQuickItem *>(parent());
0083 
0084     if (!attachee) {
0085         return;
0086     }
0087 
0088     // TODO: Use ScenePosition or introduce new type for optimized relative
0089     // position calculation to support more nested buddy.
0090 
0091     if (aBuddyFor && aBuddyFor != attachee && aBuddyFor->parentItem() != attachee) {
0092         qWarning(KirigamiLog).nospace() << "FormData.buddyFor must be a direct child of the attachee. Attachee: " << attachee << ", buddyFor: " << aBuddyFor;
0093         return;
0094     }
0095 
0096     if (m_buddyFor) {
0097         disconnect(m_buddyFor, &QObject::destroyed, this, &FormLayoutAttached::resetBuddyFor);
0098     }
0099 
0100     m_buddyFor = aBuddyFor;
0101 
0102     if (m_buddyFor) {
0103         connect(m_buddyFor, &QObject::destroyed, this, &FormLayoutAttached::resetBuddyFor);
0104     }
0105 
0106     Q_EMIT buddyForChanged();
0107 }
0108 
0109 void FormLayoutAttached::resetBuddyFor()
0110 {
0111     const auto attachee = qobject_cast<QQuickItem *>(parent());
0112     setBuddyFor(attachee);
0113 }
0114 
0115 FormLayoutAttached *FormLayoutAttached::qmlAttachedProperties(QObject *object)
0116 {
0117     return new FormLayoutAttached(object);
0118 }
0119 
0120 #include "moc_formlayoutattached.cpp"