QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include <memory>
21
22#include "qgsapplication.h"
23#include "qgsfieldmodel.h"
24#include "qgsiconutils.h"
25#include "qgslogger.h"
26#include "qgsprovidermetadata.h"
27#include "qgsproviderregistry.h"
28#include "qgsvectorlayer.h"
29
30#include "moc_qgsfieldsitem.cpp"
31
33 const QString &path,
34 const QString &connectionUri,
35 const QString &providerKey,
36 const QString &schema,
37 const QString &tableName )
38 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
39 , mSchema( schema )
40 , mTableName( tableName )
41 , mConnectionUri( connectionUri )
42{
45 if ( md )
46 {
47 try
48 {
49 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
50 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
52 {
53 mCanRename = true;
54 }
55 }
57 {
58 QgsDebugError( QStringLiteral( "Error creating fields item: %1" ).arg( ex.what() ) );
59 }
60 }
61}
62
67
68QVector<QgsDataItem *> QgsFieldsItem::createChildren()
69{
70 QVector<QgsDataItem *> children;
71 try
72 {
74 if ( md )
75 {
76 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
77 if ( conn )
78 {
79 int i = 0;
80 const QgsFields constFields { conn->fields( mSchema, mTableName ) };
81 for ( const auto &f : constFields )
82 {
83 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
84 fieldItem->setSortKey( i++ );
85 children.push_back( fieldItem );
86 }
87 }
88 }
89 }
90 catch ( const QgsProviderConnectionException &ex )
91 {
92 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
93 }
94 return children;
95}
96
98{
99 return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
100}
101
103{
104 return mConnectionUri;
105}
106
108{
109 std::unique_ptr<QgsVectorLayer> vl;
111 if ( md )
112 {
113 try
114 {
115 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
116 if ( conn )
117 {
118 vl = std::make_unique<QgsVectorLayer>( conn->tableUri( mSchema, mTableName ), QStringLiteral( "temp_layer" ), providerKey() );
119 if ( vl->isValid() )
120 {
121 return vl.release();
122 }
123 }
124 }
125 catch ( const QgsProviderConnectionException & )
126 {
127 // This should never happen!
128 QgsDebugError( QStringLiteral( "Error getting connection from %1" ).arg( mConnectionUri ) );
129 }
130 }
131 else
132 {
133 // This should never happen!
134 QgsDebugError( QStringLiteral( "Error getting metadata for provider %1" ).arg( providerKey() ) );
135 }
136 return nullptr;
137}
138
143
145{
146 return mTableName;
147}
148
150{
151 return mSchema;
152}
153
155 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
156 , mField( field )
157{
158 // Precondition
159 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
160 Q_ASSERT( fieldsItem );
161
162 if ( fieldsItem->canRenameFields() )
164
166
168}
169
173
175{
176 // Check if this is a geometry column and show the right icon
177 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
178 if ( parentFields && parentFields->tableProperty() &&
179 parentFields->tableProperty()->geometryColumn() == mName &&
180 !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
181 {
182 if ( mField.typeName() == QLatin1String( "raster" ) )
183 {
185 }
186 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
187 switch ( geomType )
188 {
190 return QgsIconUtils::iconLine();
199 }
200 }
201 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() ) };
202 // Try subtype if icon is null
203 if ( icon.isNull() )
204 {
205 return QgsFields::iconForFieldType( mField.subType() );
206 }
207 return icon;
208}
209
211{
212 if ( type() != other->type() )
213 {
214 return false;
215 }
216
217 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
218 if ( !o )
219 return false;
220
221 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
222}
223
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ Populated
Children created.
Definition qgis.h:941
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:956
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:954
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
Definition qgis.h:960
@ Rename
Item can be renamed.
Definition qgis.h:957
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Unknown
Unknown types.
Definition qgis.h:362
@ Null
No geometry.
Definition qgis.h:363
Provides common functionality for database based connections.
@ 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.
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
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
QString name() const
Returns the name of the item (the displayed text for the item).
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.
A browser 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:54
QString comment
Definition qgsfield.h:62
A browser item which contains a collection of field items.
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...
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.
Represents a vector layer which manages a vector based dataset.
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:57
The TableProperty class represents a database table or view.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.