QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
qgstiledscenedataitemguiprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenedataitemguiprovider.cpp
3 --------------------------------------
4 begin : June 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson 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
23
24#include <QMessageBox>
25#include <QFileDialog>
26
28
29void QgsTiledSceneDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selection, QgsDataItemGuiContext context )
30{
31 if ( QgsTiledSceneLayerItem *layerItem = qobject_cast< QgsTiledSceneLayerItem * >( item ) )
32 {
33 QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
34 connect( actionEdit, &QAction::triggered, this, [layerItem] { editConnection( layerItem ); } );
35 menu->addAction( actionEdit );
36
37 QAction *actionDuplicate = new QAction( tr( "Duplicate Connection" ), menu );
38 connect( actionDuplicate, &QAction::triggered, this, [layerItem] { duplicateConnection( layerItem ); } );
39 menu->addAction( actionDuplicate );
40
41 const QList< QgsTiledSceneLayerItem * > sceneConnectionItems = QgsDataItem::filteredItems<QgsTiledSceneLayerItem>( selection );
42 QAction *actionDelete = new QAction( sceneConnectionItems.size() > 1 ? tr( "Remove Connections…" ) : tr( "Remove Connection…" ), menu );
43 connect( actionDelete, &QAction::triggered, this, [sceneConnectionItems, context]
44 {
45 QgsDataItemGuiProviderUtils::deleteConnections( sceneConnectionItems, []( const QString & connectionName )
46 {
47 QgsTiledSceneProviderConnection( QString() ).remove( connectionName );
48 }, context );
49 } );
50 menu->addAction( actionDelete );
51 }
52
53 if ( QgsTiledSceneRootItem *rootItem = qobject_cast< QgsTiledSceneRootItem * >( item ) )
54 {
55 QAction *actionNewCesium = new QAction( tr( "New Cesium 3D Tiles Connection…" ), menu );
56 connect( actionNewCesium, &QAction::triggered, this, [rootItem] { newConnection( rootItem, "cesiumtiles" ); } );
57 menu->addAction( actionNewCesium );
58
59 QAction *actionNewQM = new QAction( tr( "New Quantized Mesh Connection…" ), menu );
60 connect( actionNewQM, &QAction::triggered, this, [rootItem] { newConnection( rootItem, "quantizedmesh" ); } );
61 menu->addAction( actionNewQM );
62
63 menu->addSeparator();
64
65 QAction *actionSave = new QAction( tr( "Save Connections…" ), menu );
66 connect( actionSave, &QAction::triggered, this, [] { saveConnections(); } );
67 menu->addAction( actionSave );
68
69 QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), menu );
70 connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
71 menu->addAction( actionLoadXyzTilesServers );
72 }
73}
74
75void QgsTiledSceneDataItemGuiProvider::editConnection( QgsDataItem *item )
76{
78 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
79
80 QgsTiledSceneConnectionDialog dlg;
81
82 dlg.setConnection( item->name(), uri );
83 if ( !dlg.exec() )
84 return;
85
86 QgsTiledSceneProviderConnection( QString() ).remove( item->name() );
87
89 newConnection.provider = connection.provider;
90
91 QgsTiledSceneProviderConnection::addConnection( dlg.connectionName(), newConnection );
92
93 item->parent()->refreshConnections();
94}
95
96void QgsTiledSceneDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
97{
98 const QString connectionName = item->name();
100 const QStringList connections = QgsTiledSceneProviderConnection::sTreeConnectionTiledScene->items();
101
102 const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );
103
104 QgsTiledSceneProviderConnection::addConnection( newConnectionName, connection );
105 item->parent()->refreshConnections();
106}
107
108void QgsTiledSceneDataItemGuiProvider::newConnection( QgsDataItem *item, QString provider )
109{
110 QgsTiledSceneConnectionDialog dlg;
111 if ( !dlg.exec() )
112 return;
113
115 conn.provider = provider;
116
117 QgsTiledSceneProviderConnection::addConnection( dlg.connectionName(), conn );
118
119 item->refreshConnections();
120}
121
122void QgsTiledSceneDataItemGuiProvider::saveConnections()
123{
125 dlg.exec();
126}
127
128void QgsTiledSceneDataItemGuiProvider::loadConnections( QgsDataItem *item )
129{
130 const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
131 tr( "XML files (*.xml *.XML)" ) );
132 if ( fileName.isEmpty() )
133 {
134 return;
135 }
136
138 if ( dlg.exec() == QDialog::Accepted )
139 item->refreshConnections();
140}
141
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
static const QString uniqueName(const QString &name, const QStringList &connectionNames)
Check if connection with name exists in connectionNames list and then try to append a number to it to...
static void deleteConnections(const QList< T * > &items, const std::function< void(const QString &) > &deleteConnection, QgsDataItemGuiContext context)
Handles deletion of a list of connection items.
Base class for all items in the model.
Definition qgsdataitem.h:46
QString name() const
Returns the name of the item (the displayed text for the item).
virtual void refreshConnections(const QString &providerKey=QString())
Causes a data item provider to refresh all registered connections.
QgsDataItem * parent() const
Gets item parent.
@ TiledScene
Tiled scene connection.
Represents connections to tiled scene data sources.
virtual void remove(const QString &name) const override
Deletes the connection from the settings.
static Data decodedUri(const QString &uri)
Returns a connection uri decoded to a data structure.
static Data connection(const QString &name)
Returns connection details for the stored connection with the specified name.
static void addConnection(const QString &name, const Data &connection)
Stores a new connection, under the specified connection name.
static QString encodedUri(const Data &data)
Returns connection data encoded as a string.
Represents decoded data of a tiled scene connection.