16#include <QGuiApplication>
20#include "moc_qgssettingstreemodel.cpp"
31QgsSettingsTreeModelNodeData *QgsSettingsTreeModelNodeData::createRootNodeData(
const QgsSettingsTreeNode *rootNode, QObject *parent =
nullptr )
33 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData( parent );
34 nodeData->mType = Type::RootNode;
35 nodeData->mName = rootNode->
key();
36 nodeData->mTreeNode = rootNode;
37 nodeData->fillChildren();
41void QgsSettingsTreeModelNodeData::applyChanges()
45 case Type::NamedListTreeNode:
48 case Type::NamedListItem:
50 QList<QgsSettingsTreeModelNodeData *>::iterator it = mChildren.begin();
51 for ( ; it != mChildren.end(); ++it )
52 ( *it )->applyChanges();
59 setting()->setVariantValue( mValue, mNamedParentNodes );
66bool QgsSettingsTreeModelNodeData::setValue(
const QVariant &value )
68 Q_ASSERT( mType == Type::Setting );
69 if ( !value.isValid() && mValue.isValid() )
75 else if ( !mValue.isValid() || mValue != value )
78 mIsEdited = ( value != mOriginalValue );
87 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
88 nodeData->mParent =
this;
89 nodeData->mNamedParentNodes = mNamedParentNodes;
90 nodeData->mName = node->
key();
91 nodeData->mTreeNode = node;
94 nodeData->
mType = Type::NamedListTreeNode;
96 const QStringList items = nln->
items( mNamedParentNodes );
97 for (
const QString &item : items )
99 nodeData->addChildForNamedListItemNode( item, nln );
104 nodeData->mType = Type::TreeNode;
105 nodeData->fillChildren();
107 mChildren.append( nodeData );
110void QgsSettingsTreeModelNodeData::addChildForNamedListItemNode(
const QString &item,
const QgsSettingsTreeNamedListNode *namedListNode )
112 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
113 nodeData->mType = Type::NamedListItem;
114 nodeData->mParent =
this;
115 nodeData->mNamedParentNodes = mNamedParentNodes;
116 nodeData->mNamedParentNodes.append( item );
117 nodeData->mName = item;
118 nodeData->mTreeNode = namedListNode;
119 nodeData->fillChildren();
120 mChildren.append( nodeData );
125 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
126 nodeData->mType = Type::Setting;
127 nodeData->mParent =
this;
128 nodeData->mNamedParentNodes = mNamedParentNodes;
129 nodeData->mSetting = setting;
130 nodeData->mName = setting->
name();
132 nodeData->mOriginalValue = nodeData->mValue;
133 nodeData->mExists = setting->
exists( mNamedParentNodes );
135 switch ( mNamedParentNodes.count() )
141 QgsDebugMsgLevel( QString(
"getting %1 with %2 and %3" ).arg( setting->
definitionKey(), mNamedParentNodes.at( 0 ), mNamedParentNodes.at( 1 ) ), 3 );
152 mChildren.append( nodeData );
155void QgsSettingsTreeModelNodeData::fillChildren()
157 const QList<QgsSettingsTreeNode *> childrenNodes = mTreeNode->childrenNodes();
160 addChildForTreeNode( childNode );
162 const QList<const QgsSettingsEntryBase *> childrenSettings = mTreeNode->childrenSettings();
165 addChildForSetting( setting );
173 : QAbstractItemModel( parent )
175 mRootNode = QgsSettingsTreeModelNodeData::createRootNodeData( rootNode,
this );
177 QPalette
pal = qApp->palette();
178 mEditedColorBack =
pal.color( QPalette::Active, QPalette::Dark );
179 mEditedColorFore =
pal.color( QPalette::Active, QPalette::BrightText );
180 mNotSetColor =
pal.color( QPalette::Disabled, QPalette::WindowText );
191 mRootNode->applyChanges();
197 if ( !
index.isValid() )
200 QObject *obj =
reinterpret_cast<QObject *
>(
index.internalPointer() );
201 return qobject_cast<QgsSettingsTreeModelNodeData *>( obj );
206 if ( !node || !node->parent() )
207 return QModelIndex();
209 QModelIndex parentIndex =
node2index( node->parent() );
211 int row = node->parent()->children().indexOf( node );
212 Q_ASSERT( row >= 0 );
220 return QModelIndex();
224 return QModelIndex();
227 return createIndex( row, column,
static_cast<QObject *
>( n->children().at( row ) ) );
232 if ( !child.isValid() )
233 return QModelIndex();
235 if ( QgsSettingsTreeModelNodeData *n =
index2node( child ) )
237 return indexOfParentSettingsTreeNode( n->parent() );
242 return QModelIndex();
246QModelIndex QgsSettingsTreeModel::indexOfParentSettingsTreeNode( QgsSettingsTreeModelNodeData *parentNode )
const
248 Q_ASSERT( parentNode );
250 const QgsSettingsTreeModelNodeData *grandParentNode = parentNode->parent();
251 if ( !grandParentNode )
252 return QModelIndex();
254 int row = grandParentNode->children().indexOf( parentNode );
255 Q_ASSERT( row >= 0 );
257 return createIndex( row, 0,
static_cast<QObject *
>( parentNode ) );
266 return n->children().count();
282 if ( role == Qt::ForegroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
284 if ( !node->exists() )
292 return mEditedColorFore;
296 if ( role == Qt::BackgroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
301 return mEditedColorBack;
309 if ( role == Qt::DisplayRole || role == Qt::EditRole )
318 if ( role == Qt::CheckStateRole )
320 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting && node->setting()->settingsType() ==
Qgis::SettingsType::Bool )
323 return node->value().toBool() ? Qt::Checked : Qt::Unchecked;
326 if ( role == Qt::DisplayRole || role == Qt::EditRole )
328 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting && node->setting()->settingsType() ==
Qgis::SettingsType::Bool )
335 return node->value();
338 else if ( role == Qt::BackgroundRole )
340 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
342 switch ( node->setting()->settingsType() )
356 return node->value();
365 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
367 if ( role == Qt::DisplayRole || role == Qt::EditRole )
369 return node->setting()->description();
384 if ( orientation == Qt::Orientation::Horizontal && role == Qt::DisplayRole )
386 switch (
static_cast<Column>( section ) )
391 return tr(
"Value" );
393 return tr(
"Description" );
405 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
409 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
413 return Qt::ItemIsEnabled | Qt::ItemIsEditable;
419 return Qt::ItemIsEnabled;
421 return Qt::NoItemFlags;
428 if ( role == Qt::EditRole || role == Qt::CheckStateRole )
431 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
433 if ( role == Qt::CheckStateRole )
436 nodeData->setValue( value == Qt::Checked ?
true : false );
440 nodeData->setValue( value );
454QgsSettingsTreeItemDelegate::QgsSettingsTreeItemDelegate(
QgsSettingsTreeModel *model, QObject *parent )
455 : QItemDelegate( parent )
460QWidget *QgsSettingsTreeItemDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
465 QModelIndex sourceIndex = index;
469 sourceIndex = proxyModel->mapToSource( index );
471 QgsSettingsTreeModelNodeData *nodeData = mModel->index2node( sourceIndex );
472 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
480void QgsSettingsTreeItemDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
485 const QVariant value = index.model()->data( index, Qt::DisplayRole );
490void QgsSettingsTreeItemDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
501 : QSortFilterProxyModel( parent )
504 QSortFilterProxyModel::setSourceModel( mSourceModel );
509 if ( filterText == mFilterText )
512 mFilterText = filterText;
519 return nodeShown( node );
522bool QgsSettingsTreeProxyModel::nodeShown( QgsSettingsTreeModelNodeData *node )
const
526 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
528 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
532 QModelIndex index = mSourceModel->
node2index( node ).parent();
533 while ( ( index.isValid() ) )
536 if ( parentNode->name().contains( mFilterText, Qt::CaseInsensitive ) )
539 index = index.parent();
546 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
549 const auto constChildren = node->children();
550 for ( QgsSettingsTreeModelNodeData *child : constChildren )
552 if ( nodeShown( child ) )
@ NamedList
Named List Node.
@ Variant
Generic variant.
@ Custom
Custom implementation.
@ StringList
List of strings.
@ VariantMap
Map of strings.
@ Double
Double precision number.
static QgsSettingsEditorWidgetRegistry * settingsEditorWidgetRegistry()
Returns the registry of settings editors.
Represent settings entry and provides methods for reading and writing settings values.
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
QString name() const
Returns the name of the settings.
QString definitionKey() const
Returns settings entry defining key.
QVariant valueAsVariant(const QString &dynamicKeyPart=QString()) const
Returns settings value with.
QgsSettingsTreeModel is a tree model for the settings tree.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex node2index(QgsSettingsTreeModelNodeData *node) const
Returns the index from the settings tree node.
@ Description
Description.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void applyChanges()
Apply pending changes in the model to the corresponding settings.
int columnCount(const QModelIndex &parent) const override
QgsSettingsTreeModelNodeData * index2node(const QModelIndex &index) const
Returns settings tree node for given index or the root node if the index is invalid.
QgsSettingsTreeModel(QgsSettingsTreeNode *rootNode=nullptr, QObject *parent=nullptr)
Constructor.
QModelIndex parent(const QModelIndex &child) const override
int rowCount(const QModelIndex &parent) const override
QgsSettingsTreeNamedListNode is a named list tree node for the settings tree to help organizing and i...
QStringList items(const QStringList &parentsNamedItems=QStringList()) const
Returns the list of items.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
Qgis::SettingsTreeNodeType type() const
Returns the type of node.
Qgis::SettingsTreeNodeType mType
QString key() const
Returns the key of the node (without its parents)
QgsSettingsTreeProxyModel allows filtering the settings tree.
QgsSettingsTreeProxyModel(QgsSettingsTreeNode *rootNode=nullptr, QObject *parent=nullptr)
Constructor.
void setFilterText(const QString &filterText=QString())
Sets the filter text.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)