QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsiconutils.h"
20 #include "qgsproviderregistry.h"
21 #include "qgsprovidermetadata.h"
22 #include "qgslogger.h"
23 #include "qgsapplication.h"
24 #include "qgsvectorlayer.h"
25 
27  const QString &path,
28  const QString &connectionUri,
29  const QString &providerKey,
30  const QString &schema,
31  const QString &tableName )
32  : QgsDataItem( Qgis::BrowserItemType::Fields, parent, tr( "Fields" ), path, providerKey )
33  , mSchema( schema )
34  , mTableName( tableName )
35  , mConnectionUri( connectionUri )
36 {
39  if ( md )
40  {
41  try
42  {
43  std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
44  mTableProperty = std::make_unique<QgsAbstractDatabaseProviderConnection::TableProperty>( conn->table( schema, tableName ) );
45  }
46  catch ( QgsProviderConnectionException &ex )
47  {
48  QgsDebugMsg( QStringLiteral( "Error creating fields item: %1" ).arg( ex.what() ) );
49  }
50  }
51 }
52 
54 {
55 
56 }
57 
58 QVector<QgsDataItem *> QgsFieldsItem::createChildren()
59 {
60  QVector<QgsDataItem *> children;
61  try
62  {
64  if ( md )
65  {
66  std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
67  if ( conn )
68  {
69  int i = 0;
70  const QgsFields constFields { conn->fields( mSchema, mTableName ) };
71  for ( const auto &f : constFields )
72  {
73  QgsFieldItem *fieldItem { new QgsFieldItem( this, f ) };
74  fieldItem->setSortKey( i++ );
75  children.push_back( fieldItem );
76  }
77  }
78  }
79  }
80  catch ( const QgsProviderConnectionException &ex )
81  {
82  children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
83  }
84  return children;
85 }
86 
88 {
89  return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
90 }
91 
93 {
94  return mConnectionUri;
95 }
96 
98 {
99  std::unique_ptr<QgsVectorLayer> vl;
101  if ( md )
102  {
103  try
104  {
105  std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
106  if ( conn )
107  {
108  vl.reset( new QgsVectorLayer( conn->tableUri( mSchema, mTableName ), QStringLiteral( "temp_layer" ), providerKey() ) );
109  if ( vl->isValid() )
110  {
111  return vl.release();
112  }
113  }
114  }
115  catch ( const QgsProviderConnectionException & )
116  {
117  // This should never happen!
118  QgsDebugMsg( QStringLiteral( "Error getting connection from %1" ).arg( mConnectionUri ) );
119  }
120  }
121  else
122  {
123  // This should never happen!
124  QgsDebugMsg( QStringLiteral( "Error getting metadata for provider %1" ).arg( providerKey() ) );
125  }
126  return nullptr;
127 }
128 
130 {
131  return mTableProperty.get();
132 }
133 
135 {
136  return mTableName;
137 }
138 
139 QString QgsFieldsItem::schema() const
140 {
141  return mSchema;
142 }
143 
145  : QgsDataItem( Qgis::BrowserItemType::Field, parent, field.name(), parent->path() + '/' + field.name(), parent->providerKey() )
146  , mField( field )
147 {
148  // Precondition
149  Q_ASSERT( static_cast<QgsFieldsItem *>( parent ) );
151  const auto constraints { field.constraints().constraints() };
152  QStringList constraintsText;
153  if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
154  {
155  constraintsText.push_back( tr( "NOT NULL" ) );
156  }
157  if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
158  {
159  constraintsText.push_back( tr( "UNIQUE" ) );
160  }
161  if ( ! constraintsText.isEmpty() )
162  {
163  setToolTip( QStringLiteral( "<ul><li>%1</li></ul>" ).arg( constraintsText.join( QLatin1String( "</li><li>" ) ) ) );
164  }
165 }
166 
168 {
169 }
170 
172 {
173  // Check if this is a geometry column and show the right icon
174  QgsFieldsItem *parentFields { static_cast<QgsFieldsItem *>( parent() ) };
175  if ( parentFields && parentFields->tableProperty() &&
176  parentFields->tableProperty()->geometryColumn() == mName &&
177  !parentFields->tableProperty()->geometryColumnTypes().isEmpty() )
178  {
179  if ( mField.typeName() == QLatin1String( "raster" ) )
180  {
181  return QgsIconUtils::iconRaster();
182  }
183  const QgsWkbTypes::GeometryType geomType { QgsWkbTypes::geometryType( parentFields->tableProperty()->geometryColumnTypes().first().wkbType ) };
184  switch ( geomType )
185  {
186  case QgsWkbTypes::GeometryType::LineGeometry:
187  return QgsIconUtils::iconLine();
188  case QgsWkbTypes::GeometryType::PointGeometry:
189  return QgsIconUtils::iconPoint();
190  case QgsWkbTypes::GeometryType::PolygonGeometry:
191  return QgsIconUtils::iconPolygon();
192  case QgsWkbTypes::GeometryType::UnknownGeometry:
194  case QgsWkbTypes::GeometryType::NullGeometry:
196  }
197  }
198  const QIcon icon { QgsFields::iconForFieldType( mField.type(), mField.subType() ) };
199  // Try subtype if icon is null
200  if ( icon.isNull() )
201  {
202  return QgsFields::iconForFieldType( mField.subType() );
203  }
204  return icon;
205 }
206 
207 bool QgsFieldItem::equal( const QgsDataItem *other )
208 {
209  if ( type() != other->type() )
210  {
211  return false;
212  }
213 
214  const QgsFieldItem *o = qobject_cast<const QgsFieldItem *>( other );
215  if ( !o )
216  return false;
217 
218  return ( mPath == o->mPath && mName == o->mName && mField == o->mField );
219 }
220 
QgsIconUtils::iconRaster
static QIcon iconRaster()
Returns an icon representing raster layers.
Definition: qgsiconutils.cpp:69
QgsFieldItem::QgsFieldItem
QgsFieldItem(QgsDataItem *parent, const QgsField &field)
Constructor for QgsFieldItem, with the specified parent item and field.
Definition: qgsfieldsitem.cpp:144
QgsFieldsItem::layer
QgsVectorLayer * layer()
Creates and returns a (possibly NULL) layer from the connection URI and schema/table information.
Definition: qgsfieldsitem.cpp:97
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
QgsIconUtils::iconPolygon
static QIcon iconPolygon()
Returns an icon representing polygon geometries.
Definition: qgsiconutils.cpp:54
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFieldItem::~QgsFieldItem
~QgsFieldItem() override
Definition: qgsfieldsitem.cpp:167
QgsIconUtils::iconLine
static QIcon iconLine()
Returns an icon representing line geometries.
Definition: qgsiconutils.cpp:49
QgsDataItem::setToolTip
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QgsField::typeName
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:139
QgsFieldItem
A layer field item, information about the connection URI, the schema and the table as well as the lay...
Definition: qgsfieldsitem.h:113
QgsFieldsItem::tableName
QString tableName() const
Returns the table name.
Definition: qgsfieldsitem.cpp:134
QgsIconUtils::iconDefaultLayer
static QIcon iconDefaultLayer()
Returns a default icon for layers, which aren't the standard raster/vector/...
Definition: qgsiconutils.cpp:89
field
const QgsField & field
Definition: qgsfield.h:463
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsFieldsItem
A collection of field items with some internal logic to retrieve the fields and a the vector layer in...
Definition: qgsfieldsitem.h:33
QgsDataItem::mCapabilities
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QgsAbstractDatabaseProviderConnection::TableProperty
The TableProperty class represents a database table or view.
Definition: qgsabstractdatabaseproviderconnection.h:265
QgsFieldsItem::tableProperty
QgsAbstractDatabaseProviderConnection::TableProperty * tableProperty() const
Returns the (possibly NULL) properties of the table this fields belong to.
Definition: qgsfieldsitem.cpp:129
qgsapplication.h
Qgis::BrowserItemCapability::Collapse
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
qgsprovidermetadata.h
QgsFieldItem::equal
bool equal(const QgsDataItem *other) override
Returns true if this item is equal to another item (by testing item type and path).
Definition: qgsfieldsitem.cpp:207
qgsproviderregistry.h
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
QgsFieldConstraints::constraints
Constraints constraints
Definition: qgsfieldconstraints.h:36
QgsException::what
QString what() const
Definition: qgsexception.h:48
Qgis::BrowserItemCapability::RefreshChildrenWhenItemIsRefreshed
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn (since QGIS 3....
Qgis::BrowserItemState::Populated
@ Populated
Children created.
QgsDataItem::setState
virtual void setState(Qgis::BrowserItemState state)
Set item state.
Definition: qgsdataitem.cpp:557
qgsfieldsitem.h
QgsIconUtils::iconPoint
static QIcon iconPoint()
Returns an icon representing point geometries.
Definition: qgsiconutils.cpp:44
QgsProviderRegistry::providerMetadata
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Definition: qgsproviderregistry.cpp:873
QgsDataItem::mName
QString mName
Definition: qgsdataitem.h:449
QgsFieldsItem::~QgsFieldsItem
~QgsFieldsItem() override
Definition: qgsfieldsitem.cpp:53
QgsIconUtils::iconGeometryCollection
static QIcon iconGeometryCollection()
Returns an icon representing geometry collections.
Definition: qgsiconutils.cpp:59
QgsProviderConnectionException
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
QgsErrorItem
Data item that can be used to report problems (e.g. network error)
Definition: qgsdataitem.h:540
QgsProviderMetadata
Holds data provider key, description, and associated shared library file or function pointer informat...
Definition: qgsprovidermetadata.h:177
QgsFields::iconForFieldType
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns an icon corresponding to a field type.
Definition: qgsfields.cpp:294
qgsvectorlayer.h
QgsField::subType
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:134
QgsFieldsItem::schema
QString schema() const
Returns the schema name.
Definition: qgsfieldsitem.cpp:139
QgsDataItem::mPath
QString mPath
Definition: qgsdataitem.h:455
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsiconutils.h
QgsField::constraints
QgsFieldConstraints constraints
Definition: qgsfield.h:63
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
qgslogger.h
QgsFieldItem::icon
QIcon icon() override
Definition: qgsfieldsitem.cpp:171
QgsFieldsItem::QgsFieldsItem
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.
Definition: qgsfieldsitem.cpp:26
QgsDataItem::providerKey
QString providerKey() const
Returns the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:383
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
QgsFieldItem::field
QgsField field() const
Returns the field definition.
Definition: qgsfieldsitem.h:142
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
QgsDataItem::setSortKey
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
Definition: qgsdataitem.cpp:101
QgsFieldsItem::createChildren
QVector< QgsDataItem * > createChildren() override
Create children.
Definition: qgsfieldsitem.cpp:58
QgsDataItem::type
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QgsFieldsItem::icon
QIcon icon() override
Definition: qgsfieldsitem.cpp:87
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44
Qgis::BrowserItemCapability::Fertile
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
QgsField::type
QVariant::Type type
Definition: qgsfield.h:58
QgsFieldsItem::connectionUri
QString connectionUri() const
Returns the connection URI.
Definition: qgsfieldsitem.cpp:92
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50