QGIS API Documentation  3.0.2-Girona (307d082)
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  QStandardItem *item = new QStandardItem( provider->name() );
46  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
47  item->setData( provider->id(), Qt::UserRole + 1 );
48  modelAvailable->appendRow( item );
49  }
50  mListAvailable->setModel( modelAvailable );
51 
52  // populate used
53  int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
54  for ( int i = 0; i < widgetsCount; ++i )
55  {
56  QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
57  if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
58  {
59  QStandardItem *item = new QStandardItem( provider->name() );
60  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
61  item->setData( provider->id(), Qt::UserRole + 1 );
62  modelUsed->appendRow( item );
63  }
64  }
65  mListUsed->setModel( modelUsed );
66 }
67 
68 void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
69 {
70  if ( !mListAvailable->currentIndex().isValid() )
71  return;
72 
73  QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
75  if ( !provider )
76  return;
77 
78  if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mListUsed->model() ) )
79  {
80  QStandardItem *item = new QStandardItem( provider->name() );
81  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
82  item->setData( provider->id(), Qt::UserRole + 1 );
83  model->appendRow( item );
84  }
85 }
86 
87 void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
88 {
89  if ( !mListUsed->currentIndex().isValid() )
90  return;
91 
92  int row = mListUsed->currentIndex().row();
93  mListUsed->model()->removeRow( row );
94 }
95 
97 {
98  if ( !mLayer )
99  return;
100 
101  // clear old properties
102  int widgetsCount = mLayer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
103  for ( int i = 0; i < widgetsCount; ++i )
104  {
105  mLayer->removeCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) );
106  }
107 
108  // setup new properties
109  int newCount = mListUsed->model()->rowCount();
110  mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/count" ), newCount );
111  for ( int i = 0; i < newCount; ++i )
112  {
113  QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
114  mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ), providerId );
115  }
116 }
Base class for all map layer types.
Definition: qgsmaplayer.h:56
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)
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:61
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.
QgsLayerTreeEmbeddedConfigWidget(QWidget *parent=nullptr)
A widget to configure layer tree embedded widgets for a particular map layer.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void setLayer(QgsMapLayer *layer)
Initialize widget with a map layer.
QgsLayerTreeEmbeddedWidgetProvider * provider(const QString &providerId) const
Get provider object from the provider&#39;s ID.
virtual QString name() const =0
Human readable name - may be translatable with tr()