QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsdatabasetablemodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatabasetablemodel.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_qgsdatabasetablemodel.cpp"
17#include "qgsproviderregistry.h"
18#include "qgsprovidermetadata.h"
20#include "qgsiconutils.h"
21#include <QIcon>
22
23QgsDatabaseTableModel::QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema, QObject *parent )
24 : QAbstractItemModel( parent )
25 , mSchema( schema )
26{
28 Q_ASSERT( metadata );
29
30 mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) );
31 Q_ASSERT( mConnection );
32 init();
33}
34
35QgsDatabaseTableModel::QgsDatabaseTableModel( QgsAbstractDatabaseProviderConnection *connection, const QString &schema, QObject *parent )
36 : QAbstractItemModel( parent )
37 , mConnection( connection )
38 , mSchema( schema )
39{
40 Q_ASSERT( mConnection );
41 init();
42}
43
44void QgsDatabaseTableModel::init()
45{
46 Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Tables );
47 mTables = mConnection->tables( mSchema );
48}
49
50QModelIndex QgsDatabaseTableModel::parent( const QModelIndex &child ) const
51{
52 Q_UNUSED( child )
53 return QModelIndex();
54}
55
56
57int QgsDatabaseTableModel::rowCount( const QModelIndex &parent ) const
58{
59 if ( parent.isValid() )
60 return 0;
61
62 return mTables.count() + ( mAllowEmpty ? 1 : 0 );
63}
64
65int QgsDatabaseTableModel::columnCount( const QModelIndex &parent ) const
66{
67 Q_UNUSED( parent )
68 return 1;
69}
70
71
72QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const
73{
74 if ( !index.isValid() )
75 return QVariant();
76
77 if ( index.row() == 0 && mAllowEmpty )
78 {
79 if ( role == static_cast< int >( CustomRole::Empty ) )
80 return true;
81
82 return QVariant();
83 }
84
85 if ( index.row() - ( mAllowEmpty ? 1 : 0 ) >= mTables.count() )
86 return QVariant();
87
88 const QgsAbstractDatabaseProviderConnection::TableProperty &table = mTables[ index.row() - ( mAllowEmpty ? 1 : 0 ) ];
89 switch ( role )
90 {
91 case static_cast< int >( CustomRole::Empty ):
92 return false;
93
94 case Qt::DisplayRole:
95 case Qt::ToolTipRole:
96 case Qt::EditRole:
97 {
98 return mSchema.isEmpty() && !table.schema().isEmpty() ? QStringLiteral( "%1.%2" ).arg( table.schema(), table.tableName() ) : table.tableName();
99 }
100
101 case static_cast< int >( CustomRole::TableName ):
102 {
103 return table.tableName();
104 }
105
106 case Qt::DecorationRole:
107 case static_cast< int >( CustomRole::WkbType ):
108 case static_cast< int >( CustomRole::Crs ):
109 {
110 if ( table.geometryColumnTypes().empty() )
111 {
112 if ( role == Qt::DecorationRole )
114 else
115 return QVariant();
116 }
117
118 if ( role == Qt::DecorationRole )
119 {
120 const Qgis::GeometryType geomType = QgsWkbTypes::geometryType( table.geometryColumnTypes().at( 0 ).wkbType );
121 switch ( geomType )
122 {
124 {
126 }
128 {
130 }
132 {
133 return QgsIconUtils::iconLine();
134 }
136 {
138 }
141 }
142
144 }
145 else if ( role == static_cast< int >( CustomRole::WkbType ) )
146 return static_cast< quint32>( table.geometryColumnTypes().at( 0 ).wkbType );
147 else if ( role == static_cast< int >( CustomRole::Crs ) )
148 return table.geometryColumnTypes().at( 0 ).crs;
149
150 return QVariant();
151 }
152
153 case static_cast< int >( CustomRole::Schema ):
154 return table.schema();
155
156 case static_cast< int >( CustomRole::TableFlags ):
157 return static_cast< int >( table.flags() );
158
159 case static_cast< int >( CustomRole::Comment ):
160 return table.comment();
161
162 case static_cast< int >( CustomRole::CustomInfo ):
163 return table.info();
164
165 }
166
167 return QVariant();
168}
169
170QModelIndex QgsDatabaseTableModel::index( int row, int column, const QModelIndex &parent ) const
171{
172 if ( hasIndex( row, column, parent ) )
173 {
174 return createIndex( row, column, row );
175 }
176
177 return QModelIndex();
178}
179
181{
182 if ( allowEmpty == mAllowEmpty )
183 return;
184
185 if ( allowEmpty )
186 {
187 beginInsertRows( QModelIndex(), 0, 0 );
188 mAllowEmpty = true;
189 endInsertRows();
190 }
191 else
192 {
193 beginRemoveRows( QModelIndex(), 0, 0 );
194 mAllowEmpty = false;
195 endRemoveRows();
196 }
197}
198
200{
201 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > newTables = mConnection->tables( mSchema );
202 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > oldTables = mTables;
203
204 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &oldTable : oldTables )
205 {
206 if ( !newTables.contains( oldTable ) )
207 {
208 const int r = mTables.indexOf( oldTable );
209 beginRemoveRows( QModelIndex(), r + ( mAllowEmpty ? 1 : 0 ), r + ( mAllowEmpty ? 1 : 0 ) );
210 mTables.removeAt( r );
211 endRemoveRows();
212 }
213 }
214
215 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &newTable : newTables )
216 {
217 if ( !mTables.contains( newTable ) )
218 {
219 beginInsertRows( QModelIndex(), mTables.count() + ( mAllowEmpty ? 1 : 0 ), mTables.count() + ( mAllowEmpty ? 1 : 0 ) );
220 mTables.append( newTable );
221 endInsertRows();
222 }
223 }
224}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
QgsDatabaseTableModel(const QString &provider, const QString &connection, const QString &schema=QString(), QObject *parent=nullptr)
Constructor for QgsDatabaseTableModel, for the specified provider and connection name.
QModelIndex parent(const QModelIndex &child) const override
void refresh()
Refreshes the table list by querying the underlying connection.
QModelIndex index(int row, int column, const QModelIndex &parent) const override
void setAllowEmptyTable(bool allowEmpty)
Sets whether an optional empty table ("not set") option is present in the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
@ CustomInfo
Custom info variant map role.
@ Crs
CRS for primary (first) geometry column in table.
@ WkbType
WKB type for primary (first) geometry column in table.
@ Empty
Entry is an empty entry.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
static QIcon iconLine()
Returns an icon representing line geometries.
static QIcon iconPolygon()
Returns an icon representing polygon geometries.
static QIcon iconGeometryCollection()
Returns an icon representing geometry collections.
static QIcon iconPoint()
Returns an icon representing point geometries.
static QIcon iconTable()
Returns an icon representing non-spatial layers (tables).
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.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
The TableProperty class represents a database table or view.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.
QVariantMap info() const
Returns additional information about the table.
QString schema() const
Returns the schema or an empty string for backends that do not support a schema.