Skip to content
Permalink
Browse files

use connection name in wms,wfs,wcs data item path instead of uri, gro…

…up ows servers by connection name instead of uri
  • Loading branch information
blazek committed Nov 10, 2014
1 parent cb9bd88 commit 1018c91f296ab5eb3a6c5e74fec4fc5d8af2206c
@@ -82,6 +82,7 @@ class QgsDataItem : QObject
QIcon icon() const;
QString name() const;
QString path() const;
void setPath( const QString );

void setIcon( QIcon icon );

@@ -280,8 +280,6 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
{
foundChild = false; // assume that the next child item will not be found

int bestLength = 0;
QModelIndex bestIndex;
for ( int i = 0; i < rowCount( theIndex ); i++ )
{
QModelIndex idx = index( i, 0, theIndex );
@@ -295,18 +293,19 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
return idx; // we have found the item we have been looking for
}

// not yet perfect, e.g. if a directory was deleted, it can jump to another one which starts with the same name
// but be careful, there are no common path separators, for example WMS contains slashes in its name
if ( path.startsWith( item->path() ) && item->path().length() > bestLength )
// paths are slash separated identifier
if ( path.startsWith( item->path() + "/" ) )
{
// we have found a preceding item: stop searching on this level and go deeper
// We have found a preceding item: stop searching on this level and go deeper.
// Currently some providers (e.g. Postgres) are using multithread in
// QgsDataItem::createChildren(), i.e. we cannot get to children here as they
// are not yet created by separate thread
item->populate();
foundChild = true;
bestIndex = idx;
bestLength = item->path().length();
theIndex = idx;
break;
}
}
theIndex = bestIndex;
}

if ( matchFlag == Qt::MatchStartsWith )
@@ -119,6 +119,7 @@ class CORE_EXPORT QgsDataItem : public QObject
QIcon icon() const { return mIcon; }
QString name() const { return mName; }
QString path() const { return mPath; }
void setPath( const QString path ) { mPath = path; }

void setIcon( QIcon icon ) { mIcon = icon; }

@@ -132,7 +133,11 @@ class CORE_EXPORT QgsDataItem : public QObject
QVector<QgsDataItem*> mChildren; // easier to have it always
bool mPopulated;
QString mName;
QString mPath; // it is also used to identify item in tree
// Path is slash ('/') separated chain of item identifiers which are usually item names, but may be differen if it is
// necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
// name but different paths). Identifiers in path must not contain '/' characters.
// The path is used to identify item in tree.
QString mPath;
QString mToolTip;
QIcon mIcon;

@@ -39,16 +39,19 @@ QVector<QgsDataItem*> QgsOWSConnectionItem::createChildren()
{
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> children;
QVector<QgsDataItem*> serviceItems;
QMap<QgsDataItem*, QString> serviceItems; // service/provider key

int layerCount = 0;
// Try to open with WMS,WFS,WCS
foreach ( QString key, QStringList() << "wms" << "WFS" << "gdal" )
foreach ( QString key, QStringList() << "wms" << "WFS" << "wcs" )
{
QgsDebugMsg( "Add connection for provider " + key );
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( key );
if ( !library )
{
QgsDebugMsg( "Cannot get provider " + key );
continue;
}

dataItem_t * dItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
if ( !dItem )
@@ -57,47 +60,65 @@ QVector<QgsDataItem*> QgsOWSConnectionItem::createChildren()
continue;
}

QgsDataItem *item = dItem( mPath, this ); // empty path -> top level
QString path = key.toLower() + ":/" + name();
QgsDebugMsg( "path = " + path );
QgsDataItem *item = dItem( path, this ); // empty path -> top level
if ( !item )
{
QgsDebugMsg( "Connection not found by provider" );
continue;
}

item->populate();

layerCount += item->rowCount();
if ( item->rowCount() > 0 )
{
QgsDebugMsg( "Add new item : " + item->name() );
serviceItems.append( item );
serviceItems.insert( item, key );
}
else
{
//delete item;
}
}

