QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
36QgsFieldsItem::QgsFieldsItem( QgsDataItem *parent, const QString &path, const QString &connectionUri, const QString &providerKey, const QString &schema, const QString &tableName )
37 : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
38 , mSchema( schema )
39 , mTableName( tableName )
40 , mConnectionUri( connectionUri )
41{
44 if ( md )
45 {
46 try
47 {
48 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
49 mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
51 {
52 mCanRename = true;
53 }
54 }
56 {
57 QgsDebugError( u"Error creating fields item: %1"_s.arg( ex.what() ) );
58 }
59 }
60}
61
64
65QVector<QgsDataItem *> QgsFieldsItem::createChildren()
66{
67 QVector<QgsDataItem *> children;
68 try
69 {
71 if ( md )
72 {
73 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
74 if ( conn )
75 {
76 int i = 0;
77 mFields = conn->fields( mSchema, mTableName );
78 for ( const QgsField &f : mFields )
79 {
80 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
81 fieldItem->setSortKey( i++ );
82 children.push_back( fieldItem );
83 }
84 }
85 }
86 }
87 catch ( const QgsProviderConnectionException &ex )
88 {
89 children.push_back( new QgsErrorItem( this, ex.what(), path() + u"/error"_s ) );
90 }
91 return children;
92}
93
95{
96 return QgsApplication::getThemeIcon( u"mSourceFields.svg"_s );
97}
98
100{
101 return mConnectionUri;
102}
103
105{
106 std::unique_ptr<QgsVectorLayer> vl;
108 if ( md )
109 {
110 try
111 {
112 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
113 if ( conn )
114 {
115 vl = std::make_unique<QgsVectorLayer>( conn->tableUri( mSchema, mTableName ), u"temp_layer"_s, providerKey() );
116 if ( vl->isValid() )
117 {
118 return vl.release();
119 }
120 }
121 }
122 catch ( const QgsProviderConnectionException & )
123 {
124 // This should never happen!
125 QgsDebugError( u"Error getting connection from %1"_s.arg( mConnectionUri ) );
126 }
127 }
128 else
129 {
130 // This should never happen!
131 QgsDebugError( u"Error getting metadata for provider %1"_s.arg( providerKey() ) );
132 }
133 return nullptr;
134}
135
137{
138 return mFields;
139}
140
145
147{
148 return mTableName;
149}
150
152{
153 return mSchema;
154}
155
157 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
158 , mField( field )
159{
160 // Precondition
161 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
162 Q_ASSERT( fieldsItem );
163
164 if ( fieldsItem->canRenameFields() )
166
168
170}
171
174
176{
177 // Check if this is a geometry column and show the right icon
178 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
179 if ( parentFields && parentFields->tableProperty() && parentFields->tableProperty()->geometryColumn() == mName && !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
180 {
181 if ( mField.typeName() == "raster"_L1 )
182 {
184 }
185 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
186 switch ( geomType )
187 {
189 return QgsIconUtils::iconLine();
198 }
199 }
200 const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() ) };
201 // Try subtype if icon is null
202 if ( icon.isNull() )
203 {
204 return QgsFields::iconForFieldType( mField.subType() );
205 }
206 return icon;
207}
208
210{
211 if ( type() != other->type() )
212 {
213 return false;
214 }
215
216 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
217 if ( !o )
218 return false;
219
220 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
221}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
@ Populated
Children created.
Definition qgis.h:967
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:982
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:980
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
Definition qgis.h:986
@ Rename
Item can be renamed.
Definition qgis.h:983
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
@ Null
No geometry.
Definition qgis.h:384
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.
QgsFields fields() const
Returns the fields contained by the item.
~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.