QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
21 #include <QStringListModel>
22 #include <QStandardItemModel>
23 
25  : QWidget( parent )
26  , mLayer( nullptr )
27 {
28  setupUi( this );
29 }
30 
32 {
33  mLayer = layer;
34 
35  connect( mBtnAdd, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) );
36  connect( mBtnRemove, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) );
37 
38  QStandardItemModel* modelAvailable = new QStandardItemModel( this );
39  QStandardItemModel* modelUsed = new QStandardItemModel( this );
40 
41  // populate available
42  Q_FOREACH ( const QString& providerId, QgsLayerTreeEmbeddedWidgetRegistry::instance()->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( "embeddedWidgets/count", 0 ).toInt();
54  for ( int i = 0; i < widgetsCount; ++i )
55  {
56  QString providerId = layer->customProperty( QString( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
57  if ( QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->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( "embeddedWidgets/count", 0 ).toInt();
103  for ( int i = 0; i < widgetsCount; ++i )
104  {
105  mLayer->removeCustomProperty( QString( "embeddedWidgets/%1/id" ).arg( i ) );
106  }
107 
108  // setup new properties
109  int newCount = mListUsed->model()->rowCount();
110  mLayer->setCustomProperty( "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( QString( "embeddedWidgets/%1/id" ).arg( i ), providerId );
115  }
116 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setupUi(QWidget *widget)
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 void setData(const QVariant &value, int role)
int toInt(bool *ok) const
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 setFlags(QFlags< Qt::ItemFlag > flags)
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.
static QgsLayerTreeEmbeddedWidgetRegistry * instance()
Means of accessing canonical single instance.
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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsLayerTreeEmbeddedWidgetProvider * provider(const QString &providerId) const
Get provider object from the provider&#39;s ID.
QChar * data()
QString toString() const
void appendRow(const QList< QStandardItem * > &items)
virtual QString name() const =0
Human readable name - may be translatable with tr()