foreach ( QgsDataItem* item, serviceItems )
foreach ( QgsDataItem* item, serviceItems.keys() )
{
QgsDebugMsg( QString( "serviceItems.size = %1 layerCount = %2 rowCount = %3" ).arg( serviceItems.size() ).arg( layerCount ).arg( item->rowCount() ) );
QString providerKey = serviceItems.value( item );
if ( serviceItems.size() == 1 || layerCount <= 30 || item->rowCount() <= 10 )
{
// Add layers directly to OWS connection
foreach ( QgsDataItem* subItem, item->children() )
{
item->removeChildItem( subItem );
subItem->setParent( this );
replacePath( subItem, providerKey.toLower() + ":/", "ows:/" );
children.append( subItem );
}
delete item;
}
else // Add service
{
replacePath( item, item->path(), path() + "/" + providerKey.toLower() );
children.append( item );
}
}

return children;
}

// reset path recursively
void QgsOWSConnectionItem::replacePath( QgsDataItem* item, QString before, QString after )
{
item->setPath( item->path().replace( before, after ) );
foreach ( QgsDataItem* subItem, item->children() )
{
replacePath( subItem, before, after );
}
}

bool QgsOWSConnectionItem::equal( const QgsDataItem *other )
{
if ( type() != other->type() )
@@ -166,25 +187,20 @@ QVector<QgsDataItem*> QgsOWSRootItem::createChildren()
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> connections;
// Combine all WMS,WFS,WCS connections
QMap<QString, QStringList> uris;
QStringList connNames;
foreach ( QString service, QStringList() << "WMS" << "WFS" << "WCS" )
{
foreach ( QString connName, QgsOWSConnection::connectionList( service ) )
{
QgsOWSConnection connection( service, connName );

QString encodedUri = connection.uri().encodedUri();
QStringList labels = uris.value( encodedUri );
if ( !labels.contains( connName ) )
if ( !connNames.contains( connName ) )
{
labels << connName;
connNames << connName;
}
uris[encodedUri] = labels;
}
}
foreach ( QString encodedUri, uris.keys() )
foreach ( QString connName, connNames )
{
QgsDataItem * conn = new QgsOWSConnectionItem( this, uris.value( encodedUri ).join( " / " ), encodedUri );
QgsDataItem * conn = new QgsOWSConnectionItem( this, connName, "ows:/" + connName );
connections.append( conn );
}
return connections;
@@ -32,6 +32,9 @@ class QgsOWSConnectionItem : public QgsDataCollectionItem
public slots:
void editConnection();
void deleteConnection();

private:
void replacePath( QgsDataItem* item, QString before, QString after );
};

class QgsOWSRootItem : public QgsDataCollectionItem
@@ -24,8 +24,9 @@
#include <QFileInfo>
#include <QSettings>

QgsWCSConnectionItem::QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path )
QgsWCSConnectionItem::QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path , QString uri )
: QgsDataCollectionItem( parent, name, path )
, mUri( uri )
{
mIcon = QgsApplication::getThemeIcon( "mIconWcs.svg" );
}
@@ -40,10 +41,9 @@ QVector<QgsDataItem*> QgsWCSConnectionItem::createChildren()
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> children;

QString encodedUri = mPath;
QgsDataSourceURI uri;
uri.setEncodedUri( encodedUri );
QgsDebugMsg( "encodedUri = " + encodedUri );
uri.setEncodedUri( mUri );
QgsDebugMsg( "mUri = " + mUri );

mCapabilities.setUri( uri );

@@ -234,9 +234,8 @@ QVector<QgsDataItem*>QgsWCSRootItem::createChildren()
QVector<QgsDataItem*> connections;
foreach ( QString connName, QgsOWSConnection::connectionList( "WCS" ) )
{
//QgsDataItem * conn = new QgsWCSConnectionItem( this, connName, mPath + "/" + connName );
QgsOWSConnection connection( "WCS", connName );
QgsDataItem * conn = new QgsWCSConnectionItem( this, connName, connection.uri().encodedUri() );
QgsDataItem * conn = new QgsWCSConnectionItem( this, connName, mPath + "/" + connName, connection.uri().encodedUri() );

conn->setIcon( QgsApplication::getThemeIcon( "mIconConnect.png" ) );
connections.append( conn );
@@ -298,9 +297,18 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return new QgsWCSRootItem( parentItem, "WCS", "wcs:" );
}

