Qt Model Utilities  2.0.3
A set of utilities for the model/view framework of Qt
insertproxymodel.h
1 /****************************************************************************\
2  Copyright 2018 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 INSERTPROXY_H
15 #define INSERTPROXY_H
16 #include <modelutilities_global.h>
17 #include <QAbstractProxyModel>
18 #include <QVariant>
19 class InsertProxyModelPrivate;
20 class MODELUTILITIES_EXPORT InsertProxyModel : public QAbstractProxyModel
21 {
22  Q_OBJECT
23  Q_PROPERTY(InsertDirections insertDirection READ insertDirection WRITE setInsertDirection NOTIFY insertDirectionChanged)
24  Q_PROPERTY(bool mergeDisplayEdit READ mergeDisplayEdit WRITE setMergeDisplayEdit NOTIFY mergeDisplayEditChanged)
25  Q_DISABLE_COPY(InsertProxyModel)
26  Q_DECLARE_PRIVATE_D(m_dptr, InsertProxyModel)
27  InsertProxyModelPrivate *m_dptr;
28 
29 public:
30  enum InsertDirection { NoInsert = 0x0, InsertRow = 0x1, InsertColumn = 0x2 };
31  Q_DECLARE_FLAGS(InsertDirections, InsertDirection)
32 #if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
33  Q_FLAG(InsertDirections)
34 #else
35  Q_FLAGS(InsertDirections)
36 #endif
37  explicit InsertProxyModel(QObject *parent = Q_NULLPTR);
39  void setSourceModel(QAbstractItemModel *newSourceModel) Q_DECL_OVERRIDE;
40  QModelIndex buddy(const QModelIndex &index) const Q_DECL_OVERRIDE;
41  bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
42  int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
43  int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
44  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
45  bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
46  QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
47  bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
48  bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) Q_DECL_OVERRIDE;
49  QMap<int, QVariant> itemData(const QModelIndex &index) const Q_DECL_OVERRIDE;
50  QModelIndex mapFromSource(const QModelIndex &sourceIndex) const Q_DECL_OVERRIDE;
51  QModelIndex mapToSource(const QModelIndex &proxyIndex) const Q_DECL_OVERRIDE;
52  Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
53  QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
54  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
55  bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
56  bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
57  bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent,
58  int destinationChild) Q_DECL_OVERRIDE;
59  bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
60  bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
61  bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent,
62  int destinationChild) Q_DECL_OVERRIDE;
63  void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE;
64  InsertDirections insertDirection() const;
65  void setInsertDirection(const InsertDirections &direction);
66  virtual QVariant dataForCorner(int role = Qt::DisplayRole) const;
67  virtual void setDataForCorner(const QVariant &value, int role = Qt::EditRole);
68  bool mergeDisplayEdit() const;
69  void setMergeDisplayEdit(bool val);
70 
71 protected:
72  virtual bool validRow() const;
73  virtual bool validColumn() const;
74  virtual Qt::ItemFlags flagForExtra(bool isRow, int section) const;
75  InsertProxyModel(InsertProxyModelPrivate &dptr, QObject *parent);
76 public Q_SLOTS:
77  virtual bool commitRow();
78  virtual bool commitColumn();
79 Q_SIGNALS:
80  void dataForCornerChanged(const QVector<int> &roles);
81  void extraDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
82  void mergeDisplayEditChanged(bool separate);
83  void insertDirectionChanged(InsertDirections direction);
84 };
85 Q_DECLARE_METATYPE(InsertProxyModel::InsertDirections)
86 Q_DECLARE_OPERATORS_FOR_FLAGS(InsertProxyModel::InsertDirections)
87 #endif // INSERTPROXY_H
This proxy model provides an extra row and column to handle user insertions.
Definition: insertproxymodel.h:21
InsertDirection
Definition: insertproxymodel.h:30