QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
qgshelp.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgshelp.cpp
3
--------------------------------------
4
Date : December 2016
5
Copyright : (C) 2016 by Alexander Bruy
6
Email : alexander dot bruy at gmail dot com
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
16
#include "
qgshelp.h
"
17
18
#include <memory>
19
20
#include "
qgis.h
"
21
#include "
qgsapplication.h
"
22
#include "
qgsblockingnetworkrequest.h
"
23
#include "
qgsexpressioncontext.h
"
24
#include "
qgsexpressioncontextutils.h
"
25
#include "
qgsmessagelog.h
"
26
#include "
qgsnetworkaccessmanager.h
"
27
#include "
qgssetrequestinitiator_p.h
"
28
#include "
qgssettings.h
"
29
30
#include <QDesktopServices>
31
#include <QFileInfo>
32
#include <QNetworkProxy>
33
#include <QNetworkProxyFactory>
34
#include <QRegularExpression>
35
#include <QString>
36
#include <QTcpSocket>
37
#include <QUrl>
38
39
using namespace
Qt::StringLiterals;
40
41
void
QgsHelp::openHelp
(
const
QString &key )
42
{
43
QDesktopServices::openUrl(
QgsHelp::helpUrl
( key ) );
44
}
45
46
QUrl
QgsHelp::helpUrl
(
const
QString &key )
47
{
48
QUrl helpNotFound = QUrl::fromLocalFile(
QgsApplication::pkgDataPath
() +
"/doc/nohelp.html"
);
49
50
const
QgsSettings
settings;
51
const
QStringList paths = settings.
value
( u
"help/helpSearchPath"
_s ).toStringList();
52
if
( paths.isEmpty() )
53
{
54
QgsMessageLog::logMessage
( QObject::tr(
"Help location is not configured!"
), QObject::tr(
"QGIS Help"
) );
55
return
helpNotFound;
56
}
57
58
std::unique_ptr<QgsExpressionContextScope> scope(
QgsExpressionContextUtils::globalScope
() );
59
60
QUrl
helpUrl
;
61
QString helpPath, fullPath;
62
bool
helpFound =
false
;
63
64
const
auto
constPaths = paths;
65
for
(
const
QString &path : constPaths )
66
{
67
if
( path.endsWith(
"\\"
_L1 ) || path.endsWith(
'/'
_L1 ) )
68
{
69
fullPath = path.left( path.size() - 1 );
70
}
71
else
72
{
73
fullPath = path;
74
}
75
76
const
auto
constVariableNames = scope->variableNames();
77
for
(
const
QString &var : constVariableNames )
78
{
79
const
QRegularExpression rx( u
"(<!\\$\\$)*(\\$%1)"
_s.arg( var ) );
80
fullPath.replace( rx, scope->variable( var ).toString() );
81
}
82
const
thread_local
QRegularExpression pathRx( u
"(\\$\\$)"
_s );
83
fullPath.replace( pathRx, u
"$"
_s );
84
85
helpPath = u
"%1/%2"
_s.arg( fullPath, key );
86
87
QgsMessageLog::logMessage
( QObject::tr(
"Trying to open help using key '%1'. Full URI is '%2'…"
).arg( key ).arg( helpPath ), QObject::tr(
"QGIS Help"
),
Qgis::MessageLevel::Info
);
88
89
if
( helpPath.startsWith(
"http"
_L1 ) )
90
{
91
if
( !QgsHelp::urlExists( helpPath ) )
92
{
93
continue
;
94
}
95
helpUrl
= QUrl( helpPath );
96
}
97
else
98
{
99
const
QString filePath = helpPath.mid( 0, helpPath.lastIndexOf(
'#'
_L1 ) );
100
if
( !QFileInfo::exists( filePath ) )
101
{
102
continue
;
103
}
104
helpUrl
= QUrl::fromLocalFile( filePath );
105
const
int
pos = helpPath.lastIndexOf(
'#'
_L1 );
106
if
( pos != -1 )
107
{
108
helpUrl
.setFragment( helpPath.mid( helpPath.lastIndexOf(
'#'
_L1 ) + 1, -1 ) );
109
}
110
}
111
112
helpFound =
true
;
113
break
;
114
}
115
116
return
helpFound ?
helpUrl
: helpNotFound;
117
}
118
119
bool
QgsHelp::urlExists(
const
QString &url )
120
{
121
const
QUrl
helpUrl
( url );
122
123
QgsBlockingNetworkRequest
request;
124
QNetworkRequest req(
helpUrl
);
125
QgsSetRequestInitiatorClass
( req, u
"QgsHelp"
_s );
126
127
QgsBlockingNetworkRequest::ErrorCode
errCode = request.
head
( req );
128
return
errCode ==
QgsBlockingNetworkRequest::NoError
;
129
}
Qgis::Info
@ Info
Information message.
Definition
qgis.h:161
QgsApplication::pkgDataPath
static QString pkgDataPath()
Returns the common root path of all application data directories.
Definition
qgsapplication.cpp:856
QgsBlockingNetworkRequest
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
Definition
qgsblockingnetworkrequest.h:51
QgsBlockingNetworkRequest::head
ErrorCode head(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "head" operation on the specified request.
Definition
qgsblockingnetworkrequest.cpp:92
QgsBlockingNetworkRequest::ErrorCode
ErrorCode
Error codes.
Definition
qgsblockingnetworkrequest.h:56
QgsBlockingNetworkRequest::NoError
@ NoError
No error was encountered.
Definition
qgsblockingnetworkrequest.h:57
QgsExpressionContextUtils::globalScope
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Definition
qgsexpressioncontextutils.cpp:52
QgsHelp::helpUrl
static QUrl helpUrl(const QString &key)
Returns URI of the help topic for the given key.
Definition
qgshelp.cpp:46
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition
qgshelp.cpp:41
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
QgsSettings
Stores settings for use within QGIS.
Definition
qgssettings.h:68
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition
qgssettings.cpp:169
qgis.h
qgsapplication.h
qgsblockingnetworkrequest.h
qgsexpressioncontext.h
qgsexpressioncontextutils.h
qgshelp.h
qgsmessagelog.h
qgsnetworkaccessmanager.h
qgssetrequestinitiator_p.h
QgsSetRequestInitiatorClass
#define QgsSetRequestInitiatorClass(request, _class)
Definition
qgssetrequestinitiator_p.h:26
qgssettings.h
Generated on
for QGIS API Documentation by
1.17.0