QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
21#include "qgsapplication.h"
23#include "qgslogger.h"
24#include "qgsprovidermetadata.h"
25#include "qgsproviderregistry.h"
26
27#include <QRegularExpression>
28
29#include "moc_qgsdatacollectionitem.cpp"
30
32 const QString &name,
33 const QString &path,
34 const QString &providerKey )
35 : QgsDataItem( Qgis::BrowserItemType::Collection, parent, name, path, providerKey )
36{
38 mIconName = QStringLiteral( "/mIconDbSchema.svg" );
39}
40
42{
43 QgsDebugMsgLevel( "mName = " + mName + " mPath = " + mPath, 2 );
44
45// Do not delete children, children are deleted by QObject parent
46#if 0
47 const auto constMChildren = mChildren;
48 for ( QgsDataItem *i : constMChildren )
49 {
50 QgsDebugMsgLevel( QStringLiteral( "delete child = 0x%0" ).arg( static_cast<qlonglong>( i ), 8, 16, QLatin1Char( '0' ) ), 2 );
51 delete i;
52 }
53#endif
54}
55
57{
58 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconDbSchema.svg" ) );
59}
60
61QIcon QgsDataCollectionItem::openDirIcon( const QColor &fillColor, const QColor &strokeColor )
62{
63 return fillColor.isValid() || strokeColor.isValid()
64 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpenParams.svg" ), fillColor, strokeColor )
65 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpen.svg" ) );
66}
67
68QIcon QgsDataCollectionItem::homeDirIcon( const QColor &fillColor, const QColor &strokeColor )
69{
70 return fillColor.isValid() || strokeColor.isValid()
71 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHomeParams.svg" ), fillColor, strokeColor )
72 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHome.svg" ) );
73}
74
76{
77 const QString dataProviderKey { QgsApplication::dataItemProviderRegistry()->dataProviderKey( providerKey() ) };
79
80 if ( ! md )
81 {
82 return nullptr;
83 }
84
85 const QString connectionName { name() };
86
87 try
88 {
89 // First try to retrieve the connection by name if this is a stored connection
90 if ( md->findConnection( connectionName ) )
91 {
92 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) );
93 }
94
95 // If that fails, try to create a connection from the path, in case this is a
96 // filesystem-based DB (gpkg or spatialite)
97 // The name is useless, we need to get the file path from the data item path
98 const QString databaseFilePath { path().remove( QRegularExpression( R"re([\aZ]{2,}://)re" ) ) };
99
100 if ( QFile::exists( databaseFilePath ) )
101 {
102 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( databaseFilePath, {} ) );
103 }
104 }
106 {
107 // This is expected and it is not an error in case the provider does not implement
108 // the connections API
109 }
110 return nullptr;
111}
112
113QIcon QgsDataCollectionItem::iconDir( const QColor &fillColor, const QColor &strokeColor )
114{
115 return fillColor.isValid() || strokeColor.isValid()
116 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), fillColor, strokeColor )
117 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolder.svg" ) );
118}
119
120
121
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
Definition qgis.h:954
Provides common functionality for database based connections.
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.
QString mName
QVector< QgsDataItem * > mChildren
QString mPath
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 mIconName
QString name() const
Returns the name of the item (the displayed text for the item).
QString path() const
QgsDataItem * parent() const
Gets item parent.
QString providerKey() const
Returns the provider key that created this item (e.g.
Custom exception class for provider connection related exceptions.
Holds data provider key, description, and associated shared library file or function pointer informat...
QgsAbstractProviderConnection * findConnection(const QString &name, bool cached=true)
Searches and returns a (possibly nullptr) connection from the stored provider connections.
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.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61