QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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"
25#include "qgsmessagelog.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
39using namespace Qt::StringLiterals;
40
41void QgsHelp::openHelp( const QString &key )
42{
43 QDesktopServices::openUrl( QgsHelp::helpUrl( key ) );
44}
45
46QUrl 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
119bool QgsHelp::urlExists( const QString &url )
120{
121 const QUrl helpUrl( url );
122
124 QNetworkRequest req( helpUrl );
125 QgsSetRequestInitiatorClass( req, u"QgsHelp"_s );
126
127 QgsBlockingNetworkRequest::ErrorCode errCode = request.head( req );
128 return errCode == QgsBlockingNetworkRequest::NoError;
129}
@ Info
Information message.
Definition qgis.h:160
static QString pkgDataPath()
Returns the common root path of all application data directories.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode head(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "head" operation on the specified request.
@ NoError
No error was encountered.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
static QUrl helpUrl(const QString &key)
Returns URI of the help topic for the given key.
Definition qgshelp.cpp:46
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
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())
Adds a message to the log instance (and creates it if necessary).
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
#define QgsSetRequestInitiatorClass(request, _class)