QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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 "qgsfielddomain.h"
24#include "qgsfieldmodel.h"
25#include "qgsiconutils.h"
26#include "qgslogger.h"
27#include "qgsprovidermetadata.h"
28#include "qgsproviderregistry.h"
29#include "qgsvectorlayer.h"
30
31#include <QString>
32
33#include "moc_qgsfieldsitem.cpp"
34
35using namespace Qt::StringLiterals;
36
37QgsFieldsItem::QgsFieldsItem( QgsDataItem *parent, const QString &path, const QString &connectionUri, const QString &providerKey, const QString &schema, 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( u"Error creating fields item: %1"_s.arg( ex.what() ) );
59 }
60 }
61}
62
65
66QVector<QgsDataItem *> QgsFieldsItem::createChildren()
67{
68 QVector<QgsDataItem *> children;
69 try
70 {
72 if ( md )
73 {
74 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
75 if ( conn )
76 {
77 int i = 0;
78 mFields = conn->fields( mSchema, mTableName );
79 for ( const QgsField &f : mFields )
80 {
81 QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
82 const QString domainName = f.constraints().domainName();
83 if ( !domainName.isEmpty() )
84 {
85 std::unique_ptr< QgsFieldDomain > domain( conn->fieldDomain( domainName ) );
86 if ( domain )
87 fieldItem->setDomain( domain.release() );
88 }
89 fieldItem->setSortKey( i++ );
90 children.push_back( fieldItem );
91 }
92 }
93 }
94 }
95 catch ( const QgsProviderConnectionException &ex )
96 {
97 children.push_back( new QgsErrorItem( this, ex.what(), path() + u"/error"_s ) );
98 }
99 return children;
100}
101
103{
104 return QgsApplication::getThemeIcon( u"mSourceFields.svg"_s );
105}
106
108{
109 return mConnectionUri;
110}
111
113{
114 std::unique_ptr<QgsVectorLayer> vl;
116 if ( md )
117 {
118 try
119 {
120 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
121 if ( conn )
122 {
123 vl = std::make_unique<QgsVectorLayer>( conn->tableUri( mSchema, mTableName ), u"temp_layer"_s, providerKey() );
124 if ( vl->isValid() )
125 {
126 return vl.release();
127 }
128 }
129 }
130 catch ( const QgsProviderConnectionException & )
131 {
132 // This should never happen!
133 QgsDebugError( u"Error getting connection from %1"_s.arg( mConnectionUri ) );
134 }
135 }
136 else
137 {
138 // This should never happen!
139 QgsDebugError( u"Error getting metadata for provider %1"_s.arg( providerKey() ) );
140 }
141 return nullptr;
142}
143
145{
146 return mFields;
147}
148
153
155{
156 return mTableName;
157}
158
160{
161 return mSchema;
162}
163
165 : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
166 , mField( field )
167{
168 // Precondition
169 QgsFieldsItem *fieldsItem = qgis::down_cast<QgsFieldsItem *>( parent );
170 Q_ASSERT( fieldsItem );
171
172 if ( fieldsItem->canRenameFields() )
174
176
178}
179
182
184{
185 mDomain.reset( domain );
187}
188
190{
191 QIcon icon;
192
193 // Check if this is a geometry column and show the right icon
194 QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
195 if ( parentFields && parentFields->tableProperty() && parentFields->tableProperty()->geometryColumn() == mName && !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
196 {
197 if ( mField.typeName() == "raster"_L1 )
198 {
200 }
201 else
202 {
203 const Qgis::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
204 switch ( geomType )
205 {
208 break;
211 break;
214 break;
217 break;
220 break;
221 }
222 }
223 }
224
225 if ( icon.isNull() )
226 {
227 icon = QgsFields::iconForFieldType( mField.type(), mField.subType(), mField.typeName() );
228 // Try subtype if icon is null
229 if ( icon.isNull() )
230 icon = QgsFields::iconForFieldType( mField.subType() );
231 }
232
233 if ( mDomain )
234 icon = QgsIconUtils::addOverlay( icon, QgsApplication::iconPath( u"/field_indicators/mIndicatorFieldDomain.svg"_s ) );
235
236 return icon;
237}
238
240{
241 if ( type() != other->type() )
242 {
243 return false;
244 }
245
246 const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
247 if ( !o )
248 return false;
249
250 return ( mPath == o->mPath && mName == o->mName && mField == o->mField && mField.comment() == o->mField.comment() );
251}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
@ Populated
Children created.
Definition qgis.h:1023
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:1038
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:1036
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
Definition qgis.h:1042
@ Rename
Item can be renamed.
Definition qgis.h:1039
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:399
@ Point
Points.
Definition qgis.h:400
@ Line
Lines.
Definition qgis.h:401
@ Polygon
Polygons.
Definition qgis.h:402
@ Unknown
Unknown types.
Definition qgis.h:403
@ Null
No geometry.
Definition qgis.h:404
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.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
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
Base class for field domains.
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.
void setDomain(QgsFieldDomain *domain)
Sets the domain for the field.
QIcon icon() override
static QString fieldToolTip(const QgsField &field, const QString &predefinedComment=QString())
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:45
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 addOverlay(const QIcon &icon, const QString &overlayPath, QSize size=QSize(16, 16))
Returns a copy of icon with an overlay icon rendered from the image file at overlayPath.
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:71
The TableProperty class represents a database table or view.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.