QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsprovidermetadata.h"
19#include "qgsproviderregistry.h"
20
21#include "moc_qgsdatabaseschemamodel.cpp"
22
23QgsDatabaseSchemaModel::QgsDatabaseSchemaModel( const QString &provider, const QString &connection, QObject *parent )
24 : QAbstractItemModel( parent )
25{
27 Q_ASSERT( metadata );
28
29 mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) );
30 Q_ASSERT( mConnection );
31 init();
32}
33
35 : QAbstractItemModel( parent )
36 , mConnection( connection )
37{
38 Q_ASSERT( mConnection );
39 init();
40}
41
42void QgsDatabaseSchemaModel::init()
43{
44 Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Schemas );
45 mSchemas = mConnection->schemas();
46}
47
48QModelIndex QgsDatabaseSchemaModel::parent( const QModelIndex &child ) const
49{
50 Q_UNUSED( child )
51 return QModelIndex();
52}
53
54
55int QgsDatabaseSchemaModel::rowCount( const QModelIndex &parent ) const
56{
57 if ( parent.isValid() )
58 return 0;
59
60 return mSchemas.count() + ( mAllowEmpty ? 1 : 0 );
61}
62
63int QgsDatabaseSchemaModel::columnCount( const QModelIndex &parent ) const
64{
65 Q_UNUSED( parent )
66 return 1;
67}
68
69
70QVariant QgsDatabaseSchemaModel::data( const QModelIndex &index, int role ) const
71{
72 if ( !index.isValid() )
73 return QVariant();
74
75 if ( index.row() == 0 && mAllowEmpty )
76 {
77 if ( role == static_cast< int >( CustomRole::Empty ) )
78 return true;
79
80 return QVariant();
81 }
82
83 const QString schemaName = mSchemas.value( index.row() - ( mAllowEmpty ? 1 : 0 ) );
84 switch ( role )
85 {
86 case static_cast< int >( CustomRole::Empty ):
87 return false;
88
89 case Qt::DisplayRole:
90 case Qt::EditRole:
91 case Qt::ToolTipRole:
92 {
93 return schemaName;
94 }
95 }
96
97 return QVariant();
98}
99
100QModelIndex QgsDatabaseSchemaModel::index( int row, int column, const QModelIndex &parent ) const
101{
102 if ( hasIndex( row, column, parent ) )
103 {
104 return createIndex( row, column, row );
105 }
106
107 return QModelIndex();
108}
109
111{
112 if ( allowEmpty == mAllowEmpty )
113 return;
114
115 if ( allowEmpty )
116 {
117 beginInsertRows( QModelIndex(), 0, 0 );
118 mAllowEmpty = true;
119 endInsertRows();
120 }
121 else
122 {
123 beginRemoveRows( QModelIndex(), 0, 0 );
124 mAllowEmpty = false;
125 endRemoveRows();
126 }
127}
128
130{
131 const QStringList newSchemas = mConnection->schemas();
132 const QStringList oldSchemas = mSchemas;
133
134 for ( const QString &oldSchema : oldSchemas )
135 {
136 if ( !newSchemas.contains( oldSchema ) )
137 {
138 const int r = mSchemas.indexOf( oldSchema ) ;
139 beginRemoveRows( QModelIndex(), r + ( mAllowEmpty ? 1 : 0 ), r + ( mAllowEmpty ? 1 : 0 ) );
140 mSchemas.removeAt( r );
141 endRemoveRows();
142 }
143 }
144
145 for ( const QString &newSchema : newSchemas )
146 {
147 if ( !mSchemas.contains( newSchema ) )
148 {
149 beginInsertRows( QModelIndex(), mSchemas.count() + ( mAllowEmpty ? 1 : 0 ), mSchemas.count() + ( mAllowEmpty ? 1 : 0 ) );
150 mSchemas.append( newSchema );
151 endInsertRows();
152 }
153 }
154}
Provides common functionality for database based connections.
@ 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
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.