QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 return QVariant();
173}
174
175QModelIndex QgsDatabaseTableModel::index( int row, int column, const QModelIndex &parent ) const
176{
177 if ( hasIndex( row, column, parent ) )
178 {
179 return createIndex( row, column, row );
180 }
181
182 return QModelIndex();
183}
184
186{
187 if ( allowEmpty == mAllowEmpty )
188 return;
189
190 if ( allowEmpty )
191 {
192 beginInsertRows( QModelIndex(), 0, 0 );
193 mAllowEmpty = true;
194 endInsertRows();
195 }
196 else
197 {
198 beginRemoveRows( QModelIndex(), 0, 0 );
199 mAllowEmpty = false;
200 endRemoveRows();
201 }
202}
203
205{
206 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > newTables = mConnection->tables( mSchema );
207 const QList< QgsAbstractDatabaseProviderConnection::TableProperty > oldTables = mTables;
208
209 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &oldTable : oldTables )
210 {
211 if ( !newTables.contains( oldTable ) )
212 {
213 const int r = mTables.indexOf( oldTable );
214 beginRemoveRows( QModelIndex(), r + ( mAllowEmpty ? 1 : 0 ), r + ( mAllowEmpty ? 1 : 0 ) );
215 mTables.removeAt( r );
216 endRemoveRows();
217 }
218 }
219
220 for ( const QgsAbstractDatabaseProviderConnection::TableProperty &newTable : newTables )
221 {
222 if ( !mTables.contains( newTable ) )
223 {
224 beginInsertRows( QModelIndex(), mTables.count() + ( mAllowEmpty ? 1 : 0 ), mTables.count() + ( mAllowEmpty ? 1 : 0 ) );
225 mTables.append( newTable );
226 endInsertRows();
227 }
228 }
229}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
@ Null
No geometry.
Definition qgis.h:384
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.