QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsdatabasetablecombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatabasetablecombobox.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#include "qgsapplication.h"
21
22#include <QHBoxLayout>
23#include <QString>
24#include <QToolButton>
25
26#include "moc_qgsdatabasetablecombobox.cpp"
27
28using namespace Qt::StringLiterals;
29
30QgsDatabaseTableComboBox::QgsDatabaseTableComboBox( const QString &provider, const QString &connection, const QString &schema, QWidget *parent )
31 : QWidget( parent )
32 , mProvider( provider )
33 , mConnection( connection )
34 , mSchema( schema )
35{
36 if ( !provider.isEmpty() && !connection.isEmpty() )
37 mModel = new QgsDatabaseTableModel( provider, connection, schema, this );
38 init();
39}
40
42 : QWidget( parent )
43 , mSchema( schema )
44{
45 mModel = new QgsDatabaseTableModel( connection, schema, this );
46 init();
47}
48
50{
51 mAllowEmpty = allowEmpty;
52 if ( mModel )
53 mModel->setAllowEmptyTable( allowEmpty );
54}
55
57{
58 return mAllowEmpty;
59}
60
61void QgsDatabaseTableComboBox::init()
62{
63 mComboBox = new QComboBox();
64
65 mSortModel = new QgsDatabaseTableComboBoxSortModel( this );
66 if ( mModel )
67 mSortModel->setSourceModel( mModel );
68 mSortModel->setSortRole( Qt::DisplayRole );
69 mSortModel->setSortLocaleAware( true );
70 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
71 mSortModel->setDynamicSortFilter( true );
72 mSortModel->sort( 0 );
73
74 mComboBox->setModel( mSortModel );
75
76 QHBoxLayout *l = new QHBoxLayout();
77 l->setContentsMargins( 0, 0, 0, 0 );
78 l->addWidget( mComboBox );
79
80 QToolButton *refreshButton = new QToolButton();
81 refreshButton->setAutoRaise( true );
82 refreshButton->setToolTip( tr( "Refresh tables" ) );
83 refreshButton->setIcon( QgsApplication::getThemeIcon( u"mActionRefresh.svg"_s ) );
84 l->addWidget( refreshButton );
85 setLayout( l );
86
87 connect( refreshButton, &QToolButton::clicked, this, &QgsDatabaseTableComboBox::refreshTables );
88
89 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsDatabaseTableComboBox::indexChanged );
90 connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsDatabaseTableComboBox::rowsChanged );
91 connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsDatabaseTableComboBox::rowsChanged );
92}
93
94void QgsDatabaseTableComboBox::setTable( const QString &table, const QString &schema )
95{
96 if ( schema == currentSchema() && table == currentTable() )
97 return;
98
99 if ( table.isEmpty() )
100 {
101 if ( mAllowEmpty )
102 mComboBox->setCurrentIndex( 0 );
103 else
104 mComboBox->setCurrentIndex( -1 );
105
106 emit tableChanged( QString() );
107 return;
108 }
109
110 const QModelIndexList idxs = mSortModel->match( mSortModel->index( 0, 0 ), static_cast<int>( QgsDatabaseTableModel::CustomRole::TableName ), table, -1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
111 for ( const QModelIndex &proxyIdx : idxs )
112 {
113 if ( proxyIdx.isValid() && proxyIdx.data( static_cast<int>( QgsDatabaseTableModel::CustomRole::TableName ) ).toString() == table
114 && ( schema.isEmpty() || proxyIdx.data( static_cast<int>( QgsDatabaseTableModel::CustomRole::Schema ) ).toString() == schema ) )
115 {
116 mComboBox->setCurrentIndex( proxyIdx.row() );
118 return;
119 }
120 }
121 mComboBox->setCurrentIndex( -1 );
122 emit tableChanged( QString() );
123}
124
125void QgsDatabaseTableComboBox::setConnectionName( const QString &connection, const QString &provider )
126{
127 if ( provider.isEmpty() && mConnection == connection )
128 return;
129
130 if ( !provider.isEmpty() )
131 mProvider = provider;
132
133 mConnection = connection;
134
135 const QString oldTable = currentTable();
136 const QString oldSchema = currentSchema();
137 QgsDatabaseTableModel *oldModel = mModel;
138 if ( !mConnection.isEmpty() )
139 {
140 mModel = new QgsDatabaseTableModel( mProvider, mConnection, mSchema, this );
141 mModel->setAllowEmptyTable( mAllowEmpty );
142 }
143 else
144 mModel = nullptr;
145
146 mSortModel->setSourceModel( mModel );
147 if ( oldModel )
148 oldModel->deleteLater();
149 if ( currentTable() != oldTable || currentSchema() != oldSchema )
150 setTable( oldTable, oldSchema );
151}
152
153void QgsDatabaseTableComboBox::setSchema( const QString &schema )
154{
155 if ( schema == mSchema )
156 return;
157 mSchema = schema;
158
159 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
160 {
161 const QString oldTable = currentTable();
162 QgsDatabaseTableModel *oldModel = mModel;
163 mModel = new QgsDatabaseTableModel( mProvider, mConnection, mSchema, this );
164 mModel->setAllowEmptyTable( mAllowEmpty );
165 mSortModel->setSourceModel( mModel );
166 oldModel->deleteLater();
167 setTable( oldTable );
168 }
169}
170
172{
173 const QString oldSchema = currentSchema();
174 const QString oldTable = currentTable();
175 if ( mModel )
176 mModel->refresh();
177 setTable( oldTable, oldSchema );
178}
179
181{
182 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
183 if ( !proxyIndex.isValid() )
184 {
185 return QString();
186 }
187
188 return mSortModel->data( proxyIndex, static_cast<int>( QgsDatabaseTableModel::CustomRole::Schema ) ).toString();
189}
190
192{
193 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
194 if ( !proxyIndex.isValid() )
195 {
196 return QString();
197 }
198
199 return mSortModel->data( proxyIndex, static_cast<int>( QgsDatabaseTableModel::CustomRole::TableName ) ).toString();
200}
201
202void QgsDatabaseTableComboBox::indexChanged( int i )
203{
204 Q_UNUSED( i )
205 emit tableChanged( currentTable() );
206}
207
208void QgsDatabaseTableComboBox::rowsChanged()
209{
210 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
211 {
212 //currently selected connection item has changed
214 }
215 else if ( mComboBox->count() == 0 )
216 {
217 emit tableChanged( QString() );
218 }
219}
220
222QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
223 : QSortFilterProxyModel( parent )
224{
225}
226
227bool QgsDatabaseTableComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
228{
229 // empty row is always first
230 if ( sourceModel()->data( left, static_cast<int>( QgsDatabaseTableModel::CustomRole::Empty ) ).toBool() )
231 return true;
232 else if ( sourceModel()->data( right, static_cast<int>( QgsDatabaseTableModel::CustomRole::Empty ) ).toBool() )
233 return false;
234
235 // default mode is alphabetical order
236 const QString leftStr = sourceModel()->data( left ).toString();
237 const QString rightStr = sourceModel()->data( right ).toString();
238 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
239}
240
Provides common functionality for database based connections.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setSchema(const QString &schema)
Sets the schema from which to retrieve the available tables.
void tableChanged(const QString &table, const QString &schema=QString())
Emitted whenever the currently selected table changes.
void setTable(const QString &table, const QString &schema=QString())
Sets the current table selected in the combo box.
void setConnectionName(const QString &connection, const QString &provider=QString())
Sets the database connection name from which to retrieve the available tables.
void refreshTables()
Refreshes the list of available tables.
QString currentSchema() const
Returns the schema of the current table selected in the combo box.
bool allowEmptyTable() const
Returns true if the combobox allows the empty table ("not set") choice.
void setAllowEmptyTable(bool allowEmpty)
Sets whether an optional empty table ("not set") option is present in the combobox.
QgsDatabaseTableComboBox(const QString &provider, const QString &connection, const QString &schema=QString(), QWidget *parent=nullptr)
Constructor for QgsDatabaseTableComboBox, for the specified provider and connection.
QString currentTable() const
Returns the name of the current table selected in the combo box.
A model containing tables from a database connection.