25#include "moc_qgsdatabaseschemacombobox.cpp"
29 , mProvider( provider )
31 if ( !provider.isEmpty() && !connection.isEmpty() )
45 mAllowEmpty = allowEmpty;
47 mModel->setAllowEmptySchema( mAllowEmpty );
55void QgsDatabaseSchemaComboBox::init()
57 mComboBox =
new QComboBox();
59 mSortModel =
new QgsDatabaseSchemaComboBoxSortModel(
this );
62 mSortModel->setSourceModel( mModel );
64 mSortModel->setSortRole( Qt::DisplayRole );
65 mSortModel->setSortLocaleAware(
true );
66 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
67 mSortModel->setDynamicSortFilter(
true );
68 mSortModel->sort( 0 );
70 mComboBox->setModel( mSortModel );
72 QHBoxLayout *l =
new QHBoxLayout();
73 l->setContentsMargins( 0, 0, 0, 0 );
74 l->addWidget( mComboBox );
76 QToolButton *refreshButton =
new QToolButton();
77 refreshButton->setAutoRaise(
true );
78 refreshButton->setToolTip( tr(
"Refresh schemas" ) );
80 l->addWidget( refreshButton );
85 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseSchemaComboBox::indexChanged );
86 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
87 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
95 if ( schema.isEmpty() )
98 mComboBox->setCurrentIndex( 0 );
100 mComboBox->setCurrentIndex( -1 );
106 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
109 const QModelIndex proxyIdx = idx.at( 0 );
110 if ( proxyIdx.isValid() )
112 mComboBox->setCurrentIndex( proxyIdx.row() );
117 mComboBox->setCurrentIndex( -1 );
123 if ( !provider.isEmpty() )
124 mProvider = provider;
128 if ( !connection.isEmpty() && !mProvider.isEmpty() )
131 mModel->setAllowEmptySchema( mAllowEmpty );
132 mSortModel->setSourceModel( mModel );
137 mSortModel->setSourceModel(
nullptr );
140 oldModel->deleteLater();
156 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
157 if ( !proxyIndex.isValid() )
162 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
165void QgsDatabaseSchemaComboBox::indexChanged(
int i )
171void QgsDatabaseSchemaComboBox::rowsChanged()
173 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
178 else if ( mComboBox->count() == 0 )
186QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
187 : QSortFilterProxyModel( parent )
191bool QgsDatabaseSchemaComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
200 const QString leftStr = sourceModel()->data( left ).toString();
201 const QString rightStr = sourceModel()->data( right ).toString();
202 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
Provides common functionality for database based connections.
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.
@ Empty
Entry is an empty entry.