QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
19 QgsProviderConnectionComboBox::QgsProviderConnectionComboBox( const QString &provider, QWidget *parent )
20  : QComboBox( parent )
21 {
22  setProvider( provider );
23 }
24 
26  : QComboBox( parent )
27 {
28 }
29 
30 void 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::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsChanged );
37  delete mSortModel;
38  delete mModel;
39  }
40 
41  mModel = new QgsProviderConnectionModel( provider, this );
42 
43  mSortModel = new QgsProviderConnectionComboBoxSortModel( this );
44  mSortModel->setSourceModel( mModel );
45  mSortModel->setSortRole( Qt::DisplayRole );
46  mSortModel->setSortLocaleAware( true );
47  mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
48  mSortModel->setDynamicSortFilter( true );
49  mSortModel->sort( 0 );
50 
51  setModel( mSortModel );
52 
53  connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsProviderConnectionComboBox::indexChanged );
54  connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsProviderConnectionComboBox::rowsChanged );
55  connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsProviderConnectionComboBox::rowsChanged );
56 }
57 
59 {
60  mModel->setAllowEmptyConnection( allowEmpty );
61 }
62 
64 {
65  return mModel->allowEmptyConnection();
66 }
67 
68 void QgsProviderConnectionComboBox::setConnection( const QString &connection )
69 {
70  if ( connection == currentConnection() )
71  return;
72 
73  if ( connection.isEmpty() )
74  {
75  if ( mModel->allowEmptyConnection() )
76  setCurrentIndex( 0 );
77  else
78  setCurrentIndex( -1 );
79  emit connectionChanged( QString() );
80  return;
81  }
82 
83  const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), QgsProviderConnectionModel::RoleConnectionName, connection, Qt::MatchFixedString | Qt::MatchCaseSensitive );
84  if ( !idx.empty() )
85  {
86  const QModelIndex proxyIdx = idx.at( 0 );
87  if ( proxyIdx.isValid() )
88  {
89  setCurrentIndex( proxyIdx.row() );
91  return;
92  }
93  }
94  setCurrentIndex( -1 );
95  emit connectionChanged( QString() );
96 }
97 
99 {
100  const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
101  if ( !proxyIndex.isValid() )
102  {
103  return QString();
104  }
105 
106  return mSortModel->data( proxyIndex, QgsProviderConnectionModel::RoleConnectionName ).toString();
107 }
108 
110 {
111  const QModelIndex proxyIndex = mSortModel->index( currentIndex(), 0 );
112  if ( !proxyIndex.isValid() )
113  {
114  return QString();
115  }
116 
117  return mSortModel->data( proxyIndex, QgsProviderConnectionModel::RoleUri ).toString();
118 }
119 
120 void QgsProviderConnectionComboBox::indexChanged( int i )
121 {
122  Q_UNUSED( i )
124 }
125 
126 void QgsProviderConnectionComboBox::rowsChanged()
127 {
128  if ( count() == 1 || ( mModel->allowEmptyConnection() && count() == 2 && currentIndex() == 1 ) )
129  {
130  //currently selected connection item has changed
132  }
133  else if ( count() == 0 )
134  {
135  emit connectionChanged( QString() );
136  }
137 }
138 
139 
141 QgsProviderConnectionComboBoxSortModel::QgsProviderConnectionComboBoxSortModel( QObject *parent )
142  : QSortFilterProxyModel( parent )
143 {
144 
145 }
146 
147 bool QgsProviderConnectionComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
148 {
149  // empty row is always first
150  if ( sourceModel()->data( left, QgsProviderConnectionModel::RoleEmpty ).toBool() )
151  return true;
152  else if ( sourceModel()->data( right, QgsProviderConnectionModel::RoleEmpty ).toBool() )
153  return false;
154 
155  // default mode is alphabetical order
156  const QString leftStr = sourceModel()->data( left ).toString();
157  const QString rightStr = sourceModel()->data( right ).toString();
158  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
159 }
160 
161 
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.
@ RoleEmpty
Entry is an empty entry.