-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgslayertreeembeddedconfigwidget.cpp
119 lines (102 loc) · 4.54 KB
/
qgslayertreeembeddedconfigwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***************************************************************************
qgslayertreeembeddedconfigwidget.cpp
--------------------------------------
Date : May 2016
Copyright : (C) 2016 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayertreeembeddedconfigwidget.h"
#include "qgsmaplayer.h"
#include "qgslayertreeembeddedwidgetregistry.h"
#include "qgsgui.h"
#include <QStringListModel>
#include <QStandardItemModel>
QgsLayerTreeEmbeddedConfigWidget::QgsLayerTreeEmbeddedConfigWidget( QWidget *parent )
: QWidget( parent )
{
setupUi( this );
}
void QgsLayerTreeEmbeddedConfigWidget::setLayer( QgsMapLayer *layer )
{
mLayer = layer;
connect( mBtnAdd, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onAddClicked );
connect( mBtnRemove, &QAbstractButton::clicked, this, &QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked );
QStandardItemModel *modelAvailable = new QStandardItemModel( this );
QStandardItemModel *modelUsed = new QStandardItemModel( this );
// populate available
Q_FOREACH ( const QString &providerId, QgsGui::layerTreeEmbeddedWidgetRegistry()->providers() )
{
QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId );
if ( provider->supportsLayer( mLayer ) )
{
QStandardItem *item = new QStandardItem( provider->name() );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
item->setData( provider->id(), Qt::UserRole + 1 );
modelAvailable->appendRow( item );
}
}
mListAvailable->setModel( modelAvailable );
// populate used
int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
for ( int i = 0; i < widgetsCount; ++i )
{
QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
{
QStandardItem *item = new QStandardItem( provider->name() );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
item->setData( provider->id(), Qt::UserRole + 1 );
modelUsed->appendRow( item );
}
}
mListUsed->setModel( modelUsed );
}
void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
{
if ( !mListAvailable->currentIndex().isValid() )
return;
QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId );
if ( !provider )
return;
if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mListUsed->model() ) )
{
QStandardItem *item = new QStandardItem( provider->name() );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
item->setData( provider->id(), Qt::UserRole + 1 );
model->appendRow( item );
}
}
void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
{
if ( !mListUsed->currentIndex().isValid() )
return;
int row = mListUsed->currentIndex().row();
mListUsed->model()->removeRow( row );
}
void QgsLayerTreeEmbeddedConfigWidget::applyToLayer()
{
if ( !mLayer )
return;
// clear old properties
int widgetsCount = mLayer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
for ( int i = 0; i < widgetsCount; ++i )
{
mLayer->removeCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) );
}
// setup new properties
int newCount = mListUsed->model()->rowCount();
mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/count" ), newCount );
for ( int i = 0; i < newCount; ++i )
{
QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
mLayer->setCustomProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ), providerId );
}
}