QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgslayertreeembeddedconfigwidget.cpp"
18
19#include "qgsmaplayer.h"
21#include "qgsgui.h"
22#include <QStringListModel>
23#include <QStandardItemModel>
24
26 : QWidget( parent )
27
28{
29 setupUi( this );
30}
31
33{
34 mLayer = layer;
35
36 connect( mBtnAdd, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onAddClicked );
37 connect( mBtnRemove, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked );
38
39 QStandardItemModel *modelAvailable = new QStandardItemModel( this );
40 QStandardItemModel *modelUsed = new QStandardItemModel( this );
41
42 // populate available
43 const auto constProviders = QgsGui::layerTreeEmbeddedWidgetRegistry()->providers();
44 for ( const QString &providerId : constProviders )
45 {
47 if ( provider->supportsLayer( mLayer ) )
48 {
49 QStandardItem *item = new QStandardItem( provider->name() );
50 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
51 item->setData( provider->id(), Qt::UserRole + 1 );
52 modelAvailable->appendRow( item );
53 }
54 }
55 mListAvailable->setModel( modelAvailable );
56
57 // populate used
58 const int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
59 for ( int i = 0; i < widgetsCount; ++i )
60 {
61 const QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
62 if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
63 {
64 QStandardItem *item = new QStandardItem( provider->name() );
65 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
66 item->setData( provider->id(), Qt::UserRole + 1 );
67 modelUsed->appendRow( item );
68 }
69 }
70 mListUsed->setModel( modelUsed );
71}
72
73void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
74{
75 if ( !mListAvailable->currentIndex().isValid() )
76 return;
77
78 const QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
80 if ( !provider )
81 return;
82
83 if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mListUsed->model() ) )
84 {
85 QStandardItem *item = new QStandardItem( provider->name() );
86 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
87 item->setData( provider->id(), Qt::UserRole + 1 );
88 model->appendRow( item );
89 }
90}
91
92void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
93{
94 if ( !mListUsed->currentIndex().isValid() )
95 return;
96
97 const int row = mListUsed->currentIndex().row();
98 mListUsed->model()->removeRow( row );
99}
100
102{
103 if ( !mLayer )
104 return;
105
106 // clear old properties
107 const int widgetsCount = mLayer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
108 for ( int i = 0; i < widgetsCount; ++i )
109 {
110 mLayer->removeCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) );
111 }
112
113 // setup new properties
114 const int newCount = mListUsed->model()->rowCount();
115 mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/count" ), newCount );
116 for ( int i = 0; i < newCount; ++i )
117 {
118 const QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
119 mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ), providerId );
120 }
121}
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition qgsgui.cpp:129
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:76
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.