QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
17#include "moc_qgstiledscenedataitemguiprovider.cpp"
24
25#include <QMessageBox>
26#include <QFileDialog>
27
29
30void QgsTiledSceneDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selection, QgsDataItemGuiContext context )
31{
32 if ( QgsTiledSceneLayerItem *layerItem = qobject_cast< QgsTiledSceneLayerItem * >( item ) )
33 {
34 QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
35 connect( actionEdit, &QAction::triggered, this, [layerItem] { editConnection( layerItem ); } );
36 menu->addAction( actionEdit );
37
38 QAction *actionDuplicate = new QAction( tr( "Duplicate Connection" ), menu );
39 connect( actionDuplicate, &QAction::triggered, this, [layerItem] { duplicateConnection( layerItem ); } );
40 menu->addAction( actionDuplicate );
41
42 const QList< QgsTiledSceneLayerItem * > sceneConnectionItems = QgsDataItem::filteredItems<QgsTiledSceneLayerItem>( selection );
43 QAction *actionDelete = new QAction( sceneConnectionItems.size() > 1 ? tr( "Remove Connections…" ) : tr( "Remove Connection…" ), menu );
44 connect( actionDelete, &QAction::triggered, this, [sceneConnectionItems, context]
45 {
46 QgsDataItemGuiProviderUtils::deleteConnections( sceneConnectionItems, []( const QString & connectionName )
47 {
48 QgsTiledSceneProviderConnection( QString() ).remove( connectionName );
49 }, context );
50 } );
51 menu->addAction( actionDelete );
52 }
53
54 if ( QgsTiledSceneRootItem *rootItem = qobject_cast< QgsTiledSceneRootItem * >( item ) )
55 {
56 QAction *actionNewCesium = new QAction( tr( "New Cesium 3D Tiles Connection…" ), menu );
57 connect( actionNewCesium, &QAction::triggered, this, [rootItem] { newConnection( rootItem, "cesiumtiles" ); } );
58 menu->addAction( actionNewCesium );
59
60 QAction *actionNewQM = new QAction( tr( "New Quantized Mesh Connection…" ), menu );
61 connect( actionNewQM, &QAction::triggered, this, [rootItem] { newConnection( rootItem, "quantizedmesh" ); } );
62 menu->addAction( actionNewQM );
63
64 menu->addSeparator();
65
66 QAction *actionSave = new QAction( tr( "Save Connections…" ), menu );
67 connect( actionSave, &QAction::triggered, this, [] { saveConnections(); } );
68 menu->addAction( actionSave );
69
70 QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), menu );
71 connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
72 menu->addAction( actionLoadXyzTilesServers );
73 }
74}
75
76void QgsTiledSceneDataItemGuiProvider::editConnection( QgsDataItem *item )
77{
79 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
80
81 QgsTiledSceneConnectionDialog dlg;
82
83 dlg.setConnection( item->name(), uri );
84 if ( !dlg.exec() )
85 return;
86
87 QgsTiledSceneProviderConnection( QString() ).remove( item->name() );
88
90 newConnection.provider = connection.provider;
91
92 QgsTiledSceneProviderConnection::addConnection( dlg.connectionName(), newConnection );
93
94 item->parent()->refreshConnections();
95}
96
97void QgsTiledSceneDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
98{
99 const QString connectionName = item->name();
101 const QStringList connections = QgsTiledSceneProviderConnection::sTreeConnectionTiledScene->items();
102
103 const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );
104
105 QgsTiledSceneProviderConnection::addConnection( newConnectionName, connection );
106 item->parent()->refreshConnections();
107}
108
109void QgsTiledSceneDataItemGuiProvider::newConnection( QgsDataItem *item, QString provider )
110{
111 QgsTiledSceneConnectionDialog dlg;
112 if ( !dlg.exec() )
113 return;
114
116 conn.provider = provider;
117
118 QgsTiledSceneProviderConnection::addConnection( dlg.connectionName(), conn );
119
120 item->refreshConnections();
121}
122
123void QgsTiledSceneDataItemGuiProvider::saveConnections()
124{
126 dlg.exec();
127}
128
129void QgsTiledSceneDataItemGuiProvider::loadConnections( QgsDataItem *item )
130{
131 const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
132 tr( "XML files (*.xml *.XML)" ) );
133 if ( fileName.isEmpty() )
134 {
135 return;
136 }
137
139 if ( dlg.exec() == QDialog::Accepted )
140 item->refreshConnections();
141}
142
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.