Skip to content
Permalink
Browse files

New table dialog menu provider

  • Loading branch information
elpaso committed Jul 14, 2020
1 parent 1f11d31 commit d8a45f92a31b77c2fef62e5ac6059495cf8299c8
@@ -654,6 +654,48 @@ Shared home directory icon.
%End
};


class QgsDatabaseSchemaItem : QgsDataCollectionItem
{
%Docstring
A Collection that represents a database schema item

.. versionadded:: 3.16
%End

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

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

The ``name`` argument specifies the text to show in the model for the item. A translated string should
be used wherever appropriate.

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

The optional ``providerKey`` string can be used to specify the key for the QgsDataItemProvider that created this item.
%End

~QgsDatabaseSchemaItem();


static QIcon iconDataCollection();
%Docstring
Returns the standard browser data collection icon.

.. seealso:: :py:func:`iconDir`
%End

protected:

};


class QgsDirectoryItem : QgsDataCollectionItem
{
%Docstring
@@ -98,6 +98,25 @@ The ``context`` argument gives the wider context under which the context menu is
and contains accessors for useful objects like the application message bar.

The base class method has no effect.
%End

virtual void populateDatabaseContextMenu( QgsDataItem *item,
QMenu *menu,
const QList<QgsDataItem *> &selectedItems,
QgsDataItemGuiContext context ) final;
%Docstring
populateDatabaseContextMenu adds database related actions to QgsDatabaseSchemaItem and :py:class:`QgsDataCollectionItem`
instances for providers that support the connections API and for connections having the required capabilities.

Concrete classes should call this method from :py:func:`~QgsDataItemGuiProvider.populateContextMenu` if they want to add common actions to the
generated context menu.

:param item: item the actions belong to
:param menu: menu that will contain actions created by this method
:param selectedItems: selected items
:param context: data item gui context

.. versionadded:: 3.16
%End

virtual bool rename( QgsDataItem *item, const QString &name, QgsDataItemGuiContext context );
@@ -14,11 +14,14 @@ class QgsNewVectorTableDialog : QDialog
{
%Docstring
The QgsNewVectorTableDialog class is a provider-agnostic database vector
and aspatial table designer dialog.
and aspatial table designer dialog based on the connections API.

It allows to design a new vector or aspatial database table by defining the schema
(if supported by the provider) and table names, the list of QgsFields,
the geometry type and the SRID (if the table is not aspatial).
(if supported by the provider) and table name, the list of QgsFields,
the optional geometry type and SRID.

The actual creation of the table is delegated to the connections API method
:py:func:`QgsAbstractDatabaseProviderConnection.createVectorTable()`

.. versionadded:: 3.16
%End
@@ -36,21 +39,36 @@ QgsNewVectorTableDialog constructor
:param parent: optional parent
%End

void setSchemaName( const QString &name );

void setTableName( const QString &name );

void setGeometryType( QgsWkbTypes::Type type );

QString tableName() const;

QString schemaName() const;

QString geometryColumnName() const;

QgsFields fields() const;

QgsWkbTypes::Type geometryType() const;

QgsCoordinateReferenceSystem crs() const;

void setFields( const QgsFields &fields );

protected:
virtual void showEvent( QShowEvent *event );

};






/************************************************************************
* This file has been generated automatically from *
* *
@@ -102,7 +102,7 @@ void QgsGeoPackageRootItem::newConnection()
}

QgsGeoPackageCollectionItem::QgsGeoPackageCollectionItem( QgsDataItem *parent, const QString &name, const QString &path )
: QgsDataCollectionItem( parent, name, path, QStringLiteral( "GPKG" ) )
: QgsDataCollectionItem( parent, name, path, QStringLiteral( "ogr" ) )
{
mToolTip = QString( path ).remove( QLatin1String( "gpkg:/" ) );
mCapabilities |= Collapse;
@@ -1741,6 +1741,23 @@ QStringList QgsZipItem::getZipFileList()
return mZipFileList;
}


QgsDatabaseSchemaItem::QgsDatabaseSchemaItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey )
: QgsDataCollectionItem( parent, name, path, providerKey )
{

}

QgsDatabaseSchemaItem::~QgsDatabaseSchemaItem()
{

}

QIcon QgsDatabaseSchemaItem::iconDataCollection()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconDbSchema.svg" ) );
}

///@cond PRIVATE

QgsProjectHomeItem::QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path )
@@ -666,6 +666,44 @@ class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem
static QIcon homeDirIcon();
};


/**
* \ingroup core
* A Collection that represents a database schema item
* \since QGIS 3.16
*/
class CORE_EXPORT QgsDatabaseSchemaItem : public QgsDataCollectionItem
{
Q_OBJECT
public:

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

~QgsDatabaseSchemaItem() override;


/**
* Returns the standard browser data collection icon.
* \see iconDir()
*/
static QIcon iconDataCollection();

protected:

};


/**
* \ingroup core
* A directory: contains subdirectories and layers
@@ -40,9 +40,10 @@
#include "qgsgeopackageproviderconnection.h"

void QgsGeoPackageItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &,
const QList<QgsDataItem *> &selectedItems,
QgsDataItemGuiContext context )
{
populateDatabaseContextMenu( item, menu, selectedItems, context );
if ( QgsGeoPackageVectorLayerItem *layerItem = qobject_cast< QgsGeoPackageVectorLayerItem * >( item ) )
{
// Check capabilities
@@ -14,7 +14,12 @@
***************************************************************************/

#include "qgsdataitemguiprovider.h"

#include "qgsdataitem.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgsnewvectortabledialog.h"
#include "qgsmessagebar.h"
//
// QgsDataItemGuiContext
//
@@ -35,6 +40,68 @@ void QgsDataItemGuiContext::setMessageBar( QgsMessageBar *messageBar )

void QgsDataItemGuiProvider::populateContextMenu( QgsDataItem *, QMenu *, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
{

}

void QgsDataItemGuiProvider::populateDatabaseContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context )
{
Q_UNUSED( selectedItems )
// Add create new table for connections
if ( QgsDataCollectionItem * collectionItem { qobject_cast<QgsDataCollectionItem *>( item ) } )
{
QgsProviderMetadata *md { QgsProviderRegistry::instance()->providerMetadata( collectionItem->providerKey() ) };
if ( md )
{
const bool isSchema { qobject_cast<QgsDatabaseSchemaItem *>( item ) };
const QString connectionName { isSchema ? collectionItem->parent()->name() : collectionItem->name() };
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) ) );
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::CreateVectorTable ) )
{
QAction *newTableAction = new QAction( QObject::tr( "New Table…" ), menu );
QObject::connect( newTableAction, &QAction::triggered, collectionItem, [ collectionItem, connectionName, md, isSchema, context]
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) ) };
QgsNewVectorTableDialog dlg { conn2.get(), nullptr };
// TODO: dlg.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
if ( isSchema )
{
dlg.setSchemaName( collectionItem->name() );
}
if ( dlg.exec() == QgsNewVectorTableDialog::DialogCode::Accepted )
{
const QgsFields fields { dlg.fields() };
const QString tableName { dlg.tableName() };
const QString schemaName { dlg.schemaName() };
const QString geometryColumn { dlg.geometryColumnName() };
const QgsWkbTypes::Type geometryType { dlg.geometryType() };
const QgsCoordinateReferenceSystem crs { dlg.crs( ) };
QMap<QString, QVariant> options;
if ( ! geometryColumn.isEmpty() )
{
options[ QStringLiteral( "geometryColumn" ) ] = geometryColumn;
}
try
{
conn2->createVectorTable( schemaName, tableName, fields, geometryType, crs, true, &options );
collectionItem->refresh();
if ( context.messageBar() )
{
context.messageBar()->pushSuccess( QObject::tr( "New Table Created" ), QObject::tr( "Table '%1' was created successfully." ).arg( tableName ) );
}
}
catch ( QgsProviderConnectionException &ex )
{
if ( context.messageBar() )
{
context.messageBar()->pushCritical( QObject::tr( "New Table Creation Error" ), QObject::tr( "Error creating new table '%1': %2" ).arg( tableName, ex.what() ) );
}
}
}
} );
menu->addAction( newTableAction );
}
}
}
}

bool QgsDataItemGuiProvider::rename( QgsDataItem *, const QString &, QgsDataItemGuiContext )
@@ -119,6 +119,24 @@ class GUI_EXPORT QgsDataItemGuiProvider
virtual void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context );

/**
* \brief populateDatabaseContextMenu adds database related actions to QgsDatabaseSchemaItem and QgsDataCollectionItem
* instances for providers that support the connections API and for connections having the required capabilities.
*
* Concrete classes should call this method from populateContextMenu() if they want to add common actions to the
* generated context menu.
*
* \param item item the actions belong to
* \param menu menu that will contain actions created by this method
* \param selectedItems selected items
* \param context data item gui context
* \since QGIS 3.16
*/
virtual void populateDatabaseContextMenu( QgsDataItem *item,
QMenu *menu,
const QList<QgsDataItem *> &selectedItems,
QgsDataItemGuiContext context ) final;

/**
* Sets a new \a name for the item, and returns TRUE if the item was successfully renamed.
*

0 comments on commit d8a45f9

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