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