Skip to content

Commit

Permalink
Expose DB fields to the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jul 8, 2020
1 parent 1878fc7 commit f00d209
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 3 deletions.
85 changes: 85 additions & 0 deletions python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -49,6 +49,8 @@ Parent/children hierarchy is not based on QObject.
Favorites,
Project,
Custom,
Fields,
Field,
};


Expand Down Expand Up @@ -900,6 +902,89 @@ Creates a new data item from the specified path.
};


class QgsFieldsItem : QgsDataItem
{
%Docstring
A collection of fields item

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgsdataitem.h"
%End
public:

QgsFieldsItem( QgsDataItem *parent /TransferThis/,
const QString &name,
const QString &path,
const QString &providerKey,
const QString schema,
const QString tableName );
%Docstring
Constructor for QgsFieldsItem, with the specified ``parent`` item.

The ``path`` argument gives the item path in the browser tree. The ``path`` string can take any form,
but QgsDataItem items pointing to different logical locations should always use a different item ``path``.

The ``providerKey`` string (added in QGIS 3.12) can be used to specify the key for the QgsDataItemProvider that created this item.
The ``name`` argument specifies the text to show in the model for the item. A translated string should
be used wherever appropriate.
%End

~QgsFieldsItem();

virtual QVector<QgsDataItem *> createChildren();


virtual QIcon icon();


protected:

static QIcon openFieldsIcon();
%Docstring
Shared open fields icon.
%End

static QIcon fieldsIcon();
%Docstring
Shared closed fields icon.
%End

};


class QgsFieldItem : QgsDataItem
{
%Docstring
A layer field item

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgsdataitem.h"
%End
public:

QgsFieldItem( QgsDataItem *parent /TransferThis/,
const QgsField &field );
%Docstring
Constructor for QgsFieldItem, with the specified ``parent`` item and /a field.
The ``name`` argument specifies the text to show in the model for the item. A translated string should
be used wherever appropriate.
%End

~QgsFieldItem();

virtual QIcon icon();


};






Expand Down
11 changes: 10 additions & 1 deletion src/core/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -315,7 +315,16 @@ QgsGeoPackageCollectionItem *QgsGeoPackageAbstractLayerItem::collection() const
QgsGeoPackageVectorLayerItem::QgsGeoPackageVectorLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType )
: QgsGeoPackageAbstractLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) )
{
mCapabilities |= Rename;
mCapabilities |= ( Rename | Fertile );
setState( QgsDataItem::State::NotPopulated );
}


QVector<QgsDataItem *> QgsGeoPackageVectorLayerItem::createChildren()
{
QVector<QgsDataItem *> children;
children.push_back( new QgsFieldsItem( this, tr( "Columns" ), uri() + QStringLiteral( "/columns/ " ), providerKey(), QString(), name() ) );
return children;
}


Expand Down
2 changes: 2 additions & 0 deletions src/core/providers/ogr/qgsgeopackagedataitems.h
Expand Up @@ -126,6 +126,8 @@ class CORE_EXPORT QgsGeoPackageVectorLayerItem final: public QgsGeoPackageAbstra
QgsGeoPackageVectorLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType );
bool executeDeleteLayer( QString &errCause ) override;

// QgsDataItem interface
QVector<QgsDataItem *> createChildren() override;
};

