QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsdatabaseschemamodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatabaseschemamodel.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***************************************************************************/
16#include "moc_qgsdatabaseschemamodel.cpp"
17#include "qgsproviderregistry.h"
18#include "qgsprovidermetadata.h"
20
21QgsDatabaseSchemaModel::QgsDatabaseSchemaModel( const QString &provider, const QString &connection, QObject *parent )
22 : QAbstractItemModel( parent )
23{
25 Q_ASSERT( metadata );
26
27 mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) );
28 Q_ASSERT( mConnection );
29 init();
30}
31
33 : QAbstractItemModel( parent )
34 , mConnection( connection )
35{
36 Q_ASSERT( mConnection );
37 init();
38}
39
40void QgsDatabaseSchemaModel::init()
41{
42 Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Schemas );
43 mSchemas = mConnection->schemas();
44}
45
46QModelIndex QgsDatabaseSchemaModel::parent( const QModelIndex &child ) const
47{
48 Q_UNUSED( child )
49 return QModelIndex();
50}
51
52
53int QgsDatabaseSchemaModel::rowCount( const QModelIndex &parent ) const
54{
55 if ( parent.isValid() )
56 return 0;
57
58 return mSchemas.count() + ( mAllowEmpty ? 1 : 0 );
59}
60
61int QgsDatabaseSchemaModel::columnCount( const QModelIndex &parent ) const
62{
63 Q_UNUSED( parent )
64 return 1;
65}
66
67
68QVariant QgsDatabaseSchemaModel::data( const QModelIndex &index, int role ) const
69{
70 if ( !index.isValid() )
71 return QVariant();
72
73 if ( index.row() == 0 && mAllowEmpty )
74 {
75 if ( role == static_cast< int >( CustomRole::Empty ) )
76 return true;
77
78 return QVariant();
79 }
80
81 const QString schemaName = mSchemas.value( index.row() - ( mAllowEmpty ? 1 : 0 ) );
82 switch ( role )
83 {
84 case static_cast< int >( CustomRole::Empty ):
85 return false;
86
87 case Qt::DisplayRole:
88 case Qt::EditRole:
89 case Qt::ToolTipRole:
90 {
91 return schemaName;
92 }
93 }
94
95 return QVariant();
96}
97
98QModelIndex QgsDatabaseSchemaModel::index( int row, int column, const QModelIndex &parent ) const
99{
100 if ( hasIndex( row, column, parent ) )
101 {
102 return createIndex( row, column, row );
103 }
104
105 return QModelIndex();
106}
107
109{
110 if ( allowEmpty == mAllowEmpty )
111 return;
112
113 if ( allowEmpty )
114 {
115 beginInsertRows( QModelIndex(), 0, 0 );
116 mAllowEmpty = true;
117 endInsertRows();
118 }
119 else
120 {
121 beginRemoveRows( QModelIndex(), 0, 0 );
122 mAllowEmpty = false;
123 endRemoveRows();
124 }
125}
126
128{
129 const QStringList newSchemas = mConnection->schemas();
130 const QStringList oldSchemas = mSchemas;
131
132 for ( const QString &oldSchema : oldSchemas )
133 {
134 if ( !newSchemas.contains( oldSchema ) )
135 {
136 const int r = mSchemas.indexOf( oldSchema ) ;
137 beginRemoveRows( QModelIndex(), r + ( mAllowEmpty ? 1 : 0 ), r + ( mAllowEmpty ? 1 : 0 ) );
138 mSchemas.removeAt( r );
139 endRemoveRows();
140 }
141 }
142
143 for ( const QString &newSchema : newSchemas )
144 {
145 if ( !mSchemas.contains( newSchema ) )
146 {
147 beginInsertRows( QModelIndex(), mSchemas.count() + ( mAllowEmpty ? 1 : 0 ), mSchemas.count() + ( mAllowEmpty ? 1 : 0 ) );
148 mSchemas.append( newSchema );
149 endInsertRows();
150 }
151 }
152}
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
@ Schemas
Can list schemas (if not set, the connection does not support schemas)
void setAllowEmptySchema(bool allowEmpty)
Sets whether an optional empty schema ("not set") option is present in the model.
QModelIndex parent(const QModelIndex &child) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
@ Empty
Entry is an empty entry.
QgsDatabaseSchemaModel(const QString &provider, const QString &connection, QObject *parent=nullptr)
Constructor for QgsDatabaseSchemaModel, for the specified provider and connection name.
void refresh()
Refreshes the schema list by querying the underlying connection.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex index(int row, int column, const QModelIndex &parent) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Holds data provider key, description, and associated shared library file or function pointer informat...
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.