File indexing completed on 2024-04-21 15:02:53

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 
0012 class LegendLayoutAttached : public QObject
0013 {
0014     Q_OBJECT
0015     Q_PROPERTY(qreal minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged)
0016     Q_PROPERTY(qreal preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged)
0017     Q_PROPERTY(qreal maximumWidth READ maximumWidth WRITE setMaximumWidth NOTIFY maximumWidthChanged)
0018 
0019 public:
0020     explicit LegendLayoutAttached(QObject *parent = nullptr);
0021 
0022     qreal minimumWidth() const;
0023     void setMinimumWidth(qreal newMinimumWidth);
0024     Q_SIGNAL void minimumWidthChanged();
0025 
0026     qreal preferredWidth() const;
0027     void setPreferredWidth(qreal newPreferredWidth);
0028     Q_SIGNAL void preferredWidthChanged();
0029 
0030     qreal maximumWidth() const;
0031     void setMaximumWidth(qreal newMaximumWidth);
0032     Q_SIGNAL void maximumWidthChanged();
0033 
0034 private:
0035     qreal m_minimumWidth = -1.0;
0036     qreal m_preferredWidth = -1.0;
0037     qreal m_maximumWidth = -1.0;
0038 };
0039 
0040 class LegendLayout : public QQuickItem
0041 {
0042     Q_OBJECT
0043     Q_PROPERTY(qreal horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing NOTIFY horizontalSpacingChanged)
0044     Q_PROPERTY(qreal verticalSpacing READ verticalSpacing WRITE setVerticalSpacing NOTIFY verticalSpacingChanged)
0045     Q_PROPERTY(qreal preferredWidth READ preferredWidth NOTIFY preferredWidthChanged)
0046 
0047 public:
0048     explicit LegendLayout(QQuickItem *parent = nullptr);
0049 
0050     qreal horizontalSpacing() const;
0051     void setHorizontalSpacing(qreal newHorizontalSpacing);
0052     Q_SIGNAL void horizontalSpacingChanged();
0053 
0054     qreal verticalSpacing() const;
0055     void setVerticalSpacing(qreal newVerticalSpacing);
0056     Q_SIGNAL void verticalSpacingChanged();
0057 
0058     qreal preferredWidth() const;
0059     Q_SIGNAL void preferredWidthChanged();
0060 
0061     static LegendLayoutAttached *qmlAttachedProperties(QObject *object)
0062     {
0063         return new LegendLayoutAttached(object);
0064     }
0065 
0066 protected:
0067     void componentComplete() override;
0068     void updatePolish() override;
0069 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0070     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0071 #else
0072     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0073 #endif
0074     void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override;
0075 
0076 private:
0077     void updateItem(int index, QQuickItem *item);
0078     std::tuple<int, int, qreal, qreal> determineColumns();
0079 
0080     qreal m_horizontalSpacing = 0.0;
0081     qreal m_verticalSpacing = 0.0;
0082     qreal m_preferredWidth = 0.0;
0083 
0084     bool m_completed = false;
0085 };
0086 
0087 QML_DECLARE_TYPEINFO(LegendLayout, QML_HAS_ATTACHED_PROPERTIES)
0088 
0089 #endif // LEGENDLAYOUT_H