/**
Expand Down
77 changes: 77 additions & 0 deletions src/core/qgsdataitem.cpp
Expand Up @@ -43,6 +43,7 @@
#include "qgsanimatedicon.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsprovidermetadata.h"

// use GDAL VSI mechanism
#define CPL_SUPRESS_CPLUSPLUS //#spellok
Expand Down Expand Up @@ -110,6 +111,82 @@ QIcon QgsDataCollectionItem::iconDir()
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolder.svg" ) );
}


QgsFieldsItem::QgsFieldsItem( QgsDataItem *parent,
const QString &name,
const QString &path,
const QString &providerKey,
const QString schema,
const QString tableName )
: QgsDataItem( QgsDataItem::Fields, parent, name, path, providerKey )
, mSchema( schema )
, mTableName( tableName )
{
mCapabilities |= ( Fertile | Collapse );
}

QgsFieldsItem::~QgsFieldsItem()
{

}

QVector<QgsDataItem *> QgsFieldsItem::createChildren()
{
QVector<QgsDataItem *> children;
try
{
QgsProviderMetadata *md { QgsProviderRegistry::instance()->providerMetadata( providerKey() ) };
if ( md )
{
QgsAbstractDatabaseProviderConnection *conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( parent()->path( ), {} ) ) };
if ( conn )
{
const QgsFields constFields { conn->fields( mSchema, mTableName ) };
for ( const auto &f : constFields )
{
children.push_back( new QgsFieldItem( this, f ) );
}
}
}
}
catch ( const QgsProviderConnectionException &ex )
{
children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
}
return children;
}

QIcon QgsFieldsItem::icon()
{
return state() == Populated ? openFieldsIcon() : fieldsIcon();
}

QIcon QgsFieldsItem::openFieldsIcon()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields.svg" ) );
}

QIcon QgsFieldsItem::fieldsIcon()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields.svg" ) );
}

QgsFieldItem::QgsFieldItem( QgsDataItem *parent, const QgsField &field )
: QgsDataItem( QgsDataItem::Type::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
, mField( field )
{
setState( QgsDataItem::State::Populated );
}

QgsFieldItem::~QgsFieldItem()
{
}

QIcon QgsFieldItem::icon()
{
return QgsFields::iconForFieldType( mField.type() );
}

QIcon QgsFavoritesItem::iconFavorites()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavourites.svg" ) );
Expand Down
85 changes: 85 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -82,6 +82,8 @@ class CORE_EXPORT QgsDataItem : public QObject
Favorites, //!< Represents a favorite item
Project, //!< Represents a QGIS project
Custom, //!< Custom item type
Fields, //!< Collection of fields
Field, //!< Vector layer field
};

Q_ENUM( Type )
Expand Down Expand Up @@ -895,6 +897,89 @@ class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
};


/**
* \ingroup core
* A collection of fields item
* \since QGIS 3.16
*/
class CORE_EXPORT QgsFieldsItem : public QgsDataItem
{
Q_OBJECT
public:

/**
* Constructor for QgsFieldsItem, with the specified \a parent item.
*
* The \a path argument gives the item path in the browser tree. The \a path string can take any form,
* but QgsDataItem items pointing to different logical locations should always use a different item \a path.
*
* The \a providerKey string (added in QGIS 3.12) can be used to specify the key for the QgsDataItemProvider that created this item.
* The \a name argument specifies the text to show in the model for the item. A translated string should
* be used wherever appropriate.
*/
QgsFieldsItem( QgsDataItem *parent SIP_TRANSFERTHIS,
const QString &name,
const QString &path,
const QString &providerKey,
const QString schema,
const QString tableName );

~QgsFieldsItem() override;

QVector<QgsDataItem *> createChildren() override;

QIcon icon() override;

protected:

/**
* Shared open fields icon.
*/
static QIcon openFieldsIcon();

/**
* Shared closed fields icon.
*/
static QIcon fieldsIcon();

private:

QString mSchema;
QString mTableName;
};


/**
* \ingroup core
* A layer field item
* \since QGIS 3.16
*/
class CORE_EXPORT QgsFieldItem : public QgsDataItem
{
Q_OBJECT
public:

/**
* Constructor for QgsFieldItem, with the specified \a parent item and /a field.
* The \a name argument specifies the text to show in the model for the item. A translated string should
* be used wherever appropriate.
*/
QgsFieldItem( QgsDataItem *parent SIP_TRANSFERTHIS,
const QgsField &field );

~QgsFieldItem() override;

// QgsDataItem interface
QIcon icon() override;

private:

const QgsField mField;

};



///@cond PRIVATE
#ifndef SIP_RUN

Expand Down
11 changes: 9 additions & 2 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -314,9 +314,9 @@ QgsPGLayerItem::QgsPGLayerItem( QgsDataItem *parent, const QString &name, const
: QgsLayerItem( parent, name, path, QString(), layerType, layerProperty.isRaster ? QStringLiteral( "postgresraster" ) : QStringLiteral( "postgres" ) )
, mLayerProperty( layerProperty )
{
mCapabilities |= Delete;
mCapabilities |= Delete | Fertile;
mUri = createUri();
setState( Populated );
setState( NotPopulated );
Q_ASSERT( mLayerProperty.size() == 1 );
}

Expand Down Expand Up @@ -576,3 +576,10 @@ bool QgsPGSchemaItem::layerCollection() const
{
return true;
}

QVector<QgsDataItem *> QgsPGLayerItem::createChildren()
{
QVector<QgsDataItem *> children;
children.push_back( new QgsFieldsItem( this, tr( "Columns" ), uri() + QStringLiteral( "/columns/ " ), providerKey(), mLayerProperty.schemaName, mLayerProperty.tableName ) );
return children;
}
5 changes: 5 additions & 0 deletions src/providers/postgres/qgspostgresdataitems.h
Expand Up @@ -100,10 +100,15 @@ class QgsPGLayerItem : public QgsLayerItem

const QgsPostgresLayerProperty &layerInfo() const { return mLayerProperty; }

QVector<QgsDataItem *> createChildren() override;

private:
QgsPostgresLayerProperty mLayerProperty;

};



//! Provider for Postgres data item
class QgsPostgresDataItemProvider : public QgsDataItemProvider
{
Expand Down

0 comments on commit f00d209

Please sign in to comment.