1 /***************************************************************************
2   qgswmsdataitemguiproviders.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 
16 #include "qgswmsdataitemguiproviders.h"
17 
18 #include "qgswmsdataitems.h"
19 
20 #include "qgsnewhttpconnection.h"
21 #include "qgswmsconnection.h"
22 #include "qgsxyzconnectiondialog.h"
23 #include "qgsxyzconnection.h"
24 #include "qgsmanageconnectionsdialog.h"
25 #include "qgswmssourceselect.h"
26 
27 #include <QFileDialog>
28 #include <QMessageBox>
29 
_paramWidget(QgsDataItem * root)30 static QWidget *_paramWidget( QgsDataItem *root )
31 {
32   if ( qobject_cast<QgsWMSRootItem *>( root ) != nullptr )
33   {
34     return new QgsWMSSourceSelect( nullptr, Qt::WindowFlags(), QgsProviderRegistry::WidgetMode::Manager );
35   }
36   else
37   {
38     return nullptr;
39   }
40 }
41 
populateContextMenu(QgsDataItem * item,QMenu * menu,const QList<QgsDataItem * > &,QgsDataItemGuiContext)42 void QgsWmsDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
43 {
44   if ( QgsWMSConnectionItem *connItem = qobject_cast< QgsWMSConnectionItem * >( item ) )
45   {
46     QAction *actionRefresh = new QAction( tr( "Refresh" ), this );
47     connect( actionRefresh, &QAction::triggered, this, [connItem] { refreshConnection( connItem ); } );
48     menu->addAction( actionRefresh );
49 
50     menu->addSeparator();
51 
52     QAction *actionEdit = new QAction( tr( "Edit…" ), this );
53     connect( actionEdit, &QAction::triggered, this, [connItem] { editConnection( connItem ); } );
54     menu->addAction( actionEdit );
55 
56     QAction *actionDelete = new QAction( tr( "Delete" ), this );
57     connect( actionDelete, &QAction::triggered, this, [connItem] { deleteConnection( connItem ); } );
58     menu->addAction( actionDelete );
59   }
60 
61   if ( QgsWMSRootItem *rootItem = qobject_cast< QgsWMSRootItem * >( item ) )
62   {
63     QAction *actionNew = new QAction( tr( "New Connection…" ), this );
64     connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
65     menu->addAction( actionNew );
66 
67     QAction *actionSaveServers = new QAction( tr( "Save Connections…" ), this );
68     connect( actionSaveServers, &QAction::triggered, this, [] { saveConnections(); } );
69     menu->addAction( actionSaveServers );
70 
71     QAction *actionLoadServers = new QAction( tr( "Load Connections…" ), this );
72     connect( actionLoadServers, &QAction::triggered, this, [rootItem] { loadConnections( rootItem ); } );
73     menu->addAction( actionLoadServers );
74   }
75 }
76 
createParamWidget(QgsDataItem * root,QgsDataItemGuiContext)77 QWidget *QgsWmsDataItemGuiProvider::createParamWidget( QgsDataItem *root, QgsDataItemGuiContext )
78 {
79   return _paramWidget( root );
80 }
81 
editConnection(QgsDataItem * item)82 void QgsWmsDataItemGuiProvider::editConnection( QgsDataItem *item )
83 {
84   QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWms, QStringLiteral( "qgis/connections-wms/" ), item->name(), QgsNewHttpConnection::FlagShowHttpSettings );
85 
86   if ( nc.exec() )
87   {
88     // the parent should be updated
89     item->parent()->refreshConnections();
90   }
91 }
92 
deleteConnection(QgsDataItem * item)93 void QgsWmsDataItemGuiProvider::deleteConnection( QgsDataItem *item )
94 {
95   if ( QMessageBox::question( nullptr, tr( "Delete Connection" ), tr( "Are you sure you want to delete the connection “%1”?" ).arg( item->name() ),
96                               QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
97     return;
98 
99   QgsWMSConnection::deleteConnection( item->name() );
100   // the parent should be updated
101   item->parent()->refreshConnections();
102 }
103 
newConnection(QgsDataItem * item)104 void QgsWmsDataItemGuiProvider::newConnection( QgsDataItem *item )
105 {
106   QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWms, QStringLiteral( "qgis/connections-wms/" ), QString(), QgsNewHttpConnection::FlagShowHttpSettings );
107 
108   if ( nc.exec() )
109   {
110     item->refreshConnections();
111   }
112 }
113 
refreshConnection(QgsDataItem * item)114 void QgsWmsDataItemGuiProvider::refreshConnection( QgsDataItem *item )
115 {
116   // Updating the item and its children only
117   item->refresh();
118 }
119 
saveConnections()120 void QgsWmsDataItemGuiProvider::saveConnections()
121 {
122   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::WMS );
123   dlg.exec();
124 }
125 
loadConnections(QgsDataItem * item)126 void QgsWmsDataItemGuiProvider::loadConnections( QgsDataItem *item )
127 {
128   QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
129                      tr( "XML files (*.xml *.XML)" ) );
130   if ( fileName.isEmpty() )
131   {
132     return;
133   }
134 
135   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::WMS, fileName );
136   if ( dlg.exec() == QDialog::Accepted )
137     item->refreshConnections();
138 }
139 
140 // -----------
141 
142 
populateContextMenu(QgsDataItem * item,QMenu * menu,const QList<QgsDataItem * > &,QgsDataItemGuiContext)143 void QgsXyzDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
144 {
145   if ( QgsXyzLayerItem *layerItem = qobject_cast< QgsXyzLayerItem * >( item ) )
146   {
147     QAction *actionEdit = new QAction( tr( "Edit…" ), this );
148     connect( actionEdit, &QAction::triggered, this, [layerItem] { editConnection( layerItem ); } );
149     menu->addAction( actionEdit );
150 
151     QAction *actionDelete = new QAction( tr( "Delete" ), this );
152     connect( actionDelete, &QAction::triggered, this, [layerItem] { deleteConnection( layerItem ); } );
153     menu->addAction( actionDelete );
154   }
155 
156   if ( QgsXyzTileRootItem *rootItem = qobject_cast< QgsXyzTileRootItem * >( item ) )
157   {
158     QAction *actionNew = new QAction( tr( "New Connection…" ), this );
159     connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
160     menu->addAction( actionNew );
161 
162     QAction *actionSaveXyzTilesServers = new QAction( tr( "Save Connections…" ), this );
163     connect( actionSaveXyzTilesServers, &QAction::triggered, this, [] { saveXyzTilesServers(); } );
164     menu->addAction( actionSaveXyzTilesServers );
165 
166     QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), this );
167     connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadXyzTilesServers( rootItem ); } );
168     menu->addAction( actionLoadXyzTilesServers );
169   }
170 }
171 
createParamWidget(QgsDataItem * root,QgsDataItemGuiContext)172 QWidget *QgsXyzDataItemGuiProvider::createParamWidget( QgsDataItem *root, QgsDataItemGuiContext )
173 {
174   return _paramWidget( root );
175 }
176 
editConnection(QgsDataItem * item)177 void QgsXyzDataItemGuiProvider::editConnection( QgsDataItem *item )
178 {
179   QgsXyzConnectionDialog dlg;
180   dlg.setConnection( QgsXyzConnectionUtils::connection( item->name() ) );
181   if ( !dlg.exec() )
182     return;
183 
184   QgsXyzConnectionUtils::deleteConnection( item->name() );
185   QgsXyzConnectionUtils::addConnection( dlg.connection() );
186 
187   item->parent()->refreshConnections();
188 }
189 
deleteConnection(QgsDataItem * item)190 void QgsXyzDataItemGuiProvider::deleteConnection( QgsDataItem *item )
191 {
192   if ( QMessageBox::question( nullptr, tr( "Delete Connection" ), tr( "Are you sure you want to delete the connection “%1”?" ).arg( item->name() ),
193                               QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
194     return;
195 
196   QgsXyzConnectionUtils::deleteConnection( item->name() );
197 
198   item->parent()->refreshConnections();
199 }
200 
newConnection(QgsDataItem * item)201 void QgsXyzDataItemGuiProvider::newConnection( QgsDataItem *item )
202 {
203   QgsXyzConnectionDialog dlg;
204   if ( !dlg.exec() )
205     return;
206 
207   QgsXyzConnectionUtils::addConnection( dlg.connection() );
208   item->refreshConnections();
209 }
210 
saveXyzTilesServers()211 void QgsXyzDataItemGuiProvider::saveXyzTilesServers()
212 {
213   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::XyzTiles );
214   dlg.exec();
215 }
216 
loadXyzTilesServers(QgsDataItem * item)217 void QgsXyzDataItemGuiProvider::loadXyzTilesServers( QgsDataItem *item )
218 {
219   QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
220                      tr( "XML files (*.xml *.XML)" ) );
221   if ( fileName.isEmpty() )
222   {
223     return;
224   }
225 
226   QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::XyzTiles, fileName );
227   if ( dlg.exec() == QDialog::Accepted )
228     item->refreshConnections();
229 }
230