QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
qgsrelationshipsitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrelationshipsitem.cpp
3 -------------------
4 begin : 2022-07-28
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
19#include "qgsproviderregistry.h"
20#include "qgsprovidermetadata.h"
21#include "qgsapplication.h"
22#include "qgsmessagelog.h"
24
26 const QString &path,
27 const QString &connectionUri,
28 const QString &providerKey, const QString &schema, const QString &tableName )
29 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, tr( "Relationships" ), path, providerKey )
30 , mConnectionUri( connectionUri )
31 , mSchema( schema )
32 , mTableName( tableName )
33{
35}
36
38
40{
41 QVector<QgsDataItem *> children;
42 try
43 {
45 if ( md )
46 {
47 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
48 if ( conn && ( conn->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::RetrieveRelationships ) )
49 {
50 QString relationError;
51 QList< QgsWeakRelation > relations;
52 try
53 {
54 relations = conn->relationships( mSchema, mTableName );
55 }
57 {
58 relationError = ex.what();
59 }
60
61 for ( const QgsWeakRelation &relation : std::as_const( relations ) )
62 {
63 try
64 {
65 QgsRelationshipItem *relationshipItem = new QgsRelationshipItem( this, relation );
66 children.push_back( relationshipItem );
67 }
69 {
71 }
72 }
73
74 if ( !relationError.isEmpty() )
75 {
76 children.push_back( new QgsErrorItem( this, relationError, path() + QStringLiteral( "/relationerror" ) ) );
77 }
78 }
79 }
80 }
81 catch ( const QgsProviderConnectionException &ex )
82 {
83 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
84 }
85 return children;
86}
87
89{
90 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
91}
92
94{
95 return mConnectionUri;
96}
97
98//
99// QgsRelationshipItem
100//
101
103 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, relation.name(), parent->path() + '/' + relation.name(), parent->providerKey() )
104 , mRelation( relation )
105{
106 // Precondition
107 Q_ASSERT( dynamic_cast<QgsRelationshipsItem *>( parent ) );
109 setToolTip( mRelation.name() );
110}
111
113{
114 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
115}
116
118{
119 return mRelation;
120}
121
123
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
@ 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,...
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn (since QGIS 3....
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
virtual QList< QgsWeakRelation > relationships(const QString &schema=QString(), const QString &tableName=QString()) const SIP_THROW(QgsProviderConnectionException)
Returns a list of relationships detected in the database.
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 setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QString path() const
Definition: qgsdataitem.h:354
virtual void setState(Qgis::BrowserItemState state)
Set item state.
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
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
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).
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.
A browser item representing a relationship.
QgsRelationshipItem(QgsDataItem *parent, const QgsWeakRelation &relation)
Constructor for QgsRelationshipItem, with the specified parent item and relation.
const QgsWeakRelation & relation() const
Returns the associated relationship.
~QgsRelationshipItem() override
Contains a collection of relationship items.
~QgsRelationshipsItem() override
QString connectionUri() const
Returns the connection URI.
QVector< QgsDataItem * > createChildren() override
Create children.
QgsRelationshipsItem(QgsDataItem *parent, const QString &path, const QString &connectionUri, const QString &providerKey, const QString &schema=QString(), const QString &tableName=QString())
Constructor for QgsRelationshipsItem, with the specified parent item.
The QgsWeakRelation class represent a QgsRelation with possibly unresolved layers or unmatched fields...
QString name() const
Returns the relationship's name.