QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  Q_FOREACH ( const QString &providerId, QgsGui::layerTreeEmbeddedWidgetRegistry()->providers() )
43  {
45  if ( provider->supportsLayer( mLayer ) )
46  {
47  QStandardItem *item = new QStandardItem( provider->name() );
48  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
49  item->setData( provider->id(), Qt::UserRole + 1 );
50  modelAvailable->appendRow( item );
51  }
52  }
53  mListAvailable->setModel( modelAvailable );
54 
55  // populate used
56  int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
57  for ( int i = 0; i < widgetsCount; ++i )
58  {
59  QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
60  if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
61  {
62  QStandardItem *item = new QStandardItem( provider->name() );
63  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
64  item->setData( provider->id(), Qt::UserRole + 1 );
65  modelUsed->appendRow( item );
66  }
67  }
68  mListUsed->setModel( modelUsed );
69 }
70 
71 void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
72 {
73  if ( !mListAvailable->currentIndex().isValid() )
74  return;
75 
76  QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
78  if ( !provider )
79  return;
80 
81  if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mListUsed->model() ) )
82  {
83  QStandardItem *item = new QStandardItem( provider->name() );
84  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
85  item->setData( provider->id(), Qt::UserRole + 1 );
86  model->appendRow( item );
87  }
88 }
89 
90 void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
91 {
92  if ( !mListUsed->currentIndex().isValid() )
93  return;
94 
95  int row = mListUsed->currentIndex().row();
96  mListUsed->model()->removeRow( row );
97 }
98 
100 {
101  if ( !mLayer )
102  return;
103 
104  // clear old properties
105  int widgetsCount = mLayer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
106  for ( int i = 0; i < widgetsCount; ++i )
107  {
108  mLayer->removeCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) );
109  }
110 
111  // setup new properties
112  int newCount = mListUsed->model()->rowCount();
113  mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/count" ), newCount );
114  for ( int i = 0; i < newCount; ++i )
115  {
116  QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
117  mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ), providerId );
118  }
119 }
Base class for all map layer types.
Definition: qgsmaplayer.h:63
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
virtual QString id() const =0
Unique name of the provider (among other providers)
virtual bool supportsLayer(QgsMapLayer *layer)=0
Whether it makes sense to use this widget for a particular layer.
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:73
Provider interface to be implemented in order to introduce new kinds of embedded widgets for use in l...
void applyToLayer()
Store changes made in the widget to the layer.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsLayerTreeEmbeddedWidgetProvider * provider(const QString &providerId) const
Gets provider object from the provider&#39;s ID.
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.
virtual QString name() const =0
Human readable name - may be translatable with tr()