QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
project
qgsprojectbadlayerhandler.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsprojectbadlayerhandler.cpp - QgsProjectBadLayerHandler
3
4
---------------------
5
begin : 22.10.2016
6
copyright : (C) 2016 by Matthias Kuhn
7
email : matthias@opengis.ch
8
***************************************************************************
9
* *
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
14
* *
15
***************************************************************************/
16
#include "
qgsprojectbadlayerhandler.h
"
17
18
#include "
qgsapplication.h
"
19
#include "
qgsdatasourceuri.h
"
20
#include "
qgslogger.h
"
21
#include "
qgsmessagelog.h
"
22
23
#include <QFileInfo>
24
#include <QString>
25
26
using namespace
Qt::StringLiterals;
27
28
void
QgsProjectBadLayerHandler::handleBadLayers
(
const
QList<QDomNode> &layers )
29
{
30
if
( !layers.empty() )
31
QgsMessageLog::logMessage
( QObject::tr(
"%n unavailable layer(s) found:"
,
nullptr
, layers.size() ) );
32
33
for
(
const
QDomNode &layer : layers )
34
{
35
QgsMessageLog::logMessage
( QObject::tr(
" * %1"
).arg(
QgsDataSourceUri::removePassword
(
dataSource
( layer ),
true
) ) );
36
}
37
}
38
39
QgsProjectBadLayerHandler::DataType
QgsProjectBadLayerHandler::dataType
(
const
QDomNode &layerNode )
40
{
41
const
QString type = layerNode.toElement().attribute( u
"type"
_s );
42
43
if
( type.isNull() )
44
{
45
QgsDebugError
( u
"cannot find ``type'' attribute"
_s );
46
47
return
IS_BOGUS
;
48
}
49
50
if
(
"raster"
== type )
51
{
52
QgsDebugMsgLevel
( u
"is a raster"
_s, 2 );
53
54
return
IS_RASTER
;
55
}
56
else
if
(
"vector"
== type )
57
{
58
QgsDebugMsgLevel
( u
"is a vector"
_s, 2 );
59
60
return
IS_VECTOR
;
61
}
62
63
QgsDebugMsgLevel
(
"is unknown type "
+ type, 2 );
64
65
return
IS_BOGUS
;
66
}
67
68
QString
QgsProjectBadLayerHandler::dataSource
(
const
QDomNode &layerNode )
69
{
70
const
QDomNode dataSourceNode = layerNode.namedItem( u
"datasource"
_s );
71
72
if
( dataSourceNode.isNull() )
73
{
74
QgsDebugError
( u
"cannot find datasource node"
_s );
75
76
return
QString();
77
}
78
79
return
dataSourceNode.toElement().text();
80
}
81
82
QgsProjectBadLayerHandler::ProviderType
QgsProjectBadLayerHandler::providerType
(
const
QDomNode &layerNode )
83
{
84
// XXX but what about rasters that can be URLs? _Can_ they be URLs?
85
86
switch
(
dataType
( layerNode ) )
87
{
88
case
IS_VECTOR
:
89
{
90
const
QString ds =
dataSource
( layerNode );
91
92
QgsDebugMsgLevel
(
"datasource is "
+ ds, 2 );
93
94
if
( ds.contains(
"host="
_L1 ) )
95
{
96
return
IS_URL
;
97
}
98
else
if
( ds.contains(
"dbname="
_L1 ) )
99
{
100
return
IS_DATABASE
;
101
}
102
// be default, then, this should be a file based layer data source
103
// XXX is this a reasonable assumption?
104
105
return
IS_FILE
;
106
}
107
108
case
IS_RASTER
:
// rasters are currently only accessed as
109
// physical files
110
return
IS_FILE
;
111
112
default
:
113
QgsDebugError
( u
"unknown ``type'' attribute"
_s );
114
}
115
116
return
IS_Unknown
;
117
}
118
119
void
QgsProjectBadLayerHandler::setDataSource
( QDomNode &layerNode,
const
QString &
dataSource
)
120
{
121
const
QDomNode dataSourceNode = layerNode.namedItem( u
"datasource"
_s );
122
const
QDomElement dataSourceElement = dataSourceNode.toElement();
123
QDomText dataSourceText = dataSourceElement.firstChild().toText();
124
125
QgsDebugMsgLevel
(
"datasource changed from "
+ dataSourceText.data(), 2 );
126
127
dataSourceText.setData(
dataSource
);
128
129
QgsDebugMsgLevel
(
"to "
+ dataSourceText.data(), 2 );
130
}
QgsDataSourceUri::removePassword
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
Definition
qgsdatasourceuri.cpp:239
QgsMessageLog::logMessage
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Definition
qgsmessagelog.cpp:35
QgsProjectBadLayerHandler::setDataSource
void setDataSource(QDomNode &layerNode, const QString &dataSource)
Set the datasource element to the new value.
Definition
qgsprojectbadlayerhandler.cpp:119
QgsProjectBadLayerHandler::ProviderType
ProviderType
the flavors for data storage
Definition
qgsprojectbadlayerhandler.h:55
QgsProjectBadLayerHandler::IS_FILE
@ IS_FILE
Saved in a file.
Definition
qgsprojectbadlayerhandler.h:56
QgsProjectBadLayerHandler::IS_Unknown
@ IS_Unknown
Unknown type.
Definition
qgsprojectbadlayerhandler.h:59
QgsProjectBadLayerHandler::IS_DATABASE
@ IS_DATABASE
Saved in a database.
Definition
qgsprojectbadlayerhandler.h:57
QgsProjectBadLayerHandler::IS_URL
@ IS_URL
Retrieved from a URL.
Definition
qgsprojectbadlayerhandler.h:58
QgsProjectBadLayerHandler::handleBadLayers
virtual void handleBadLayers(const QList< QDomNode > &layers)
This method will be called whenever the project tries to load layers which cannot be accessed.
Definition
qgsprojectbadlayerhandler.cpp:28
QgsProjectBadLayerHandler::providerType
ProviderType providerType(const QDomNode &layerNode)
Returns the physical storage type associated with the given layer.
Definition
qgsprojectbadlayerhandler.cpp:82
QgsProjectBadLayerHandler::dataSource
QString dataSource(const QDomNode &layerNode)
Returns the data source for the given layer.
Definition
qgsprojectbadlayerhandler.cpp:68
QgsProjectBadLayerHandler::dataType
DataType dataType(const QDomNode &layerNode)
Returns data type associated with the given QgsProject file Dom node.
Definition
qgsprojectbadlayerhandler.cpp:39
QgsProjectBadLayerHandler::DataType
DataType
file data representation
Definition
qgsprojectbadlayerhandler.h:47
QgsProjectBadLayerHandler::IS_VECTOR
@ IS_VECTOR
Vector data.
Definition
qgsprojectbadlayerhandler.h:48
QgsProjectBadLayerHandler::IS_RASTER
@ IS_RASTER
Raster data.
Definition
qgsprojectbadlayerhandler.h:49
QgsProjectBadLayerHandler::IS_BOGUS
@ IS_BOGUS
Bogus data.
Definition
qgsprojectbadlayerhandler.h:50
qgsapplication.h
qgsdatasourceuri.h
qgslogger.h
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition
qgslogger.h:80
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgsmessagelog.h
qgsprojectbadlayerhandler.h
Generated on
for QGIS API Documentation by
1.17.0