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