QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprocessingvectortilewriterlayerswidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingvectortilewriterlayerswidgetwrapper.cpp
3 ---------------------
4 Date : April 2020
5 Copyright : (C) 2020 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#include "moc_qgsprocessingvectortilewriterlayerswidgetwrapper.cpp"
18
19#include <QBoxLayout>
20#include <QLineEdit>
21#include <QMessageBox>
22#include <QPushButton>
23#include <QStandardItemModel>
24#include <QToolButton>
25
26#include "qgspanelwidget.h"
27
28#include "qgsvectortilewriter.h"
29
31
33
34//
35// QgsProcessingVectorTileWriteLayerDetailsWidget
36//
37
38QgsProcessingVectorTileWriteLayerDetailsWidget::QgsProcessingVectorTileWriteLayerDetailsWidget( const QVariant &value, QgsProject *project )
39{
40 setupUi( this );
41
42 mContext.setProject( project );
43
45 mLayer = layer.layer();
46
47 if ( !mLayer )
48 return;
49
50 mSpinMinZoom->setClearValue( -1, tr( "Not set" ) );
51 mSpinMaxZoom->setClearValue( -1, tr( "Not set" ) );
52 mEditFilterExpression->setMultiLine( true );
53 mEditFilterExpression->setLayer( mLayer );
54
55 mSpinMinZoom->setValue( layer.minZoom() );
56 mSpinMaxZoom->setValue( layer.maxZoom() );
57 mEditLayerName->setText( layer.layerName() );
58 mEditLayerName->setPlaceholderText( mLayer->name() );
59 mEditFilterExpression->setExpression( layer.filterExpression() );
60
61 connect( mSpinMinZoom, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
62 connect( mSpinMaxZoom, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
63 connect( mEditLayerName, &QLineEdit::textChanged, this, &QgsPanelWidget::widgetChanged );
64 connect( mEditFilterExpression, &QgsExpressionLineEdit::expressionChanged, this, &QgsPanelWidget::widgetChanged );
65}
66
67QVariant QgsProcessingVectorTileWriteLayerDetailsWidget::value() const
68{
69 QgsVectorTileWriter::Layer layer( mLayer );
70 layer.setMinZoom( mSpinMinZoom->value() );
71 layer.setMaxZoom( mSpinMaxZoom->value() );
72 layer.setLayerName( mEditLayerName->text() );
73 layer.setFilterExpression( mEditFilterExpression->expression() );
75}
76
77//
78// QgsProcessingVectorTileWriterLayersPanelWidget
79//
80
81
82QgsProcessingVectorTileWriterLayersPanelWidget::QgsProcessingVectorTileWriterLayersPanelWidget(
83 const QVariant &value,
84 QgsProject *project,
85 QWidget *parent
86)
87 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), QVariantList(), parent )
88 , mProject( project )
89{
90 connect( listView(), &QListView::doubleClicked, this, &QgsProcessingVectorTileWriterLayersPanelWidget::configureLayer );
91
92 QPushButton *configureLayerButton = new QPushButton( tr( "Configure Layer…" ) );
93 connect( configureLayerButton, &QPushButton::clicked, this, &QgsProcessingVectorTileWriterLayersPanelWidget::configureLayer );
94 buttonBox()->addButton( configureLayerButton, QDialogButtonBox::ActionRole );
95
96 QPushButton *copyLayerButton = new QPushButton( tr( "Copy Layer" ) );
97 connect( copyLayerButton, &QPushButton::clicked, this, &QgsProcessingVectorTileWriterLayersPanelWidget::copyLayer );
98 buttonBox()->addButton( copyLayerButton, QDialogButtonBox::ActionRole );
99
100 // populate the list: first layers already selected, then layers from project not yet selected
101 mContext.setProject( project );
102
103 QSet<const QgsVectorLayer *> seenVectorLayers;
104 const QVariantList valueList = value.toList();
105 for ( const QVariant &v : valueList )
106 {
108 if ( !layer.layer() )
109 continue; // skip any invalid layers
110
111 addOption( v, titleForLayer( layer ), true );
112
113 seenVectorLayers.insert( layer.layer() );
114 }
115
116 const QList<QgsVectorLayer *> options = QgsProcessingUtils::compatibleVectorLayers( project, QList<int>() );
117 for ( const QgsVectorLayer *layer : options )
118 {
119 if ( seenVectorLayers.contains( layer ) )
120 continue;
121
122 QVariantMap vm;
123 vm["layer"] = layer->id();
124
125 const QString title = layer->name();
126
127 addOption( vm, title, false );
128 }
129}
130
131void QgsProcessingVectorTileWriterLayersPanelWidget::configureLayer()
132{
133 const QModelIndexList selection = listView()->selectionModel()->selectedIndexes();
134 if ( selection.size() != 1 )
135 {
136 QMessageBox::warning( this, tr( "Configure Layer" ), tr( "Please select a single layer." ) );
137 return;
138 }
139
140 QStandardItem *item = mModel->itemFromIndex( selection[0] );
141 const QVariant value = item->data( Qt::UserRole );
142
144 if ( panel && panel->dockMode() )
145 {
146 QgsProcessingVectorTileWriteLayerDetailsWidget *widget = new QgsProcessingVectorTileWriteLayerDetailsWidget( value, mProject );
147 widget->setPanelTitle( tr( "Configure Layer" ) );
148 widget->buttonBox()->hide();
149
150 connect( widget, &QgsProcessingVectorTileWriteLayerDetailsWidget::widgetChanged, this, [=]() {
151 setItemValue( item, widget->value() );
152 } );
153 panel->openPanel( widget );
154 }
155 else
156 {
157 QDialog dlg;
158 dlg.setWindowTitle( tr( "Configure Layer" ) );
159 QVBoxLayout *vLayout = new QVBoxLayout();
160 QgsProcessingVectorTileWriteLayerDetailsWidget *widget = new QgsProcessingVectorTileWriteLayerDetailsWidget( value, mProject );
161 vLayout->addWidget( widget );
162 connect( widget->buttonBox(), &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
163 connect( widget->buttonBox(), &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
164 dlg.setLayout( vLayout );
165 if ( dlg.exec() )
166 {
167 setItemValue( item, widget->value() );
168 }
169 }
170}
171
172void QgsProcessingVectorTileWriterLayersPanelWidget::copyLayer()
173{
174 const QModelIndexList selection = listView()->selectionModel()->selectedIndexes();
175 if ( selection.size() != 1 )
176 {
177 QMessageBox::warning( this, tr( "Copy Layer" ), tr( "Please select a single layer." ) );
178 return;
179 }
180
181 QStandardItem *item = mModel->itemFromIndex( selection[0] );
182 mModel->insertRow( selection[0].row() + 1, item->clone() );
183}
184
185void QgsProcessingVectorTileWriterLayersPanelWidget::setItemValue( QStandardItem *item, const QVariant &value )
186{
187 mContext.setProject( mProject );
188
190
191 item->setText( titleForLayer( layer ) );
192 item->setData( value, Qt::UserRole );
193}
194
195QString QgsProcessingVectorTileWriterLayersPanelWidget::titleForLayer( const QgsVectorTileWriter::Layer &layer )
196{
197 QString title = layer.layer()->name();
198
199 // add more details
200 if ( layer.minZoom() >= 0 && layer.maxZoom() >= 0 )
201 title += tr( " [zoom %1...%2]" ).arg( layer.minZoom() ).arg( layer.maxZoom() );
202 else if ( layer.minZoom() >= 0 )
203 title += tr( " [zoom >= %1]" ).arg( layer.minZoom() );
204 else if ( layer.maxZoom() >= 0 )
205 title += tr( " [zoom <= %1]" ).arg( layer.maxZoom() );
206
207 if ( !layer.layerName().isEmpty() )
208 title += tr( " [name: %1]" ).arg( layer.layerName() );
209 if ( !layer.filterExpression().isEmpty() )
210 title += tr( " [with filter]" );
211
212 return title;
213}
214
215
216//
217// QgsProcessingVectorTileWriterLayersWidget
218//
219
220
221QgsProcessingVectorTileWriterLayersWidget::QgsProcessingVectorTileWriterLayersWidget( QWidget *parent )
222 : QWidget( parent )
223{
224 QHBoxLayout *hl = new QHBoxLayout();
225 hl->setContentsMargins( 0, 0, 0, 0 );
226
227 mLineEdit = new QLineEdit();
228 mLineEdit->setEnabled( false );
229 hl->addWidget( mLineEdit, 1 );
230
231 mToolButton = new QToolButton();
232 mToolButton->setText( QString( QChar( 0x2026 ) ) );
233 hl->addWidget( mToolButton );
234
235 setLayout( hl );
236
237 updateSummaryText();
238
239 connect( mToolButton, &QToolButton::clicked, this, &QgsProcessingVectorTileWriterLayersWidget::showDialog );
240}
241
242void QgsProcessingVectorTileWriterLayersWidget::setValue( const QVariant &value )
243{
244 if ( value.isValid() )
245 mValue = value.userType() == QMetaType::Type::QVariantList ? value.toList() : QVariantList() << value;
246 else
247 mValue.clear();
248
249 updateSummaryText();
250 emit changed();
251}
252
253void QgsProcessingVectorTileWriterLayersWidget::setProject( QgsProject *project )
254{
255 mProject = project;
256}
257
258void QgsProcessingVectorTileWriterLayersWidget::showDialog()
259{
261 if ( panel && panel->dockMode() )
262 {
263 QgsProcessingVectorTileWriterLayersPanelWidget *widget = new QgsProcessingVectorTileWriterLayersPanelWidget( mValue, mProject );
264 widget->setPanelTitle( tr( "Input layers" ) );
265 connect( widget, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged, this, [=]() {
266 setValue( widget->selectedOptions() );
267 } );
268 connect( widget, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked, widget, &QgsPanelWidget::acceptPanel );
269 panel->openPanel( widget );
270 }
271 else
272 {
273 QDialog dlg;
274 dlg.setWindowTitle( tr( "Input layers" ) );
275 QVBoxLayout *vLayout = new QVBoxLayout();
276 QgsProcessingVectorTileWriterLayersPanelWidget *widget = new QgsProcessingVectorTileWriterLayersPanelWidget( mValue, mProject );
277 vLayout->addWidget( widget );
278 widget->buttonBox()->addButton( QDialogButtonBox::Cancel );
279 connect( widget->buttonBox(), &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
280 connect( widget->buttonBox(), &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
281 dlg.setLayout( vLayout );
282 if ( dlg.exec() )
283 {
284 setValue( widget->selectedOptions() );
285 }
286 }
287}
288
289void QgsProcessingVectorTileWriterLayersWidget::updateSummaryText()
290{
291 mLineEdit->setText( tr( "%n vector layer(s) selected", nullptr, mValue.count() ) );
292}
293
294//
295// QgsProcessingVectorTileWriterLayersWidgetWrapper
296//
297
298QgsProcessingVectorTileWriterLayersWidgetWrapper::QgsProcessingVectorTileWriterLayersWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
299 : QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
300{
301}
302
303QString QgsProcessingVectorTileWriterLayersWidgetWrapper::parameterType() const
304{
306}
307
308QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingVectorTileWriterLayersWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
309{
310 return new QgsProcessingVectorTileWriterLayersWidgetWrapper( parameter, type );
311}
312
313QWidget *QgsProcessingVectorTileWriterLayersWidgetWrapper::createWidget()
314{
315 mPanel = new QgsProcessingVectorTileWriterLayersWidget( nullptr );
316 mPanel->setProject( widgetContext().project() );
317 connect( mPanel, &QgsProcessingVectorTileWriterLayersWidget::changed, this, [=] {
318 emit widgetValueHasChanged( this );
319 } );
320 return mPanel;
321}
322
323void QgsProcessingVectorTileWriterLayersWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
324{
326 if ( mPanel )
327 {
328 mPanel->setProject( context.project() );
329 }
330}
331
332void QgsProcessingVectorTileWriterLayersWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
333{
334 Q_UNUSED( context )
335 if ( mPanel )
336 {
337 mPanel->setValue( value );
338 }
339}
340
341QVariant QgsProcessingVectorTileWriterLayersWidgetWrapper::widgetValue() const
342{
343 return mPanel ? mPanel->value() : QVariant();
344}
345
346QStringList QgsProcessingVectorTileWriterLayersWidgetWrapper::compatibleParameterTypes() const
347{
348 return QStringList();
349}
350
351QStringList QgsProcessingVectorTileWriterLayersWidgetWrapper::compatibleOutputTypes() const
352{
353 return QStringList();
354}
355
A widget wrapper for Processing parameter value widgets.
virtual void setWidgetContext(const QgsProcessingParameterWidgetContext &context)
Sets the context in which the Processing parameter widget is shown, e.g., the parent model algorithm,...
void expressionChanged(const QString &expression)
Emitted when the expression is changed.
QString name
Definition qgsmaplayer.h:80
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
void acceptPanel()
Accept the panel.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
bool dockMode()
Returns the dock mode state.
Contains information about the context in which a processing algorithm is executed.
WidgetType
Types of dialogs which Processing widgets can be created for.
Base class for the definition of processing parameters.
static QgsVectorTileWriter::Layer variantMapAsLayer(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
static QVariantMap layerAsVariantMap(const QgsVectorTileWriter::Layer &layer)
Converts a single input layer to QVariant representation (a QVariantMap)
static QString typeName()
Returns the type name for the parameter class.
Contains settings which reflect the context in which a Processing parameter widget is shown,...
QgsProject * project() const
Returns the project associated with the widget.
static QList< QgsVectorLayer * > compatibleVectorLayers(QgsProject *project, const QList< int > &sourceTypes=QList< int >(), bool sort=true)
Returns a list of vector layers from a project which are compatible with the processing framework.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Represents a vector layer which manages a vector based data sets.
Configuration of a single input vector layer to be included in the output.
QString layerName() const
Returns layer name in the output. If not set, layer()->name() will be used.
void setLayerName(const QString &name)
Sets layer name in the output. If not set, layer()->name() will be used.
QgsVectorLayer * layer() const
Returns vector layer of this entry.
void setMaxZoom(int maxzoom)
Sets maximum zoom level at which this layer will be used. Negative value means no max....
void setFilterExpression(const QString &expr)
Sets filter expression. If not empty, only features matching the expression will be used.
void setMinZoom(int minzoom)
Sets minimum zoom level at which this layer will be used. Negative value means no min....
QString filterExpression() const
Returns filter expression. If not empty, only features matching the expression will be used.
int maxZoom() const
Returns maximum zoom level at which this layer will be used. Negative value means no max....
int minZoom() const
Returns minimum zoom level at which this layer will be used. Negative value means no min....