20 #include <QHBoxLayout>
21 #include <QToolButton>
25 , mProvider( provider )
27 if ( !provider.isEmpty() && !connection.isEmpty() )
41 mAllowEmpty = allowEmpty;
51 void QgsDatabaseSchemaComboBox::init()
53 mComboBox =
new QComboBox();
55 mSortModel =
new QgsDatabaseSchemaComboBoxSortModel(
this );
58 mSortModel->setSourceModel( mModel );
60 mSortModel->setSortRole( Qt::DisplayRole );
61 mSortModel->setSortLocaleAware(
true );
62 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
63 mSortModel->setDynamicSortFilter(
true );
64 mSortModel->sort( 0 );
66 mComboBox->setModel( mSortModel );
68 QHBoxLayout *l =
new QHBoxLayout();
69 l->setContentsMargins( 0, 0, 0, 0 );
70 l->addWidget( mComboBox );
72 QToolButton *refreshButton =
new QToolButton();
73 refreshButton->setAutoRaise(
true );
74 refreshButton->setToolTip( tr(
"Refresh schemas" ) );
76 l->addWidget( refreshButton );
81 connect( mComboBox,
static_cast < void ( QComboBox::* )(
int )
> ( &QComboBox::activated ),
this, &QgsDatabaseSchemaComboBox::indexChanged );
82 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
83 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
91 if ( schema.isEmpty() )
94 mComboBox->setCurrentIndex( 0 );
96 mComboBox->setCurrentIndex( -1 );
102 QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
105 QModelIndex proxyIdx = idx.at( 0 );
106 if ( proxyIdx.isValid() )
108 mComboBox->setCurrentIndex( proxyIdx.row() );
113 mComboBox->setCurrentIndex( -1 );
119 if ( !provider.isEmpty() )
120 mProvider = provider;
124 if ( !connection.isEmpty() && !mProvider.isEmpty() )
128 mSortModel->setSourceModel( mModel );
133 mSortModel->setSourceModel(
nullptr );
136 oldModel->deleteLater();
152 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
153 if ( !proxyIndex.isValid() )
158 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
161 void QgsDatabaseSchemaComboBox::indexChanged(
int i )
167 void QgsDatabaseSchemaComboBox::rowsChanged()
169 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
174 else if ( mComboBox->count() == 0 )
182 QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
183 : QSortFilterProxyModel( parent )
188 bool QgsDatabaseSchemaComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
197 QString leftStr = sourceModel()->data( left ).toString();
198 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.
@ RoleEmpty
Entry is an empty entry.
void refresh()
Refreshes the schema list by querying the underlying connection.