QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "moc_qgsfieldsitem.cpp"
20#include "qgsiconutils.h"
21#include "qgsproviderregistry.h"
22#include "qgsprovidermetadata.h"
23#include "qgslogger.h"
24#include "qgsapplication.h"
25#include "qgsvectorlayer.h"
26#include "qgsfieldmodel.h"
27
29 const QString &path,
30 const QString &connectionUri,
31 const QString &providerKey,
32 const QString &schema,
33 const QString &tableName )
34 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
35 , mSchema( schema )
36 , mTableName( tableName )
37 , mConnectionUri( connectionUri )
38{
41 if ( md )
42 {
43 try
44 {
45 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
46 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
48 {
49 mCanRename = true;
50 }
51 }
53 {
54 QgsDebugError( QStringLiteral( "Error creating fields item: %1" ).arg( ex.what() ) );
55 }
56 }
57}
58
63
64QVector<QgsDataItem *> QgsFieldsItem::createChildren()
65{
66 QVector<QgsDataItem *> children;
67 try
68 {
70 if ( md )
71 {
72 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
73 if ( conn )
74 {
75 int i = 0;
76 const QgsFields constFields { conn->fields( mSchema, mTableName ) };
77 for ( const auto &f : constFields )
78 {
79 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
80 fieldItem->setSortKey( i++ );
81 children.push_back( fieldItem );
82 }
83 }
84 }
85 }
86 catch ( const QgsProviderConnectionException &ex )
87 {
88 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
89 }
90 return children;
91}
92
94{
95 return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
96}
97
99{
100 return mConnectionUri;
101}
102
104{
105 std::unique_ptr<QgsVectorLayer> vl;
107 if ( md )
108 {
109 try
110 {
111 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
112 if ( conn )
113 {
114 vl.reset( new QgsVectorLayer( conn->tableUri( mSchema, mTableName ), QStringLiteral( "temp_layer" ), providerKey() ) );
115 if ( vl->isValid() )
116 {
117 return vl.release();
118 }
119 }
120 }
121 catch ( const QgsProviderConnectionException & )
122 {
123 // This should never happen!
124 QgsDebugError( QStringLiteral( "Error getting connection from %1" ).arg( mConnectionUri ) );
125 }
126 }
127 else
128 {
129 // This should never happen!
130 QgsDebugError( QStringLiteral( "Error getting metadata for provider %1" ).arg( providerKey() ) );
131 }
132 return nullptr;
133}
134
139
141{
142 return mTableName;
143}
144
146{
147 return mSchema;
148}
149
151 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
152 , mField( field )
153{
154 // Precondition
155 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
156 Q_ASSERT( fieldsItem );
157
158 if ( fieldsItem->canRenameFields() )
160
162
164}
165
169
171{
172 // Check if this is a geometry column and show the right icon
173 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
174 if ( parentFields && parentFields->tableProperty() &&
175 parentFields->tableProperty()->geometryColumn() == mName &&
176 !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
177 {
178 if ( mField.typeName() == QLatin1String( "raster" ) )
179 {
181 }
182 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
183 switch ( geomType )
184 {
186 return QgsIconUtils::iconLine();
195 }
196 }
197 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() ) };
198 // Try subtype if icon is null
199 if ( icon.isNull() )
200 {
201 return QgsFields::iconForFieldType( mField.subType() );
202 }
203 return icon;
204}
205
207{
208 if ( type() != other->type() )
209 {
210 return false;
211 }
212
213 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
214 if ( !o )
215 return false;
216
217 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
218}
219
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.
@ Rename
Item can be renamed.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ 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()
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 data item representing a single field from a layer.
~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
QMetaType::Type type
Definition qgsfield.h:60
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:161
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:156
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 nullptr) 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 nullptr) 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:46
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, 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.