QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsdatabaseschemacombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatabaseschemacombobox.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 "qgsdatabaseschemamodel.h"
19 #include "qgsapplication.h"
20 #include <QHBoxLayout>
21 #include <QToolButton>
22 
23 QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( const QString &provider, const QString &connection, QWidget *parent )
24  : QWidget( parent )
25  , mProvider( provider )
26 {
27  if ( !provider.isEmpty() && !connection.isEmpty() )
28  mModel = new QgsDatabaseSchemaModel( provider, connection, this );
29  init();
30 }
31 
33  : QWidget( parent )
34 {
35  mModel = new QgsDatabaseSchemaModel( connection, this );
36  init();
37 }
38 
40 {
41  mAllowEmpty = allowEmpty;
42  if ( mModel )
43  mModel->setAllowEmptySchema( mAllowEmpty );
44 }
45 
47 {
48  return mAllowEmpty;
49 }
50 
51 void QgsDatabaseSchemaComboBox::init()
52 {
53  mComboBox = new QComboBox();
54 
55  mSortModel = new QgsDatabaseSchemaComboBoxSortModel( this );
56 
57  if ( mModel )
58  mSortModel->setSourceModel( mModel );
59 
60  mSortModel->setSortRole( Qt::DisplayRole );
61  mSortModel->setSortLocaleAware( true );
62  mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
63  mSortModel->setDynamicSortFilter( true );
64  mSortModel->sort( 0 );
65 
66  mComboBox->setModel( mSortModel );
67 
68  QHBoxLayout *l = new QHBoxLayout();
69  l->setContentsMargins( 0, 0, 0, 0 );
70  l->addWidget( mComboBox );
71 
72  QToolButton *refreshButton = new QToolButton();
73  refreshButton->setAutoRaise( true );
74  refreshButton->setToolTip( tr( "Refresh schemas" ) );
75  refreshButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRefresh.svg" ) ) );
76  l->addWidget( refreshButton );
77  setLayout( l );
78 
79  connect( refreshButton, &QToolButton::clicked, this, &QgsDatabaseSchemaComboBox::refreshSchemas );
80 
81  connect( mComboBox, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsDatabaseSchemaComboBox::indexChanged );
82  connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsDatabaseSchemaComboBox::rowsChanged );
83  connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsDatabaseSchemaComboBox::rowsChanged );
84 }
85 
86 void QgsDatabaseSchemaComboBox::setSchema( const QString &schema )
87 {
88  if ( schema == currentSchema() )
89  return;
90 
91  if ( schema.isEmpty() )
92  {
93  if ( mAllowEmpty )
94  mComboBox->setCurrentIndex( 0 );
95  else
96  mComboBox->setCurrentIndex( -1 );
97 
98  emit schemaChanged( QString() );
99  return;
100  }
101 
102  QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
103  if ( !idx.empty() )
104  {
105  QModelIndex proxyIdx = idx.at( 0 );
106  if ( proxyIdx.isValid() )
107  {
108  mComboBox->setCurrentIndex( proxyIdx.row() );
109  emit schemaChanged( currentSchema() );
110  return;
111  }
112  }
113  mComboBox->setCurrentIndex( -1 );
114  emit schemaChanged( QString() );
115 }
116 
117 void QgsDatabaseSchemaComboBox::setConnectionName( const QString &connection, const QString &provider )
118 {
119  if ( !provider.isEmpty() )
120  mProvider = provider;
121 
122  const QString oldSchema = currentSchema();
123  QgsDatabaseSchemaModel *oldModel = mModel;
124  if ( !connection.isEmpty() && !mProvider.isEmpty() )
125  {
126  mModel = new QgsDatabaseSchemaModel( mProvider, connection, this );
127  mModel->setAllowEmptySchema( mAllowEmpty );
128  mSortModel->setSourceModel( mModel );
129  }
130  else
131  {
132  mModel = nullptr;
133  mSortModel->setSourceModel( nullptr );
134  }
135  if ( oldModel )
136  oldModel->deleteLater();
137 
138  if ( currentSchema() != oldSchema )
139  setSchema( oldSchema );
140 }
141 
143 {
144  const QString oldSchema = currentSchema();
145  if ( mModel )
146  mModel->refresh();
147  setSchema( oldSchema );
148 }
149 
151 {
152  const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
153  if ( !proxyIndex.isValid() )
154  {
155  return QString();
156  }
157 
158  return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
159 }
160 
161 void QgsDatabaseSchemaComboBox::indexChanged( int i )
162 {
163  Q_UNUSED( i )
164  emit schemaChanged( currentSchema() );
165 }
166 
167 void QgsDatabaseSchemaComboBox::rowsChanged()
168 {
169  if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
170  {
171  //currently selected connection item has changed
172  emit schemaChanged( currentSchema() );
173  }
174  else if ( mComboBox->count() == 0 )
175  {
176  emit schemaChanged( QString() );
177  }
178 }
179 
180 
182 QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
183  : QSortFilterProxyModel( parent )
184 {
185 
186 }
187 
188 bool QgsDatabaseSchemaComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
189 {
190  // empty row is always first
191  if ( sourceModel()->data( left, QgsDatabaseSchemaModel::RoleEmpty ).toBool() )
192  return true;
193  else if ( sourceModel()->data( right, QgsDatabaseSchemaModel::RoleEmpty ).toBool() )
194  return false;
195 
196  // default mode is alphabetical order
197  QString leftStr = sourceModel()->data( left ).toString();
198  QString rightStr = sourceModel()->data( right ).toString();
199  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
200 }
201 
QgsDatabaseSchemaComboBox::refreshSchemas
void refreshSchemas()
Refreshes the list of available schemas.
Definition: qgsdatabaseschemacombobox.cpp:142
QgsDatabaseSchemaComboBox::currentSchema
QString currentSchema() const
Returns the name of the current schema selected in the combo box.
Definition: qgsdatabaseschemacombobox.cpp:150
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsDatabaseSchemaModel
A model containing schemas from a database connection.
Definition: qgsdatabaseschemamodel.h:43
QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox
QgsDatabaseSchemaComboBox(const QString &provider, const QString &connection, QWidget *parent=nullptr)
Constructor for QgsDatabaseSchemaComboBox, for the specified provider and connection.
Definition: qgsdatabaseschemacombobox.cpp:23
QgsDatabaseSchemaModel::refresh
void refresh()
Refreshes the schema list by querying the underlying connection.
Definition: qgsdatabaseschemamodel.cpp:126
QgsDatabaseSchemaComboBox::schemaChanged
void schemaChanged(const QString &schema)
Emitted whenever the currently selected schema changes.
qgsdatabaseschemamodel.h
qgsapplication.h
QgsDatabaseSchemaModel::setAllowEmptySchema
void setAllowEmptySchema(bool allowEmpty)
Sets whether an optional empty schema ("not set") option is present in the model.
Definition: qgsdatabaseschemamodel.cpp:107
QgsDatabaseSchemaComboBox::setConnectionName
void setConnectionName(const QString &connection, const QString &provider=QString())
Sets the database connection name from which to retrieve the available schemas.
Definition: qgsdatabaseschemacombobox.cpp:117
QgsDatabaseSchemaComboBox::setSchema
void setSchema(const QString &schema)
Sets the current schema selected in the combo box.
Definition: qgsdatabaseschemacombobox.cpp:86
QgsDatabaseSchemaComboBox::setAllowEmptySchema
void setAllowEmptySchema(bool allowEmpty)
Sets whether an optional empty schema ("not set") option is present in the combobox.
Definition: qgsdatabaseschemacombobox.cpp:39
qgsabstractdatabaseproviderconnection.h
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44
QgsDatabaseSchemaComboBox::allowEmptySchema
bool allowEmptySchema() const
Returns true if the combobox allows the empty schema ("not set") choice.
Definition: qgsdatabaseschemacombobox.cpp:46
QgsDatabaseSchemaModel::RoleEmpty
@ RoleEmpty
Entry is an empty entry.
Definition: qgsdatabaseschemamodel.h:51
qgsdatabaseschemacombobox.h