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