QGIS API Documentation 3.27.0-Master (9c08adf5ef)
qgsdatacollectionitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatacollectionitem.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
19#include "qgsapplication.h"
21#include "qgsprovidermetadata.h"
22#include "qgsproviderregistry.h"
24#include <QRegularExpression>
25
27 const QString &name,
28 const QString &path,
29 const QString &providerKey )
30 : QgsDataItem( Qgis::BrowserItemType::Collection, parent, name, path, providerKey )
31{
33 mIconName = QStringLiteral( "/mIconDbSchema.svg" );
34}
35
37{
38 QgsDebugMsgLevel( "mName = " + mName + " mPath = " + mPath, 2 );
39
40// Do not delete children, children are deleted by QObject parent
41#if 0
42 const auto constMChildren = mChildren;
43 for ( QgsDataItem *i : constMChildren )
44 {
45 QgsDebugMsgLevel( QStringLiteral( "delete child = 0x%0" ).arg( static_cast<qlonglong>( i ), 8, 16, QLatin1Char( '0' ) ), 2 );
46 delete i;
47 }
48#endif
49}
50
52{
53 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconDbSchema.svg" ) );
54}
55
56QIcon QgsDataCollectionItem::openDirIcon( const QColor &fillColor, const QColor &strokeColor )
57{
58 return fillColor.isValid() || strokeColor.isValid()
59 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpenParams.svg" ), fillColor, strokeColor )
60 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpen.svg" ) );
61}
62
63QIcon QgsDataCollectionItem::homeDirIcon( const QColor &fillColor, const QColor &strokeColor )
64{
65 return fillColor.isValid() || strokeColor.isValid()
66 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHomeParams.svg" ), fillColor, strokeColor )
67 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHome.svg" ) );
68}
69
71{
72 const QString dataProviderKey { QgsApplication::dataItemProviderRegistry()->dataProviderKey( providerKey() ) };
74
75 if ( ! md )
76 {
77 return nullptr;
78 }
79
80 const QString connectionName { name() };
81
82 try
83 {
84 // First try to retrieve the connection by name if this is a stored connection
85 if ( md->findConnection( connectionName ) )
86 {
87 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) );
88 }
89
90 // If that fails, try to create a connection from the path, in case this is a
91 // filesystem-based DB (gpkg or spatialite)
92 // The name is useless, we need to get the file path from the data item path
93 const QString databaseFilePath { path().remove( QRegularExpression( R"re([\aZ]{2,}://)re" ) ) };
94
95 if ( QFile::exists( databaseFilePath ) )
96 {
97 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( databaseFilePath, {} ) );
98 }
99 }
101 {
102 // This is expected and it is not an error in case the provider does not implement
103 // the connections API
104 }
105 return nullptr;
106}
107
108QIcon QgsDataCollectionItem::iconDir( const QColor &fillColor, const QColor &strokeColor )
109{
110 return fillColor.isValid() || strokeColor.isValid()
111 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), fillColor, strokeColor )
112 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolder.svg" ) );
113}
114
115
116
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
@ 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 QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QIcon homeDirIcon(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Shared home directory icon.
QgsDataCollectionItem(QgsDataItem *parent, const QString &name, const QString &path=QString(), const QString &providerKey=QString())
Constructor for QgsDataCollectionItem, with the specified parent item.
static QIcon iconDir(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Returns the standard browser directory icon.
static QIcon openDirIcon(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Shared open directory icon.
QgsAbstractDatabaseProviderConnection * databaseConnection() const override
For data items that represent a DB connection or one of its children, this method returns a connectio...
static QIcon iconDataCollection()
Returns the standard browser data collection icon.
QString dataProviderKey(const QString &dataItemProviderName)
Returns the (possibly blank) data provider key for a given data item provider name.
Base class for all items in the model.
Definition: qgsdataitem.h:46
QString mName
Definition: qgsdataitem.h:449
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:447
QString mPath
Definition: qgsdataitem.h:455
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QString mIconName
Definition: qgsdataitem.h:457
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:345
QString path() const
Definition: qgsdataitem.h:354
QString providerKey() const
Returns the provider key that created this item (e.g.
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.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39