QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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#include "qgsfieldmodel.h"
26
28 const QString &path,
29 const QString &connectionUri,
30 const QString &providerKey,
31 const QString &schema,
32 const QString &tableName )
33 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
34 , mSchema( schema )
35 , mTableName( tableName )
36 , mConnectionUri( connectionUri )
37{
40 if ( md )
41 {
42 try
43 {
44 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
45 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
47 {
48 mCanRename = true;
49 }
50 }
52 {
53 QgsDebugError( QStringLiteral( "Error creating fields item: %1" ).arg( ex.what() ) );
54 }
55 }
56}
57
62
63QVector<QgsDataItem *> QgsFieldsItem::createChildren()
64{
65 QVector<QgsDataItem *> children;
66 try
67 {
69 if ( md )
70 {
71 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
72 if ( conn )
73 {
74 int i = 0;
75 const QgsFields constFields { conn->fields( mSchema, mTableName ) };
76 for ( const auto &f : constFields )
77 {
78 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
79 fieldItem->setSortKey( i++ );
80 children.push_back( fieldItem );
81 }
82 }
83 }
84 }
85 catch ( const QgsProviderConnectionException &ex )
86 {
87 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
88 }
89 return children;
90}
91
93{
94 return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
95}
96
98{
99 return mConnectionUri;
100}
101
103{
104 std::unique_ptr<QgsVectorLayer> vl;
106 if ( md )
107 {
108 try
109 {
110 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
111 if ( conn )
112 {
113 vl.reset( new QgsVectorLayer( conn->tableUri( mSchema, mTableName ), QStringLiteral( "temp_layer" ), providerKey() ) );
114 if ( vl->isValid() )
115 {
116 return vl.release();
117 }
118 }
119 }
120 catch ( const QgsProviderConnectionException & )
121 {
122 // This should never happen!
123 QgsDebugError( QStringLiteral( "Error getting connection from %1" ).arg( mConnectionUri ) );
124 }
125 }
126 else
127 {
128 // This should never happen!
129 QgsDebugError( QStringLiteral( "Error getting metadata for provider %1" ).arg( providerKey() ) );
130 }
131 return nullptr;
132}
133
138
140{
141 return mTableName;
142}
143
145{
146 return mSchema;
147}
148
150 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
151 , mField( field )
152{
153 // Precondition
154 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
155 Q_ASSERT( fieldsItem );
156
157 if ( fieldsItem->canRenameFields() )
159
161
163}
164
168
170{
171 // Check if this is a geometry column and show the right icon
172 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
173 if ( parentFields && parentFields->tableProperty() &&
174 parentFields->tableProperty()->geometryColumn() == mName &&
175 !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
176 {
177 if ( mField.typeName() == QLatin1String( "raster" ) )
178 {
180 }
181 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
182 switch ( geomType )
183 {
185 return QgsIconUtils::iconLine();
194 }
195 }
196 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() ) };
197 // Try subtype if icon is null
198 if ( icon.isNull() )
199 {
200 return QgsFields::iconForFieldType( mField.subType() );
201 }
202 return icon;
203}
204
206{
207 if ( type() != other->type() )
208 {
209 return false;
210 }
211
212 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
213 if ( !o )
214 return false;
215
216 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
217}
218
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ 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.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:255
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
@ RenameField
Can rename existing fields via renameField() (since QGIS 3.28)
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
void setToolTip(const QString &msg)
QString mPath
QVector< QgsDataItem * > children() const
Qgis::BrowserItemCapabilities mCapabilities
Qgis::BrowserItemType type() const
QString path() const
virtual void setState(Qgis::BrowserItemState state)
Set item state.
QgsDataItem * parent() const
Gets item parent.
QString providerKey() const
Returns the provider key that created this item (e.g.
Data item that can be used to report problems (e.g.
QString what() const
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
static QString fieldToolTip(const QgsField &field)
Returns a HTML formatted tooltip string for a field, containing details like the field name,...
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:150
QVariant::Type type
Definition qgsfield.h:60
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:145
QString comment
Definition qgsfield.h:61
A collection of field items with some internal logic to retrieve the fields and a the vector layer in...
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, const QString &typeString=QString())
Returns an icon corresponding to a field type.
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.
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 Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
#define QgsDebugError(str)
Definition qgslogger.h:38
The TableProperty class represents a database table or view.