QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
qgsproviderconnectioncombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsproviderconnectioncombobox.cpp
3 --------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
18
19QgsProviderConnectionComboBox::QgsProviderConnectionComboBox( const QString &provider, QWidget *parent )
20 : QComboBox( parent )
21{
22 setProvider( provider );
23}
24
26 : QComboBox( parent )
27{
28}
29
30void QgsProviderConnectionComboBox::setProvider( const QString &provider )
31{
32 if ( mSortModel )
33 {
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 );
38 delete mSortModel;
39 delete mModel;
40 }
41
42 mModel = new QgsProviderConnectionModel( provider, this );
43
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 );
51
52 setModel( mSortModel );
53
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 );
58}
59
61{
62 mModel->setAllowEmptyConnection( allowEmpty );
63}
64
69
70void QgsProviderConnectionComboBox::setConnection( const QString &connection )
71{
72 if ( connection == currentConnection() )
73 return;
74
75 if ( connection.isEmpty() )
76 {
77 if ( mModel->allowEmptyConnection() )
78 setCurrentIndex( 0 );
79 else
80 setCurrentIndex( -1 );
81 emit connectionChanged( QString() );
82 return;
83 }
84
85 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), static_cast< int >( QgsProviderConnectionModel::CustomRole::ConnectionName ), connection, Qt::MatchFixedString | Qt::MatchCaseSensitive );
86 if ( !idx.empty() )
87 {
88 const QModelIndex proxyIdx = idx.at( 0 );
89 if ( proxyIdx.isValid() )
90 {
91 setCurrentIndex( proxyIdx.row() );
93 return;
94 }
95 }
96 setCurrentIndex( -1 );
97 emit connectionChanged( QString() );
98}
99
101{
102 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
103 if ( !proxyIndex.isValid() )
104 {
105 return QString();
106 }
107
108 return mSortModel->data( proxyIndex, static_cast< int >( QgsProviderConnectionModel::CustomRole::ConnectionName ) ).toString();
109}
110
112{
113 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
114 if ( !proxyIndex.isValid() )
115 {
116 return QString();
117 }
118
119 return mSortModel->data( proxyIndex, static_cast< int >( QgsProviderConnectionModel::CustomRole::Uri ) ).toString();
120}
121
122void QgsProviderConnectionComboBox::indexChanged( int i )
123{
124 Q_UNUSED( i )
126}
127
128void QgsProviderConnectionComboBox::rowsChanged()
129{
130 if ( count() == 1 || ( mModel->allowEmptyConnection() && count() == 2 && currentIndex() == 1 ) )
131 {
132 //currently selected connection item has changed
134 }
135 else if ( count() == 0 )
136 {
137 emit connectionChanged( QString() );
138 }
139}
140
141void QgsProviderConnectionComboBox::rowsAboutToBeRemoved()
142{
143 mPreviousConnection = currentConnection();
144}
145
146void QgsProviderConnectionComboBox::rowsRemoved()
147{
148 const QString newConnection = currentConnection();
149 if ( mPreviousConnection != newConnection )
150 {
151 if ( mModel->allowEmptyConnection() )
152 {
153 // if current connection was removed, reset to empty connection item
154 setCurrentIndex( 0 );
155 }
156 if ( currentIndex() == -1 )
157 {
158 // make sure we have a valid selection
159 setCurrentIndex( 0 );
160 }
162 }
163}
164
165
167QgsProviderConnectionComboBoxSortModel::QgsProviderConnectionComboBoxSortModel( QObject *parent )
168 : QSortFilterProxyModel( parent )
169{
170
171}
172
173bool QgsProviderConnectionComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
174{
175 // empty row is always first
176 if ( sourceModel()->data( left, static_cast< int >( QgsProviderConnectionModel::CustomRole::Empty ) ).toBool() )
177 return true;
178 else if ( sourceModel()->data( right, static_cast< int >( QgsProviderConnectionModel::CustomRole::Empty ) ).toBool() )
179 return false;
180
181 // default mode is alphabetical order
182 const QString leftStr = sourceModel()->data( left ).toString();
183 const QString rightStr = sourceModel()->data( right ).toString();
184 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
185}
186
187
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.
@ Empty
Entry is an empty entry.