[wms] Move GUI parts of data items to QgsDataItemGuiProvider subclasses

This commit is contained in:
Martin Dobias 2019-06-26 19:46:48 +02:00
parent 764fa5966f
commit f4b1b83af2
9 changed files with 288 additions and 183 deletions

View File

@ -146,6 +146,8 @@ drop, and to prevent the default drop behavior for items.
.. versionadded:: 3.10
%End
};
/************************************************************************

View File

@ -15,6 +15,8 @@
#include "qgsdataitemguiprovider.h"
#include "qgsdataitem.h"
//
// QgsDataItemGuiContext
//
@ -61,3 +63,13 @@ bool QgsDataItemGuiProvider::handleDrop( QgsDataItem *, QgsDataItemGuiContext, c
{
return false;
}
void QgsDataItemGuiProvider::setItemForAction( QAction *action, QgsDataItem *item )
{
action->setData( QVariant::fromValue( QPointer< QgsDataItem >( item ) ) );
}
QPointer<QgsDataItem> QgsDataItemGuiProvider::itemFromAction( QAction *action )
{
return action ? action->data().value<QPointer< QgsDataItem >>() : QPointer<QgsDataItem>();
}

View File

@ -23,6 +23,7 @@
#include <QMimeData>
#include <QString>
#include <QMenu>
#include <QPointer>
class QgsDataItem;
class QgsMessageBar;
@ -163,6 +164,15 @@ class GUI_EXPORT QgsDataItemGuiProvider
* \since QGIS 3.10
*/
virtual bool handleDrop( QgsDataItem *item, QgsDataItemGuiContext context, const QMimeData *data, Qt::DropAction action );
//
// utility functions
//
//! Helper function to stuff pointer to QgsDataItem into QAction - useful when creating actions for context menu
SIP_SKIP static void setItemForAction( QAction *action, QgsDataItem *item );
//! Helper function to get pointer to QgsDataItem out of QAction - useful in slots of context menu actions
SIP_SKIP static QPointer<QgsDataItem> itemFromAction( QAction *action );
};
#endif // QGSDATAITEMGUIPROVIDER_H

View File

