Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a model for schemas from a database connection
- Loading branch information
1 parent
2eb4eaf
commit a0a57d5
Showing
7 changed files
with
387 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsdatabaseschemamodel.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
class QgsDatabaseSchemaModel : QAbstractItemModel | ||
{ | ||
%Docstring | ||
A model containing schemas from a database connection. | ||
|
||
This class does not automatically subscribe to database updates. Schemas are queried | ||
from the database initially upon model construction. In order | ||
to update the listed schemas, QgsDatabaseSchemaModel.refresh() must be manually | ||
called. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsdatabaseschemamodel.h" | ||
%End | ||
public: | ||
|
||
explicit QgsDatabaseSchemaModel( const QString &provider, const QString &connection, QObject *parent /TransferThis/ = 0 ); | ||
%Docstring | ||
Constructor for QgsDatabaseSchemaModel, for the specified ``provider`` and ``connection`` name. | ||
|
||
.. warning:: | ||
|
||
The ``provider`` must support the connection API methods in its QgsProviderMetadata implementation | ||
in order for the model to work correctly. | ||
%End | ||
|
||
explicit QgsDatabaseSchemaModel( QgsAbstractDatabaseProviderConnection *connection /Transfer/, QObject *parent /TransferThis/ = 0 ); | ||
|
||
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 schema list by querying the underlying connection. | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsdatabaseschemamodel.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/*************************************************************************** | ||
qgsdatabaseschemamodel.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 "qgsdatabaseschemamodel.h" | ||
#include "qgsproviderregistry.h" | ||
#include "qgsprovidermetadata.h" | ||
#include "qgsabstractdatabaseproviderconnection.h" | ||
|
||
QgsDatabaseSchemaModel::QgsDatabaseSchemaModel( const QString &provider, const QString &connection, QObject *parent ) | ||
: QAbstractItemModel( parent ) | ||
{ | ||
QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( provider ); | ||
Q_ASSERT( metadata ); | ||
|
||
mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) ); | ||
Q_ASSERT( mConnection ); | ||
init(); | ||
} | ||
|
||
QgsDatabaseSchemaModel::QgsDatabaseSchemaModel( QgsAbstractDatabaseProviderConnection *connection, QObject *parent ) | ||
: QAbstractItemModel( parent ) | ||
, mConnection( connection ) | ||
{ | ||
Q_ASSERT( mConnection ); | ||
init(); | ||
} | ||
|
||
void QgsDatabaseSchemaModel::init() | ||
{ | ||
Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Schemas ); | ||
mSchemas = mConnection->schemas(); | ||
} | ||
|
||
QModelIndex QgsDatabaseSchemaModel::parent( const QModelIndex &child ) const | ||
{ | ||
Q_UNUSED( child ) | ||
return QModelIndex(); | ||
} | ||
|
||
|
||
int QgsDatabaseSchemaModel::rowCount( const QModelIndex &parent ) const | ||
{ | ||
if ( parent.isValid() ) | ||
return 0; | ||
|
||
return mSchemas.count(); | ||
} | ||
|
||
int QgsDatabaseSchemaModel::columnCount( const QModelIndex &parent ) const | ||
{ | ||
Q_UNUSED( parent ) | ||
return 1; | ||
} | ||
|
||
|
||
QVariant QgsDatabaseSchemaModel::data( const QModelIndex &index, int role ) const | ||
{ | ||
if ( !index.isValid() ) | ||
return QVariant(); | ||
|
||
const QString schemaName = mSchemas.value( index.row() ); | ||
switch ( role ) | ||
{ | ||
case Qt::DisplayRole: | ||
case Qt::ToolTipRole: | ||
{ | ||
return schemaName; | ||
} | ||
} | ||
|
||
return QVariant(); | ||
} | ||
|
||
QModelIndex QgsDatabaseSchemaModel::index( int row, int column, const QModelIndex &parent ) const | ||
{ | ||
if ( hasIndex( row, column, parent ) ) | ||
{ | ||
return createIndex( row, column, row ); | ||
} | ||
|
||
return QModelIndex(); | ||
} | ||
|
||
void QgsDatabaseSchemaModel::refresh() | ||
{ | ||
const QStringList newSchemas = mConnection->schemas(); | ||
const QStringList oldSchemas = mSchemas; | ||
|
||
for ( const QString &oldSchema : oldSchemas ) | ||
{ | ||
if ( !newSchemas.contains( oldSchema ) ) | ||
{ | ||
int r = mSchemas.indexOf( oldSchema ); | ||
beginRemoveRows( QModelIndex(), r, r ); | ||
mSchemas.removeAt( r ); | ||
endRemoveRows(); | ||
} | ||
} | ||
|
||
for ( const QString &newSchema : newSchemas ) | ||
{ | ||
if ( !mSchemas.contains( newSchema ) ) | ||
{ | ||
beginInsertRows( QModelIndex(), mSchemas.count(), mSchemas.count() ); | ||
mSchemas.append( newSchema ); | ||
endInsertRows(); | ||
} | ||
} | ||
} |
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,78 @@ | ||
/*************************************************************************** | ||
qgsdatabaseschemamodel.h | ||
-------------------------------------- | ||
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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSDATABASESCHEMAMODEL_H | ||
#define QGSDATABASESCHEMAMODEL_H | ||
|
||
#include <QAbstractItemModel> | ||
#include <QSortFilterProxyModel> | ||
#include <QStringList> | ||
#include <memory> | ||
|
||
#include "qgis_core.h" | ||
#include "qgis_sip.h" | ||
|
||
class QgsProviderMetadata; | ||
class QgsAbstractDatabaseProviderConnection; | ||
|
||
/** | ||
* \ingroup core | ||
* \class QgsDatabaseSchemaModel | ||
* \brief A model containing schemas from a database connection. | ||
* | ||
* This class does not automatically subscribe to database updates. Schemas are queried | ||
* from the database initially upon model construction. In order | ||
* to update the listed schemas, QgsDatabaseSchemaModel::refresh() must be manually | ||
* called. | ||
* | ||
* \since QGIS 3.14 | ||
*/ | ||
class CORE_EXPORT QgsDatabaseSchemaModel : public QAbstractItemModel | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
/** | ||
* Constructor for QgsDatabaseSchemaModel, for the specified \a provider and \a connection name. | ||
* | ||
* \warning The \a provider must support the connection API methods in its QgsProviderMetadata implementation | ||
* in order for the model to work correctly. | ||
*/ | ||
explicit QgsDatabaseSchemaModel( const QString &provider, const QString &connection, QObject *parent SIP_TRANSFERTHIS = nullptr ); | ||
|
||
explicit QgsDatabaseSchemaModel( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER, QObject *parent SIP_TRANSFERTHIS = nullptr ); | ||
|
||
// QAbstractItemModel interface | ||
QModelIndex parent( const QModelIndex &child ) const override; | ||
int rowCount( const QModelIndex &parent = QModelIndex() ) const override; | ||
int columnCount( const QModelIndex &parent = QModelIndex() ) const override; | ||
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override; | ||
QModelIndex index( int row, int column, const QModelIndex &parent ) const override; | ||
|
||
public slots: | ||
|
||
/** | ||
* Refreshes the schema list by querying the underlying connection. | ||
*/ | ||
void refresh(); | ||
|
||
private: | ||
void init(); | ||
std::unique_ptr< QgsAbstractDatabaseProviderConnection > mConnection; | ||
QStringList mSchemas; | ||
}; | ||
|
||
#endif // QGSDATABASESCHEMAMODEL_H |
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
Oops, something went wrong.