QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
17#include "moc_qgsproviderconnectioncombobox.cpp"
19
20QgsProviderConnectionComboBox::QgsProviderConnectionComboBox( const QString &provider, QWidget *parent )
21 : QComboBox( parent )
22{
23 setProvider( provider );
24}
25
27 : QComboBox( parent )
28{
29}
30
31void QgsProviderConnectionComboBox::setProvider( const QString &provider )
32{
33 if ( mSortModel )
34 {
35 disconnect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsProviderConnectionComboBox::indexChanged );
36 disconnect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsProviderConnectionComboBox::rowsChanged );
37 disconnect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
38 disconnect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsRemoved );
39 delete mSortModel;
40 delete mModel;
41 }
42
43 mModel = new QgsProviderConnectionModel( provider, this );
44
45 mSortModel = new QgsProviderConnectionComboBoxSortModel( this );
46 mSortModel->setSourceModel( mModel );
47 mSortModel->setSortRole( Qt::DisplayRole );
48 mSortModel->setSortLocaleAware( true );
49 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
50 mSortModel->setDynamicSortFilter( true );
51 mSortModel->sort( 0 );
52
53 setModel( mSortModel );
54
55 connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsProviderConnectionComboBox::indexChanged );
56 connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsProviderConnectionComboBox::rowsChanged );
57 connect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
58 connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsRemoved );
59}
60
62{
63 mModel->setAllowEmptyConnection( allowEmpty );
64}
65
70
71void QgsProviderConnectionComboBox::setConnection( const QString &connection )
72{
73 if ( connection == currentConnection() )
74 return;
75
76 if ( connection.isEmpty() )
77 {
78 if ( mModel->allowEmptyConnection() )
79 setCurrentIndex( 0 );
80 else
81 setCurrentIndex( -1 );
82 emit connectionChanged( QString() );
83 return;
84 }
85
86 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), static_cast< int >( QgsProviderConnectionModel::CustomRole::ConnectionName ), connection, Qt::MatchFixedString | Qt::MatchCaseSensitive );
87 if ( !idx.empty() )
88 {
89 const QModelIndex proxyIdx = idx.at( 0 );
90 if ( proxyIdx.isValid() )
91 {
92 setCurrentIndex( proxyIdx.row() );
94 return;
95 }
96 }
97 setCurrentIndex( -1 );
98 emit connectionChanged( QString() );
99}
100
102{
103 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
104 if ( !proxyIndex.isValid() )
105 {
106 return QString();
107 }
108
109 return mSortModel->data( proxyIndex, static_cast< int >( QgsProviderConnectionModel::CustomRole::ConnectionName ) ).toString();
110}
111
113{
114 const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
115 if ( !proxyIndex.isValid() )
116 {
117 return QString();
118 }
119
120 return mSortModel->data( proxyIndex, static_cast< int >( QgsProviderConnectionModel::CustomRole::Uri ) ).toString();
121}
122
123void QgsProviderConnectionComboBox::indexChanged( int i )
124{
125 Q_UNUSED( i )
127}
128
129void QgsProviderConnectionComboBox::rowsChanged()
130{
131 if ( count() == 1 || ( mModel->allowEmptyConnection() && count() == 2 && currentIndex() == 1 ) )
132 {
133 //currently selected connection item has changed
135 }
136 else if ( count() == 0 )
137 {
138 emit connectionChanged( QString() );
139 }
140}
141
142void QgsProviderConnectionComboBox::rowsAboutToBeRemoved()
143{
144 mPreviousConnection = currentConnection();
145}
146
147void QgsProviderConnectionComboBox::rowsRemoved()
148{
149 const QString newConnection = currentConnection();
150 if ( mPreviousConnection != newConnection )
151 {
152 if ( mModel->allowEmptyConnection() )
153 {
154 // if current connection was removed, reset to empty connection item
155 setCurrentIndex( 0 );
156 }
157 if ( currentIndex() == -1 )
158 {
159 // make sure we have a valid selection
160 setCurrentIndex( 0 );
161 }
163 }
164}
165
166
168QgsProviderConnectionComboBoxSortModel::QgsProviderConnectionComboBoxSortModel( QObject *parent )
169 : QSortFilterProxyModel( parent )
170{
171
172}
173
174bool QgsProviderConnectionComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
175{
176 // empty row is always first
177 if ( sourceModel()->data( left, static_cast< int >( QgsProviderConnectionModel::CustomRole::Empty ) ).toBool() )
178 return true;
179 else if ( sourceModel()->data( right, static_cast< int >( QgsProviderConnectionModel::CustomRole::Empty ) ).toBool() )
180 return false;
181
182 // default mode is alphabetical order
183 const QString leftStr = sourceModel()->data( left ).toString();
184 const QString rightStr = sourceModel()->data( right ).toString();
185 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
186}
187
188
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.