34 disconnect(
this,
static_cast < void ( QComboBox::* )(
int )
> ( &QComboBox::activated ),
this, &QgsProviderConnectionComboBox::indexChanged );
35 disconnect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsProviderConnectionComboBox::rowsChanged );
36 disconnect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved,
this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
37 disconnect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsProviderConnectionComboBox::rowsRemoved );
44 mSortModel =
new QgsProviderConnectionComboBoxSortModel(
this );
45 mSortModel->setSourceModel( mModel );
46 mSortModel->setSortRole( Qt::DisplayRole );
47 mSortModel->setSortLocaleAware(
true );
48 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
49 mSortModel->setDynamicSortFilter(
true );
50 mSortModel->sort( 0 );
52 setModel( mSortModel );
54 connect(
this,
static_cast < void ( QComboBox::* )(
int )
> ( &QComboBox::activated ),
this, &QgsProviderConnectionComboBox::indexChanged );
55 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsProviderConnectionComboBox::rowsChanged );
56 connect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved,
this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
57 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsProviderConnectionComboBox::rowsRemoved );
75 if ( connection.isEmpty() )
80 setCurrentIndex( -1 );
88 const QModelIndex proxyIdx = idx.at( 0 );
89 if ( proxyIdx.isValid() )
91 setCurrentIndex( proxyIdx.row() );
96 setCurrentIndex( -1 );
102 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
103 if ( !proxyIndex.isValid() )
113 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
114 if ( !proxyIndex.isValid() )
122void QgsProviderConnectionComboBox::indexChanged(
int i )
128void QgsProviderConnectionComboBox::rowsChanged()
130 if ( count() == 1 || ( mModel->
allowEmptyConnection() && count() == 2 && currentIndex() == 1 ) )
135 else if ( count() == 0 )
141void QgsProviderConnectionComboBox::rowsAboutToBeRemoved()
146void QgsProviderConnectionComboBox::rowsRemoved()
149 if ( mPreviousConnection != newConnection )
154 setCurrentIndex( 0 );
156 if ( currentIndex() == -1 )
159 setCurrentIndex( 0 );
167QgsProviderConnectionComboBoxSortModel::QgsProviderConnectionComboBoxSortModel( QObject *parent )
168 : QSortFilterProxyModel( parent )
173bool QgsProviderConnectionComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
182 const QString leftStr = sourceModel()->data( left ).toString();
183 const QString rightStr = sourceModel()->data( right ).toString();
184 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
QgsProviderConnectionComboBox(const QString &provider, QWidget *parent=nullptr)
Constructor for QgsProviderConnectionComboBox, for the specified provider.
QString currentConnectionUri() const
Returns the uri of the current connection selected in the combo box.
void setProvider(const QString &provider)
Sets the provider to be used.
void setConnection(const QString &connection)
Sets the current connection selected in the combo box.
bool allowEmptyConnection() const
Returns true if the combobox allows the empty connection ("not set") choice.
void setAllowEmptyConnection(bool allowEmpty)
Sets whether an optional empty connection ("not set") option is present in the combobox.
QString currentConnection() const
Returns the name of the current connection selected in the combo box.
void connectionChanged(const QString &connection)
Emitted whenever the currently selected connection changes.
A model containing registered connection names for a specific data provider.
bool allowEmptyConnection() const
Returns true if the model allows the empty connection ("not set") choice.
void setAllowEmptyConnection(bool allowEmpty)
Sets whether an optional empty connection ("not set") option is present in the model.
@ ConnectionName
Connection name.
@ Uri
Connection URI string.
@ Empty
Entry is an empty entry.