/
qgsarcgisrestsourceselect.cpp
537 lines (462 loc) · 18.7 KB
/
qgsarcgisrestsourceselect.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/***************************************************************************
qgsarcgisrestsourceselect.cpp
-------------------------
begin : Nov 26, 2015
copyright : (C) 2015 by Sandro Mani
email : smani@sourcepole.ch
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsarcgisrestsourceselect.h"
#include "qgsowsconnection.h"
#include "qgsnewhttpconnection.h"
#include "qgsprojectionselectiondialog.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsproject.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransform.h"
#include "qgslogger.h"
#include "qgsmanageconnectionsdialog.h"
#include "qgsexception.h"
#include "qgssettings.h"
#include "qgsmapcanvas.h"
#include "qgshelp.h"
#include "qgsgui.h"
#include "qgsbrowserguimodel.h"
#include "qgsarcgisrestdataitems.h"
#include <QButtonGroup>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QFileDialog>
#include <QRadioButton>
#include <QImageReader>
//
// QgsArcGisRestBrowserProxyModel
//
QgsArcGisRestBrowserProxyModel::QgsArcGisRestBrowserProxyModel( QObject *parent )
: QgsBrowserProxyModel( parent )
{
}
void QgsArcGisRestBrowserProxyModel::setConnectionName( const QString &name )
{
mConnectionName = name;
invalidateFilter();
}
bool QgsArcGisRestBrowserProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
if ( !QgsBrowserProxyModel::filterAcceptsRow( sourceRow, sourceParent ) )
return false;
QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
if ( QgsArcGisRestConnectionItem *connectionItem = qobject_cast< QgsArcGisRestConnectionItem * >( mModel->dataItem( sourceIndex ) ) )
{
if ( connectionItem->name() != mConnectionName )
return false;
}
return true;
}
//
// QgsArcGisRestSourceSelect
//
QgsArcGisRestSourceSelect::QgsArcGisRestSourceSelect( const QString &serviceName, ServiceType serviceType, QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode )
: QgsAbstractDataSourceWidget( parent, fl, widgetMode )
, mServiceName( serviceName )
, mServiceType( serviceType )
{
setupUi( this );
QgsGui::instance()->enableAutoGeometryRestore( this );
connect( cmbConnections, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsArcGisRestSourceSelect::cmbConnections_activated );
setupButtons( buttonBox );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsArcGisRestSourceSelect::showHelp );
setWindowTitle( QStringLiteral( "Add %1 Layer from a Server" ).arg( mServiceName ) );
if ( mServiceType == FeatureService )
{
mBuildQueryButton = buttonBox->addButton( tr( "&Build query" ), QDialogButtonBox::ActionRole );
mBuildQueryButton->setDisabled( true );
connect( mBuildQueryButton, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::buildQueryButtonClicked );
}
connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
connect( btnNew, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::addEntryToServerList );
connect( btnEdit, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::modifyEntryOfServerList );
connect( btnDelete, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::deleteEntryOfServerList );
connect( btnRefresh, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::onRefresh );
connect( btnConnect, &QAbstractButton::clicked, this, &QgsArcGisRestSourceSelect::connectToServer );
connect( btnSave, &QPushButton::clicked, this, &QgsArcGisRestSourceSelect::btnSave_clicked );
connect( btnLoad, &QPushButton::clicked, this, &QgsArcGisRestSourceSelect::btnLoad_clicked );
connect( lineFilter, &QLineEdit::textChanged, this, &QgsArcGisRestSourceSelect::filterChanged );
populateConnectionList();
lineFilter->setShowClearButton( true );
lineFilter->setShowSearchIcon( true );
QgsSettings settings;
mImageEncodingGroup = new QButtonGroup( this );
}
QgsArcGisRestSourceSelect::~QgsArcGisRestSourceSelect()
{
}
void QgsArcGisRestSourceSelect::populateImageEncodings( const QString &formats )
{
const QStringList availableEncodings = formats.split( ',' );
const QString prevFormat = getSelectedImageEncoding();
QLayoutItem *item = nullptr;
while ( ( item = gbImageEncoding->layout()->takeAt( 0 ) ) )
{
delete item->widget();
delete item;
}
const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
for ( const QString &encoding : availableEncodings )
{
bool supported = false;
for ( const QByteArray &fmt : supportedFormats )
{
if ( encoding.startsWith( fmt, Qt::CaseInsensitive ) )
{
supported = true;
}
}
if ( !supported )
{
continue;
}
QRadioButton *button = new QRadioButton( encoding, this );
if ( encoding == prevFormat )
button->setChecked( true );
gbImageEncoding->layout()->addWidget( button );
mImageEncodingGroup->addButton( button );
}
if ( !mImageEncodingGroup->checkedButton() && !mImageEncodingGroup->buttons().empty() )
mImageEncodingGroup->buttons().value( 0 )->setChecked( true );
}
QString QgsArcGisRestSourceSelect::getSelectedImageEncoding() const
{
return mImageEncodingGroup && mImageEncodingGroup->checkedButton() ? mImageEncodingGroup->checkedButton()->text() : QString();
}
void QgsArcGisRestSourceSelect::showEvent( QShowEvent * )
{
if ( QgsBrowserGuiModel *model = qobject_cast< QgsBrowserGuiModel * >( browserModel() ) )
{
mBrowserModel = model;
}
else
{
mBrowserModel = new QgsBrowserGuiModel( this );
}
mBrowserModel->initialize();
mProxyModel = new QgsArcGisRestBrowserProxyModel( this );
mProxyModel->setBrowserModel( mBrowserModel );
mBrowserView->setSettingsSection( objectName().toLower() ); // to distinguish 2 or more instances of the browser
mBrowserView->setBrowserModel( mBrowserModel );
mBrowserView->setModel( mProxyModel );
mBrowserView->setSortingEnabled( true );
mBrowserView->sortByColumn( 0, Qt::AscendingOrder );
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
mBrowserView->setTextElideMode( Qt::ElideNone );
connect( mBrowserView, &QAbstractItemView::doubleClicked, this, &QgsArcGisRestSourceSelect::treeWidgetItemDoubleClicked );
connect( mBrowserView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &QgsArcGisRestSourceSelect::treeWidgetCurrentRowChanged );
mBrowserView->expand( mProxyModel->index( 0, 0 ) );
mBrowserView->setHeaderHidden( true );
mProxyModel->setShownDataItemProviderKeyFilter( QStringList() << QStringLiteral( "AFS" ) << QStringLiteral( "arcgisfeatureserver" )
<< QStringLiteral( "AMS" ) << QStringLiteral( "arcgismapserver" ) );
const QModelIndex afsSourceIndex = mBrowserModel->findPath( QStringLiteral( "arcgisfeatureserver:" ) );
mBrowserView->setRootIndex( mProxyModel->mapFromSource( afsSourceIndex ) );
// don't show anything till connect is clicked!
mProxyModel->setConnectionName( QString() );
}
void QgsArcGisRestSourceSelect::populateConnectionList()
{
const QStringList conns = QgsOwsConnection::connectionList( mServiceName );
cmbConnections->clear();
for ( const QString &item : conns )
{
cmbConnections->addItem( item );
}
bool connectionsAvailable = !conns.isEmpty();
btnEdit->setEnabled( connectionsAvailable );
btnDelete->setEnabled( connectionsAvailable );
btnSave->setEnabled( connectionsAvailable );
//set last used connection
QString selectedConnection = QgsOwsConnection::selectedConnection( mServiceName );
int index = cmbConnections->findText( selectedConnection );
if ( index != -1 )
{
cmbConnections->setCurrentIndex( index );
}
}
void QgsArcGisRestSourceSelect::refresh()
{
populateConnectionList();
}
void QgsArcGisRestSourceSelect::addEntryToServerList()
{
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-%1/" ).arg( mServiceName.toLower() ), QString(), QgsNewHttpConnection::FlagShowHttpSettings );
nc.setWindowTitle( tr( "Create a New %1 Connection" ).arg( mServiceName ) );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
}
void QgsArcGisRestSourceSelect::modifyEntryOfServerList()
{
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionOther, QStringLiteral( "qgis/connections-%1/" ).arg( mServiceName.toLower() ), cmbConnections->currentText(), QgsNewHttpConnection::FlagShowHttpSettings );
nc.setWindowTitle( tr( "Modify %1 Connection" ).arg( mServiceName ) );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
}
void QgsArcGisRestSourceSelect::deleteEntryOfServerList()
{
const QString selectedConnection = cmbConnections->currentText();
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
.arg( selectedConnection );
QMessageBox::StandardButton result = QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No );
if ( result == QMessageBox::Yes )
{
QgsOwsConnection::deleteConnection( mServiceName, selectedConnection );
cmbConnections->removeItem( cmbConnections->currentIndex() );
emit connectionsChanged();
bool connectionsAvailable = cmbConnections->count() > 0;
btnEdit->setEnabled( connectionsAvailable );
btnDelete->setEnabled( connectionsAvailable );
btnSave->setEnabled( connectionsAvailable );
if ( selectedConnection == mConnectedService )
disconnectFromServer();
}
}
void QgsArcGisRestSourceSelect::connectToServer()
{
bool haveLayers = false;
btnConnect->setEnabled( false );
mConnectedService = cmbConnections->currentText();
QgsOwsConnection connection( mServiceName, mConnectedService );
// find index of corresponding node
if ( mBrowserModel && mProxyModel )
{
mProxyModel->setConnectionName( mConnectedService );
mBrowserView->expand( mProxyModel->index( 0, 0, mBrowserView->rootIndex() ) );
onRefresh();
}
setCursor( Qt::WaitCursor );
connectToService( connection );
unsetCursor();
btnConnect->setEnabled( true );
emit enableButtons( haveLayers );
mBuildQueryButton->setEnabled( haveLayers );
updateCrsLabel();
}
void QgsArcGisRestSourceSelect::disconnectFromServer()
{
mProxyModel->setConnectionName( QString() );
emit enableButtons( false );
}
void QgsArcGisRestSourceSelect::addButtonClicked()
{
if ( mBrowserView->selectionModel()->selectedRows().isEmpty() )
{
return;
}
QgsOwsConnection connection( mServiceName, cmbConnections->currentText() );
QString pCrsString( labelCoordRefSys->text() );
QgsCoordinateReferenceSystem pCrs( pCrsString );
//prepare canvas extent info for layers with "cache features" option not set
QgsRectangle extent;
QgsCoordinateReferenceSystem canvasCrs;
if ( auto *lMapCanvas = mapCanvas() )
{
extent = lMapCanvas->extent();
canvasCrs = lMapCanvas->mapSettings().destinationCrs();
}
//does canvas have "on the fly" reprojection set?
if ( pCrs.isValid() && canvasCrs.isValid() )
{
try
{
extent = QgsCoordinateTransform( canvasCrs, pCrs, QgsProject::instance()->transformContext() ).transform( extent );
QgsDebugMsg( QStringLiteral( "canvas transform: Canvas CRS=%1, Provider CRS=%2, BBOX=%3" )
.arg( canvasCrs.authid(), pCrs.authid(), extent.asWktCoordinates() ) );
}
catch ( const QgsCsException & )
{
// Extent is not in range for specified CRS, leave extent empty.
}
}
//create layers that user selected from this feature source
const QModelIndexList list = mBrowserView->selectionModel()->selectedRows();
for ( const QModelIndex &proxyIndex : list )
{
const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
if ( !sourceIndex.isValid() )
{
continue;
}
QgsDataItem *item = mBrowserModel->dataItem( sourceIndex );
if ( !item )
continue;
if ( QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
{
const QString layerName = layerItem->name();
QString filter;// = mServiceType == FeatureService ? mModel->itemFromIndex( mModel->index( row, 3, idx.parent() ) )->text() : QString(); //optional filter specified by user
QgsRectangle layerExtent;
if ( mServiceType == FeatureService && ( cbxFeatureCurrentViewExtent->isChecked() ) )
{
layerExtent = extent;
}
QgsDataSourceUri uri( layerItem->uri() );
uri.setParam( QStringLiteral( "crs" ), pCrsString );
if ( qobject_cast< QgsArcGisFeatureServiceLayerItem *>( layerItem ) )
{
uri.setParam( QStringLiteral( "filter" ), filter );
if ( !layerExtent.isEmpty() )
{
uri.setParam( QStringLiteral( "bbox" ), QStringLiteral( "%1,%2,%3,%4" ).arg( layerExtent.xMinimum() ).arg( layerExtent.yMinimum() ).arg( layerExtent.xMaximum() ).arg( layerExtent.yMaximum() ) );
}
emit addVectorLayer( uri.uri( false ), layerName );
}
else if ( qobject_cast< QgsArcGisMapServiceLayerItem *>( layerItem ) )
{
uri.removeParam( QStringLiteral( "format" ) );
uri.setParam( QStringLiteral( "format" ), getSelectedImageEncoding() );
emit addRasterLayer( uri.uri( false ), layerName, QStringLiteral( "arcgismapserver" ) );
}
}
}
accept();
}
void QgsArcGisRestSourceSelect::updateCrsLabel()
{
//evaluate currently selected typename and set the CRS filter in mProjectionSelector
QModelIndex currentIndex = mBrowserView->selectionModel()->currentIndex();
if ( currentIndex.isValid() )
{
const QModelIndex sourceIndex = mProxyModel->mapToSource( currentIndex );
if ( !sourceIndex.isValid() )
{
labelCoordRefSys->clear();
return;
}
if ( QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( mBrowserModel->dataItem( sourceIndex ) ) )
{
QgsDataSourceUri uri( layerItem->uri() );
labelCoordRefSys->setText( uri.param( QStringLiteral( "crs" ) ) );
}
else
{
labelCoordRefSys->clear();
}
}
}
void QgsArcGisRestSourceSelect::updateImageEncodings()
{
//evaluate currently selected typename and set the CRS filter in mProjectionSelector
QModelIndex currentIndex = mBrowserView->selectionModel()->currentIndex();
if ( currentIndex.isValid() )
{
const QModelIndex sourceIndex = mProxyModel->mapToSource( currentIndex );
if ( !sourceIndex.isValid() )
{
return;
}
if ( QgsArcGisMapServiceLayerItem *layerItem = qobject_cast< QgsArcGisMapServiceLayerItem * >( mBrowserModel->dataItem( sourceIndex ) ) )
{
populateImageEncodings( layerItem->supportedFormats() );
}
}
}
void QgsArcGisRestSourceSelect::cmbConnections_activated( int index )
{
Q_UNUSED( index )
QgsOwsConnection::setSelectedConnection( mServiceName, cmbConnections->currentText() );
}
void QgsArcGisRestSourceSelect::treeWidgetItemDoubleClicked( const QModelIndex &index )
{
QgsDebugMsg( QStringLiteral( "double-click called" ) );
QgsOwsConnection connection( mServiceName, cmbConnections->currentText() );
buildQuery( connection, index );
}
void QgsArcGisRestSourceSelect::treeWidgetCurrentRowChanged( const QModelIndex ¤t, const QModelIndex &previous )
{
Q_UNUSED( previous )
QgsDebugMsg( QStringLiteral( "treeWidget_currentRowChanged called" ) );
updateCrsLabel();
updateImageEncodings();
if ( mServiceType == FeatureService )
{
mBuildQueryButton->setEnabled( current.isValid() );
}
emit enableButtons( current.isValid() );
}
void QgsArcGisRestSourceSelect::buildQueryButtonClicked()
{
QgsDebugMsg( QStringLiteral( "mBuildQueryButton click called" ) );
QgsOwsConnection connection( mServiceName, cmbConnections->currentText() );
buildQuery( connection, mBrowserView->selectionModel()->currentIndex() );
}
void QgsArcGisRestSourceSelect::filterChanged( const QString &text )
{
mProxyModel->setFilterString( text );
}
void QgsArcGisRestSourceSelect::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/index.html" ) );
}
void QgsArcGisRestSourceSelect::btnSave_clicked()
{
QgsManageConnectionsDialog::Type serverType = mServiceType == FeatureService ? QgsManageConnectionsDialog::ArcgisFeatureServer : QgsManageConnectionsDialog::ArcgisMapServer;
QgsManageConnectionsDialog dlg( this, QgsManageConnectionsDialog::Export, serverType );
dlg.exec();
}
void QgsArcGisRestSourceSelect::btnLoad_clicked()
{
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}
QgsManageConnectionsDialog::Type serverType = mServiceType == FeatureService ? QgsManageConnectionsDialog::ArcgisFeatureServer : QgsManageConnectionsDialog::ArcgisMapServer;
QgsManageConnectionsDialog dlg( this, QgsManageConnectionsDialog::Import, serverType, fileName );
dlg.exec();
populateConnectionList();
}
void QgsArcGisRestSourceSelect::onRefresh()
{
if ( mBrowserModel )
refreshModel( mProxyModel->mapToSource( mBrowserView->rootIndex() ) );
}
void QgsArcGisRestSourceSelect::refreshModel( const QModelIndex &index )
{
if ( mBrowserModel && mProxyModel )
{
QgsDataItem *item = mBrowserModel->dataItem( index );
if ( item && ( item->capabilities2() & QgsDataItem::Fertile ) )
{
mBrowserModel->refresh( index );
}
for ( int i = 0; i < mBrowserModel->rowCount( index ); i++ )
{
QModelIndex idx = mBrowserModel->index( i, 0, index );
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
QgsDataItem *child = mBrowserModel->dataItem( idx );
// Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
// Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & QgsDataItem::Fast ) )
{
refreshModel( idx );
}
else
{
if ( child && ( child->capabilities2() & QgsDataItem::Fertile ) )
{
child->depopulate();
}
}
}
}
}