QGIS/src/gui/qgsdatabaseschemacombobox.cpp
Nyall Dawson e8e13ecb6f Add new GUI widget QgsDatabaseSchemaComboBox for selection of
available schemas for a specific data connection

(providers must implement the connections API)
2020-03-09 21:04:51 +10:00

136 lines
4.2 KiB
C++

/***************************************************************************
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() );
}
}