/src/providers/arcgisrest/qgsarcgisrestdataitemguiprovider.cpp
https://github.com/ricardogsilva/Quantum-GIS · C++ · 189 lines · 151 code · 21 blank · 17 comment · 25 complexity · 9f66a2351bfcafb531c97fee6d69c6fb MD5 · raw file
- /***************************************************************************
- qgsarcgisrestdataitemguiprovider.cpp
- --------------------------------------
- Date : June 2019
- Copyright : (C) 2019 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 "qgsarcgisrestdataitemguiprovider.h"
- #include "qgsarcgisrestdataitems.h"
- #include "qgsmanageconnectionsdialog.h"
- #include "qgsnewarcgisrestconnection.h"
- #include "qgsowsconnection.h"
- #include <QDesktopServices>
- #include <QFileDialog>
- #include <QMessageBox>
- void QgsArcGisRestDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
- {
- if ( QgsArcGisRestRootItem *rootItem = qobject_cast< QgsArcGisRestRootItem * >( item ) )
- {
- QAction *actionNew = new QAction( tr( "New Connection…" ), menu );
- connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
- menu->addAction( actionNew );
- QAction *actionSaveServers = new QAction( tr( "Save Connections…" ), menu );
- connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
- menu->addAction( actionSaveServers );
- QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), menu );
- connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
- menu->addAction( actionLoadServers );
- }
- else if ( QgsArcGisRestConnectionItem *connectionItem = qobject_cast< QgsArcGisRestConnectionItem * >( item ) )
- {
- QAction *actionRefresh = new QAction( tr( "Refresh" ), menu );
- connect( actionRefresh, &QAction::triggered, this, [connectionItem] { refreshConnection( connectionItem ); } );
- menu->addAction( actionRefresh );
- menu->addSeparator();
- QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
- connect( actionEdit, &QAction::triggered, this, [connectionItem] { editConnection( connectionItem ); } );
- menu->addAction( actionEdit );
- QAction *actionDelete = new QAction( tr( "Delete Connection…" ), menu );
- connect( actionDelete, &QAction::triggered, this, [connectionItem] { deleteConnection( connectionItem ); } );
- menu->addAction( actionDelete );
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( connectionItem->url() ) );
- } );
- menu->addAction( viewInfo );
- }
- else if ( QgsArcGisRestFolderItem *folderItem = qobject_cast< QgsArcGisRestFolderItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( folderItem->path() ) );
- } );
- menu->addAction( viewInfo );
- }
- else if ( QgsArcGisFeatureServiceItem *serviceItem = qobject_cast< QgsArcGisFeatureServiceItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
- } );
- menu->addAction( viewInfo );
- }
- else if ( QgsArcGisMapServiceItem *serviceItem = qobject_cast< QgsArcGisMapServiceItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
- } );
- menu->addAction( viewInfo );
- }
- else if ( QgsArcGisRestParentLayerItem *layerItem = qobject_cast< QgsArcGisRestParentLayerItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( layerItem->path() ) );
- } );
- menu->addAction( viewInfo );
- }
- else if ( QgsArcGisFeatureServiceLayerItem *layerItem = qobject_cast< QgsArcGisFeatureServiceLayerItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( layerItem->path() ) );
- } );
- menu->addAction( viewInfo );
- menu->addSeparator();
- }
- else if ( QgsArcGisMapServiceLayerItem *layerItem = qobject_cast< QgsArcGisMapServiceLayerItem * >( item ) )
- {
- QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
- connect( viewInfo, &QAction::triggered, this, [ = ]
- {
- QDesktopServices::openUrl( QUrl( layerItem->path() ) );
- } );
- menu->addAction( viewInfo );
- menu->addSeparator();
- }
- }
- void QgsArcGisRestDataItemGuiProvider::newConnection( QgsDataItem *item )
- {
- QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), QString() );
- nc.setWindowTitle( tr( "Create a New ArcGIS REST Server Connection" ) );
- if ( nc.exec() )
- {
- item->refresh();
- }
- }
- void QgsArcGisRestDataItemGuiProvider::editConnection( QgsDataItem *item )
- {
- QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), item->name() );
- nc.setWindowTitle( tr( "Modify ArcGIS REST Server Connection" ) );
- if ( nc.exec() )
- {
- // the parent should be updated
- item->refresh();
- if ( item->parent() )
- item->parent()->refreshConnections();
- }
- }
- void QgsArcGisRestDataItemGuiProvider::deleteConnection( QgsDataItem *item )
- {
- if ( QMessageBox::question( nullptr, QObject::tr( "Delete Connection" ),
- QObject::tr( "Are you sure you want to delete the connection to %1?" ).arg( item->name() ),
- QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
- return;
- QgsOwsConnection::deleteConnection( QStringLiteral( "arcgisfeatureserver" ), item->name() );
- // the parent should be updated
- if ( item->parent() )
- item->parent()->refreshConnections();
- }
- void QgsArcGisRestDataItemGuiProvider::refreshConnection( QgsDataItem *item )
- {
- item->refresh();
- // the parent should be updated
- if ( item->parent() )
- item->parent()->refreshConnections();
- }
- void QgsArcGisRestDataItemGuiProvider::saveConnections()
- {
- QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::ArcgisFeatureServer );
- dlg.exec();
- }
- void QgsArcGisRestDataItemGuiProvider::loadConnections( QgsDataItem *item )
- {
- const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
- tr( "XML files (*.xml *.XML)" ) );
- if ( fileName.isEmpty() )
- {
- return;
- }
- QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::ArcgisFeatureServer, fileName );
- if ( dlg.exec() == QDialog::Accepted )
- item->refreshConnections();
- }