QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 "moc_qgsrelationshipsitem.cpp"
20#include "qgsproviderregistry.h"
21#include "qgsprovidermetadata.h"
22#include "qgsapplication.h"
23#include "qgsmessagelog.h"
25
27 const QString &path,
28 const QString &connectionUri,
29 const QString &providerKey, const QString &schema, const QString &tableName )
30 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, tr( "Relationships" ), path, providerKey )
31 , mConnectionUri( connectionUri )
32 , mSchema( schema )
33 , mTableName( tableName )
34{
36}
37
39
41{
42 QVector<QgsDataItem *> children;
43 try
44 {
46 if ( md )
47 {
48 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
50 {
51 QString relationError;
52 QList< QgsWeakRelation > relations;
53 try
54 {
55 relations = conn->relationships( mSchema, mTableName );
56 }
58 {
59 relationError = ex.what();
60 }
61
62 for ( const QgsWeakRelation &relation : std::as_const( relations ) )
63 {
64 try
65 {
66 QgsRelationshipItem *relationshipItem = new QgsRelationshipItem( this, relation );
67 children.push_back( relationshipItem );
68 }
70 {
72 }
73 }
74
75 if ( !relationError.isEmpty() )
76 {
77 children.push_back( new QgsErrorItem( this, relationError, path() + QStringLiteral( "/relationerror" ) ) );
78 }
79 }
80 }
81 }
82 catch ( const QgsProviderConnectionException &ex )
83 {
84 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
85 }
86 return children;
87}
88
90{
91 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
92}
93
95{
96 return mConnectionUri;
97}
98
99//
100// QgsRelationshipItem
101//
102
104 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, relation.name(), parent->path() + '/' + relation.name(), parent->providerKey() )
105 , mRelation( relation )
106{
107 // Precondition
108 Q_ASSERT( dynamic_cast<QgsRelationshipsItem *>( parent ) );
110 setToolTip( mRelation.name() );
111}
112
114{
115 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
116}
117
119{
120 return mRelation;
121}
122
124
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ 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.
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
virtual QList< QgsWeakRelation > relationships(const QString &schema=QString(), const QString &tableName=QString()) const
Returns a list of relationships detected in the database.
@ RetrieveRelationships
Can retrieve relationships from 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)
QVector< QgsDataItem * > children() const
Qgis::BrowserItemCapabilities mCapabilities
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.
Data item that can be used to report problems (e.g.
QString what() const
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.
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.