16#include <QGuiApplication>
30QgsSettingsTreeModelNodeData *QgsSettingsTreeModelNodeData::createRootNodeData(
const QgsSettingsTreeNode *rootNode, QObject *parent =
nullptr )
32 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData( parent );
33 nodeData->mType = Type::RootNode;
34 nodeData->mName = rootNode->
key();
35 nodeData->mTreeNode = rootNode;
36 nodeData->fillChildren();
40void QgsSettingsTreeModelNodeData::applyChanges()
44 case Type::NamedListTreeNode:
47 case Type::NamedListItem:
49 QList<QgsSettingsTreeModelNodeData *>::iterator it = mChildren.begin();
50 for ( ; it != mChildren.end(); ++it )
51 ( *it )->applyChanges();
58 setting()->setVariantValue( mValue, mNamedParentNodes );
65bool QgsSettingsTreeModelNodeData::setValue(
const QVariant &value )
67 Q_ASSERT( mType == Type::Setting );
68 if ( !value.isValid() && mValue.isValid() )
74 else if ( !mValue.isValid() || mValue != value )
77 mIsEdited = ( value != mOriginalValue );
86 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
87 nodeData->mParent =
this;
88 nodeData->mNamedParentNodes = mNamedParentNodes;
89 nodeData->mName = node->
key();
90 nodeData->mTreeNode = node;
93 nodeData->
mType = Type::NamedListTreeNode;
95 const QStringList items = nln->
items( mNamedParentNodes );
96 for (
const QString &item : items )
98 nodeData->addChildForNamedListItemNode( item, nln );
103 nodeData->mType = Type::TreeNode;
104 nodeData->fillChildren();
106 mChildren.append( nodeData );
109void QgsSettingsTreeModelNodeData::addChildForNamedListItemNode(
const QString &item,
const QgsSettingsTreeNamedListNode *namedListNode )
111 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
112 nodeData->mType = Type::NamedListItem;
113 nodeData->mParent =
this;
114 nodeData->mNamedParentNodes = mNamedParentNodes;
115 nodeData->mNamedParentNodes.append( item );
116 nodeData->mName = item;
117 nodeData->mTreeNode = namedListNode;
118 nodeData->fillChildren();
119 mChildren.append( nodeData );
124 QgsSettingsTreeModelNodeData *nodeData =
new QgsSettingsTreeModelNodeData(
this );
125 nodeData->mType = Type::Setting;
126 nodeData->mParent =
this;
127 nodeData->mNamedParentNodes = mNamedParentNodes;
128 nodeData->mSetting = setting;
129 nodeData->mName = setting->
name();
131 nodeData->mOriginalValue = nodeData->mValue;
132 nodeData->mExists = setting->
exists( mNamedParentNodes );
134 switch ( mNamedParentNodes.count() )
140 QgsDebugMsgLevel( QString(
"getting %1 with %2 and %3" ).arg( setting->
definitionKey(), mNamedParentNodes.at( 0 ), mNamedParentNodes.at( 1 ) ), 3 );
151 mChildren.append( nodeData );
154void QgsSettingsTreeModelNodeData::fillChildren()
156 const QList<QgsSettingsTreeNode *> childrenNodes = mTreeNode->childrenNodes();
159 addChildForTreeNode( childNode );
161 const QList<const QgsSettingsEntryBase *> childrenSettings = mTreeNode->childrenSettings();
164 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 );
221 return QModelIndex();
225 return QModelIndex();
228 return createIndex( row, column,
static_cast<QObject *
>( n->children().at( row ) ) );
233 if ( !child.isValid() )
234 return QModelIndex();
236 if ( QgsSettingsTreeModelNodeData *n =
index2node( child ) )
238 return indexOfParentSettingsTreeNode( n->parent() );
243 return QModelIndex();
247QModelIndex QgsSettingsTreeModel::indexOfParentSettingsTreeNode( QgsSettingsTreeModelNodeData *parentNode )
const
249 Q_ASSERT( parentNode );
251 const QgsSettingsTreeModelNodeData *grandParentNode = parentNode->parent();
252 if ( !grandParentNode )
253 return QModelIndex();
255 int row = grandParentNode->children().indexOf( parentNode );
256 Q_ASSERT( row >= 0 );
258 return createIndex( row, 0,
static_cast<QObject *
>( parentNode ) );
267 return n->children().count();
283 if ( role == Qt::ForegroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
285 if ( !node->exists() )
291 if ( node->isEdited() &&
294 return mEditedColorFore;
298 if ( role == Qt::BackgroundRole && node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
301 if ( node->isEdited() &&
304 return mEditedColorBack;
312 if ( role == Qt::DisplayRole || role == Qt::EditRole )
321 if ( role == Qt::CheckStateRole )
323 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting &&
327 return node->value().toBool() ? Qt::Checked : Qt::Unchecked;
330 if ( role == Qt::DisplayRole || role == Qt::EditRole )
332 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting &&
340 return node->value();
343 else if ( role == Qt::BackgroundRole )
345 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
347 switch ( node->setting()->settingsType() )
361 return node->value();
370 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
372 if ( role == Qt::DisplayRole || role == Qt::EditRole )
374 return node->setting()->description();
389 if ( orientation == Qt::Orientation::Horizontal && role == Qt::DisplayRole )
391 switch (
static_cast<Column>( section ) )
396 return tr(
"Value" );
398 return tr(
"Description" );
410 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
414 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
418 return Qt::ItemIsEnabled | Qt::ItemIsEditable;
424 return Qt::ItemIsEnabled;
426 return Qt::NoItemFlags;
433 if ( role == Qt::EditRole || role == Qt::CheckStateRole )
436 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
438 if ( role == Qt::CheckStateRole )
441 nodeData->setValue( value == Qt::Checked ?
true : false );
445 nodeData->setValue( value );
459QgsSettingsTreeItemDelegate::QgsSettingsTreeItemDelegate(
QgsSettingsTreeModel *model, QObject *parent )
460 : QItemDelegate( parent )
465QWidget *QgsSettingsTreeItemDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
470 QModelIndex sourceIndex = index;
474 sourceIndex = proxyModel->mapToSource( index );
476 QgsSettingsTreeModelNodeData *nodeData = mModel->index2node( sourceIndex );
477 if ( nodeData->type() == QgsSettingsTreeModelNodeData::Type::Setting )
485void QgsSettingsTreeItemDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
490 const QVariant value = index.model()->data( index, Qt::DisplayRole );
495void QgsSettingsTreeItemDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
509 : QSortFilterProxyModel( parent )
512 QSortFilterProxyModel::setSourceModel( mSourceModel );
517 if ( filterText == mFilterText )
520 mFilterText = filterText;
527 return nodeShown( node );
530bool QgsSettingsTreeProxyModel::nodeShown( QgsSettingsTreeModelNodeData *node )
const
534 if ( node->type() == QgsSettingsTreeModelNodeData::Type::Setting )
536 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
540 QModelIndex index = mSourceModel->
node2index( node ).parent();
541 while ( ( index.isValid() ) )
544 if ( parentNode->name().contains( mFilterText, Qt::CaseInsensitive ) )
547 index = index.parent();
554 if ( node->name().contains( mFilterText, Qt::CaseInsensitive ) )
557 const auto constChildren = node->children();
558 for ( QgsSettingsTreeModelNodeData *child : constChildren )
560 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)