Skip to content
Permalink
Browse files

Add new GUI widget QgsDatabaseSchemaComboBox for selection of

available schemas for a specific data connection

(providers must implement the connections API)
  • Loading branch information
nyalldawson committed Mar 8, 2020
1 parent 2bdb0cc commit e8e13ecb6ff963bebc85946cd267c97746b88adb
@@ -0,0 +1,95 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsdatabaseschemacombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsDatabaseSchemaComboBox : QWidget
{
%Docstring
The QgsDatabaseSchemaComboBox class is a combo box which displays the list of schemas 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 "qgsdatabaseschemacombobox.h"
%End
public:

explicit QgsDatabaseSchemaComboBox( const QString &provider, const QString &connection, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsDatabaseSchemaComboBox, for the specified ``provider`` and ``connection``.

.. warning::

The provider must support the connection API methods in its QgsProviderMetadata implementation
in order for the model to work correctly.
%End

explicit QgsDatabaseSchemaComboBox( QgsAbstractDatabaseProviderConnection *connection /Transfer/, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsDatabaseSchemaComboBox, for the specified ``connection``.

Ownership of ``connection`` is transferred to the combobox.
%End

QString currentSchema() const;
%Docstring
Returns the name of the current schema selected in the combo box.
%End

QComboBox *comboBox();
%Docstring
Returns the combobox portion of the widget.
%End

public slots:

void setSchema( const QString &schema );
%Docstring
Sets the current schema selected in the combo box.
%End

void setConnectionName( const QString &connection, const QString &provider = QString() );
%Docstring
Sets the database connection name from which to retrieve the available schemas.

Optionally the ``provider`` can be reset too.
%End

void refreshSchemas();
%Docstring
Refreshes the list of available schemas.
%End

signals:
void schemaChanged( const QString &schema );
%Docstring
Emitted whenever the currently selected schema changes.
%End

protected slots:
void indexChanged( int i );
void rowsChanged();

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsdatabaseschemacombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -54,6 +54,7 @@
%Include auto_generated/qgscredentialdialog.sip
%Include auto_generated/qgscurveeditorwidget.sip
%Include auto_generated/qgscustomdrophandler.sip
%Include auto_generated/qgsdatabaseschemacombobox.sip
%Include auto_generated/qgsdataitemguiprovider.sip
%Include auto_generated/qgsdataitemguiproviderregistry.sip
%Include auto_generated/qgsdatasourceselectdialog.sip
@@ -321,6 +321,7 @@ SET(QGIS_GUI_SRCS
qgscredentialdialog.cpp
qgscustomdrophandler.cpp
qgscurveeditorwidget.cpp
qgsdatabaseschemacombobox.cpp
qgsdataitemguiprovider.cpp
qgsdataitemguiproviderregistry.cpp
qgsdatumtransformdialog.cpp
@@ -525,6 +526,7 @@ SET(QGIS_GUI_HDRS
qgscredentialdialog.h
qgscurveeditorwidget.h
qgscustomdrophandler.h
qgsdatabaseschemacombobox.h
qgsdataitemguiprovider.h
qgsdataitemguiproviderregistry.h
qgsdatasourcemanagerdialog.h
@@ -0,0 +1,135 @@
/***************************************************************************
qgsdatabaseschemacombobox.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 "qgsdatabaseschemacombobox.h"
#include "qgsdatabaseschemamodel.h"
#include <QHBoxLayout>

QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( const QString &provider, const QString &connection, QWidget *parent )
: QWidget( parent )
, mProvider( provider )
{
mModel = new QgsDatabaseSchemaModel( provider, connection, this );
init();
}

QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( QgsAbstractDatabaseProviderConnection *connection, QWidget *parent )
: QWidget( parent )
{
mModel = new QgsDatabaseSchemaModel( connection, this );
init();
}

void QgsDatabaseSchemaComboBox::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 );
setLayout( l );

connect( mComboBox, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsDatabaseSchemaComboBox::indexChanged );
connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsDatabaseSchemaComboBox::rowsChanged );
connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsDatabaseSchemaComboBox::rowsChanged );
}

void QgsDatabaseSchemaComboBox::setSchema( const QString &schema )
{
if ( schema == currentSchema() )
return;

if ( schema.isEmpty() )
{
mComboBox->setCurrentIndex( -1 );
emit schemaChanged( QString() );
return;
}

QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, Qt::MatchFixedString | Qt::MatchCaseSensitive );
if ( !idx.empty() )
{
QModelIndex proxyIdx = idx.at( 0 );
if ( proxyIdx.isValid() )
{
mComboBox->setCurrentIndex( proxyIdx.row() );
emit schemaChanged( currentSchema() );
return;
}
}
mComboBox->setCurrentIndex( -1 );
emit schemaChanged( QString() );
}

void QgsDatabaseSchemaComboBox::setConnectionName( const QString &connection, const QString &provider )
{
if ( !provider.isEmpty() )
mProvider = provider;

const QString oldSchema = currentSchema();
QgsDatabaseSchemaModel *oldModel = mModel;
mModel = new QgsDatabaseSchemaModel( mProvider, connection, this );
mSortModel->setSourceModel( mModel );
oldModel->deleteLater();
if ( currentSchema() != oldSchema )
setSchema( oldSchema );
}

void QgsDatabaseSchemaComboBox::refreshSchemas()
{
const QString oldSchema = currentSchema();
mModel->refresh();
setSchema( oldSchema );
}

QString QgsDatabaseSchemaComboBox::currentSchema() const
{
const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
if ( !proxyIndex.isValid() )
{
return QString();
}

return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
}

void QgsDatabaseSchemaComboBox::indexChanged( int i )
{
Q_UNUSED( i )
emit schemaChanged( currentSchema() );
}

void QgsDatabaseSchemaComboBox::rowsChanged()
{
if ( mComboBox->count() == 1 )
{
//currently selected connection item has changed
emit schemaChanged( currentSchema() );
}
else if ( mComboBox->count() == 0 )
{
emit schemaChanged( QString() );
}
}
@@ -0,0 +1,104 @@
/***************************************************************************
qgsdatabaseschemacombobox.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 QGSDATABASESCHEMACOMBOBOX_H
#define QGSDATABASESCHEMACOMBOBOX_H

#include <QComboBox>

#include "qgis_gui.h"
#include "qgis_sip.h"

class QgsDatabaseSchemaModel;
class QSortFilterProxyModel;
class QgsAbstractDatabaseProviderConnection;

/**
* \ingroup gui
* \brief The QgsDatabaseSchemaComboBox class is a combo box which displays the list of schemas 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 QgsDatabaseSchemaComboBox : public QWidget
{
Q_OBJECT

public:

/**
* Constructor for QgsDatabaseSchemaComboBox, for the specified \a provider and \a connection.
*
* \warning The provider must support the connection API methods in its QgsProviderMetadata implementation
* in order for the model to work correctly.
*/
explicit QgsDatabaseSchemaComboBox( const QString &provider, const QString &connection, QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Constructor for QgsDatabaseSchemaComboBox, for the specified \a connection.
*
* Ownership of \a connection is transferred to the combobox.
*/
explicit QgsDatabaseSchemaComboBox( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER, QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Returns the name of the current schema selected in the combo box.
*/
QString currentSchema() const;

/**
* Returns the combobox portion of the widget.
*/
QComboBox *comboBox() { return mComboBox; }

public slots:

/**
* Sets the current schema selected in the combo box.
*/
void setSchema( const QString &schema );

/**
* Sets the database connection name from which to retrieve the available schemas.
*
* Optionally the \a provider can be reset too.
*/
void setConnectionName( const QString &connection, const QString &provider = QString() );

/**
* Refreshes the list of available schemas.
*/
void refreshSchemas();

signals:
//! Emitted whenever the currently selected schema changes.
void schemaChanged( const QString &schema );

protected slots:
void indexChanged( int i );
void rowsChanged();

private:
void init();

QString mProvider;
QgsDatabaseSchemaModel *mModel = nullptr;
QSortFilterProxyModel *mSortModel = nullptr;
QComboBox *mComboBox = nullptr;
};

#endif // QGSDATABASESCHEMACOMBOBOX_H
@@ -306,6 +306,7 @@ IF (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsDbManagerPostgis test_db_manager_postgis.py)
ADD_PYTHON_TEST(PyQgsDatabaseSchemaModel test_qgsdatabaseschemamodel.py)
ADD_PYTHON_TEST(PyQgsDatabaseTableModel test_qgsdatabasetablemodel.py)
ADD_PYTHON_TEST(PyQgsDatabaseSchemaComboBox test_qgsdatabaseschemacombobox.py)
ADD_PYTHON_TEST(PyQgsProviderConnectionPostgres test_qgsproviderconnection_postgres.py)
ENDIF (ENABLE_PGTEST)

0 comments on commit e8e13ec

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