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