/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

  1. /***************************************************************************
  2. qgsarcgisrestdataitemguiprovider.cpp
  3. --------------------------------------
  4. Date : June 2019
  5. Copyright : (C) 2019 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. #include "qgsarcgisrestdataitemguiprovider.h"
  16. #include "qgsarcgisrestdataitems.h"
  17. #include "qgsmanageconnectionsdialog.h"
  18. #include "qgsnewarcgisrestconnection.h"
  19. #include "qgsowsconnection.h"
  20. #include <QDesktopServices>
  21. #include <QFileDialog>
  22. #include <QMessageBox>
  23. void QgsArcGisRestDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
  24. {
  25. if ( QgsArcGisRestRootItem *rootItem = qobject_cast< QgsArcGisRestRootItem * >( item ) )
  26. {
  27. QAction *actionNew = new QAction( tr( "New Connection…" ), menu );
  28. connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
  29. menu->addAction( actionNew );
  30. QAction *actionSaveServers = new QAction( tr( "Save Connections…" ), menu );
  31. connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
  32. menu->addAction( actionSaveServers );
  33. QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), menu );
  34. connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
  35. menu->addAction( actionLoadServers );
  36. }
  37. else if ( QgsArcGisRestConnectionItem *connectionItem = qobject_cast< QgsArcGisRestConnectionItem * >( item ) )
  38. {
  39. QAction *actionRefresh = new QAction( tr( "Refresh" ), menu );
  40. connect( actionRefresh, &QAction::triggered, this, [connectionItem] { refreshConnection( connectionItem ); } );
  41. menu->addAction( actionRefresh );
  42. menu->addSeparator();
  43. QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
  44. connect( actionEdit, &QAction::triggered, this, [connectionItem] { editConnection( connectionItem ); } );
  45. menu->addAction( actionEdit );
  46. QAction *actionDelete = new QAction( tr( "Delete Connection…" ), menu );
  47. connect( actionDelete, &QAction::triggered, this, [connectionItem] { deleteConnection( connectionItem ); } );
  48. menu->addAction( actionDelete );
  49. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  50. connect( viewInfo, &QAction::triggered, this, [ = ]
  51. {
  52. QDesktopServices::openUrl( QUrl( connectionItem->url() ) );
  53. } );
  54. menu->addAction( viewInfo );
  55. }
  56. else if ( QgsArcGisRestFolderItem *folderItem = qobject_cast< QgsArcGisRestFolderItem * >( item ) )
  57. {
  58. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  59. connect( viewInfo, &QAction::triggered, this, [ = ]
  60. {
  61. QDesktopServices::openUrl( QUrl( folderItem->path() ) );
  62. } );
  63. menu->addAction( viewInfo );
  64. }
  65. else if ( QgsArcGisFeatureServiceItem *serviceItem = qobject_cast< QgsArcGisFeatureServiceItem * >( item ) )
  66. {
  67. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  68. connect( viewInfo, &QAction::triggered, this, [ = ]
  69. {
  70. QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
  71. } );
  72. menu->addAction( viewInfo );
  73. }
  74. else if ( QgsArcGisMapServiceItem *serviceItem = qobject_cast< QgsArcGisMapServiceItem * >( item ) )
  75. {
  76. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  77. connect( viewInfo, &QAction::triggered, this, [ = ]
  78. {
  79. QDesktopServices::openUrl( QUrl( serviceItem->path() ) );
  80. } );
  81. menu->addAction( viewInfo );
  82. }
  83. else if ( QgsArcGisRestParentLayerItem *layerItem = qobject_cast< QgsArcGisRestParentLayerItem * >( item ) )
  84. {
  85. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  86. connect( viewInfo, &QAction::triggered, this, [ = ]
  87. {
  88. QDesktopServices::openUrl( QUrl( layerItem->path() ) );
  89. } );
  90. menu->addAction( viewInfo );
  91. }
  92. else if ( QgsArcGisFeatureServiceLayerItem *layerItem = qobject_cast< QgsArcGisFeatureServiceLayerItem * >( item ) )
  93. {
  94. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  95. connect( viewInfo, &QAction::triggered, this, [ = ]
  96. {
  97. QDesktopServices::openUrl( QUrl( layerItem->path() ) );
  98. } );
  99. menu->addAction( viewInfo );
  100. menu->addSeparator();
  101. }
  102. else if ( QgsArcGisMapServiceLayerItem *layerItem = qobject_cast< QgsArcGisMapServiceLayerItem * >( item ) )
  103. {
  104. QAction *viewInfo = new QAction( tr( "View Service Info" ), menu );
  105. connect( viewInfo, &QAction::triggered, this, [ = ]
  106. {
  107. QDesktopServices::openUrl( QUrl( layerItem->path() ) );
  108. } );
  109. menu->addAction( viewInfo );
  110. menu->addSeparator();
  111. }
  112. }
  113. void QgsArcGisRestDataItemGuiProvider::newConnection( QgsDataItem *item )
  114. {
  115. QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), QString() );
  116. nc.setWindowTitle( tr( "Create a New ArcGIS REST Server Connection" ) );
  117. if ( nc.exec() )
  118. {
  119. item->refresh();
  120. }
  121. }
  122. void QgsArcGisRestDataItemGuiProvider::editConnection( QgsDataItem *item )
  123. {
  124. QgsNewArcGisRestConnectionDialog nc( nullptr, QStringLiteral( "qgis/connections-arcgisfeatureserver/" ), item->name() );
  125. nc.setWindowTitle( tr( "Modify ArcGIS REST Server Connection" ) );
  126. if ( nc.exec() )
  127. {
  128. // the parent should be updated
  129. item->refresh();
  130. if ( item->parent() )
  131. item->parent()->refreshConnections();
  132. }
  133. }
  134. void QgsArcGisRestDataItemGuiProvider::deleteConnection( QgsDataItem *item )
  135. {
  136. if ( QMessageBox::question( nullptr, QObject::tr( "Delete Connection" ),
  137. QObject::tr( "Are you sure you want to delete the connection to %1?" ).arg( item->name() ),
  138. QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
  139. return;
  140. QgsOwsConnection::deleteConnection( QStringLiteral( "arcgisfeatureserver" ), item->name() );
  141. // the parent should be updated
  142. if ( item->parent() )
  143. item->parent()->refreshConnections();
  144. }
  145. void QgsArcGisRestDataItemGuiProvider::refreshConnection( QgsDataItem *item )
  146. {
  147. item->refresh();
  148. // the parent should be updated
  149. if ( item->parent() )
  150. item->parent()->refreshConnections();
  151. }
  152. void QgsArcGisRestDataItemGuiProvider::saveConnections()
  153. {
  154. QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::ArcgisFeatureServer );
  155. dlg.exec();
  156. }
  157. void QgsArcGisRestDataItemGuiProvider::loadConnections( QgsDataItem *item )
  158. {
  159. const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
  160. tr( "XML files (*.xml *.XML)" ) );
  161. if ( fileName.isEmpty() )
  162. {
  163. return;
  164. }
  165. QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::ArcgisFeatureServer, fileName );
  166. if ( dlg.exec() == QDialog::Accepted )
  167. item->refreshConnections();
  168. }