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