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