QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
32void QgsProviderConnectionComboBox::setProvider( const QString &provider )
33{
34 if ( mSortModel )
35 {
36 disconnect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsProviderConnectionComboBox::indexChanged );
37 disconnect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsProviderConnectionComboBox::rowsChanged );
38 disconnect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
39 disconnect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsRemoved );
40 delete mSortModel;
41 delete mModel;
42 }
43
44 mModel = new QgsProviderConnectionModel( provider, this );
45
46 mSortModel = new QgsProviderConnectionComboBoxSortModel( this );
47 mSortModel->setSourceModel( mModel );
48 mSortModel->setSortRole( Qt::DisplayRole );
49 mSortModel->setSortLocaleAware( true );
50 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
51 mSortModel->setDynamicSortFilter( true );
52 mSortModel->sort( 0 );
53
54 setModel( mSortModel );
55
56 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsProviderConnectionComboBox::indexChanged );
57 connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsProviderConnectionComboBox::rowsChanged );
58 connect( mSortModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QgsProviderConnectionComboBox::rowsAboutToBeRemoved );
59 connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsRemoved );
60}
61
63{
64 mModel->setAllowEmptyConnection( allowEmpty );
65}
66
68{
69 return mModel->allowEmptyConnection();
70}
71
72void QgsProviderConnectionComboBox::setConnection( const QString &connection )
73{
74 if ( connection == currentConnection() )
75 return;
76
77 if ( connection.isEmpty() )
78 {
79 if ( mModel->allowEmptyConnection() )
80 setCurrentIndex( 0 );
81 else
82 setCurrentIndex( -1 );
83 emit connectionChanged( QString() );
84 return;
85 }
86
87 const QModelIndexList idx
88 = 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
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.