QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgssettingstreemodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingstreemodel.h
3 --------------------------------------
4 Date : January 2023
5 Copyright : (C) 2023 by Denis Rouzaud
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 QGSSETTINGSTREEMODEL_H
17#define QGSSETTINGSTREEMODEL_H
18
19
20#include "qgis_sip.h"
21#include "qgis_gui.h"
22
23#include <QAbstractItemModel>
24#include <QSortFilterProxyModel>
25#include <QItemDelegate>
26
31
32#ifndef SIP_RUN
33
35
45class GUI_EXPORT QgsSettingsTreeModelNodeData : public QObject
46{
47 Q_OBJECT
48 public:
49
51 enum class Type
52 {
53 RootNode,
54 TreeNode,
55 NamedListTreeNode,
56 NamedListItem,
57 Setting
58 };
59
61 static QgsSettingsTreeModelNodeData *createRootNodeData( const QgsSettingsTreeNode *rootNode, QObject *parent );
62
64 void applyChanges();
65
67 bool isRoot() const { return mParent == nullptr; }
68
70 QStringList namedParentNodes() const { return mNamedParentNodes; }
71
73 QList<QgsSettingsTreeModelNodeData *> children() const {return mChildren;}
74
76 QgsSettingsTreeModelNodeData *parent() const {return mParent;}
77
79 Type type() const {return mType;}
80
82 QString name() const {return mName;}
83
85 QVariant value() const {return mValue;}
86
88 QVariant originalValue() const {return mOriginalValue;}
89
91 bool setValue( const QVariant &value );
92
94 bool exists() const {return mExists;}
95
97 bool isEdited() const {return mIsEdited;}
98
103 const QgsSettingsEntryBase *setting() const {return mSetting;}
104
105 private:
107 QgsSettingsTreeModelNodeData( QObject *parent ) : QObject( parent ) {}
108 void addChildForTreeNode( const QgsSettingsTreeNode *node );
109 void addChildForNamedListItemNode( const QString &item, const QgsSettingsTreeNamedListNode *namedListNode );
110 void addChildForSetting( const QgsSettingsEntryBase *setting );
111 void fillChildren();
112
113 Type mType = Type::TreeNode;
114 QString mName;
115 QVariant mValue;
116 QVariant mOriginalValue;
117 QStringList mNamedParentNodes;
118 bool mExists = false;
119 bool mIsEdited = false;
120
121 QList<QgsSettingsTreeModelNodeData *> mChildren;
122 QgsSettingsTreeModelNodeData *mParent = nullptr;
123
124 const QgsSettingsTreeNode *mTreeNode = nullptr;
125 const QgsSettingsEntryBase *mSetting = nullptr;
126};
127
128
138class GUI_EXPORT QgsSettingsTreeItemDelegate : public QItemDelegate
139{
140 Q_OBJECT
141
142 public:
144 explicit QgsSettingsTreeItemDelegate( QgsSettingsTreeModel *model, QObject *parent = nullptr );
145
146 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
147 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
148 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
149
150 private:
151 QgsSettingsTreeModel *mModel = nullptr;
152};
153
155
156#endif
157
158
166class GUI_EXPORT QgsSettingsTreeModel : public QAbstractItemModel
167{
168 Q_OBJECT
169 public:
170
172 enum class Column
173 {
174 Name,
175 Value,
176 Description,
177 };
178
180 QgsSettingsTreeModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
181
183
185 void applyChanges();
186
190 QgsSettingsTreeModelNodeData *index2node( const QModelIndex &index ) const SIP_SKIP;
191
193 QModelIndex node2index( QgsSettingsTreeModelNodeData *node ) const SIP_SKIP;
194
195
196 QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
197 QModelIndex parent( const QModelIndex &child ) const override;
198 int rowCount( const QModelIndex &parent ) const override;
199 int columnCount( const QModelIndex &parent ) const override;
200 QVariant data( const QModelIndex &index, int role ) const override;
201 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
202 Qt::ItemFlags flags( const QModelIndex &index ) const override;
203 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
204
205 private:
206 QModelIndex indexOfParentSettingsTreeNode( QgsSettingsTreeModelNodeData *parentNode ) const;
207
208 QgsSettingsTreeModelNodeData *mRootNode = nullptr;
209
210 QColor mEditedColorBack;
211 QColor mEditedColorFore;
212 QColor mNotSetColor;
213
214};
215
223class GUI_EXPORT QgsSettingsTreeProxyModel : public QSortFilterProxyModel
224{
225 Q_OBJECT
226 public:
228 QgsSettingsTreeProxyModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
229
231 void applyChanges() {mSourceModel->applyChanges();}
232
233 public slots:
235 void setFilterText( const QString &filterText = QString() );
236
237
238 protected:
239 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
240
241 private:
242 QgsSettingsTreeModel *mSourceModel = nullptr;
243
244 bool nodeShown( QgsSettingsTreeModelNodeData *node ) const;
245 QString mFilterText;
246};
247
248#endif // QGSSETTINGSTREEMODEL_H
Represent settings entry and provides methods for reading and writing settings values.
QgsSettingsTreeModel is a tree model for the settings tree.
QgsSettingsTreeNamedListNode is a named list tree node for the settings tree to help organizing and i...
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
QgsSettingsTreeProxyModel allows filtering the settings tree.
void applyChanges()
Apply pending changes in the model to the corresponding settings.
#define SIP_SKIP
Definition qgis_sip.h:126