Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model for database tables from a connection
Optionally limited to a specific schema, if desired
- Loading branch information
1 parent
e0ca4d4
commit f7731bc
Showing
10 changed files
with
559 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsdatabasetablemodel.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsDatabaseTableModel : QAbstractItemModel | ||
{ | ||
%Docstring | ||
A model containing tables from a database connection. | ||
|
||
This class does not automatically subscribe to database updates. Tables are queried | ||
from the database initially upon model construction. In order | ||
to update the listed tbales, QgsDatabaseTableModel.refresh() must be manually | ||
called. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsdatabasetablemodel.h" | ||
%End | ||
public: | ||
|
||
enum Role | ||
{ | ||
RoleTableName, | ||
RoleSchema, | ||
RoleTableFlags, | ||
RoleComment, | ||
RoleCustomInfo, | ||
RoleWkbType, | ||
RoleCrs, | ||
}; | ||
|
||
explicit QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema = QString(), QObject *parent /TransferThis/ = 0 ); | ||
%Docstring | ||
Constructor for QgsDatabaseTableModel, for the specified ``provider`` and ``connection`` name. | ||
|
||
The optional ``schema`` argument can be used to restrict the tables to those from a specific schema. | ||
|
||
.. warning:: | ||
|
||
The ``provider`` must support the connection API methods in its QgsProviderMetadata implementation | ||
in order for the model to work correctly. | ||
%End | ||
|
||
explicit QgsDatabaseTableModel( QgsAbstractDatabaseProviderConnection *connection /Transfer/, const QString &schema = QString(), QObject *parent /TransferThis/ = 0 ); | ||
%Docstring | ||
Constructor for QgsDatabaseTableModel, for the specified ``connection``. | ||
|
||
The optional ``schema`` argument can be used to restrict the tables to those from a specific schema. | ||
|
||
Ownership of ``connection`` is transferred to the model. | ||
%End | ||
|
||
virtual QModelIndex parent( const QModelIndex &child ) const; | ||
|
||
virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const; | ||
|
||
virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const; | ||
|
||
virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; | ||
|
||
virtual QModelIndex index( int row, int column, const QModelIndex &parent ) const; | ||
|
||
|
||
public slots: | ||
|
||
void refresh(); | ||
%Docstring | ||
Refreshes the table list by querying the underlying connection. | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsdatabasetablemodel.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/*************************************************************************** | ||
qgsdatabasetablemodel.cpp | ||
------------------------ | ||
Date : March 2020 | ||
Copyright : (C) 2020 Nyall Dawson | ||
Email : nyall dot dawson 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 "qgsdatabasetablemodel.h" | ||
#include "qgsproviderregistry.h" | ||
#include "qgsprovidermetadata.h" | ||
#include "qgsabstractdatabaseproviderconnection.h" | ||
#include "qgsdataitem.h" | ||
|
||
QgsDatabaseTableModel::QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema, QObject *parent ) | ||
: QAbstractItemModel( parent ) | ||
, mSchema( schema ) | ||
{ | ||
QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( provider ); | ||
Q_ASSERT( metadata ); | ||
|
||
mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) ); | ||
Q_ASSERT( mConnection ); | ||
init(); | ||
} | ||
|
||
QgsDatabaseTableModel::QgsDatabaseTableModel( QgsAbstractDatabaseProviderConnection *connection, const QString &schema, QObject *parent ) | ||
: QAbstractItemModel( parent ) | ||
, mConnection( connection ) | ||
, mSchema( schema ) | ||
{ | ||
Q_ASSERT( mConnection ); | ||
init(); | ||
} | ||
|
||
void QgsDatabaseTableModel::init() | ||
{ | ||
Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Tables ); | ||
mTables = mConnection->tables( mSchema ); | ||
} | ||
|
||
QModelIndex QgsDatabaseTableModel::parent( const QModelIndex &child ) const | ||
{ | ||
Q_UNUSED( child ) | ||
return QModelIndex(); | ||
} | ||
|
||
|
||
int QgsDatabaseTableModel::rowCount( const QModelIndex &parent ) const | ||
{ | ||
if ( parent.isValid() ) | ||
return 0; | ||
|
||
return mTables.count(); | ||
} | ||
|
||
int QgsDatabaseTableModel::columnCount( const QModelIndex &parent ) const | ||
{ | ||
Q_UNUSED( parent ) | ||
return 1; | ||
} | ||
|
||
|
||
QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const | ||
{ | ||
if ( !index.isValid() ) | ||
return QVariant(); | ||
|
||
if ( index.row() >= mTables.count() ) | ||
return QVariant(); | ||
|
||
const QgsAbstractDatabaseProviderConnection::TableProperty &table = mTables[ index.row() ]; | ||
switch ( role ) | ||
{ | ||
case Qt::DisplayRole: | ||
case Qt::ToolTipRole: | ||
{ | ||
return mSchema.isEmpty() && !table.schema().isEmpty() ? QStringLiteral( "%1.%2" ).arg( table.schema(), table.tableName() ) : table.tableName(); | ||
} | ||
|
||
case RoleTableName: | ||
{ | ||
return table.tableName(); | ||
} | ||
|
||
case Qt::DecorationRole: | ||
case RoleWkbType: | ||
case RoleCrs: | ||
{ | ||
if ( table.geometryColumnTypes().empty() ) | ||
{ | ||
if ( role == Qt::DecorationRole ) | ||
return QgsLayerItem::iconTable(); | ||
else | ||
return QVariant(); | ||
} | ||
|
||
if ( role == Qt::DecorationRole ) | ||
{ | ||
const QgsWkbTypes::GeometryType geomType = QgsWkbTypes::geometryType( table.geometryColumnTypes().at( 0 ).wkbType ); | ||
switch ( geomType ) | ||
{ | ||
case QgsWkbTypes::PointGeometry: | ||
{ | ||
return QgsLayerItem::iconPoint(); | ||
} | ||
case QgsWkbTypes::PolygonGeometry : | ||
{ | ||
return QgsLayerItem::iconPolygon(); | ||
} | ||
case QgsWkbTypes::LineGeometry : | ||
{ | ||
return QgsLayerItem::iconLine(); | ||
} | ||
default: | ||
return QgsLayerItem::iconTable(); | ||
} | ||
return QVariant(); | ||
} | ||
else if ( role == RoleWkbType ) | ||
return table.geometryColumnTypes().at( 0 ).wkbType; | ||
else if ( role == RoleCrs ) | ||
return table.geometryColumnTypes().at( 0 ).crs; | ||
|
||
return QVariant(); | ||
} | ||
|
||
case RoleSchema: | ||
return table.schema(); | ||
|
||
case RoleTableFlags: | ||
return static_cast< int >( table.flags() ); | ||
|
||
case RoleComment: | ||
return table.comment(); | ||
|
||
case RoleCustomInfo: | ||
return table.info(); | ||
|
||
} | ||
|
||
return QVariant(); | ||
} | ||
|
||
QModelIndex QgsDatabaseTableModel::index( int row, int column, const QModelIndex &parent ) const | ||
{ | ||
if ( hasIndex( row, column, parent ) ) | ||
{ | ||
return createIndex( row, column, row ); | ||
} | ||
|
||
return QModelIndex(); | ||
} | ||
|
||
void QgsDatabaseTableModel::refresh() | ||
{ | ||
const QList< QgsAbstractDatabaseProviderConnection::TableProperty > newTables = mConnection->tables( mSchema ); | ||
const QList< QgsAbstractDatabaseProviderConnection::TableProperty > oldTables = mTables; | ||
|
||
for ( const QgsAbstractDatabaseProviderConnection::TableProperty &oldTable : oldTables ) | ||
{ | ||
if ( !newTables.contains( oldTable ) ) | ||
{ | ||
int r = mTables.indexOf( oldTable ); | ||
beginRemoveRows( QModelIndex(), r, r ); | ||
mTables.removeAt( r ); | ||
endRemoveRows(); | ||
} | ||
} | ||
|
||
for ( const QgsAbstractDatabaseProviderConnection::TableProperty &newTable : newTables ) | ||
{ | ||
if ( !mTables.contains( newTable ) ) | ||
{ | ||
beginInsertRows( QModelIndex(), mTables.count(), mTables.count() ); | ||
mTables.append( newTable ); | ||
endInsertRows(); | ||
} | ||
} | ||
} |
Oops, something went wrong.