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

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #ifndef AXISLABELS_H
0009 #define AXISLABELS_H
0010 
0011 #include <memory>
0012 
0013 #include <QQuickItem>
0014 #include <Qt>
0015 #include <qqmlregistration.h>
0016 
0017 class ChartDataSource;
0018 class ItemBuilder;
0019 
0020 class AxisLabels;
0021 
0022 class AxisLabelsAttached : public QObject
0023 {
0024     Q_OBJECT
0025     QML_ANONYMOUS
0026 
0027 public:
0028     explicit AxisLabelsAttached(QObject *parent = nullptr);
0029 
0030     Q_PROPERTY(int index READ index NOTIFY indexChanged)
0031     int index() const;
0032     void setIndex(int newIndex);
0033     Q_SIGNAL void indexChanged();
0034 
0035     Q_PROPERTY(QString label READ label NOTIFY labelChanged)
0036     QString label() const;
0037     void setLabel(const QString &newLabel);
0038     Q_SIGNAL void labelChanged();
0039 
0040 private:
0041     int m_index = -1;
0042     QString m_label;
0043 };
0044 
0045 /**
0046  * An item that uses a delegate to place axis labels on a chart.
0047  */
0048 class AxisLabels : public QQuickItem
0049 {
0050     Q_OBJECT
0051     QML_ELEMENT
0052     QML_ATTACHED(AxisLabelsAttached)
0053 
0054 public:
0055     enum class Direction { HorizontalLeftRight, HorizontalRightLeft, VerticalTopBottom, VerticalBottomTop };
0056     Q_ENUM(Direction)
0057 
0058     explicit AxisLabels(QQuickItem *parent = nullptr);
0059     ~AxisLabels() override;
0060 
0061     Q_PROPERTY(AxisLabels::Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
0062     AxisLabels::Direction direction() const;
0063     Q_SLOT void setDirection(AxisLabels::Direction newDirection);
0064     Q_SIGNAL void directionChanged();
0065 
0066     Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
0067     QQmlComponent *delegate() const;
0068     Q_SLOT void setDelegate(QQmlComponent *newDelegate);
0069     Q_SIGNAL void delegateChanged();
0070 
0071     Q_PROPERTY(ChartDataSource *source READ source WRITE setSource NOTIFY sourceChanged)
0072     ChartDataSource *source() const;
0073     Q_SLOT void setSource(ChartDataSource *newSource);
0074     Q_SIGNAL void sourceChanged();
0075 
0076     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
0077     Qt::Alignment alignment() const;
0078     Q_SLOT void setAlignment(Qt::Alignment newAlignment);
0079     Q_SIGNAL void alignmentChanged();
0080 
0081     Q_PROPERTY(bool constrainToBounds READ constrainToBounds WRITE setConstrainToBounds NOTIFY constrainToBoundsChanged)
0082     bool constrainToBounds() const;
0083     Q_SLOT void setConstrainToBounds(bool newConstrainToBounds);
0084     Q_SIGNAL void constrainToBoundsChanged();
0085 
0086     static AxisLabelsAttached *qmlAttachedProperties(QObject *object)
0087     {
0088         return new AxisLabelsAttached(object);
0089     }
0090 
0091 protected:
0092     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0093 
0094 private:
0095     void scheduleLayout();
0096     bool isHorizontal();
0097     void updateLabels();
0098     void layout();
0099     void onBeginCreate(int index, QQuickItem *item);
0100 
0101     Direction m_direction = Direction::HorizontalLeftRight;
0102     ChartDataSource *m_source = nullptr;
0103     Qt::Alignment m_alignment = Qt::AlignHCenter | Qt::AlignVCenter;
0104     bool m_constrainToBounds = true;
0105 
0106     std::unique_ptr<ItemBuilder> m_itemBuilder;
0107     bool m_layoutScheduled = false;
0108 };
0109 
0110 #endif // AXISLABELS_H