QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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().count() )
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 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:64
@ Populated
Children created.
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Base class for all items in the model.
Definition: qgsdataitem.h:46
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
QString mName
Definition: qgsdataitem.h:449
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QString path() const
Definition: qgsdataitem.h:354
virtual void setState(Qgis::BrowserItemState state)
Set item state.
QString providerKey() const
Returns the provider key that created this item (e.g.
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:541
QString what() const
Definition: qgsexception.h:48
Q_GADGET Constraints constraints
A layer field item, information about the connection URI, the schema and the table as well as the lay...
~QgsFieldItem() override
QgsFieldItem(QgsDataItem *parent, const QgsField &field)
Constructor for QgsFieldItem, with the specified parent item and field.
QIcon icon() override
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:139
QVariant::Type type
Definition: qgsfield.h:58
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:134
QgsFieldConstraints constraints
Definition: qgsfield.h:63
A collection of field items with some internal logic to retrieve the fields and a the vector layer in...
Definition: qgsfieldsitem.h:34
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 NULL) 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 NULL) layer from the connection URI and schema/table information.
~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(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns an icon corresponding to a field type.
Definition: qgsfields.cpp:294
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/...
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.
Definition: qgsexception.h:101
Holds data provider key, description, and associated shared library file or function pointer informat...
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 data sets.
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
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
const QgsField & field
Definition: qgsfield.h:463
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
The TableProperty class represents a database table or view.