QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsfielddomainsitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfielddomainsitem.h
3  -------------------
4  begin : 2022-01-27
5  copyright : (C) 2022 Nyall Dawson
6  email : nyall dot dawson 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 "qgsfielddomainsitem.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 #include "qgsfielddomain.h"
26 #include "qgsmessagelog.h"
27 
29  const QString &path,
30  const QString &connectionUri,
31  const QString &providerKey )
32  : QgsDataItem( Qgis::BrowserItemType::Custom, parent, tr( "Field Domains" ), path, providerKey )
33  , mConnectionUri( connectionUri )
34 {
36 }
37 
39 
40 QVector<QgsDataItem *> QgsFieldDomainsItem::createChildren()
41 {
42  QVector<QgsDataItem *> children;
43  try
44  {
46  if ( md )
47  {
48  std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
49  if ( conn && ( conn->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::RetrieveFieldDomain ) )
50  {
51  QString domainError;
52  QStringList fieldDomains;
53  try
54  {
55  fieldDomains = conn->fieldDomainNames();
56  }
57  catch ( QgsProviderConnectionException &ex )
58  {
59  domainError = ex.what();
60  }
61 
62  for ( const QString &name : std::as_const( fieldDomains ) )
63  {
64  try
65  {
66  std::unique_ptr< QgsFieldDomain > domain( conn->fieldDomain( name ) );
67  QgsFieldDomainItem *fieldDomainItem { new QgsFieldDomainItem( this, domain.release() ) };
68  children.push_back( fieldDomainItem );
69  }
70  catch ( QgsProviderConnectionException &ex )
71  {
73  }
74  }
75 
76  if ( !domainError.isEmpty() )
77  {
78  children.push_back( new QgsErrorItem( this, domainError, path() + QStringLiteral( "/domainerror" ) ) );
79  }
80  }
81  }
82  }
83  catch ( const QgsProviderConnectionException &ex )
84  {
85  children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
86  }
87  return children;
88 }
89 
91 {
92  return QgsApplication::getThemeIcon( QStringLiteral( "mSourceFields.svg" ) );
93 }
94 
96 {
97  return mConnectionUri;
98 }
99 
100 //
101 // QgsFieldDomainItem
102 //
103 
105  : QgsDataItem( Qgis::BrowserItemType::Custom, parent, domain->name(), parent->path() + '/' + domain->name(), parent->providerKey() )
106  , mDomain( domain )
107 {
108  // Precondition
109  Q_ASSERT( dynamic_cast<QgsFieldDomainsItem *>( parent ) );
111  setToolTip( domain->description().isEmpty() ? domain->name() : domain->description() );
112 }
113 
115 {
116  switch ( mDomain->type() )
117  {
119  return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldText.svg" ) );
121  return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) );
123  return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldText.svg" ) );
124  }
126 }
127 
129 {
130  return mDomain.get();
131 }
132 
134 
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
Qgis::FieldDomainType::Glob
@ Glob
Glob string pattern field domain.
QgsFieldDomainItem::fieldDomain
const QgsFieldDomain * fieldDomain()
Returns the associated field domain.
Definition: qgsfielddomainsitem.cpp:128
QgsFieldDomain::name
QString name() const
Returns the name of the field domain.
Definition: qgsfielddomain.h:88
QgsFieldDomainsItem::createChildren
QVector< QgsDataItem * > createChildren() override
Create children.
Definition: qgsfielddomainsitem.cpp:40
QgsDataItem::setToolTip
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:345
QgsDataItem::mCapabilities
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QgsFieldDomainsItem::~QgsFieldDomainsItem
~QgsFieldDomainsItem() override
QgsFieldDomainItem::~QgsFieldDomainItem
~QgsFieldDomainItem() override
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
qgsproviderregistry.h
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
QgsFieldDomainsItem
Contains a collection of field domain items.
Definition: qgsfielddomainsitem.h:31
qgsfielddomain.h
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.
qgsfielddomainsitem.h
QgsFieldDomainsItem::connectionUri
QString connectionUri() const
Returns the connection URI.
Definition: qgsfielddomainsitem.cpp:95
QgsDataItem::setState
virtual void setState(Qgis::BrowserItemState state)
Set item state.
Definition: qgsdataitem.cpp:557
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
BUILTIN_UNREACHABLE
#define BUILTIN_UNREACHABLE
Definition: qgis.h:2907
QgsAbstractDatabaseProviderConnection::fieldDomainNames
virtual QStringList fieldDomainNames() const SIP_THROW(QgsProviderConnectionException)
Returns a list of field domain names present on the provider.
Definition: qgsabstractdatabaseproviderconnection.cpp:1262
QgsProviderRegistry::providerMetadata
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Definition: qgsproviderregistry.cpp:873
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
qgsvectorlayer.h
Qgis::FieldDomainType::Range
@ Range
Numeric range field domain (min/max)
QgsFieldDomainItem
A browser item representing a field domain.
Definition: qgsfielddomainsitem.h:83
qgsiconutils.h
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
QgsFieldDomain::description
QString description() const
Returns the description of the field domain.
Definition: qgsfielddomain.h:102
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
QgsFieldDomain
Base class for field domains.
Definition: qgsfielddomain.h:33
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
QgsDataItem::providerKey
QString providerKey() const
Returns the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:383
QgsFieldDomainItem::QgsFieldDomainItem
QgsFieldDomainItem(QgsDataItem *parent, QgsFieldDomain *domain)
Constructor for QgsFieldDomainItem, with the specified parent item and domain.
Definition: qgsfielddomainsitem.cpp:104
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
QgsFieldDomainsItem::QgsFieldDomainsItem
QgsFieldDomainsItem(QgsDataItem *parent, const QString &path, const QString &connectionUri, const QString &providerKey)
Constructor for QgsFieldDomainsItem, with the specified parent item.
Definition: qgsfielddomainsitem.cpp:28
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
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,...
QgsFieldDomainsItem::icon
QIcon icon() override
Definition: qgsfielddomainsitem.cpp:90
Qgis::FieldDomainType::Coded
@ Coded
Coded field domain.
qgsmessagelog.h
QgsFieldDomainItem::icon
QIcon icon() override
Definition: qgsfielddomainsitem.cpp:114