KChart

KChartAttributesModel.h
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef __KCHART_ATTRIBUTES_MODEL_H__
10 #define __KCHART_ATTRIBUTES_MODEL_H__
11 
12 #include "KChartAbstractProxyModel.h"
13 #include <QMap>
14 #include <QVariant>
15 
16 #include "KChartGlobal.h"
17 
18 namespace KChart {
19 
20 /**
21  * @brief A proxy model used for decorating data with attributes.
22  *
23  * An AttributesModel forwards data from and to the source model and adds attributes,
24  * data that influences the graphical rendering of the source model data.
25  * The attributes are distinguished from the source model's data by their @p role values.
26  * Therefore this class does not need to, and does not, change the data layout from the
27  * source model's; indexes that refer to the same data have the same row and column
28  * values in both models.
29  * Attribute changes, that is changes to data with the attribute role, via the interface
30  * of this class (including setData()) are stored internally and not forwarded to the source model.
31  */
32 class KCHART_EXPORT AttributesModel : public AbstractProxyModel
33 {
34  Q_OBJECT
35  KCHART_DECLARE_PRIVATE_BASE_POLYMORPHIC( AttributesModel )
36 public:
37  enum PaletteType {
38  PaletteTypeDefault = 0,
39  PaletteTypeRainbow = 1,
40  PaletteTypeSubdued = 2
41  };
42 
43  explicit AttributesModel( QAbstractItemModel* model, QObject * parent = nullptr );
44  ~AttributesModel() override;
45 
46  /** Copies the internal data (maps and palette) of another
47  * AttributesModel* into this one.
48  */
49  void initFrom( const AttributesModel* other );
50 
51  /** Returns true if both, all of the attributes set, and
52  * the palette set is equal in both of the AttributeModels.
53  */
54  bool compare( const AttributesModel* other ) const;
55 
56  bool compareAttributes( int role, const QVariant& a, const QVariant& b ) const;
57 
58  /* Attributes Model specific API */
59  bool setModelData( const QVariant value, int role );
60  QVariant modelData( int role ) const;
61 
62  /** Returns whether the given role corresponds to one of the known
63  * internally used ones. */
64  bool isKnownAttributesRole( int role ) const;
65 
66  /** Sets the palettetype used by this attributesmodel */
67  void setPaletteType( PaletteType type );
68  PaletteType paletteType() const;
69 
70  /** Returns the data that were specified at global level,
71  * or the default data, or QVariant().
72  */
73  QVariant data(int role) const;
74 
75  /** Returns the data that were specified at per column level,
76  * or the globally set data, or the default data, or QVariant().
77  */
78  QVariant data(int column, int role) const;
79 
80  /** \reimpl */
81  QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
82  /** \reimpl */
83  int rowCount(const QModelIndex& ) const override;
84  /** \reimpl */
85  int columnCount(const QModelIndex& ) const override;
86  /** \reimpl */
87  QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const override;
88  /** \reimpl */
89  bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::DisplayRole) override;
90  /** Remove any explicit attributes settings that might have been specified before. */
91  bool resetData ( const QModelIndex & index, int role = Qt::DisplayRole);
92  /** \reimpl */
93  bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value,
94  int role = Qt::DisplayRole) override;
95  /** Returns default values for the header data. */
96  virtual QVariant defaultHeaderData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
97  /** Remove any explicit attributes settings that might have been specified before. */
98  bool resetHeaderData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole);
99  /** \reimpl */
100  void setSourceModel ( QAbstractItemModel* sourceModel ) override;
101 
102  /** Define the default value for a certain role.
103  Passing a default-constructed QVariant is equivalent to removing the default. */
104  void setDefaultForRole( int role, const QVariant& value );
105 
106  /** Set the dimension of the dataset in the source model. \sa AbstractDiagram::setDatasetDimension */
107  void setDatasetDimension( int dimension );
108  int datasetDimension() const;
109 
110 Q_SIGNALS:
111  void attributesChanged( const QModelIndex&, const QModelIndex& );
112 
113 private Q_SLOTS:
114  void slotRowsAboutToBeInserted( const QModelIndex& parent, int start, int end );
115  void slotColumnsAboutToBeInserted( const QModelIndex& parent, int start, int end );
116  void slotRowsInserted( const QModelIndex& parent, int start, int end );
117  void slotColumnsInserted( const QModelIndex& parent, int start, int end );
118 
119  void slotRowsAboutToBeRemoved( const QModelIndex& parent, int start, int end );
120  void slotColumnsAboutToBeRemoved( const QModelIndex& parent, int start, int end );
121  void slotRowsRemoved( const QModelIndex& parent, int start, int end );
122  void slotColumnsRemoved( const QModelIndex& parent, int start, int end );
123 
124  void slotDataChanged( const QModelIndex& topLeft, const QModelIndex& bottomRight );
125 
126 private:
127  // helper
128  QVariant defaultsForRole( int role ) const;
129  bool compareHeaderDataMaps( const QMap< int, QMap< int, QVariant > >& mapA,
130  const QMap< int, QMap< int, QVariant > >& mapB ) const;
131 
132  void removeEntriesFromDataMap( int start, int end );
133  void removeEntriesFromDirectionDataMaps( Qt::Orientation dir, int start, int end );
134 };
135 
136 }
137 
138 #endif
DisplayRole
Base class for all proxy models used inside KChart.
Q_SCRIPTABLE Q_NOREPLY void start()
Orientation
A proxy model used for decorating data with attributes.
Contains KChart macros.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Oct 3 2023 03:51:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.