QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsvectortiledataitemguiprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectortiledataitemguiprovider.cpp
3  --------------------------------------
4  Date : March 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 
18 #include "qgsvectortiledataitems.h"
22 
23 #include <QFileDialog>
24 #include <QMessageBox>
25 
27 
28 void QgsVectorTileDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
29 {
30  if ( QgsVectorTileLayerItem *layerItem = qobject_cast< QgsVectorTileLayerItem * >( item ) )
31  {
32  QAction *actionEdit = new QAction( tr( "Edit…" ), this );
33  connect( actionEdit, &QAction::triggered, this, [layerItem] { editConnection( layerItem ); } );
34  menu->addAction( actionEdit );
35 
36  QAction *actionDelete = new QAction( tr( "Delete" ), this );
37  connect( actionDelete, &QAction::triggered, this, [layerItem] { deleteConnection( layerItem ); } );
38  menu->addAction( actionDelete );
39  }
40 
41  if ( QgsVectorTileRootItem *rootItem = qobject_cast< QgsVectorTileRootItem * >( item ) )
42  {
43  QAction *actionNew = new QAction( tr( "New Connection…" ), this );
44  connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
45  menu->addAction( actionNew );
46 
47  QAction *actionSaveXyzTilesServers = new QAction( tr( "Save Connections…" ), this );
48  connect( actionSaveXyzTilesServers, &QAction::triggered, this, [] { saveXyzTilesServers(); } );
49  menu->addAction( actionSaveXyzTilesServers );
50 
51  QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), this );
52  connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadXyzTilesServers( rootItem ); } );
53  menu->addAction( actionLoadXyzTilesServers );
54  }
55 }
56 
57 void QgsVectorTileDataItemGuiProvider::editConnection( QgsDataItem *item )
58 {
59  QgsVectorTileConnectionDialog dlg;
60  QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( item->name() ) );
61  dlg.setConnection( item->name(), uri );
62  if ( !dlg.exec() )
63  return;
64 
65  QgsVectorTileProviderConnection::deleteConnection( item->name() );
66  QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
67  QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
68 
69  item->parent()->refreshConnections();
70 }
71 
72 void QgsVectorTileDataItemGuiProvider::deleteConnection( QgsDataItem *item )
73 {
74  if ( QMessageBox::question( nullptr, tr( "Delete Connection" ), tr( "Are you sure you want to delete the connection “%1”?" ).arg( item->name() ),
75  QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
76  return;
77 
78  QgsVectorTileProviderConnection::deleteConnection( item->name() );
79 
80  item->parent()->refreshConnections();
81 }
82 
83 void QgsVectorTileDataItemGuiProvider::newConnection( QgsDataItem *item )
84 {
85  QgsVectorTileConnectionDialog dlg;
86  if ( !dlg.exec() )
87  return;
88 
89  QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
90  QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
91 
92  item->refreshConnections();
93 }
94 
95 void QgsVectorTileDataItemGuiProvider::saveXyzTilesServers()
96 {
98  dlg.exec();
99 }
100 
101 void QgsVectorTileDataItemGuiProvider::loadXyzTilesServers( QgsDataItem *item )
102 {
103  QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
104  tr( "XML files (*.xml *.XML)" ) );
105  if ( fileName.isEmpty() )
106  {
107  return;
108  }
109 
111  dlg.exec();
112  item->refreshConnections();
113 }
114 
QgsManageConnectionsDialog
Definition: qgsmanageconnectionsdialog.h:31
qgsmanageconnectionsdialog.h
qgsvectortiledataitemguiprovider.h
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:318
qgsvectortileconnectiondialog.h
QgsDataItem::refreshConnections
virtual void refreshConnections(const QString &providerKey=QString())
Causes a data item provider to refresh all registered connections.
Definition: qgsdataitem.cpp:387
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:304
QgsManageConnectionsDialog::Import
@ Import
Definition: qgsmanageconnectionsdialog.h:39
QgsManageConnectionsDialog::VectorTile
@ VectorTile
Definition: qgsmanageconnectionsdialog.h:55
qgsvectortileconnection.h
QgsDataItemGuiContext
Definition: qgsdataitemguiprovider.h:39
QgsManageConnectionsDialog::Export
@ Export
Definition: qgsmanageconnectionsdialog.h:38
qgsvectortiledataitems.h
QgsDataItem
Definition: qgsdataitem.h:49