QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
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
@ RoleEmpty
Entry is an empty entry.
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) SIP_THROW(QgsProviderConnectionException)
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.