QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsprojectstoredobjectmanagermodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectstoredobjectmanagermodel.h
3 ------------------
4 Date : January 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSPROJECTSTOREDOBJECTMANAGERMODEL_H
17#define QGSPROJECTSTOREDOBJECTMANAGERMODEL_H
18
19#include "qgis_core.h"
20#include "qgis_sip.h"
22
23#include <QAbstractListModel>
24#include <QObject>
25#include <QSortFilterProxyModel>
26
36class CORE_EXPORT QgsProjectStoredObjectManagerModelBase : public QAbstractListModel
37{
38 Q_OBJECT
39
40 public:
41
42 // *INDENT-OFF*
43
47 enum class CustomRole : int
48 {
49 Object = Qt::UserRole + 1,
50 };
51 Q_ENUM( CustomRole )
52 // *INDENT-ON*
53
54
58
59 int rowCount( const QModelIndex &parent ) const override;
60 QVariant data( const QModelIndex &index, int role ) const override;
61 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
62 Qt::ItemFlags flags( const QModelIndex &index ) const override;
63
64 protected slots:
65
67#ifndef SIP_RUN
68 void objectAboutToBeAdded( const QString &name );
69 void objectAboutToBeRemoved( const QString &name );
70 void objectAdded( const QString &name );
71 void objectRemoved( const QString &name );
72#endif
74
75 protected:
76
78#ifndef SIP_RUN
79 virtual void objectAboutToBeAddedInternal( const QString & ) {}
80 virtual void objectAboutToBeRemovedInternal( const QString & ) {}
81 virtual void objectAddedInternal( const QString & ) {}
82 virtual void objectRemovedInternal( const QString & ) {}
83
84 virtual int rowCountInternal( const QModelIndex & ) const { return 0; }
85 virtual QVariant dataInternal( const QModelIndex &, int ) const { return QVariant(); }
86 virtual bool setDataInternal( const QModelIndex &, const QVariant &, int = Qt::EditRole ) { return false; }
87 virtual Qt::ItemFlags flagsInternal( const QModelIndex & ) const { return Qt::ItemFlags();}
88#endif
90};
91
101template<class T>
103{
104
105 public:
106
111
116 T *objectFromIndex( const QModelIndex &index ) const;
117
122 QModelIndex indexFromObject( T *object ) const;
123
128 void setAllowEmptyObject( bool allowEmpty );
129
134 bool allowEmptyObject() const { return mAllowEmpty; }
135
136 protected:
137
139 int rowCountInternal( const QModelIndex &parent ) const override;
140 QVariant dataInternal( const QModelIndex &index, int role ) const override;
141 bool setDataInternal( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
142 Qt::ItemFlags flagsInternal( const QModelIndex &index ) const override;
143
144 void objectAboutToBeAddedInternal( const QString &name ) override;
145 void objectAboutToBeRemovedInternal( const QString &name ) override;
146 void objectAddedInternal( const QString &name ) override;
147 void objectRemovedInternal( const QString &name ) override;
148 void objectRenamedInternal( T *object, const QString &newName );
149 QVariant objectToVariant( T *object ) const;
151
154
155 private:
156 bool mAllowEmpty = false;
157};
158
159
160
161
172class CORE_EXPORT QgsProjectStoredObjectManagerProxyModelBase : public QSortFilterProxyModel
173{
174 Q_OBJECT
175
176 public:
177
181 explicit QgsProjectStoredObjectManagerProxyModelBase( QObject *parent SIP_TRANSFERTHIS = nullptr );
182 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
183 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
184
190 QString filterString() const { return mFilterString; }
191
192 public slots:
193
200 void setFilterString( const QString &filter );
201
202 protected:
203
211 virtual bool filterAcceptsRowInternal( int sourceRow, const QModelIndex &sourceParent ) const;
212
215};
216
217
228template<class T>
230{
231
232 public:
233
237 explicit QgsProjectStoredObjectManagerProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr );
238
239 protected:
240
241 bool filterAcceptsRowInternal( int sourceRow, const QModelIndex &sourceParent ) const override;
242
243};
244
245
246#endif // QGSPROJECTSTOREDOBJECTMANAGERMODEL_H
Template class for storage of a set of objects attached to a QgsProject.
Base class for list models representing the objects available in a QgsAbstractProjectStoredObjectMana...
QgsProjectStoredObjectManagerModelBase(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerModelBase, with the specified parent object.
void setAllowEmptyObject(bool allowEmpty)
Sets whether an optional empty object ("not set") option is present in the model.
QModelIndex indexFromObject(T *object) const
Returns the model index corresponding to an object.
T * objectFromIndex(const QModelIndex &index) const
Returns the object at the corresponding index.
QgsProjectStoredObjectManagerModel(QgsAbstractProjectStoredObjectManager< T > *manager, QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerModel, showing the objects from the specified manager.
PRIVATE QgsAbstractProjectStoredObjectManager< T > * mObjectManager
Object manager.
bool allowEmptyObject() const
Returns true if the model allows the empty object ("not set") choice.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QString filterString() const
Returns the current filter string, if set.
QgsProjectStoredObjectManagerProxyModelBase(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerProxyModelBase.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
bool filterAcceptsRowInternal(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the proxy accepts the matching source row.
QgsProjectStoredObjectManagerProxyModel(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerProxyModel.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53