Qt Model Utilities  2.0.3
A set of utilities for the model/view framework of Qt
genericmodel.h
1 /****************************************************************************\
2  Copyright 2021 Luca Beldi
3  Licensed under the Apache License, Version 2.0 (the "License");
4  you may not use this file except in compliance with the License.
5  You may obtain a copy of the License at
6  http://www.apache.org/licenses/LICENSE-2.0
7  Unless required by applicable law or agreed to in writing, software
8  distributed under the License is distributed on an "AS IS" BASIS,
9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  See the License for the specific language governing permissions and
11  limitations under the License.
12 \****************************************************************************/
13 
14 #ifndef GENERICMODEL_H
15 #define GENERICMODEL_H
16 #include <modelutilities_global.h>
17 #include <QAbstractItemModel>
18 #include <QVariant>
19 #include <QStringList>
20 class GenericModelPrivate;
21 class MODELUTILITIES_EXPORT GenericModel : public QAbstractItemModel
22 {
23  Q_OBJECT
24  Q_PROPERTY(bool mergeDisplayEdit READ mergeDisplayEdit WRITE setMergeDisplayEdit NOTIFY mergeDisplayEditChanged)
25  Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged)
26  Q_DISABLE_COPY(GenericModel)
27  Q_DECLARE_PRIVATE_D(m_dptr, GenericModel)
28  friend class GenericModelItem;
29 
30 public:
31  explicit GenericModel(QObject *parent = Q_NULLPTR);
32  ~GenericModel();
33  void setRoleNames(const QHash<int, QByteArray> &rNames);
34  QHash<int, QByteArray> roleNames() const override;
35  bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
36  bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
37  bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
38  bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
39  QMap<int, QVariant> itemData(const QModelIndex &index) const override;
40  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
41  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
42  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
43  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
44  Qt::ItemFlags flags(const QModelIndex &index) const override;
45  virtual bool setFlags(const QModelIndex &index, Qt::ItemFlags flags);
46  bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
47  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
48  QMimeData *mimeData(const QModelIndexList &indexes) const override;
49  QStringList mimeTypes() const override;
50  bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
51  bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent,
52  int destinationChild) override;
53  bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
54  QModelIndex parent(const QModelIndex &index) const override;
55  bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
56  bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
57  bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
58  void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
59  void sort(int column, const QModelIndex &parent, Qt::SortOrder order = Qt::AscendingOrder, bool recursive = true);
60  QSize span(const QModelIndex &index) const override;
61  bool setSpan(const QModelIndex &index, const QSize &size);
62  Qt::DropActions supportedDragActions() const override;
63  Qt::DropActions supportedDropActions() const override;
64  bool mergeDisplayEdit() const;
65  void setMergeDisplayEdit(bool val);
66  int sortRole() const;
67  void setSortRole(int role);
68 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
69  void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override;
70  bool clearItemData(const QModelIndex &index) override;
71 #else
72  bool clearItemData(const QModelIndex &index);
73 #endif
74 
75 Q_SIGNALS:
76  void mergeDisplayEditChanged(bool val);
77  void sortRoleChanged(int val);
78 
79 protected:
80  GenericModel(GenericModelPrivate &dptr, QObject *parent);
81  virtual bool mimeForValue(QMimeData *data, const QVariant &value) const;
82  GenericModelPrivate *m_dptr;
83 };
84 #endif // GENERICMODEL_H
This is a full implementation for generic use of the QAbstractItemModel interface.
Definition: genericmodel.h:22