QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
21#include "qgsapplication.h"
22#include "qgsmessagelog.h"
23#include "qgsprovidermetadata.h"
24#include "qgsproviderregistry.h"
25
26#include "moc_qgsrelationshipsitem.cpp"
27
29 const QString &path,
30 const QString &connectionUri,
31 const QString &providerKey, const QString &schema, const QString &tableName )
32 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, tr( "Relationships" ), path, providerKey )
33 , mConnectionUri( connectionUri )
34 , mSchema( schema )
35 , mTableName( tableName )
36{
38}
39
41
43{
44 QVector<QgsDataItem *> children;
45 try
46 {
48 if ( md )
49 {
50 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( mConnectionUri, {} ) ) };
52 {
53 QString relationError;
54 QList< QgsWeakRelation > relations;
55 try
56 {
57 relations = conn->relationships( mSchema, mTableName );
58 }
60 {
61 relationError = ex.what();
62 }
63
64 for ( const QgsWeakRelation &relation : std::as_const( relations ) )
65 {
66 try
67 {
68 QgsRelationshipItem *relationshipItem = new QgsRelationshipItem( this, relation );
69 children.push_back( relationshipItem );
70 }
72 {
74 }
75 }
76
77 if ( !relationError.isEmpty() )
78 {
79 children.push_back( new QgsErrorItem( this, relationError, path() + QStringLiteral( "/relationerror" ) ) );
80 }
81 }
82 }
83 }
84 catch ( const QgsProviderConnectionException &ex )
85 {
86 children.push_back( new QgsErrorItem( this, ex.what(), path() + QStringLiteral( "/error" ) ) );
87 }
88 return children;
89}
90
92{
93 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
94}
95
97{
98 return mConnectionUri;
99}
100
101//
102// QgsRelationshipItem
103//
104
106 : QgsDataItem( Qgis::BrowserItemType::Custom, parent, relation.name(), parent->path() + '/' + relation.name(), parent->providerKey() )
107 , mRelation( relation )
108{
109 // Precondition
110 Q_ASSERT( dynamic_cast<QgsRelationshipsItem *>( parent ) );
112 setToolTip( mRelation.name() );
113}
114
116{
117 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconBrowserRelations.svg" ) );
118}
119
121{
122 return mRelation;
123}
124
126
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ Populated
Children created.
Definition qgis.h:941
@ Collapse
The collapse/expand status for this items children should be ignored in order to avoid undesired netw...
Definition qgis.h:956
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:954
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
Definition qgis.h:960
Provides common functionality for database based connections.
@ 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.
void setToolTip(const QString &msg)
QVector< QgsDataItem * > children() const
Qgis::BrowserItemCapabilities mCapabilities
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
QString name() const
Returns the name of the item (the displayed text for the item).
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.
A browser 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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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...
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
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.
QString tableName() const
Returns the table name for filtering relationships, if set.
QString schema() const
Returns the schema for filtering relationships, if set.
Represent a QgsRelation with possibly unresolved layers or unmatched fields.