26#include "moc_qgsdatabaseschemacombobox.cpp"
28using namespace Qt::StringLiterals;
32 , mProvider( provider )
34 if ( !provider.isEmpty() && !connection.isEmpty() )
48 mAllowEmpty = allowEmpty;
50 mModel->setAllowEmptySchema( mAllowEmpty );
58void QgsDatabaseSchemaComboBox::init()
60 mComboBox =
new QComboBox();
62 mSortModel =
new QgsDatabaseSchemaComboBoxSortModel(
this );
65 mSortModel->setSourceModel( mModel );
67 mSortModel->setSortRole( Qt::DisplayRole );
68 mSortModel->setSortLocaleAware(
true );
69 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
70 mSortModel->setDynamicSortFilter(
true );
71 mSortModel->sort( 0 );
73 mComboBox->setModel( mSortModel );
75 QHBoxLayout *l =
new QHBoxLayout();
76 l->setContentsMargins( 0, 0, 0, 0 );
77 l->addWidget( mComboBox );
79 QToolButton *refreshButton =
new QToolButton();
80 refreshButton->setAutoRaise(
true );
81 refreshButton->setToolTip( tr(
"Refresh schemas" ) );
83 l->addWidget( refreshButton );
88 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseSchemaComboBox::indexChanged );
89 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
90 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
98 if ( schema.isEmpty() )
101 mComboBox->setCurrentIndex( 0 );
103 mComboBox->setCurrentIndex( -1 );
109 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
112 const QModelIndex proxyIdx = idx.at( 0 );
113 if ( proxyIdx.isValid() )
115 mComboBox->setCurrentIndex( proxyIdx.row() );
120 mComboBox->setCurrentIndex( -1 );
126 if ( !provider.isEmpty() )
127 mProvider = provider;
131 if ( !connection.isEmpty() && !mProvider.isEmpty() )
134 mModel->setAllowEmptySchema( mAllowEmpty );
135 mSortModel->setSourceModel( mModel );
140 mSortModel->setSourceModel(
nullptr );
143 oldModel->deleteLater();
159 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
160 if ( !proxyIndex.isValid() )
165 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
168void QgsDatabaseSchemaComboBox::indexChanged(
int i )
174void QgsDatabaseSchemaComboBox::rowsChanged()
176 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
181 else if ( mComboBox->count() == 0 )
189QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
190 : QSortFilterProxyModel( parent )
194bool QgsDatabaseSchemaComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
203 const QString leftStr = sourceModel()->data( left ).toString();
204 const QString rightStr = sourceModel()->data( right ).toString();
205 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.