QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
31
32#include "moc_qgsfieldsitem.cpp"
33
34using namespace Qt::StringLiterals;
35
37 const QString &path,
38 const QString &connectionUri,
39 const QString &providerKey,
40 const QString &schema,
41 const QString &tableName )
42 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
43 , mSchema( schema )
44 , mTableName( tableName )
45 , mConnectionUri( connectionUri )
46{
49 if ( md )
50 {
51 try
52 {
53 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
54 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
56 {
57 mCanRename = true;
58 }
59 }
61 {
62 QgsDebugError( u"Error creating fields item: %1"_s.arg( ex.what() ) );
63 }
64 }
65}
66
71
72QVector<QgsDataItem *> QgsFieldsItem::createChildren()
73{
74 QVector<QgsDataItem *> children;
75 try
76 {
78 if ( md )
79 {
80 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
81 if ( conn )
82 {
83 int i = 0;
84 const QgsFields constFields { conn->fields( mSchema, mTableName ) };
85 for ( const auto &f : constFields )
86 {
87 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
88 fieldItem->setSortKey( i++ );
89 children.push_back( fieldItem );
90 }
91 }
92 }
93 }
94 catch ( const QgsProviderConnectionException &ex )
95 {
96 children.push_back( new QgsErrorItem( this, ex.what(), path() + u"/error"_s ) );
97 }
98 return children;
99}
100
102{
103 return QgsApplication::getThemeIcon( u"mSourceFields.svg"_s );
104}
105
107{
108 return mConnectionUri;
109}
110
112{
113 std::unique_ptr<QgsVectorLayer> vl;
115 if ( md )
116 {
117 try
118 {
119 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
120 if ( conn )
121 {
122 vl = std::make_unique<QgsVectorLayer>( conn->tableUri( mSchema, mTableName ), u"temp_layer"_s, providerKey() );
123 if ( vl->isValid() )
124 {
125 return vl.release();
126 }
127 }
128 }
129 catch ( const QgsProviderConnectionException & )
130 {
131 // This should never happen!
132 QgsDebugError( u"Error getting connection from %1"_s.arg( mConnectionUri ) );
133 }
134 }
135 else
136 {
137 // This should never happen!
138 QgsDebugError( u"Error getting metadata for provider %1"_s.arg( providerKey() ) );
139 }
140 return nullptr;
141}
142
147
149{
150 return mTableName;
151}
152
154{
155 return mSchema;
156}
157
159 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
160 , mField( field )
161{
162 // Precondition
163 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
164 Q_ASSERT( fieldsItem );
165
166 if ( fieldsItem->canRenameFields() )
168
170
172}
173
177
179{
180 // Check if this is a geometry column and show the right icon
181 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
182 if ( parentFields && parentFields->tableProperty() &&
183 parentFields->tableProperty()->geometryColumn() == mName &&
184 !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
185 {
186 if ( mField.typeName() == "raster"_L1 )
187 {
189 }
190 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
191 switch ( geomType )
192 {
194 return QgsIconUtils::iconLine();
203 }
204 }
205 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() ) };
206 // Try subtype if icon is null
207 if ( icon.isNull() )
208 {
209 return QgsFields::iconForFieldType( mField.subType() );
210 }
211 return icon;
212}
213
215{
216 if ( type() != other->type() )
217 {
218 return false;
219 }
220
221 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
222 if ( !o )
223 return false;
224
225 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
226}
227
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
@ Populated
Children created.
Definition qgis.h:960
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:975
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:973
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
Definition qgis.h:979
@ Rename
Item can be renamed.
Definition qgis.h:976
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:365
@ Point
Points.
Definition qgis.h:366
@ Line
Lines.
Definition qgis.h:367
@ Polygon
Polygons.
Definition qgis.h:368
@ Unknown
Unknown types.
Definition qgis.h:369
@ Null
No geometry.
Definition qgis.h:370
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:56
QString comment
Definition qgsfield.h:64
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:59
The TableProperty class represents a database table or view.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.