QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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#include "moc_qgsdatacollectionitem.cpp"
20#include "qgsapplication.h"
22#include "qgsprovidermetadata.h"
23#include "qgsproviderregistry.h"
25#include "qgslogger.h"
26
27#include <QRegularExpression>
28
30 const QString &name,
31 const QString &path,
32 const QString &providerKey )
33 : QgsDataItem( Qgis::BrowserItemType::Collection, parent, name, path, providerKey )
34{
36 mIconName = QStringLiteral( "/mIconDbSchema.svg" );
37}
38
40{
41 QgsDebugMsgLevel( "mName = " + mName + " mPath = " + mPath, 2 );
42
43// Do not delete children, children are deleted by QObject parent
44#if 0
45 const auto constMChildren = mChildren;
46 for ( QgsDataItem *i : constMChildren )
47 {
48 QgsDebugMsgLevel( QStringLiteral( "delete child = 0x%0" ).arg( static_cast<qlonglong>( i ), 8, 16, QLatin1Char( '0' ) ), 2 );
49 delete i;
50 }
51#endif
52}
53
55{
56 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconDbSchema.svg" ) );
57}
58
59QIcon QgsDataCollectionItem::openDirIcon( const QColor &fillColor, const QColor &strokeColor )
60{
61 return fillColor.isValid() || strokeColor.isValid()
62 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpenParams.svg" ), fillColor, strokeColor )
63 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpen.svg" ) );
64}
65
66QIcon QgsDataCollectionItem::homeDirIcon( const QColor &fillColor, const QColor &strokeColor )
67{
68 return fillColor.isValid() || strokeColor.isValid()
69 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHomeParams.svg" ), fillColor, strokeColor )
70 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHome.svg" ) );
71}
72
74{
75 const QString dataProviderKey { QgsApplication::dataItemProviderRegistry()->dataProviderKey( providerKey() ) };
77
78 if ( ! md )
79 {
80 return nullptr;
81 }
82
83 const QString connectionName { name() };
84
85 try
86 {
87 // First try to retrieve the connection by name if this is a stored connection
88 if ( md->findConnection( connectionName ) )
89 {
90 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionName ) );
91 }
92
93 // If that fails, try to create a connection from the path, in case this is a
94 // filesystem-based DB (gpkg or spatialite)
95 // The name is useless, we need to get the file path from the data item path
96 const QString databaseFilePath { path().remove( QRegularExpression( R"re([\aZ]{2,}://)re" ) ) };
97
98 if ( QFile::exists( databaseFilePath ) )
99 {
100 return static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( databaseFilePath, {} ) );
101 }
102 }
104 {
105 // This is expected and it is not an error in case the provider does not implement
106 // the connections API
107 }
108 return nullptr;
109}
110
111QIcon QgsDataCollectionItem::iconDir( const QColor &fillColor, const QColor &strokeColor )
112{
113 return fillColor.isValid() || strokeColor.isValid()
114 ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), fillColor, strokeColor )
115 : QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolder.svg" ) );
116}
117
118
119
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ 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...
virtual void remove(const QString &name) const =0
Deletes the connection from the settings.
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
QVector< QgsDataItem * > mChildren
QString mPath
Qgis::BrowserItemCapabilities mCapabilities
QString mIconName
QString name() const
Returns the name of the item (the displayed text for the item).
QString path() const
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...
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