File indexing completed on 2024-04-21 03:56:42

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #ifndef LEGENDLAYOUT_H
0008 #define LEGENDLAYOUT_H
0009 
0010 #include <QQuickItem>
0011 #include <qqmlregistration.h>
0012 
0013 class LegendLayoutAttached : public QObject
0014 {
0015     Q_OBJECT
0016     QML_ANONYMOUS
0017 
0018 public:
0019     explicit LegendLayoutAttached(QObject *parent = nullptr);
0020 
0021     Q_PROPERTY(qreal minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged)
0022     qreal minimumWidth() const;
0023     void setMinimumWidth(qreal newMinimumWidth);
0024     Q_SIGNAL void minimumWidthChanged();
0025     bool isMinimumWidthValid() const;
0026 
0027     Q_PROPERTY(qreal preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged)
0028     qreal preferredWidth() const;
0029     void setPreferredWidth(qreal newPreferredWidth);
0030     Q_SIGNAL void preferredWidthChanged();
0031     bool isPreferredWidthValid() const;
0032 
0033     Q_PROPERTY(qreal maximumWidth READ maximumWidth WRITE setMaximumWidth NOTIFY maximumWidthChanged)
0034     qreal maximumWidth() const;
0035     void setMaximumWidth(qreal newMaximumWidth);
0036     Q_SIGNAL void maximumWidthChanged();
0037     bool isMaximumWidthValid() const;
0038 
0039 private:
0040     std::optional<qreal> m_minimumWidth;
0041     std::optional<qreal> m_preferredWidth;
0042     std::optional<qreal> m_maximumWidth;
0043 };
0044 
0045 class LegendLayout : public QQuickItem
0046 {
0047     Q_OBJECT
0048     QML_ELEMENT
0049     QML_ATTACHED(LegendLayoutAttached)
0050 
0051 public:
0052     explicit LegendLayout(QQuickItem *parent = nullptr);
0053 
0054     Q_PROPERTY(qreal horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing NOTIFY horizontalSpacingChanged)
0055     qreal horizontalSpacing() const;
0056     void setHorizontalSpacing(qreal newHorizontalSpacing);
0057     Q_SIGNAL void horizontalSpacingChanged();
0058 
0059     Q_PROPERTY(qreal verticalSpacing READ verticalSpacing WRITE setVerticalSpacing NOTIFY verticalSpacingChanged)
0060     qreal verticalSpacing() const;
0061     void setVerticalSpacing(qreal newVerticalSpacing);
0062     Q_SIGNAL void verticalSpacingChanged();
0063 
0064     Q_PROPERTY(qreal preferredWidth READ preferredWidth NOTIFY preferredWidthChanged)
0065     qreal preferredWidth() const;
0066     Q_SIGNAL void preferredWidthChanged();
0067 
0068     static LegendLayoutAttached *qmlAttachedProperties(QObject *object)
0069     {
0070         return new LegendLayoutAttached(object);
0071     }
0072 
0073 protected:
0074     void componentComplete() override;
0075     void updatePolish() override;
0076     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0077     void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override;
0078 
0079 private:
0080     void updateItem(int index, QQuickItem *item);
0081     std::tuple<int, int, qreal, qreal> determineColumns();
0082 
0083     qreal m_horizontalSpacing = 0.0;
0084     qreal m_verticalSpacing = 0.0;
0085     qreal m_preferredWidth = 0.0;
0086 
0087     bool m_completed = false;
0088 };
0089 
0090 #endif // LEGENDLAYOUT_H