QGIS API Documentation 3.99.0-Master (8e76e220402)
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
19#include "qgsapplication.h"
21
22#include <QHBoxLayout>
23#include <QString>
24#include <QToolButton>
25
26#include "moc_qgsdatabaseschemacombobox.cpp"
27
28using namespace Qt::StringLiterals;
29
30QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( const QString &provider, const QString &connection, QWidget *parent )
31 : QWidget( parent )
32 , mProvider( provider )
33{
34 if ( !provider.isEmpty() && !connection.isEmpty() )
35 mModel = new QgsDatabaseSchemaModel( provider, connection, this );
36 init();
37}
38
40 : QWidget( parent )
41{
42 mModel = new QgsDatabaseSchemaModel( connection, this );
43 init();
44}
45
47{
48 mAllowEmpty = allowEmpty;
49 if ( mModel )
50 mModel->setAllowEmptySchema( mAllowEmpty );
51}
52
54{
55 return mAllowEmpty;
56}
57
58void QgsDatabaseSchemaComboBox::init()
59{
60 mComboBox = new QComboBox();
61
62 mSortModel = new QgsDatabaseSchemaComboBoxSortModel( this );
63
64 if ( mModel )
65 mSortModel->setSourceModel( mModel );
66
67 mSortModel->setSortRole( Qt::DisplayRole );
68 mSortModel->setSortLocaleAware( true );
69 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
70 mSortModel->setDynamicSortFilter( true );
71 mSortModel->sort( 0 );
72
73 mComboBox->setModel( mSortModel );
74
75 QHBoxLayout *l = new QHBoxLayout();
76 l->setContentsMargins( 0, 0, 0, 0 );
77 l->addWidget( mComboBox );
78
79 QToolButton *refreshButton = new QToolButton();
80 refreshButton->setAutoRaise( true );
81 refreshButton->setToolTip( tr( "Refresh schemas" ) );
82 refreshButton->setIcon( QgsApplication::getThemeIcon( u"mActionRefresh.svg"_s ) );
83 l->addWidget( refreshButton );
84 setLayout( l );
85
86 connect( refreshButton, &QToolButton::clicked, this, &QgsDatabaseSchemaComboBox::refreshSchemas );
87
88 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsDatabaseSchemaComboBox::indexChanged );
89 connect( mSortModel, &QAbstractItemModel::rowsInserted, this, &QgsDatabaseSchemaComboBox::rowsChanged );
90 connect( mSortModel, &QAbstractItemModel::rowsRemoved, this, &QgsDatabaseSchemaComboBox::rowsChanged );
91}
92
93void QgsDatabaseSchemaComboBox::setSchema( const QString &schema )
94{
95 if ( schema == currentSchema() )
96 return;
97
98 if ( schema.isEmpty() )
99 {
100 if ( mAllowEmpty )
101 mComboBox->setCurrentIndex( 0 );
102 else
103 mComboBox->setCurrentIndex( -1 );
104
105 emit schemaChanged( QString() );
106 return;
107 }
108
109 const QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
110 if ( !idx.empty() )
111 {
112 const QModelIndex proxyIdx = idx.at( 0 );
113 if ( proxyIdx.isValid() )
114 {
115 mComboBox->setCurrentIndex( proxyIdx.row() );
117 return;
118 }
119 }
120 mComboBox->setCurrentIndex( -1 );
121 emit schemaChanged( QString() );
122}
123
124void QgsDatabaseSchemaComboBox::setConnectionName( const QString &connection, const QString &provider )
125{
126 if ( !provider.isEmpty() )
127 mProvider = provider;
128
129 const QString oldSchema = currentSchema();
130 QgsDatabaseSchemaModel *oldModel = mModel;
131 if ( !connection.isEmpty() && !mProvider.isEmpty() )
132 {
133 mModel = new QgsDatabaseSchemaModel( mProvider, connection, this );
134 mModel->setAllowEmptySchema( mAllowEmpty );
135 mSortModel->setSourceModel( mModel );
136 }
137 else
138 {
139 mModel = nullptr;
140 mSortModel->setSourceModel( nullptr );
141 }
142 if ( oldModel )
143 oldModel->deleteLater();
144
145 if ( currentSchema() != oldSchema )
146 setSchema( oldSchema );
147}
148
150{
151 const QString oldSchema = currentSchema();
152 if ( mModel )
153 mModel->refresh();
154 setSchema( oldSchema );
155}
156
158{
159 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
160 if ( !proxyIndex.isValid() )
161 {
162 return QString();
163 }
164
165 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
166}
167
168void QgsDatabaseSchemaComboBox::indexChanged( int i )
169{
170 Q_UNUSED( i )
172}
173
174void QgsDatabaseSchemaComboBox::rowsChanged()
175{
176 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
177 {
178 //currently selected connection item has changed
180 }
181 else if ( mComboBox->count() == 0 )
182 {
183 emit schemaChanged( QString() );
184 }
185}
186
187
189QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
190 : QSortFilterProxyModel( parent )
191{
192}
193
194bool QgsDatabaseSchemaComboBoxSortModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
195{
196 // empty row is always first
197 if ( sourceModel()->data( left, static_cast<int>( QgsDatabaseSchemaModel::CustomRole::Empty ) ).toBool() )
198 return true;
199 else if ( sourceModel()->data( right, static_cast<int>( QgsDatabaseSchemaModel::CustomRole::Empty ) ).toBool() )
200 return false;
201
202 // default mode is alphabetical order
203 const QString leftStr = sourceModel()->data( left ).toString();
204 const QString rightStr = sourceModel()->data( right ).toString();
205 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
206}
207
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 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.