QGIS API Documentation 3.29.0-Master (006c3c0232)
qgslayertreeembeddedconfigwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreeembeddedconfigwidget.cpp
3 --------------------------------------
4 Date : May 2016
5 Copyright : (C) 2016 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
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
17
18#include "qgsmaplayer.h"
20#include "qgsgui.h"
21#include <QStringListModel>
22#include <QStandardItemModel>
23
25 : QWidget( parent )
26
27{
28 setupUi( this );
29}
30
32{
33 mLayer = layer;
34
35 connect( mBtnAdd, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onAddClicked );
36 connect( mBtnRemove, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked );
37
38 QStandardItemModel *modelAvailable = new QStandardItemModel( this );
39 QStandardItemModel *modelUsed = new QStandardItemModel( this );
40
41 // populate available
42 const auto constProviders = QgsGui::layerTreeEmbeddedWidgetRegistry()->providers();
43 for ( const QString &providerId : constProviders )
44 {
46 if ( provider->supportsLayer( mLayer ) )
47 {
48 QStandardItem *item = new QStandardItem( provider->name() );
49 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
50 item->setData( provider->id(), Qt::UserRole + 1 );
51 modelAvailable->appendRow( item );
52 }
53 }
54 mListAvailable->setModel( modelAvailable );
55
56 // populate used
57 const int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
58 for ( int i = 0; i < widgetsCount; ++i )
59 {
60 const QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
61 if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
62 {
63 QStandardItem *item = new QStandardItem( provider->name() );
64 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
65 item->setData( provider->id(), Qt::UserRole + 1 );
66 modelUsed->appendRow( item );
67 }
68 }
69 mListUsed->setModel( modelUsed );
70}
71
72void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
73{
74 if ( !mListAvailable->currentIndex().isValid() )
75 return;
76
77 const QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
79 if ( !provider )
80 return;
81
82 if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mListUsed->model() ) )
83 {
84 QStandardItem *item = new QStandardItem( provider->name() );
85 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
86 item->setData( provider->id(), Qt::UserRole + 1 );
87 model->appendRow( item );
88 }
89}
90
91void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
92{
93 if ( !mListUsed->currentIndex().isValid() )
94 return;
95
96 const int row = mListUsed->currentIndex().row();
97 mListUsed->model()->removeRow( row );
98}
99
101{
102 if ( !mLayer )
103 return;
104
105 // clear old properties
106 const int widgetsCount = mLayer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
107 for ( int i = 0; i < widgetsCount; ++i )
108 {
109 mLayer->removeCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) );
110 }
111
112 // setup new properties
113 const int newCount = mListUsed->model()->rowCount();
114 mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/count" ), newCount );
115 for ( int i = 0; i < newCount; ++i )
116 {
117 const QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
118 mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ), providerId );
119 }
120}
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:118
void applyToLayer()
Store changes made in the widget to the layer.
QgsLayerTreeEmbeddedConfigWidget(QWidget *parent=nullptr)
A widget to configure layer tree embedded widgets for a particular map layer.
void setLayer(QgsMapLayer *layer)
Initialize widget with a map layer.
Provider interface to be implemented in order to introduce new kinds of embedded widgets for use in l...
virtual QString id() const =0
Unique name of the provider (among other providers)
virtual QString name() const =0
Human readable name - may be translatable with tr()
virtual bool supportsLayer(QgsMapLayer *layer)=0
Whether it makes sense to use this widget for a particular layer.
QStringList providers() const
Returns list of all registered providers.
QgsLayerTreeEmbeddedWidgetProvider * provider(const QString &providerId) const
Gets provider object from the provider's ID.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.