QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsiconutils.h"
19#include "qgsprovidermetadata.h"
20#include "qgsproviderregistry.h"
21
22#include <QIcon>
23
24#include "moc_qgsdatabasetablemodel.cpp"
25
26QgsDatabaseTableModel::QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema, QObject *parent )
27 : QAbstractItemModel( parent )
28 , mSchema( schema )
29{
31 Q_ASSERT( metadata );
32
33 mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) );
34 Q_ASSERT( mConnection );
35 init();
36}
37
39 : QAbstractItemModel( parent )
40 , mConnection( connection )
41 , mSchema( schema )
42{
43 Q_ASSERT( mConnection );
44 init();
45}
46
47void QgsDatabaseTableModel::init()
48{
49 Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Tables );
50 mTables = mConnection->tables( mSchema );
51}
52
53QModelIndex QgsDatabaseTableModel::parent( const QModelIndex &child ) const
54{
55 Q_UNUSED( child )
56 return QModelIndex();
57}
58
59
60int QgsDatabaseTableModel::rowCount( const QModelIndex &parent ) const
61{
62 if ( parent.isValid() )
63 return 0;
64
65 return mTables.count() + ( mAllowEmpty ? 1 : 0 );
66}
67
68int QgsDatabaseTableModel::columnCount( const QModelIndex &parent ) const
69{
70 Q_UNUSED( parent )
71 return 1;
72}
73
74
75QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const
76{
77 if ( !index.isValid() )
78 return QVariant();
79
80 if ( index.row() == 0 && mAllowEmpty )
81 {
82 if ( role == static_cast< int >( CustomRole::Empty ) )
83 return true;
84
85 return QVariant();
86 }
87
88 if ( index.row() - ( mAllowEmpty ? 1 : 0 ) >= mTables.count() )
89 return QVariant();
90
91 const QgsAbstractDatabaseProviderConnection::TableProperty &table = mTables[ index.row() - ( mAllowEmpty ? 1 : 0 ) ];
92 switch ( role )
93 {
94 case static_cast< int >( CustomRole::Empty ):
95 return false;
96
97 case Qt::DisplayRole:
98 case Qt::ToolTipRole:
99 case Qt::EditRole:
100 {
101 return mSchema.isEmpty() && !table.schema().isEmpty() ? QStringLiteral( "%1.%2" ).arg( table.schema(), table.tableName() ) : table.tableName();
102 }
103
104 case static_cast< int >( CustomRole::TableName ):
105 {
106 return table.tableName();
107 }
108
109 case Qt::DecorationRole:
110 case static_cast< int >( CustomRole::WkbType ):
111 case static_cast< int >( CustomRole::Crs ):
112 {
113 if ( table.geometryColumnTypes().empty() )
114 {
115 if ( role == Qt::DecorationRole )
117 else
118 return QVariant();
119 }
120
121 if ( role == Qt::DecorationRole )
122 {
123 const Qgis::GeometryType geomType = QgsWkbTypes::geometryType( table.geometryColumnTypes().at( 0 ).wkbType );
124 switch ( geomType )
125 {
127 {
129 }
131 {
133 }
135 {
136 return QgsIconUtils::iconLine();
137 }
139 {
141 }
144 }
145
147 }
148 else if ( role == static_cast< int >( CustomRole::WkbType ) )
149 return static_cast< quint32>( table.geometryColumnTypes().at( 0 ).wkbType );
150 else if ( role == static_cast< int >( CustomRole::Crs ) )
151 return table.geometryColumnTypes().at( 0 ).crs;
152
153 return QVariant();
154 }
155
156 case static_cast< int >( CustomRole::Schema ):
157 return table.schema();
158
159 case static_cast< int >( CustomRole::TableFlags ):
160 return static_cast< int >( table.flags() );
161
162 case static_cast< int >( CustomRole::Comment ):
163 return table.comment();
164
165 case static_cast< int >( CustomRole::CustomInfo ):
166 return table.info();
167
168 }
169
170 return QVariant();
171}
172
173QModelIndex QgsDatabaseTableModel::index( int row, int column, const QModelIndex &parent ) const
174{
175 if ( hasIndex( row, column, parent ) )
176 {
177 return createIndex( row, column, row );
178 }
179
180 return QModelIndex();
181}
182
184{
185 if ( allowEmpty == mAllowEmpty )
186 return;
187
188 if ( allowEmpty )
189 {
190 beginInsertRows( QModelIndex(), 0, 0 );
191 mAllowEmpty = true;
192 endInsertRows();
193 }
194 else
195 {
196 beginRemoveRows( QModelIndex(), 0, 0 );
197 mAllowEmpty = false;
198 endRemoveRows();
199 }
200}
201
203{
204 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > newTables = mConnection->tables( mSchema );
205 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > oldTables = mTables;
206
207 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &oldTable : oldTables )
208 {
209 if ( !newTables.contains( oldTable ) )
210 {
211 const int r = mTables.indexOf( oldTable );
212 beginRemoveRows( QModelIndex(), r + ( mAllowEmpty ? 1 : 0 ), r + ( mAllowEmpty ? 1 : 0 ) );
213 mTables.removeAt( r );
214 endRemoveRows();
215 }
216 }
217
218 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &newTable : newTables )
219 {
220 if ( !mTables.contains( newTable ) )
221 {
222 beginInsertRows( QModelIndex(), mTables.count() + ( mAllowEmpty ? 1 : 0 ), mTables.count() + ( mAllowEmpty ? 1 : 0 ) );
223 mTables.append( newTable );
224 endInsertRows();
225 }
226 }
227}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Unknown
Unknown types.
Definition qgis.h:362
@ Null
No geometry.
Definition qgis.h:363
Provides common functionality for database based connections.
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.
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.