// OWS server
QgsDebugMsg( "connection found in uri" );
return new QgsWCSConnectionItem( parentItem, "WCS", thePath );
// path schema: wcs:/connection name (used by OWS)
if ( thePath.startsWith( "wcs:/" ) )
{
QString connectionName = thePath.split( '/' ).last();
if ( QgsOWSConnection::connectionList( "WCS" ).contains( connectionName ) )
{
QgsOWSConnection connection( "WCS", connectionName );
return new QgsWCSConnectionItem( parentItem, "WCS", thePath, connection.uri().encodedUri() );
}
}

return 0;
}

QGISEXTERN QgsWCSSourceSelect * selectWidget( QWidget * parent, Qt::WindowFlags fl )
@@ -23,7 +23,7 @@ class QgsWCSConnectionItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path );
QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path, QString uri );
~QgsWCSConnectionItem();

QVector<QgsDataItem*> createChildren();
@@ -37,6 +37,9 @@ class QgsWCSConnectionItem : public QgsDataCollectionItem
public slots:
void editConnection();
void deleteConnection();

private:
QString mUri;
};

// WCS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
@@ -38,8 +38,9 @@ QgsWFSLayerItem::~QgsWFSLayerItem()

////

QgsWFSConnectionItem::QgsWFSConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path ), mName( name ), mCapabilities( NULL )
QgsWFSConnectionItem::QgsWFSConnectionItem( QgsDataItem* parent, QString name, QString path , QString uri )
: QgsDataCollectionItem( parent, name, path )
, mUri( uri )
{
mIcon = QgsApplication::getThemeIcon( "mIconWfs.svg" );
}
@@ -52,12 +53,11 @@ QVector<QgsDataItem*> QgsWFSConnectionItem::createChildren()
{
mGotCapabilities = false;

QString encodedUri = mPath;
QgsDataSourceURI uri;
uri.setEncodedUri( encodedUri );
QgsDebugMsg( "encodedUri = " + encodedUri );
uri.setEncodedUri( mUri );
QgsDebugMsg( "mUri = " + mUri );

mCapabilities = new QgsWFSCapabilities( encodedUri );
mCapabilities = new QgsWFSCapabilities( mUri );
connect( mCapabilities, SIGNAL( gotCapabilities() ), this, SLOT( gotCapabilities() ) );

mCapabilities->requestCapabilities();
@@ -153,7 +153,8 @@ QVector<QgsDataItem*> QgsWFSRootItem::createChildren()
foreach ( QString connName, QgsOWSConnection::connectionList( "WFS" ) )
{
QgsOWSConnection connection( "WFS", connName );
QgsDataItem * conn = new QgsWFSConnectionItem( this, connName, connection.uri().encodedUri() );
QString path = "wfs:/" + connName;
QgsDataItem * conn = new QgsWFSConnectionItem( this, connName, path, connection.uri().encodedUri() );
conn->setIcon( QgsApplication::getThemeIcon( "mIconConnect.png" ) );
connections.append( conn );
}
@@ -214,5 +215,16 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return new QgsWFSRootItem( parentItem, "WFS", "wfs:" );
}

return new QgsWFSConnectionItem( parentItem, "WFS", thePath );
// path schema: wfs:/connection name (used by OWS)
if ( thePath.startsWith( "wfs:/" ) )
{
QString connectionName = thePath.split( '/' ).last();
if ( QgsOWSConnection::connectionList( "WFS" ).contains( connectionName ) )
{
QgsOWSConnection connection( "WFS", connectionName );
return new QgsWFSConnectionItem( parentItem, "WMS", thePath, connection.uri().encodedUri() );
}
}

return 0;
}
@@ -34,7 +34,6 @@ class QgsWFSRootItem : public QgsDataCollectionItem

public slots:
void connectionsChanged();

void newConnection();
};

@@ -44,7 +43,7 @@ class QgsWFSConnectionItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsWFSConnectionItem( QgsDataItem* parent, QString name, QString path );
QgsWFSConnectionItem( QgsDataItem* parent, QString name, QString path, QString uri );
~QgsWFSConnectionItem();

QVector<QgsDataItem*> createChildren();
@@ -59,7 +58,7 @@ class QgsWFSConnectionItem : public QgsDataCollectionItem
void deleteConnection();

private:
QString mName;
QString mUri;

QgsWFSCapabilities* mCapabilities;
bool mGotCapabilities;

0 comments on commit 1018c91

Please sign in to comment.
You can’t perform that action at this time.