QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsfieldsitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldsitem.cpp
3 -------------------
4 begin : 2011-04-01
5 copyright : (C) 2011 Radim Blazek
6 email : radim dot blazek at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsfieldsitem.h"
19#include "qgsiconutils.h"
20#include "qgsproviderregistry.h"
21#include "qgsprovidermetadata.h"
22#include "qgslogger.h"
23#include "qgsapplication.h"
24#include "qgsvectorlayer.h"
25
27 const QString &path,
28 const QString &connectionUri,
29 const QString &providerKey,
30 const QString &schema,
31 const QString &tableName )
32 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
33 , mSchema( schema )
34 , mTableName( tableName )
35 , mConnectionUri( connectionUri )
36{
39 if ( md )
40 {
41 try
42 {
43 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
44 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
45 if ( conn->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::RenameField )
46 {
47 mCanRename = true;
48 }
49 }
51 {
52 QgsDebugMsg( QStringLiteral( "Error creating fields item: %1" ).arg( ex.what() ) );
53 }
54 }
55}
56
58{
59
60}
61
62QVector<QgsDataItem *> QgsFieldsItem::createChildren()
63{
64 QVector<QgsDataItem *> children;
65 try
66 {
68 if ( md )
69 {
70 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
71 if ( conn )
72 {
73 int i = 0;
74 const QgsFields constFields { conn->fields( mSchema, mTableName ) };
75 for ( const auto &f : constFields )
76 {
77 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
78 fieldItem->setSortKey( i++ );
79 children.push_back( fieldItem );
80 }
81 }
82 }
83 }
84 catch ( const QgsProviderConnectionException &ex )
85 {
86 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
87 }
88 return children;
89}
90
92{
93 return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
94}
95
97{
98 return mConnectionUri;
99}
100
102{
103 std::unique_ptr<QgsVectorLayer> vl;
105 if ( md )
106 {
107 try
108 {
109 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
110 if ( conn )
111 {
112 vl.reset( new QgsVectorLayer( conn->tableUri( mSchema, mTableName ), QStringLiteral( "temp_layer" ), providerKey() ) );
113 if ( vl->isValid() )
114 {
115 return vl.release();
116 }
117 }
118 }
119 catch ( const QgsProviderConnectionException & )
120 {
121 // This should never happen!
122 QgsDebugMsg( QStringLiteral( "Error getting connection from %1" ).arg( mConnectionUri ) );
123 }
124 }
125 else
126 {
127 // This should never happen!
128 QgsDebugMsg( QStringLiteral( "Error getting metadata for provider %1" ).arg( providerKey() ) );
129 }
130 return nullptr;
131}
132
134{
135 return mTableProperty.get();
136}
137
139{
140 return mTableName;
141}
142
144{
145 return mSchema;
146}
147
149 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
150 , mField( field )
151{
152 // Precondition
153 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
154 Q_ASSERT( fieldsItem );
155
156 if ( fieldsItem->canRenameFields() )
158
160 const auto constraints { field.constraints().constraints() };
161 QStringList constraintsText;
162 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
163 {
164 constraintsText.push_back( tr( "NOT NULL" ) );
165 }
166 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
167 {
168 constraintsText.push_back( tr( "UNIQUE" ) );
169 }
170 if ( ! constraintsText.isEmpty() )
171 {
172 setToolTip( QStringLiteral( "<ul><li>%1</li></ul>" ).arg( constraintsText.join( QLatin1String( "</li><li>" ) ) ) );
173 }
174}
175
177{
178}
179
181{
182 // Check if this is a geometry column and show the right icon
183 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
184 if ( parentFields && parentFields->tableProperty() &&
185 parentFields->tableProperty()->geometryColumn() == mName &&
186 !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
187 {
188 if ( mField.typeName() == QLatin1String( "raster" ) )
189 {
191 }
192 const QgsWkbTypes::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
193 switch ( geomType )
194 {
195 case QgsWkbTypes::GeometryType::LineGeometry:
196 return QgsIconUtils::iconLine();
197 case QgsWkbTypes::GeometryType::PointGeometry:
199 case QgsWkbTypes::GeometryType::PolygonGeometry:
201 case QgsWkbTypes::GeometryType::UnknownGeometry:
203 case QgsWkbTypes::GeometryType::NullGeometry:
205 }
206 }
207 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType() ) };
208 // Try subtype if icon is null
209 if ( icon.isNull() )
210 {
211 return QgsFields::iconForFieldType( mField.subType() );
212 }
213 return icon;
214}
215
217{
218 if ( type() != other->type() )
219 {
220 return false;
221 }
222
223 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
224 if ( !o )
225 return false;
226
227 return ( mPath == o->mPath && mName == o->mName && mField == o->mField );
228}
229
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
@ Populated
Children created.
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn (since QGIS 3....
@ Rename
Item can be renamed.
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Base class for all items in the model.
Definition: qgsdataitem.h:46
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
QString mName
Definition: qgsdataitem.h:449
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QString mPath
Definition: qgsdataitem.h:455
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QString path() const
Definition: qgsdataitem.h:354
virtual void setState(Qgis::BrowserItemState state)
Set item state.
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
QString providerKey() const
Returns the provider key that created this item (e.g.
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:541
QString what() const
Definition: qgsexception.h:48
Q_GADGET Constraints constraints
A layer field item, information about the connection URI, the schema and the table as well as the lay...
~QgsFieldItem() override
bool equal(const QgsDataItem *other) override
Returns true if this item is equal to another item (by testing item type and path).
QgsFieldItem(QgsDataItem *parent, const QgsField &field)
Constructor for QgsFieldItem, with the specified parent item and field.
QgsField field() const
Returns the field definition.
QIcon icon() override
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:140
QVariant::Type type
Definition: qgsfield.h:58
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:135
QgsFieldConstraints constraints
Definition: qgsfield.h:63
A collection of field items with some internal logic to retrieve the fields and a the vector layer in...
Definition: qgsfieldsitem.h:34
QString tableName() const
Returns the table name.
QgsFieldsItem(QgsDataItem *parent, const QString &path, const QString &connectionUri, const QString &providerKey, const QString &schema, const QString &tableName)
Constructor for QgsFieldsItem, with the specified parent item.
QgsAbstractDatabaseProviderConnection::TableProperty * tableProperty() const
Returns the (possibly NULL) properties of the table this fields belong to.
QString connectionUri() const
Returns the connection URI.
QVector< QgsDataItem * > createChildren() override
Create children.
QgsVectorLayer * layer()
Creates and returns a (possibly NULL) layer from the connection URI and schema/table information.
bool canRenameFields() const
Returns true if the connection supports renaming fields.
~QgsFieldsItem() override
QString schema() const
Returns the schema name.
QIcon icon() override
Container of fields for a vector layer.
Definition: qgsfields.h:45
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns an icon corresponding to a field type.
Definition: qgsfields.cpp:294
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 iconDefaultLayer()
Returns a default icon for layers, which aren't the standard raster/vector/... types.
static QIcon iconPoint()
Returns an icon representing point geometries.
static QIcon iconRaster()
Returns an icon representing raster layers.
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
Holds data provider key, description, and associated shared library file or function pointer informat...
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.
Represents a vector layer which manages a vector based data sets.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
const QgsField & field
Definition: qgsfield.h:463
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
The TableProperty class represents a database table or view.