Warning, file /frameworks/kquickcharts/src/decorations/AxisLabels.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 
0016 class ChartDataSource;
0017 class ItemBuilder;
0018 
0019 class AxisLabels;
0020 
0021 class AxisLabelsAttached : public QObject
0022 {
0023     Q_OBJECT
0024     Q_PROPERTY(int index READ index NOTIFY indexChanged)
0025     Q_PROPERTY(QString label READ label NOTIFY labelChanged)
0026 
0027 public:
0028     explicit AxisLabelsAttached(QObject *parent = nullptr);
0029 
0030     int index() const;
0031     void setIndex(int newIndex);
0032     Q_SIGNAL void indexChanged();
0033 
0034     QString label() const;
0035     void setLabel(const QString &newLabel);
0036     Q_SIGNAL void labelChanged();
0037 
0038 private:
0039     int m_index = -1;
0040     QString m_label;
0041 };
0042 
0043 /**
0044  * An item that uses a delegate to place axis labels on a chart.
0045  */
0046 class AxisLabels : public QQuickItem
0047 {
0048     Q_OBJECT
0049     Q_PROPERTY(AxisLabels::Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
0050     Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
0051     Q_PROPERTY(ChartDataSource *source READ source WRITE setSource NOTIFY sourceChanged)
0052     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
0053     Q_PROPERTY(bool constrainToBounds READ constrainToBounds WRITE setConstrainToBounds NOTIFY constrainToBoundsChanged)
0054 
0055 public:
0056     enum class Direction { HorizontalLeftRight, HorizontalRightLeft, VerticalTopBottom, VerticalBottomTop };
0057     Q_ENUM(Direction)
0058 
0059     explicit AxisLabels(QQuickItem *parent = nullptr);
0060     ~AxisLabels() override;
0061 
0062     AxisLabels::Direction direction() const;
0063     Q_SLOT void setDirection(AxisLabels::Direction newDirection);
0064     Q_SIGNAL void directionChanged();
0065 
0066     QQmlComponent *delegate() const;
0067     Q_SLOT void setDelegate(QQmlComponent *newDelegate);
0068     Q_SIGNAL void delegateChanged();
0069 
0070     ChartDataSource *source() const;
0071     Q_SLOT void setSource(ChartDataSource *newSource);
0072     Q_SIGNAL void sourceChanged();
0073 
0074     Qt::Alignment alignment() const;
0075     Q_SLOT void setAlignment(Qt::Alignment newAlignment);
0076     Q_SIGNAL void alignmentChanged();
0077 
0078     bool constrainToBounds() const;
0079     Q_SLOT void setConstrainToBounds(bool newConstrainToBounds);
0080     Q_SIGNAL void constrainToBoundsChanged();
0081 
0082     static AxisLabelsAttached *qmlAttachedProperties(QObject *object)
0083     {
0084         return new AxisLabelsAttached(object);
0085     }
0086 
0087 protected:
0088 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0089     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0090 #else
0091     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0092 #endif
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 QML_DECLARE_TYPEINFO(AxisLabels, QML_HAS_ATTACHED_PROPERTIES)
0111 
0112 #endif // AXISLABELS_H