File indexing completed on 2024-04-28 15:27:42

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef FORMLAYOUTATTACHED_H
0008 #define FORMLAYOUTATTACHED_H
0009 
0010 #include <QObject>
0011 #include <QQmlEngine>
0012 
0013 class QQuickItem;
0014 
0015 /**
0016  * @brief This attached property contains the information for decorating a FormLayout:
0017  *
0018  * It contains the text labels of fields and information about sections.
0019  *
0020  * Some of its properties can be used with other QtQuick.Layouts.Layout types.
0021  *
0022  * Example usage:
0023  * @include formlayout.qml
0024  *
0025  * @see org::kde::kirigami::FormLayout
0026  * @since org.kde.kirigami 2.3
0027  */
0028 class FormLayoutAttached : public QObject
0029 {
0030     Q_OBJECT
0031     /**
0032      * @brief This property holds the text for the field's label.
0033      */
0034     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
0035 
0036     /**
0037      * @brief This property holds the alignment for the field's label.
0038      */
0039     Q_PROPERTY(int labelAlignment READ labelAlignment WRITE setLabelAlignment NOTIFY labelAlignmentChanged)
0040 
0041     /**
0042      * @brief This property sets whether this field acts as a section separator.
0043      *
0044      * default: ``false``
0045      *
0046      * You can use it in the following ways:
0047      * * As space between two fields:
0048      * @code
0049      * QQC2.TextField {
0050      *     Kirigami.FormData.label: "Label:"
0051      * }
0052      * Item {
0053      *     Kirigami.FormData.isSection: true
0054      * }
0055      * QQC2.TextField {
0056      *     Kirigami.FormData.label: "Label:"
0057      * }
0058      * @endcode
0059      *
0060      * * As space with a section title:
0061      * @code
0062      * QQC2.TextField {
0063      *     Kirigami.FormData.label: "Label:"
0064      * }
0065      * Item {
0066      *     Kirigami.FormData.label: "Section Title"
0067      *     Kirigami.FormData.isSection: true
0068      * }
0069      * QQC2.TextField {
0070      *     Kirigami.FormData.label: "Label text"
0071      * }
0072      * @endcode
0073      *
0074      * * As space with a section title and a separator line:
0075      * @code
0076      * QQC2.TextField {
0077      *     Kirigami.FormData.label: "Label:"
0078      * }
0079      * Kirigami.Separator {
0080      *     Kirigami.FormData.label: "Section Title"
0081      *     Kirigami.FormData.isSection: true
0082      * }
0083      * QQC2.TextField {
0084      *     Kirigami.FormData.label: "Label:"
0085      * }
0086      * @endcode
0087      *
0088      * @see org::kde::kirigami::FormLayout
0089      */
0090     Q_PROPERTY(bool isSection READ isSection WRITE setIsSection NOTIFY isSectionChanged)
0091 
0092     /**
0093      * @brief This property sets whether a checkbox should be added before the field's item.
0094      *
0095      * default: ``false``
0096      */
0097     Q_PROPERTY(bool checkable READ checkable WRITE setCheckable NOTIFY checkableChanged)
0098 
0099     /**
0100      * @brief This property sets whether the checkbox created by the ::checkable
0101      * property should be checked.
0102      *
0103      * default: ``false``
0104      *
0105      * @see ::checkable
0106      */
0107     Q_PROPERTY(bool checked READ checked WRITE setChecked NOTIFY checkedChanged)
0108 
0109     /**
0110      * @brief This property sets whether the label or checkbox created by the
0111      * FormLayout attached property should be enabled.
0112      *
0113      * default: ``true``
0114      */
0115     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
0116 
0117     /**
0118      * This property can only be used
0119      * in conjunction with a Kirigami.FormData.label,
0120      * often in a layout that is a child of a FormLayout.
0121      *
0122      * It then turns the item specified into a "buddy"
0123      * of the label, making it work as if it were
0124      * a child of the org::kde::kirigami::FormLayout.
0125      *
0126      * A buddy item is useful for instance when the label has a keyboard accelerator,
0127      * which when triggered provides active keyboard focus to the buddy item.
0128      *
0129      * @code
0130      * Kirigami.FormLayout {
0131      *     ColumnLayout {
0132      *         // If the accelerator is in the letter S,
0133      *         // pressing Alt+S gives focus to the slider.
0134      *         Kirigami.FormData.label: "Slider label:"
0135      *         Kirigami.FormData.buddyFor: slider
0136      *
0137      *         QQC2.Slider {
0138      *             id: slider
0139      *             from: 0
0140      *             to: 100
0141      *             value: 50
0142      *         }
0143      *     }
0144      * }
0145      * @endcode
0146      */
0147     Q_PROPERTY(QQuickItem *buddyFor READ buddyFor WRITE setBuddyFor NOTIFY buddyForChanged)
0148 
0149 public:
0150     explicit FormLayoutAttached(QObject *parent = nullptr);
0151     ~FormLayoutAttached() override;
0152 
0153     void setLabel(const QString &text);
0154     QString label() const;
0155 
0156     void setIsSection(bool section);
0157     bool isSection() const;
0158 
0159     void setCheckable(bool checkable);
0160     bool checkable() const;
0161 
0162     void setChecked(bool checked);
0163     bool checked() const;
0164 
0165     void setEnabled(bool enabled);
0166     bool enabled() const;
0167 
0168     QQuickItem *buddyFor() const;
0169     void setBuddyFor(QQuickItem *buddyfor);
0170 
0171     int labelAlignment() const;
0172     void setLabelAlignment(int alignment);
0173 
0174     // QML attached property
0175     static FormLayoutAttached *qmlAttachedProperties(QObject *object);
0176 
0177 Q_SIGNALS:
0178     void labelChanged();
0179     void isSectionChanged();
0180     void checkableChanged();
0181     void checkedChanged();
0182     void enabledChanged();
0183     void buddyForChanged();
0184     void labelAlignmentChanged();
0185 
0186 private:
0187     QString m_label;
0188     QString m_actualDecoratedLabel;
0189     QString m_decoratedLabel;
0190     QPointer<QQuickItem> m_buddyFor;
0191     bool m_isSection = false;
0192     bool m_checkable = false;
0193     bool m_checked = false;
0194     bool m_enabled = true;
0195     int m_labelAlignment = 0;
0196 };
0197 
0198 QML_DECLARE_TYPEINFO(FormLayoutAttached, QML_HAS_ATTACHED_PROPERTIES)
0199 
0200 #endif // FORMLAYOUTATTACHED_H