File indexing completed on 2024-05-12 04:20:31

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef __KCHART_ATTRIBUTES_MODEL_H__
0010 #define __KCHART_ATTRIBUTES_MODEL_H__
0011 
0012 #include "KChartAbstractProxyModel.h"
0013 #include <QMap>
0014 #include <QVariant>
0015 
0016 #include "KChartGlobal.h"
0017 
0018 namespace KChart {
0019 
0020 /**
0021   * @brief A proxy model used for decorating data with attributes.
0022   *
0023   *        An AttributesModel forwards data from and to the source model and adds attributes,
0024   *        data that influences the graphical rendering of the source model data.
0025   *        The attributes are distinguished from the source model's data by their @p role values.
0026   *        Therefore this class does not need to, and does not, change the data layout from the
0027   *        source model's; indexes that refer to the same data have the same row and column
0028   *        values in both models.
0029   *        Attribute changes, that is changes to data with the attribute role, via the interface
0030   *        of this class (including setData()) are stored internally and not forwarded to the source model.
0031   */
0032 class KCHART_EXPORT AttributesModel : public AbstractProxyModel
0033 {
0034     Q_OBJECT
0035     KCHART_DECLARE_PRIVATE_BASE_POLYMORPHIC( AttributesModel )
0036 public:
0037     enum PaletteType {
0038         PaletteTypeDefault = 0,
0039         PaletteTypeRainbow = 1,
0040         PaletteTypeSubdued = 2
0041     };
0042 
0043     explicit AttributesModel( QAbstractItemModel* model, QObject * parent = nullptr );
0044     ~AttributesModel() override;
0045 
0046     /** Copies the internal data (maps and palette) of another
0047      *  AttributesModel* into this one.
0048      */
0049     void initFrom( const AttributesModel* other );
0050 
0051     /** Returns true if both, all of the attributes set, and
0052      * the palette set is equal in both of the AttributeModels.
0053      */
0054     bool compare( const AttributesModel* other ) const;
0055 
0056     bool compareAttributes( int role, const QVariant& a, const QVariant& b ) const;
0057 
0058     /* Attributes Model specific API */
0059     bool setModelData( const QVariant value, int role );
0060     QVariant modelData( int role ) const;
0061 
0062     /** Returns whether the given role corresponds to one of the known
0063      * internally used ones. */
0064     bool isKnownAttributesRole( int role ) const;
0065 
0066     /** Sets the palettetype used by this attributesmodel */
0067     void setPaletteType( PaletteType type );
0068     PaletteType paletteType() const;
0069 
0070     /** Returns the data that were specified at global level,
0071       * or the default data, or QVariant().
0072       */
0073     QVariant data(int role) const;
0074 
0075     /** Returns the data that were specified at per column level,
0076       * or the globally set data, or the default data, or QVariant().
0077       */
0078     QVariant data(int column, int role) const;
0079 
0080     /** \reimpl */
0081     QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
0082     /** \reimpl */
0083     int rowCount(const QModelIndex& ) const override;
0084     /** \reimpl */
0085     int columnCount(const QModelIndex& ) const override;
0086     /** \reimpl */
0087     QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const override;
0088     /** \reimpl */
0089     bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::DisplayRole) override;
0090     /** Remove any explicit attributes settings that might have been specified before. */
0091     bool resetData ( const QModelIndex & index, int role = Qt::DisplayRole);
0092     /** \reimpl */
0093     bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value,
0094                          int role = Qt::DisplayRole) override;
0095     /** Returns default values for the header data. */
0096     virtual QVariant defaultHeaderData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
0097     /** Remove any explicit attributes settings that might have been specified before. */
0098     bool resetHeaderData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole);
0099     /** \reimpl */
0100     void setSourceModel ( QAbstractItemModel* sourceModel ) override;
0101 
0102     /** Define the default value for a certain role.
0103         Passing a default-constructed QVariant is equivalent to removing the default. */
0104     void setDefaultForRole( int role, const QVariant& value );
0105 
0106     /** Set the dimension of the dataset in the source model. \sa AbstractDiagram::setDatasetDimension */
0107     void setDatasetDimension( int dimension );
0108     int datasetDimension() const;
0109 
0110 Q_SIGNALS:
0111     void attributesChanged( const QModelIndex&, const QModelIndex& );
0112 
0113 private Q_SLOTS:
0114     void slotRowsAboutToBeInserted( const QModelIndex& parent, int start, int end );
0115     void slotColumnsAboutToBeInserted( const QModelIndex& parent, int start, int end );
0116     void slotRowsInserted( const QModelIndex& parent, int start, int end );
0117     void slotColumnsInserted( const QModelIndex& parent, int start, int end );
0118 
0119     void slotRowsAboutToBeRemoved( const QModelIndex& parent, int start, int end );
0120     void slotColumnsAboutToBeRemoved( const QModelIndex& parent, int start, int end );
0121     void slotRowsRemoved( const QModelIndex& parent, int start, int end );
0122     void slotColumnsRemoved( const QModelIndex& parent, int start, int end );
0123 
0124     void slotDataChanged( const QModelIndex& topLeft, const QModelIndex& bottomRight );
0125 
0126 private:
0127     // helper
0128     QVariant defaultsForRole( int role ) const;
0129     bool compareHeaderDataMaps( const QMap< int, QMap< int, QVariant > >& mapA,
0130                                 const QMap< int, QMap< int, QVariant > >& mapB ) const;
0131 
0132     void removeEntriesFromDataMap( int start, int end );
0133     void removeEntriesFromDirectionDataMaps( Qt::Orientation dir, int start, int end );
0134 };
0135 
0136 }
0137 
0138 #endif