QGIS API Documentation 3.43.0-Master (37eec98dbf6)
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
6 Email : denis@opengis.ch
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:
50 enum class Type
51 {
52 RootNode,
53 TreeNode,
54 NamedListTreeNode,
55 NamedListItem,
56 Setting
57 };
58
60 static QgsSettingsTreeModelNodeData *createRootNodeData( const QgsSettingsTreeNode *rootNode, QObject *parent );
61
63 void applyChanges();
64
66 bool isRoot() const { return !mParent; }
67
69 QStringList namedParentNodes() const { return mNamedParentNodes; }
70
72 QList<QgsSettingsTreeModelNodeData *> children() const { return mChildren; }
73
75 QgsSettingsTreeModelNodeData *parent() const { return mParent; }
76
78 Type type() const { return mType; }
79
81 QString name() const { return mName; }
82
84 QVariant value() const { return mValue; }
85
87 QVariant originalValue() const { return mOriginalValue; }
88
90 bool setValue( const QVariant &value );
91
93 bool exists() const { return mExists; }
94
96 bool isEdited() const { return mIsEdited; }
97
102 const QgsSettingsEntryBase *setting() const { return mSetting; }
103
104 private:
106 QgsSettingsTreeModelNodeData( QObject *parent )
107 : 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:
171 enum class Column
172 {
173 Name,
174 Value,
175 Description,
176 };
177
179 QgsSettingsTreeModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
180
182
184 void applyChanges();
185
189 QgsSettingsTreeModelNodeData *index2node( const QModelIndex &index ) const SIP_SKIP;
190
192 QModelIndex node2index( QgsSettingsTreeModelNodeData *node ) const SIP_SKIP;
193
194
195 QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
196 QModelIndex parent( const QModelIndex &child ) const override;
197 int rowCount( const QModelIndex &parent ) const override;
198 int columnCount( const QModelIndex &parent ) const override;
199 QVariant data( const QModelIndex &index, int role ) const override;
200 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
201 Qt::ItemFlags flags( const QModelIndex &index ) const override;
202 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
203
204 private:
205 QModelIndex indexOfParentSettingsTreeNode( QgsSettingsTreeModelNodeData *parentNode ) const;
206
207 QgsSettingsTreeModelNodeData *mRootNode = nullptr;
208
209 QColor mEditedColorBack;
210 QColor mEditedColorFore;
211 QColor mNotSetColor;
212};
213
221class GUI_EXPORT QgsSettingsTreeProxyModel : public QSortFilterProxyModel
222{
223 Q_OBJECT
224 public:
226 QgsSettingsTreeProxyModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
227
229 void applyChanges() { mSourceModel->applyChanges(); }
230
231 public slots:
233 void setFilterText( const QString &filterText = QString() );
234
235
236 protected:
237 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
238
239 private:
240 QgsSettingsTreeModel *mSourceModel = nullptr;
241
242 bool nodeShown( QgsSettingsTreeModelNodeData *node ) const;
243 QString mFilterText;
244};
245
246#endif // QGSSETTINGSTREEMODEL_H
Represents a settings entry and provides methods for reading and writing settings values.
A tree model for the settings tree.
A named list tree node for the settings tree to help organizing and introspecting the tree.
A tree node for the settings tree to help organizing and introspecting the tree.
A proxy model which 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