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 );
174 : QAbstractItemModel( parent )
176 mRootNode = QgsSettingsTreeModelNodeData::createRootNodeData( rootNode,
this );
178 QPalette
pal = qApp->palette();
179 mEditedColorBack =
pal.color( QPalette::Active, QPalette::Dark );
180 mEditedColorFore =
pal.color( QPalette::Active, QPalette::BrightText );
181 mNotSetColor =
pal.color( QPalette::Disabled, QPalette::WindowText );
192 mRootNode->applyChanges();
198 if ( !
index.isValid() )
201 QObject *obj =
reinterpret_cast<QObject *
>(
index.internalPointer() );
202 return qobject_cast<QgsSettingsTreeModelNodeData *>( obj );
207 if ( !node || !node->parent() )
208 return QModelIndex();
210 QModelIndex parentIndex =
node2index( node->parent() );
212 int row = node->parent()->children().indexOf( node );
213 Q_ASSERT( row >= 0 );
222 return QModelIndex();
226 return QModelIndex();
229 return createIndex( row, column,
static_cast<QObject *
>( n->children().at( row ) ) );
234 if ( !child.isValid() )
235 return QModelIndex();
237 if ( QgsSettingsTreeModelNodeData *n =
index2node( child ) )
239 return indexOfParentSettingsTreeNode( n->parent() );
244 return QModelIndex();
248QModelIndex QgsSettingsTreeModel::indexOfParentSettingsTreeNode( QgsSettingsTreeModelNodeData *parentNode )
const
250 Q_ASSERT( parentNode );
252 const QgsSettingsTreeModelNodeData *grandParentNode = parentNode->parent();
253 if ( !grandParentNode )
254 return QModelIndex();
256 int row = grandParentNode->children().indexOf( parentNode );
257 Q_ASSERT( row >= 0 );
259 return createIndex( row, 0,
static_cast<QObject *
>( parentNode ) );
268 return n->children().count();
284 if ( role == Qt::ForegroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
286 if ( !node->exists() )
292 if ( node->isEdited() &&
295 return mEditedColorFore;
299 if ( role == Qt::BackgroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
302 if ( node->isEdited() &&
305 return mEditedColorBack;
313 if ( role == Qt::DisplayRole || role == Qt::EditRole )
322 if ( role == Qt::CheckStateRole )
324 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting &&
328 return node->value().toBool() ? Qt::Checked : Qt::Unchecked;
331 if ( role == Qt::DisplayRole || role == Qt::EditRole )
333 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting &&
341 return node->value();
344 else if ( role == Qt::BackgroundRole )
346 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
348 switch ( node->setting()->settingsType() )
362 return node->value();
371 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
373 if ( role == Qt::DisplayRole || role == Qt::EditRole )
375 return node->setting()->description();
390 if ( orientation == Qt::Orientation::Horizontal && role == Qt::DisplayRole )
392 switch (
static_cast<Column>( section ) )
397 return tr(
"Value" );
399 return tr(
"Description" );
411 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
415 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
419 return Qt::ItemIsEnabled | Qt::ItemIsEditable;
425 return Qt::ItemIsEnabled;
427 return Qt::NoItemFlags;
434 if ( role == Qt::EditRole || role == Qt::CheckStateRole )
437 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
439 if ( role == Qt::CheckStateRole )
442 nodeData->setValue( value == Qt::Checked ?
true : false );
446 nodeData->setValue( value );
460QgsSettingsTreeItemDelegate::QgsSettingsTreeItemDelegate(
QgsSettingsTreeModel *model, QObject *parent )
461 : QItemDelegate( parent )
466QWidget *QgsSettingsTreeItemDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
471 QModelIndex sourceIndex = index;
475 sourceIndex = proxyModel->mapToSource( index );
477 QgsSettingsTreeModelNodeData *nodeData = mModel->index2node( sourceIndex );
478 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
486void QgsSettingsTreeItemDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
491 const QVariant value = index.model()->data( index, Qt::DisplayRole );
496void QgsSettingsTreeItemDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
510 : QSortFilterProxyModel( parent )
513 QSortFilterProxyModel::setSourceModel( mSourceModel );
518 if ( filterText == mFilterText )
521 mFilterText = filterText;
528 return nodeShown( node );
531bool QgsSettingsTreeProxyModel::nodeShown( QgsSettingsTreeModelNodeData *node )
const
535 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
537 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
541 QModelIndex index = mSourceModel->
node2index( node ).parent();
542 while ( ( index.isValid() ) )
545 if ( parentNode->name().contains( mFilterText, Qt::CaseInsensitive ) )
548 index = index.parent();
555 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
558 const auto constChildren = node->children();
559 for ( QgsSettingsTreeModelNodeData *child : constChildren )
561 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)