QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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_gui.h"
21#include "qgis_sip.h"
22
23#include <QAbstractItemModel>
24#include <QItemDelegate>
25#include <QSortFilterProxyModel>
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 {}
109 void addChildForTreeNode( const QgsSettingsTreeNode *node );
110 void addChildForNamedListItemNode( const QString &item, const QgsSettingsTreeNamedListNode *namedListNode );
111 void addChildForSetting( const QgsSettingsEntryBase *setting );
112 void fillChildren();
113
114 Type mType = Type::TreeNode;
115 QString mName;
116 QVariant mValue;
117 QVariant mOriginalValue;
118 QStringList mNamedParentNodes;
119 bool mExists = false;
120 bool mIsEdited = false;
121
122 QList<QgsSettingsTreeModelNodeData *> mChildren;
123 QgsSettingsTreeModelNodeData *mParent = nullptr;
124
125 const QgsSettingsTreeNode *mTreeNode = nullptr;
126 const QgsSettingsEntryBase *mSetting = nullptr;
127};
128
129
139class GUI_EXPORT QgsSettingsTreeItemDelegate : public QItemDelegate
140{
141 Q_OBJECT
142
143 public:
145 explicit QgsSettingsTreeItemDelegate( QgsSettingsTreeModel *model, QObject *parent = nullptr );
146
147 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
148 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
149 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
150
151 private:
152 QgsSettingsTreeModel *mModel = nullptr;
153};
154
156
157#endif
158
159
167class GUI_EXPORT QgsSettingsTreeModel : public QAbstractItemModel
168{
169 Q_OBJECT
170 public:
172 enum class Column
173 {
174 Name,
175 Value,
176 Description,
177 };
178
180 QgsSettingsTreeModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
181
182 ~QgsSettingsTreeModel() override;
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
222class GUI_EXPORT QgsSettingsTreeProxyModel : public QSortFilterProxyModel
223{
224 Q_OBJECT
225 public:
227 QgsSettingsTreeProxyModel( QgsSettingsTreeNode *rootNode = nullptr, QObject *parent = nullptr );
228
230 void applyChanges() { mSourceModel->applyChanges(); }
231
232 public slots:
234 void setFilterText( const QString &filterText = QString() );
235
236
237 protected:
238 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
239
240 private:
241 QgsSettingsTreeModel *mSourceModel = nullptr;
242
243 bool nodeShown( QgsSettingsTreeModelNodeData *node ) const;
244 QString mFilterText;
245};
246
247#endif // QGSSETTINGSTREEMODEL_H
Represents a settings entry and provides methods for reading and writing settings values.
A tree model for the settings tree.
QgsSettingsTreeModel(QgsSettingsTreeNode *rootNode=nullptr, QObject *parent=nullptr)
Constructor.
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.
void applyChanges()
Apply pending changes in the model to the corresponding settings.
QgsSettingsTreeProxyModel(QgsSettingsTreeNode *rootNode=nullptr, QObject *parent=nullptr)
Constructor.
#define SIP_SKIP
Definition qgis_sip.h:133