mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-13 00:07:27 -05:00
Add new GUI widget QgsDatabaseTableComboBox for selection of
available tables for a specific data connection (providers must implement the connections API)
This commit is contained in:
parent
bd5dcd29a1
commit
35411af6f6
111
python/gui/auto_generated/qgsdatabasetablecombobox.sip.in
Normal file
111
python/gui/auto_generated/qgsdatabasetablecombobox.sip.in
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/************************************************************************
|
||||||
|
* This file has been generated automatically from *
|
||||||
|
* *
|
||||||
|
* src/gui/qgsdatabasetablecombobox.h *
|
||||||
|
* *
|
||||||
|
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class QgsDatabaseTableComboBox : QWidget
|
||||||
|
{
|
||||||
|
%Docstring
|
||||||
|
The QgsDatabaseTableComboBox class is a combo box which displays the list of tables for a specific database connection.
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
|
||||||
|
The provider must support the connection API methods in its QgsProviderMetadata implementation
|
||||||
|
in order for the combobox to work correctly.
|
||||||
|
|
||||||
|
.. versionadded:: 3.14
|
||||||
|
%End
|
||||||
|
|
||||||
|
%TypeHeaderCode
|
||||||
|
#include "qgsdatabasetablecombobox.h"
|
||||||
|
%End
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit QgsDatabaseTableComboBox( const QString &provider, const QString &connection, const QString &schema = QString(), QWidget *parent /TransferThis/ = 0 );
|
||||||
|
%Docstring
|
||||||
|
Constructor for QgsDatabaseTableComboBox, for the specified ``provider`` and ``connection``.
|
||||||
|
|
||||||
|
The optional ``schema`` argument can be used to restrict the listed tables to 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 QgsDatabaseTableComboBox( QgsAbstractDatabaseProviderConnection *connection /Transfer/, const QString &schema = QString(), QWidget *parent /TransferThis/ = 0 );
|
||||||
|
%Docstring
|
||||||
|
Constructor for QgsDatabaseTableComboBox, for the specified ``connection``.
|
||||||
|
|
||||||
|
The optional ``schema`` argument can be used to restrict the listed tables to a specific schema.
|
||||||
|
|
||||||
|
Ownership of ``connection`` is transferred to the combobox.
|
||||||
|
%End
|
||||||
|
|
||||||
|
QString currentTable() const;
|
||||||
|
%Docstring
|
||||||
|
Returns the name of the current table selected in the combo box.
|
||||||
|
%End
|
||||||
|
|
||||||
|
QString currentSchema() const;
|
||||||
|
%Docstring
|
||||||
|
Returns the schema of the current table selected in the combo box.
|
||||||
|
%End
|
||||||
|
|
||||||
|
QComboBox *comboBox();
|
||||||
|
%Docstring
|
||||||
|
Returns the combobox portion of the widget.
|
||||||
|
%End
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
void setTable( const QString &table, const QString &schema = QString() );
|
||||||
|
%Docstring
|
||||||
|
Sets the current table selected in the combo box.
|
||||||
|
|
||||||
|
If necessary, the ``schema`` can be specified too.
|
||||||
|
%End
|
||||||
|
|
||||||
|
void setConnectionName( const QString &connection, const QString &provider = QString() );
|
||||||
|
%Docstring
|
||||||
|
Sets the database connection name from which to retrieve the available tables.
|
||||||
|
|
||||||
|
Optionally the ``provider`` can be reset too.
|
||||||
|
%End
|
||||||
|
|
||||||
|
void setSchema( const QString &schema );
|
||||||
|
%Docstring
|
||||||
|
Sets the ``schema`` from which to retrieve the available tables.
|
||||||
|
%End
|
||||||
|
|
||||||
|
void refreshTables();
|
||||||
|
%Docstring
|
||||||
|
Refreshes the list of available tables.
|
||||||
|
%End
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void tableChanged( const QString &table, const QString &schema = QString() );
|
||||||
|
%Docstring
|
||||||
|
Emitted whenever the currently selected table changes.
|
||||||
|
%End
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
void indexChanged( int i );
|
||||||
|
void rowsChanged();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* This file has been generated automatically from *
|
||||||
|
* *
|
||||||
|
* src/gui/qgsdatabasetablecombobox.h *
|
||||||
|
* *
|
||||||
|
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
|
||||||
|
************************************************************************/
|
||||||
@ -55,6 +55,7 @@
|
|||||||
%Include auto_generated/qgscurveeditorwidget.sip
|
%Include auto_generated/qgscurveeditorwidget.sip
|
||||||
%Include auto_generated/qgscustomdrophandler.sip
|
%Include auto_generated/qgscustomdrophandler.sip
|
||||||
%Include auto_generated/qgsdatabaseschemacombobox.sip
|
%Include auto_generated/qgsdatabaseschemacombobox.sip
|
||||||
|
%Include auto_generated/qgsdatabasetablecombobox.sip
|
||||||
%Include auto_generated/qgsdataitemguiprovider.sip
|
%Include auto_generated/qgsdataitemguiprovider.sip
|
||||||
%Include auto_generated/qgsdataitemguiproviderregistry.sip
|
%Include auto_generated/qgsdataitemguiproviderregistry.sip
|
||||||
%Include auto_generated/qgsdatasourceselectdialog.sip
|
%Include auto_generated/qgsdatasourceselectdialog.sip
|
||||||
|
|||||||
@ -322,6 +322,7 @@ SET(QGIS_GUI_SRCS
|
|||||||
qgscustomdrophandler.cpp
|
qgscustomdrophandler.cpp
|
||||||
qgscurveeditorwidget.cpp
|
qgscurveeditorwidget.cpp
|
||||||
qgsdatabaseschemacombobox.cpp
|
qgsdatabaseschemacombobox.cpp
|
||||||
|
qgsdatabasetablecombobox.cpp
|
||||||
qgsdataitemguiprovider.cpp
|
qgsdataitemguiprovider.cpp
|
||||||
qgsdataitemguiproviderregistry.cpp
|
qgsdataitemguiproviderregistry.cpp
|
||||||
qgsdatumtransformdialog.cpp
|
qgsdatumtransformdialog.cpp
|
||||||
@ -527,6 +528,7 @@ SET(QGIS_GUI_HDRS
|
|||||||
qgscurveeditorwidget.h
|
qgscurveeditorwidget.h
|
||||||
qgscustomdrophandler.h
|
qgscustomdrophandler.h
|
||||||
qgsdatabaseschemacombobox.h
|
qgsdatabaseschemacombobox.h
|
||||||
|
qgsdatabasetablecombobox.h
|
||||||
qgsdataitemguiprovider.h
|
qgsdataitemguiprovider.h
|
||||||
qgsdataitemguiproviderregistry.h
|
qgsdataitemguiproviderregistry.h
|
||||||
qgsdatasourcemanagerdialog.h
|
qgsdatasourcemanagerdialog.h
|
||||||
|
|||||||
@ -78,7 +78,7 @@ void QgsDatabaseSchemaComboBox::setSchema( const QString &schema )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, Qt::MatchFixedString | Qt::MatchCaseSensitive );
|
QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
|
||||||
if ( !idx.empty() )
|
if ( !idx.empty() )
|
||||||
{
|
{
|
||||||
QModelIndex proxyIdx = idx.at( 0 );
|
QModelIndex proxyIdx = idx.at( 0 );
|
||||||
|
|||||||
174
src/gui/qgsdatabasetablecombobox.cpp
Normal file
174
src/gui/qgsdatabasetablecombobox.cpp
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
qgsdatabasetablecombobox.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 "qgsdatabasetablecombobox.h"
|
||||||
|
#include "qgsdatabasetablemodel.h"
|
||||||
|
#include "qgsapplication.h"
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QToolButton>
|
||||||
|
|
||||||
|
QgsDatabaseTableComboBox::QgsDatabaseTableComboBox( const QString &provider, const QString &connection, const QString &schema, QWidget *parent )
|
||||||
|
: QWidget( parent )
|
||||||
|
, mProvider( provider )
|
||||||
|
, mConnection( connection )
|
||||||
|
, mSchema( schema )
|
||||||
|
{
|
||||||
|
mModel = new QgsDatabaseTableModel( provider, connection, schema, this );
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
QgsDatabaseTableComboBox::QgsDatabaseTableComboBox( QgsAbstractDatabaseProviderConnection *connection, const QString &schema, QWidget *parent )
|
||||||
|
: QWidget( parent )
|
||||||
|
, mSchema( schema )
|
||||||
|
{
|
||||||
|
mModel = new QgsDatabaseTableModel( connection, schema, this );
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::init()
|
||||||
|
{
|
||||||
|
mComboBox = new QComboBox();
|
||||||
|
|
||||||
|
mSortModel = new QSortFilterProxyModel( this );
|
||||||
|
mSortModel->setSourceModel( mModel );
|
||||||
|
mSortModel->setSortRole( Qt::DisplayRole );
|
||||||
|
mSortModel->setSortLocaleAware( true );
|
||||||
|
mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
|
||||||
|
mSortModel->setDynamicSortFilter( true );
|
||||||
|
mSortModel->sort( 0 );
|
||||||
|
|
||||||
|
mComboBox->setModel( mSortModel );
|
||||||
|
|
||||||
|
QHBoxLayout *l = new QHBoxLayout();
|
||||||
|
l->setContentsMargins( 0, 0, 0, 0 );
|
||||||
|
l->addWidget( mComboBox );
|
||||||
|
|
||||||
|
QToolButton *refreshButton = new QToolButton();
|
||||||
|
refreshButton->setAutoRaise( true );
|
||||||
|
refreshButton->setToolTip( tr( "Refresh tables" ) );
|
||||||
|
refreshButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRefresh.svg" ) ) );
|
||||||
|
l->addWidget( refreshButton );
|
||||||
|
setLayout( l );
|
||||||
|
|
||||||
|
connect( refreshButton, &QToolButton::clicked, this, &QgsDatabaseTableComboBox::refreshTables );
|
||||||
|
|
||||||
|
connect( mComboBox, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsDatabaseTableComboBox::indexChanged );
|
||||||
|
connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsDatabaseTableComboBox::rowsChanged );
|
||||||
|
connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsDatabaseTableComboBox::rowsChanged );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::setTable( const QString &table, const QString &schema )
|
||||||
|
{
|
||||||
|
if ( schema == currentSchema() && table == currentTable() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ( table.isEmpty() )
|
||||||
|
{
|
||||||
|
mComboBox->setCurrentIndex( -1 );
|
||||||
|
emit tableChanged( QString() );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QModelIndexList idxs = mSortModel->match( mSortModel->index( 0, 0 ), QgsDatabaseTableModel::RoleTableName, table, -1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
|
||||||
|
for ( const QModelIndex &proxyIdx : idxs )
|
||||||
|
{
|
||||||
|
if ( proxyIdx.isValid() && proxyIdx.data( QgsDatabaseTableModel::RoleTableName ).toString() == table
|
||||||
|
&& ( schema.isEmpty() || proxyIdx.data( QgsDatabaseTableModel::RoleSchema ).toString() == schema ) )
|
||||||
|
{
|
||||||
|
mComboBox->setCurrentIndex( proxyIdx.row() );
|
||||||
|
emit tableChanged( currentTable(), currentSchema() );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mComboBox->setCurrentIndex( -1 );
|
||||||
|
emit tableChanged( QString() );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::setConnectionName( const QString &connection, const QString &provider )
|
||||||
|
{
|
||||||
|
if ( !provider.isEmpty() )
|
||||||
|
mProvider = provider;
|
||||||
|
|
||||||
|
mConnection = connection;
|
||||||
|
|
||||||
|
const QString oldTable = currentTable();
|
||||||
|
const QString oldSchema = currentSchema();
|
||||||
|
QgsDatabaseTableModel *oldModel = mModel;
|
||||||
|
mModel = new QgsDatabaseTableModel( mProvider, mConnection, mSchema, this );
|
||||||
|
mSortModel->setSourceModel( mModel );
|
||||||
|
oldModel->deleteLater();
|
||||||
|
if ( currentTable() != oldTable || currentSchema() != oldSchema )
|
||||||
|
setTable( oldTable, oldSchema );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::setSchema( const QString &schema )
|
||||||
|
{
|
||||||
|
const QString oldTable = currentTable();
|
||||||
|
QgsDatabaseTableModel *oldModel = mModel;
|
||||||
|
mSchema = schema;
|
||||||
|
mModel = new QgsDatabaseTableModel( mProvider, mConnection, mSchema, this );
|
||||||
|
mSortModel->setSourceModel( mModel );
|
||||||
|
oldModel->deleteLater();
|
||||||
|
setTable( oldTable );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::refreshTables()
|
||||||
|
{
|
||||||
|
const QString oldSchema = currentSchema();
|
||||||
|
const QString oldTable = currentTable();
|
||||||
|
mModel->refresh();
|
||||||
|
setTable( oldTable, oldSchema );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsDatabaseTableComboBox::currentSchema() const
|
||||||
|
{
|
||||||
|
const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
|
||||||
|
if ( !proxyIndex.isValid() )
|
||||||
|
{
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return mSortModel->data( proxyIndex, QgsDatabaseTableModel::RoleSchema ).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsDatabaseTableComboBox::currentTable() const
|
||||||
|
{
|
||||||
|
const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
|
||||||
|
if ( !proxyIndex.isValid() )
|
||||||
|
{
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return mSortModel->data( proxyIndex, QgsDatabaseTableModel::RoleTableName ).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::indexChanged( int i )
|
||||||
|
{
|
||||||
|
Q_UNUSED( i )
|
||||||
|
emit tableChanged( currentTable() );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsDatabaseTableComboBox::rowsChanged()
|
||||||
|
{
|
||||||
|
if ( mComboBox->count() == 1 )
|
||||||
|
{
|
||||||
|
//currently selected connection item has changed
|
||||||
|
emit tableChanged( currentTable(), currentSchema() );
|
||||||
|
}
|
||||||
|
else if ( mComboBox->count() == 0 )
|
||||||
|
{
|
||||||
|
emit tableChanged( QString() );
|
||||||
|
}
|
||||||
|
}
|
||||||
122
src/gui/qgsdatabasetablecombobox.h
Normal file
122
src/gui/qgsdatabasetablecombobox.h
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
qgsdatabasetablecombobox.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 QGSDATABASETABLECOMBOBOX_H
|
||||||
|
#define QGSDATABASETABLECOMBOBOX_H
|
||||||
|
|
||||||
|
#include <QComboBox>
|
||||||
|
|
||||||
|
#include "qgis_gui.h"
|
||||||
|
#include "qgis_sip.h"
|
||||||
|
|
||||||
|
class QgsDatabaseTableModel;
|
||||||
|
class QSortFilterProxyModel;
|
||||||
|
class QgsAbstractDatabaseProviderConnection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \ingroup gui
|
||||||
|
* \brief The QgsDatabaseTableComboBox class is a combo box which displays the list of tables for a specific database connection.
|
||||||
|
*
|
||||||
|
* \warning The provider must support the connection API methods in its QgsProviderMetadata implementation
|
||||||
|
* in order for the combobox to work correctly.
|
||||||
|
*
|
||||||
|
* \since QGIS 3.14
|
||||||
|
*/
|
||||||
|
class GUI_EXPORT QgsDatabaseTableComboBox : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for QgsDatabaseTableComboBox, for the specified \a provider and \a connection.
|
||||||
|
*
|
||||||
|
* The optional \a schema argument can be used to restrict the listed tables to a specific schema.
|
||||||
|
*
|
||||||
|
* \warning The provider must support the connection API methods in its QgsProviderMetadata implementation
|
||||||
|
* in order for the model to work correctly.
|
||||||
|
*/
|
||||||
|
explicit QgsDatabaseTableComboBox( const QString &provider, const QString &connection, const QString &schema = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for QgsDatabaseTableComboBox, for the specified \a connection.
|
||||||
|
*
|
||||||
|
* The optional \a schema argument can be used to restrict the listed tables to a specific schema.
|
||||||
|
*
|
||||||
|
* Ownership of \a connection is transferred to the combobox.
|
||||||
|
*/
|
||||||
|
explicit QgsDatabaseTableComboBox( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER, const QString &schema = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the current table selected in the combo box.
|
||||||
|
*/
|
||||||
|
QString currentTable() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the schema of the current table selected in the combo box.
|
||||||
|
*/
|
||||||
|
QString currentSchema() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the combobox portion of the widget.
|
||||||
|
*/
|
||||||
|
QComboBox *comboBox() { return mComboBox; }
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the current table selected in the combo box.
|
||||||
|
*
|
||||||
|
* If necessary, the \a schema can be specified too.
|
||||||
|
*/
|
||||||
|
void setTable( const QString &table, const QString &schema = QString() );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the database connection name from which to retrieve the available tables.
|
||||||
|
*
|
||||||
|
* Optionally the \a provider can be reset too.
|
||||||
|
*/
|
||||||
|
void setConnectionName( const QString &connection, const QString &provider = QString() );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the \a schema from which to retrieve the available tables.
|
||||||
|
*/
|
||||||
|
void setSchema( const QString &schema );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refreshes the list of available tables.
|
||||||
|
*/
|
||||||
|
void refreshTables();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
//! Emitted whenever the currently selected table changes.
|
||||||
|
void tableChanged( const QString &table, const QString &schema = QString() );
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
void indexChanged( int i );
|
||||||
|
void rowsChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void init();
|
||||||
|
|
||||||
|
QString mProvider;
|
||||||
|
QString mConnection;
|
||||||
|
QString mSchema;
|
||||||
|
QgsDatabaseTableModel *mModel = nullptr;
|
||||||
|
QSortFilterProxyModel *mSortModel = nullptr;
|
||||||
|
QComboBox *mComboBox = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // QGSDATABASETABLECOMBOBOX_H
|
||||||
@ -307,6 +307,7 @@ IF (ENABLE_PGTEST)
|
|||||||
ADD_PYTHON_TEST(PyQgsDatabaseSchemaModel test_qgsdatabaseschemamodel.py)
|
ADD_PYTHON_TEST(PyQgsDatabaseSchemaModel test_qgsdatabaseschemamodel.py)
|
||||||
ADD_PYTHON_TEST(PyQgsDatabaseTableModel test_qgsdatabasetablemodel.py)
|
ADD_PYTHON_TEST(PyQgsDatabaseTableModel test_qgsdatabasetablemodel.py)
|
||||||
ADD_PYTHON_TEST(PyQgsDatabaseSchemaComboBox test_qgsdatabaseschemacombobox.py)
|
ADD_PYTHON_TEST(PyQgsDatabaseSchemaComboBox test_qgsdatabaseschemacombobox.py)
|
||||||
|
ADD_PYTHON_TEST(PyQgsDatabaseTableComboBox test_qgsdatabasetablecombobox.py)
|
||||||
ADD_PYTHON_TEST(PyQgsProviderConnectionPostgres test_qgsproviderconnection_postgres.py)
|
ADD_PYTHON_TEST(PyQgsProviderConnectionPostgres test_qgsproviderconnection_postgres.py)
|
||||||
ENDIF (ENABLE_PGTEST)
|
ENDIF (ENABLE_PGTEST)
|
||||||
|
|
||||||
|
|||||||
214
tests/src/python/test_qgsdatabasetablecombobox.py
Normal file
214
tests/src/python/test_qgsdatabasetablecombobox.py
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""QGIS Unit tests for QgsDatabaseTableComboBox
|
||||||
|
|
||||||
|
.. note:: 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.
|
||||||
|
"""
|
||||||
|
__author__ = 'Nyall Dawson'
|
||||||
|
__date__ = '8/03/2020'
|
||||||
|
__copyright__ = 'Copyright 2020, The QGIS Project'
|
||||||
|
|
||||||
|
import qgis # NOQA
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from qgis.core import (
|
||||||
|
QgsProviderRegistry,
|
||||||
|
QgsFields,
|
||||||
|
QgsField,
|
||||||
|
QgsWkbTypes,
|
||||||
|
QgsCoordinateReferenceSystem
|
||||||
|
)
|
||||||
|
from qgis.gui import QgsDatabaseTableComboBox
|
||||||
|
from qgis.PyQt.QtCore import QCoreApplication, QVariant
|
||||||
|
from qgis.PyQt.QtTest import QSignalSpy
|
||||||
|
|
||||||
|
from qgis.testing import unittest
|
||||||
|
from utilities import unitTestDataPath, start_app
|
||||||
|
|
||||||
|
start_app()
|
||||||
|
|
||||||
|
TEST_DATA_DIR = unitTestDataPath()
|
||||||
|
|
||||||
|
|
||||||
|
class TestQgsDatabaseTableComboBox(unittest.TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
"""Run before all tests"""
|
||||||
|
|
||||||
|
QCoreApplication.setOrganizationName("QGIS_Test")
|
||||||
|
QCoreApplication.setOrganizationDomain(cls.__name__)
|
||||||
|
QCoreApplication.setApplicationName(cls.__name__)
|
||||||
|
start_app()
|
||||||
|
cls.postgres_conn = "service='qgis_test'"
|
||||||
|
if 'QGIS_PGTEST_DB' in os.environ:
|
||||||
|
cls.postgres_conn = os.environ['QGIS_PGTEST_DB']
|
||||||
|
cls.uri = cls.postgres_conn + ' sslmode=disable'
|
||||||
|
|
||||||
|
def testCombo(self):
|
||||||
|
""" test combobox functionality """
|
||||||
|
md = QgsProviderRegistry.instance().providerMetadata('postgres')
|
||||||
|
conn = md.createConnection(self.uri, {})
|
||||||
|
md.saveConnection(conn, 'mycon')
|
||||||
|
|
||||||
|
m = QgsDatabaseTableComboBox('postgres', 'mycon')
|
||||||
|
spy = QSignalSpy(m.tableChanged)
|
||||||
|
self.assertGreaterEqual(m.comboBox().count(), 3)
|
||||||
|
|
||||||
|
text = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertIn('information_schema.attributes', text)
|
||||||
|
self.assertIn('qgis_test.some_poly_data', text)
|
||||||
|
self.assertLess(text.index('information_schema.attributes'), text.index('qgis_test.some_poly_data'))
|
||||||
|
self.assertEqual(m.currentSchema(), 'information_schema')
|
||||||
|
self.assertEqual(m.currentTable(), '_pg_foreign_data_wrappers')
|
||||||
|
|
||||||
|
m.setSchema('qgis_test')
|
||||||
|
text = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertNotIn('information_schema.attributes', text)
|
||||||
|
self.assertNotIn('attributes', text)
|
||||||
|
self.assertIn('some_poly_data', text)
|
||||||
|
|
||||||
|
self.assertEqual(m.currentTable(), '')
|
||||||
|
self.assertEqual(m.currentSchema(), '')
|
||||||
|
self.assertEqual(len(spy), 1)
|
||||||
|
self.assertFalse(spy[-1][0])
|
||||||
|
|
||||||
|
m.setTable('')
|
||||||
|
self.assertEqual(m.currentTable(), '')
|
||||||
|
self.assertEqual(m.currentSchema(), '')
|
||||||
|
self.assertEqual(len(spy), 1)
|
||||||
|
self.assertFalse(spy[-1][0])
|
||||||
|
m.setTable('someData')
|
||||||
|
self.assertEqual(len(spy), 2)
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(m.currentTable(), 'someData')
|
||||||
|
self.assertEqual(spy[-1][0], 'someData')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
fields = QgsFields()
|
||||||
|
fields.append(QgsField('test', QVariant.String))
|
||||||
|
conn.createVectorTable('qgis_test', 'myNewTable', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem('EPSG:3857'), False, {})
|
||||||
|
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
# tables are not automatically refreshed
|
||||||
|
self.assertEqual(text2, text)
|
||||||
|
|
||||||
|
# but setting a new connection should fix this!
|
||||||
|
md = QgsProviderRegistry.instance().providerMetadata('postgres')
|
||||||
|
conn2 = md.createConnection(self.uri, {})
|
||||||
|
md.saveConnection(conn2, 'another')
|
||||||
|
m.setConnectionName('another', 'postgres')
|
||||||
|
# ideally there'd be no extra signal here, but it's a minor issue...
|
||||||
|
self.assertEqual(len(spy), 3)
|
||||||
|
self.assertEqual(m.currentTable(), 'someData')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'someData')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertNotEqual(text2, text)
|
||||||
|
self.assertIn('myNewTable', text2)
|
||||||
|
|
||||||
|
m.setTable('myNewTable')
|
||||||
|
self.assertEqual(len(spy), 4)
|
||||||
|
self.assertEqual(m.currentTable(), 'myNewTable')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'myNewTable')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
# no auto drop
|
||||||
|
conn.dropVectorTable('qgis_test', 'myNewTable')
|
||||||
|
self.assertEqual(len(spy), 4)
|
||||||
|
self.assertEqual(m.currentTable(), 'myNewTable')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'myNewTable')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
m.refreshTables()
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertNotIn('myNewTable', text2)
|
||||||
|
self.assertEqual(len(spy), 5)
|
||||||
|
self.assertFalse(m.currentSchema())
|
||||||
|
self.assertFalse(spy[-1][0])
|
||||||
|
|
||||||
|
def testComboAllSchemas(self):
|
||||||
|
""" test combobox functionality showing all schemas """
|
||||||
|
md = QgsProviderRegistry.instance().providerMetadata('postgres')
|
||||||
|
conn = md.createConnection(self.uri, {})
|
||||||
|
md.saveConnection(conn, 'mycon2')
|
||||||
|
|
||||||
|
m = QgsDatabaseTableComboBox('postgres', 'mycon2')
|
||||||
|
spy = QSignalSpy(m.tableChanged)
|
||||||
|
self.assertGreaterEqual(m.comboBox().count(), 3)
|
||||||
|
|
||||||
|
text = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertIn('information_schema.attributes', text)
|
||||||
|
self.assertIn('qgis_test.some_poly_data', text)
|
||||||
|
self.assertLess(text.index('information_schema.attributes'), text.index('qgis_test.some_poly_data'))
|
||||||
|
self.assertEqual(m.currentSchema(), 'information_schema')
|
||||||
|
self.assertEqual(m.currentTable(), '_pg_foreign_data_wrappers')
|
||||||
|
|
||||||
|
m.setTable('')
|
||||||
|
self.assertEqual(m.currentTable(), '')
|
||||||
|
self.assertEqual(m.currentSchema(), '')
|
||||||
|
self.assertEqual(len(spy), 1)
|
||||||
|
self.assertFalse(spy[-1][0])
|
||||||
|
m.setTable('someData', 'qgis_test')
|
||||||
|
self.assertEqual(len(spy), 2)
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(m.currentTable(), 'someData')
|
||||||
|
self.assertEqual(spy[-1][0], 'someData')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
fields = QgsFields()
|
||||||
|
fields.append(QgsField('test', QVariant.String))
|
||||||
|
conn.createVectorTable('qgis_test', 'myNewTable', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem('EPSG:3857'), False, {})
|
||||||
|
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
# tables are not automatically refreshed
|
||||||
|
self.assertEqual(text2, text)
|
||||||
|
|
||||||
|
# but setting a new connection should fix this!
|
||||||
|
md = QgsProviderRegistry.instance().providerMetadata('postgres')
|
||||||
|
conn2 = md.createConnection(self.uri, {})
|
||||||
|
md.saveConnection(conn2, 'another')
|
||||||
|
m.setConnectionName('another', 'postgres')
|
||||||
|
# ideally there'd be no extra signal here, but it's a minor issue...
|
||||||
|
self.assertEqual(len(spy), 3)
|
||||||
|
self.assertEqual(m.currentTable(), 'someData')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'someData')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertNotEqual(text2, text)
|
||||||
|
self.assertIn('qgis_test.myNewTable', text2)
|
||||||
|
|
||||||
|
m.setTable('myNewTable', 'qgis_test')
|
||||||
|
self.assertEqual(len(spy), 4)
|
||||||
|
self.assertEqual(m.currentTable(), 'myNewTable')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'myNewTable')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
# no auto drop
|
||||||
|
conn.dropVectorTable('qgis_test', 'myNewTable')
|
||||||
|
self.assertEqual(len(spy), 4)
|
||||||
|
self.assertEqual(m.currentTable(), 'myNewTable')
|
||||||
|
self.assertEqual(m.currentSchema(), 'qgis_test')
|
||||||
|
self.assertEqual(spy[-1][0], 'myNewTable')
|
||||||
|
self.assertEqual(spy[-1][1], 'qgis_test')
|
||||||
|
|
||||||
|
m.refreshTables()
|
||||||
|
text2 = [m.comboBox().itemText(i) for i in range(m.comboBox().count())]
|
||||||
|
self.assertNotIn('qgis_test.myNewTable', text2)
|
||||||
|
self.assertEqual(len(spy), 5)
|
||||||
|
self.assertFalse(m.currentSchema())
|
||||||
|
self.assertFalse(spy[-1][0])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
x
Reference in New Issue
Block a user