17#include "moc_qgsdatabaseschemacombobox.cpp"
26 , mProvider( provider )
28 if ( !provider.isEmpty() && !connection.isEmpty() )
42 mAllowEmpty = allowEmpty;
52void QgsDatabaseSchemaComboBox::init()
54 mComboBox =
new QComboBox();
56 mSortModel =
new QgsDatabaseSchemaComboBoxSortModel(
this );
59 mSortModel->setSourceModel( mModel );
61 mSortModel->setSortRole( Qt::DisplayRole );
62 mSortModel->setSortLocaleAware(
true );
63 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
64 mSortModel->setDynamicSortFilter(
true );
65 mSortModel->sort( 0 );
67 mComboBox->setModel( mSortModel );
69 QHBoxLayout *l =
new QHBoxLayout();
70 l->setContentsMargins( 0, 0, 0, 0 );
71 l->addWidget( mComboBox );
73 QToolButton *refreshButton =
new QToolButton();
74 refreshButton->setAutoRaise(
true );
75 refreshButton->setToolTip( tr(
"Refresh schemas" ) );
77 l->addWidget( refreshButton );
82 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseSchemaComboBox::indexChanged );
83 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
84 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
92 if ( schema.isEmpty() )
95 mComboBox->setCurrentIndex( 0 );
97 mComboBox->setCurrentIndex( -1 );
103 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
106 const QModelIndex proxyIdx = idx.at( 0 );
107 if ( proxyIdx.isValid() )
109 mComboBox->setCurrentIndex( proxyIdx.row() );
114 mComboBox->setCurrentIndex( -1 );
120 if ( !provider.isEmpty() )
121 mProvider = provider;
125 if ( !connection.isEmpty() && !mProvider.isEmpty() )
129 mSortModel->setSourceModel( mModel );
134 mSortModel->setSourceModel(
nullptr );
137 oldModel->deleteLater();
153 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
154 if ( !proxyIndex.isValid() )
159 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
162void QgsDatabaseSchemaComboBox::indexChanged(
int i )
168void QgsDatabaseSchemaComboBox::rowsChanged()
170 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
175 else if ( mComboBox->count() == 0 )
183QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
184 : QSortFilterProxyModel( parent )
188bool QgsDatabaseSchemaComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
197 const QString leftStr = sourceModel()->data( left ).toString();
198 const QString rightStr = sourceModel()->data( right ).toString();
199 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setConnectionName(const QString &connection, const QString &provider=QString())
Sets the database connection name from which to retrieve the available schemas.
QgsDatabaseSchemaComboBox(const QString &provider, const QString &connection, QWidget *parent=nullptr)
Constructor for QgsDatabaseSchemaComboBox, for the specified provider and connection.
void setSchema(const QString &schema)
Sets the current schema selected in the combo box.
void refreshSchemas()
Refreshes the list of available schemas.
void schemaChanged(const QString &schema)
Emitted whenever the currently selected schema changes.
bool allowEmptySchema() const
Returns true if the combobox allows the empty schema ("not set") choice.
void setAllowEmptySchema(bool allowEmpty)
Sets whether an optional empty schema ("not set") option is present in the combobox.
QString currentSchema() const
Returns the name of the current schema selected in the combo box.
A model containing schemas from a database connection.
void setAllowEmptySchema(bool allowEmpty)
Sets whether an optional empty schema ("not set") option is present in the model.
@ Empty
Entry is an empty entry.
void refresh()
Refreshes the schema list by querying the underlying connection.