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