@ -15,12 +15,14 @@ IF (WITH_GUI)
SET(WMS_SRCS ${WMS_SRCS}
qgswmsprovidergui.cpp
qgswmssourceselect.cpp
qgswmsdataitemguiproviders.cpp
qgstilescalewidget.cpp
qgswmtsdimensions.cpp
qgsxyzconnectiondialog.cpp
)
SET(WMS_MOC_HDRS ${WMS_MOC_HDRS}
qgswmssourceselect.h
qgswmsdataitemguiproviders.h
qgstilescalewidget.h
qgswmtsdimensions.h
qgsxyzconnectiondialog.h

View File

@ -0,0 +1,195 @@
/***************************************************************************
qgswmsdataitemguiproviders.cpp
--------------------------------------
Date : June 2019
Copyright : (C) 2019 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgswmsdataitemguiproviders.h"
#include "qgswmsdataitems.h"
#include "qgsnewhttpconnection.h"
#include "qgswmsconnection.h"
#include "qgsxyzconnectiondialog.h"
#include "qgsxyzconnection.h"
#include "qgsmanageconnectionsdialog.h"
#include <QFileDialog>
void QgsWmsDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
{
if ( QgsWMSConnectionItem *connItem = qobject_cast< QgsWMSConnectionItem * >( item ) )
{
QAction *actionEdit = new QAction( tr( "Edit…" ), this );
setItemForAction( actionEdit, connItem );
connect( actionEdit, &QAction::triggered, this, &QgsWmsDataItemGuiProvider::editConnection );
menu->addAction( actionEdit );
QAction *actionDelete = new QAction( tr( "Delete" ), this );
setItemForAction( actionDelete, connItem );
connect( actionDelete, &QAction::triggered, this, &QgsWmsDataItemGuiProvider::deleteConnection );
menu->addAction( actionDelete );
}
if ( QgsWMSRootItem *wmsRootItem = qobject_cast< QgsWMSRootItem * >( item ) )
{
QAction *actionNew = new QAction( tr( "New Connection…" ), this );
setItemForAction( actionNew, wmsRootItem );
connect( actionNew, &QAction::triggered, this, &QgsWmsDataItemGuiProvider::newConnection );
menu->addAction( actionNew );
}
}
void QgsWmsDataItemGuiProvider::editConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWms, QStringLiteral( "qgis/connections-wms/" ), item->name() );
if ( nc.exec() )
{
// the parent should be updated
item->parent()->refreshConnections();
}
}
void QgsWmsDataItemGuiProvider::deleteConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsWMSConnection::deleteConnection( item->name() );
// the parent should be updated
item->parent()->refreshConnections();
}
void QgsWmsDataItemGuiProvider::newConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsNewHttpConnection nc( nullptr );
if ( nc.exec() )
{
item->refreshConnections();
}
}
// -----------
void QgsXyzDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
{
if ( QgsXyzLayerItem *layerItem = qobject_cast< QgsXyzLayerItem * >( item ) )
{
QAction *actionEdit = new QAction( tr( "Edit…" ), this );
setItemForAction( actionEdit, layerItem );
connect( actionEdit, &QAction::triggered, this, &QgsXyzDataItemGuiProvider::editConnection );
menu->addAction( actionEdit );
QAction *actionDelete = new QAction( tr( "Delete" ), this );
setItemForAction( actionDelete, layerItem );
connect( actionDelete, &QAction::triggered, this, &QgsXyzDataItemGuiProvider::deleteConnection );
menu->addAction( actionDelete );
}
if ( QgsXyzTileRootItem *rootItem = qobject_cast< QgsXyzTileRootItem * >( item ) )
{
QAction *actionNew = new QAction( tr( "New Connection…" ), this );
setItemForAction( actionNew, rootItem );
connect( actionNew, &QAction::triggered, this, &QgsXyzDataItemGuiProvider::newConnection );
menu->addAction( actionNew );
QAction *saveXyzTilesServers = new QAction( tr( "Save Connections…" ), this );
setItemForAction( saveXyzTilesServers, rootItem );
connect( saveXyzTilesServers, &QAction::triggered, this, &QgsXyzDataItemGuiProvider::saveXyzTilesServers );
menu->addAction( saveXyzTilesServers );
QAction *loadXyzTilesServers = new QAction( tr( "Load Connections…" ), this );
setItemForAction( loadXyzTilesServers, rootItem );
connect( loadXyzTilesServers, &QAction::triggered, this, &QgsXyzDataItemGuiProvider::loadXyzTilesServers );
menu->addAction( loadXyzTilesServers );
}
}
void QgsXyzDataItemGuiProvider::editConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsXyzConnectionDialog dlg;
dlg.setConnection( QgsXyzConnectionUtils::connection( item->name() ) );
if ( !dlg.exec() )
return;
QgsXyzConnectionUtils::deleteConnection( item->name() );
QgsXyzConnectionUtils::addConnection( dlg.connection() );
item->parent()->refreshConnections();
}
void QgsXyzDataItemGuiProvider::deleteConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsXyzConnectionUtils::deleteConnection( item->name() );
item->parent()->refreshConnections();
}
void QgsXyzDataItemGuiProvider::newConnection()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QgsXyzConnectionDialog dlg;
if ( !dlg.exec() )
return;
QgsXyzConnectionUtils::addConnection( dlg.connection() );
item->refreshConnections();
}
void QgsXyzDataItemGuiProvider::saveXyzTilesServers()
{
QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::XyzTiles );
dlg.exec();
}
void QgsXyzDataItemGuiProvider::loadXyzTilesServers()
{
QPointer< QgsDataItem > item = itemFromAction( qobject_cast<QAction *>( sender() ) );
if ( !item )
return;
QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}
QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::XyzTiles, fileName );
dlg.exec();
item->refreshConnections();
}

View File

@ -0,0 +1,59 @@
/***************************************************************************
qgswmsdataitemguiproviders.h
--------------------------------------
Date : June 2019
Copyright : (C) 2019 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSWMSDATAITEMGUIPROVIDERS_H
#define QGSWMSDATAITEMGUIPROVIDERS_H
#include "qgsdataitemguiprovider.h"
class QgsWmsDataItemGuiProvider : public QObject, public QgsDataItemGuiProvider
{
Q_OBJECT
public:
QString name() override { return QStringLiteral( "WMS" ); }
void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
private slots:
void editConnection();
void deleteConnection();
void newConnection();
};
class QgsXyzDataItemGuiProvider : public QObject, public QgsDataItemGuiProvider
{
Q_OBJECT
public:
QString name() override { return QStringLiteral( "XYZ Tiles" ); }
void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
private slots:
void editConnection();
void deleteConnection();
void newConnection();
void saveXyzTilesServers();
void loadXyzTilesServers();
};
#endif // QGSWMSDATAITEMGUIPROVIDERS_H

View File

@ -24,18 +24,11 @@
#ifdef HAVE_GUI
#include "qgswmssourceselect.h"
#include "qgsnewhttpconnection.h"
#include "qgstilescalewidget.h"
#include "qgsxyzconnectiondialog.h"
#include "qgsmanageconnectionsdialog.h"
#endif
#include "qgsgeonodeconnection.h"
#include "qgsgeonoderequest.h"
#include "qgssettings.h"
#include <QInputDialog>
#include <QFileDialog>
// ---------------------------------------------------------------------------
QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem *parent, QString name, QString path, QString uri )
: QgsDataCollectionItem( parent, name, path )
@ -201,42 +194,6 @@ bool QgsWMSConnectionItem::equal( const QgsDataItem *other )
return ( mPath == o->mPath && mName == o->mName );
}
#ifdef HAVE_GUI
QList<QAction *> QgsWMSConnectionItem::actions( QWidget *parent )
{
QList<QAction *> lst;
QAction *actionEdit = new QAction( tr( "Edit…" ), parent );
connect( actionEdit, &QAction::triggered, this, &QgsWMSConnectionItem::editConnection );
lst.append( actionEdit );
QAction *actionDelete = new QAction( tr( "Delete" ), parent );
connect( actionDelete, &QAction::triggered, this, &QgsWMSConnectionItem::deleteConnection );
lst.append( actionDelete );
return lst;
}
void QgsWMSConnectionItem::editConnection()
{
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWms, QStringLiteral( "qgis/connections-wms/" ), mName );
if ( nc.exec() )
{
// the parent should be updated
mParent->refreshConnections();
}
}
void QgsWMSConnectionItem::deleteConnection()
{
QgsWMSConnection::deleteConnection( mName );
// the parent should be updated
mParent->refreshConnections();
}
#endif
// ---------------------------------------------------------------------------
QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem *parent, QString name, QString path, const QgsWmsCapabilitiesProperty &capabilitiesProperty, const QgsDataSourceUri &dataSourceUri, const QgsWmsLayerProperty &layerProperty )
@ -376,33 +333,11 @@ QVector<QgsDataItem *> QgsWMSRootItem::createChildren()
}
#ifdef HAVE_GUI
QList<QAction *> QgsWMSRootItem::actions( QWidget *parent )
{
QList<QAction *> lst;
QAction *actionNew = new QAction( tr( "New Connection…" ), parent );
connect( actionNew, &QAction::triggered, this, &QgsWMSRootItem::newConnection );
lst.append( actionNew );
return lst;
}
QWidget *QgsWMSRootItem::paramWidget()
{
QgsWMSSourceSelect *select = new QgsWMSSourceSelect( nullptr, nullptr, QgsProviderRegistry::WidgetMode::Manager );
return select;
}
void QgsWMSRootItem::newConnection()
{
QgsNewHttpConnection nc( nullptr );
if ( nc.exec() )
{
refreshConnections();
}
}
#endif
@ -455,56 +390,6 @@ QVector<QgsDataItem *> QgsXyzTileRootItem::createChildren()
return connections;
}
#ifdef HAVE_GUI
QList<QAction *> QgsXyzTileRootItem::actions( QWidget *parent )
{
QList<QAction *> lst;
QAction *actionNew = new QAction( tr( "New Connection…" ), parent );
connect( actionNew, &QAction::triggered, this, &QgsXyzTileRootItem::newConnection );
QAction *saveXyzTilesServers = new QAction( tr( "Save Connections…" ), parent );
connect( saveXyzTilesServers, &QAction::triggered, this, &QgsXyzTileRootItem::saveXyzTilesServers );
QAction *loadXyzTilesServers = new QAction( tr( "Load Connections…" ), parent );
connect( loadXyzTilesServers, &QAction::triggered, this, &QgsXyzTileRootItem::loadXyzTilesServers );
lst.append( actionNew );
lst.append( saveXyzTilesServers );
lst.append( loadXyzTilesServers );
return lst;
}
void QgsXyzTileRootItem::newConnection()
{
QgsXyzConnectionDialog dlg;
if ( !dlg.exec() )
return;
QgsXyzConnectionUtils::addConnection( dlg.connection() );
refreshConnections();
}
void QgsXyzTileRootItem::saveXyzTilesServers()
{
QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::XyzTiles );
dlg.exec();
}
void QgsXyzTileRootItem::loadXyzTilesServers()
{
QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}
QgsManageConnectionsDialog dlg( nullptr, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::XyzTiles, fileName );
dlg.exec();
refreshConnections();
}
#endif
// ---------------------------------------------------------------------------
@ -515,42 +400,6 @@ QgsXyzLayerItem::QgsXyzLayerItem( QgsDataItem *parent, QString name, QString pat
setState( Populated );
}
#ifdef HAVE_GUI
QList<QAction *> QgsXyzLayerItem::actions( QWidget *parent )
{
QList<QAction *> lst = QgsLayerItem::actions( parent );
QAction *actionEdit = new QAction( tr( "Edit…" ), parent );
connect( actionEdit, &QAction::triggered, this, &QgsXyzLayerItem::editConnection );
lst << actionEdit;
QAction *actionDelete = new QAction( tr( "Delete" ), parent );
connect( actionDelete, &QAction::triggered, this, &QgsXyzLayerItem::deleteConnection );
lst << actionDelete;
return lst;
}
void QgsXyzLayerItem::editConnection()
{
QgsXyzConnectionDialog dlg;
dlg.setConnection( QgsXyzConnectionUtils::connection( mName ) );
if ( !dlg.exec() )
return;
QgsXyzConnectionUtils::deleteConnection( mName );
QgsXyzConnectionUtils::addConnection( dlg.connection() );
mParent->refreshConnections();
}
void QgsXyzLayerItem::deleteConnection()
{
QgsXyzConnectionUtils::deleteConnection( mName );
mParent->refreshConnections();
}
#endif
// ---------------------------------------------------------------------------

View File

@ -33,16 +33,7 @@ class QgsWMSConnectionItem : public QgsDataCollectionItem
QVector<QgsDataItem *> createChildren() override;
bool equal( const QgsDataItem *other ) override;
#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *parent ) override;
#endif
public slots:
#ifdef HAVE_GUI
void editConnection();
void deleteConnection();
#endif
void deleteLater() override;
private:
@ -102,14 +93,10 @@ class QgsWMSRootItem : public QgsDataCollectionItem
QVariant sortKey() const override { return 7; }
#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *parent ) override;
QWidget *paramWidget() override;
#endif
public slots:
#ifdef HAVE_GUI
void newConnection();
#endif
};
@ -139,16 +126,6 @@ class QgsXyzTileRootItem : public QgsDataCollectionItem
QVariant sortKey() const override { return 8; }
#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *parent ) override;
#endif
private slots:
#ifdef HAVE_GUI
void newConnection();
void saveXyzTilesServers();
void loadXyzTilesServers();
#endif
};
//! Item implementation for XYZ tile layers
@ -158,15 +135,6 @@ class QgsXyzLayerItem : public QgsLayerItem
public:
QgsXyzLayerItem( QgsDataItem *parent, QString name, QString path, const QString &encodedUri );
#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *parent ) override;
#endif
public slots:
#ifdef HAVE_GUI
void editConnection();
void deleteConnection();
#endif
};

View File

@ -18,6 +18,7 @@
#include "qgssourceselectprovider.h"
#include "qgstilescalewidget.h"
#include "qgsproviderguimetadata.h"
#include "qgswmsdataitemguiproviders.h"
//! Provider for WMS layers source select
@ -47,6 +48,13 @@ class QgsWmsProviderGuiMetadata: public QgsProviderGuiMetadata
return providers;
}
QList<QgsDataItemGuiProvider *> dataItemGuiProviders() override
{
return QList<QgsDataItemGuiProvider *>()
<< new QgsWmsDataItemGuiProvider
<< new QgsXyzDataItemGuiProvider;
}
void registerGui( QMainWindow *widget ) override
{
QgsTileScaleWidget::showTileScale( widget );