Skip to content

Commit 9a94da8

Browse files
committed
new QgsHelp class that uses QGIS User Guide as help source
1 parent 981810b commit 9a94da8

File tree

5 files changed

+240
-1
lines changed

5 files changed

+240
-1
lines changed

doc/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ELSE(TXT2TAGS_EXECUTABLE)
2222
)
2323
ENDIF(TXT2TAGS_EXECUTABLE)
2424

25-
SET(QGIS_DOC_FILES ${QGIS_DOC_FILES} index.html news.html developersmap.html contributors.json favicon.ico style.css release-sponsors.html AUTHORS CONTRIBUTORS SPONSORS DONORS TRANSLATORS LICENSE)
25+
SET(QGIS_DOC_FILES ${QGIS_DOC_FILES} index.html news.html developersmap.html nohelp.html contributors.json favicon.ico style.css release-sponsors.html AUTHORS CONTRIBUTORS SPONSORS DONORS TRANSLATORS LICENSE)
2626

2727
INSTALL(FILES ${QGIS_DOC_FILES} DESTINATION ${QGIS_DATA_DIR}/doc)
2828
INSTALL(FILES ../images/icons/qgis-icon-60x60.png DESTINATION ${QGIS_DATA_DIR}/doc/images)

doc/nohelp.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>No help found</title>
5+
<meta charset="UTF-8">
6+
</head>
7+
8+
<body>
9+
<center><a href="http://qgis.org"><img src="qgis-icon-60x60.png" border=0></a></center>
10+
<h1><center>Oops!</center></h1>
11+
<p>Sorry, help is not available. Probably you have no Internet connection,
12+
location of the help files is not configured or there is no help content
13+
for requested topic.</p>
14+
<p>Please make sure that correct location of the help files specified
15+
in the QGIS options dialog (Settings&nbsp;&rarr; Options&nbsp;&rarr;
16+
System).</p>
17+
</body>
18+
19+
</html>

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ SET(QGIS_CORE_SRCS
135135
qgsgeometryvalidator.cpp
136136
qgsgml.cpp
137137
qgsgmlschema.cpp
138+
qgshelp.cpp
138139
qgshistogram.cpp
139140
qgsinterval.cpp
140141
qgsjsonutils.cpp
@@ -481,6 +482,7 @@ SET(QGIS_CORE_MOC_HDRS
481482
qgsgeometryvalidator.h
482483
qgsgml.h
483484
qgsgmlschema.h
485+
qgshelp.h
484486
qgsmaplayer.h
485487
qgsmaplayerlegend.h
486488
qgsmaplayermodel.h

src/core/qgshelp.cpp

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 <QSettings>
19+
#include <QLocale>
20+
#include <QUrl>
21+
#include <QFileInfo>
22+
#include <QTcpSocket>
23+
#include <QDesktopServices>
24+
25+
#include "qgis.h"
26+
#include "qgsapplication.h"
27+
28+
QgsHelp *QgsHelp::sHelp = nullptr; // Singleton instance
29+
30+
void QgsHelp::openHelp( const QString& key )
31+
{
32+
if ( !sHelp )
33+
{
34+
sHelp = new QgsHelp();
35+
}
36+
37+
QDesktopServices::openUrl( sHelp->helpUrl( key ) );
38+
}
39+
40+
QUrl QgsHelp::helpUrl( const QString& key )
41+
{
42+
if ( !sHelp )
43+
{
44+
sHelp = new QgsHelp();
45+
}
46+
47+
QSettings settings;
48+
QUrl helpNotFound = QUrl::fromLocalFile( QgsApplication::pkgDataPath() + "/doc/nohelp.html" );
49+
50+
QString paths = settings.value( QStringLiteral( "help/helpSearchPath" ), "" ).toString();
51+
if ( paths.isEmpty() )
52+
{
53+
return helpNotFound;
54+
}
55+
56+
QString qgisLocale;
57+
bool overrideLocale = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
58+
if ( overrideLocale )
59+
{
60+
qgisLocale = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
61+
}
62+
else
63+
{
64+
qgisLocale = QLocale::system().name().left( 2 );
65+
}
66+
67+
QString qgisVersion;
68+
if ( Qgis::QGIS_VERSION_INT / 100 % 100 == 99 )
69+
{
70+
qgisVersion = QStringLiteral( "testing" );
71+
qgisLocale = QStringLiteral( "en" );
72+
}
73+
else
74+
{
75+
qgisVersion = QStringLiteral( "%1.%2" ).arg( Qgis::QGIS_VERSION_INT / 10000 ).arg( Qgis::QGIS_VERSION_INT / 100 % 100 );
76+
}
77+
78+
QString suffix = QStringLiteral( "%1/%2/docs/user_manual/%3" ).arg( qgisVersion ).arg( qgisLocale ).arg( key );
79+
80+
QUrl myUrl;
81+
QString helpPath;
82+
bool helpFound = false;
83+
84+
QStringList pathList = paths.split( '|' );
85+
QStringList::const_iterator pathIt = pathList.constBegin();
86+
for ( ; pathIt != pathList.constEnd(); ++pathIt )
87+
{
88+
helpPath = QStringLiteral( "%1/%2" ).arg( *pathIt ).arg( suffix );
89+
90+
if (( *pathIt ).startsWith( QStringLiteral( "http://" ) ) )
91+
{
92+
if ( !sHelp->urlExists( helpPath ) )
93+
{
94+
continue;
95+
}
96+
myUrl = QUrl( helpPath );
97+
}
98+
else
99+
{
100+
if ( !QFileInfo( helpPath.mid( 0, helpPath.lastIndexOf( "#" ) ) ).exists() )
101+
{
102+
continue;
103+
}
104+
myUrl = QUrl::fromLocalFile( helpPath );
105+
myUrl.setFragment( helpPath.mid( helpPath.lastIndexOf( "#" ), -1 ) );
106+
}
107+
108+
helpFound = true;
109+
break;
110+
}
111+
112+
return helpFound ? myUrl : helpNotFound;
113+
}
114+
115+
116+
QgsHelp::QgsHelp()
117+
{
118+
}
119+
120+
QgsHelp::~QgsHelp()
121+
{
122+
}
123+
124+
bool QgsHelp::urlExists( const QString& url ) const
125+
{
126+
QUrl myUrl( url );
127+
QTcpSocket socket;
128+
129+
socket.connectToHost( myUrl.host(), 80 );
130+
if ( socket.waitForConnected() )
131+
{
132+
socket.write( "HEAD " + myUrl.path().toUtf8() + " HTTP/1.1\r\n"
133+
"Host: " + myUrl.host().toUtf8() + "\r\n\r\n" );
134+
if ( socket.waitForReadyRead() )
135+
{
136+
QByteArray bytes = socket.readAll();
137+
if ( bytes.contains( "200 OK" ) || bytes.contains( "302 Found" ) )
138+
{
139+
return true;
140+
}
141+
}
142+
}
143+
144+
return false;
145+
}

src/core/qgshelp.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***************************************************************************
2+
qgshelp.h
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+
#ifndef QGSHELP_H
17+
#define QGSHELP_H
18+
19+
#include <QObject>
20+
21+
/** \ingroup core
22+
* @brief The class provides help URI for the given key.
23+
*
24+
* Help can be stored online, on the local directory or on the intranet
25+
* server. Location of the QGIS help can be configured in QGIS options.
26+
* Multiple locations are supported, they will be used in order of
27+
* preference, from top to bottom.
28+
*
29+
* URI construction takes in account following information:
30+
* - QGIS version
31+
* - language of the QGIS UI
32+
*
33+
* If no help found, default error page with information how to setup
34+
* help system will be shown.
35+
*
36+
* @note added in QGIS 3.0
37+
*/
38+
class CORE_EXPORT QgsHelp : public QObject
39+
{
40+
Q_OBJECT
41+
public:
42+
/** Opens help topic for the given help key using default system
43+
* web browser. If help topic not found, builtin error page shown.
44+
* @param key key which identified help topic
45+
* @note added in QGIS 3.0
46+
*/
47+
static void openHelp( const QString& key );
48+
49+
/** Returns URI of the help topic for the given key. If help topic
50+
* not found, URI of the builtin error page returned.
51+
* @param key key which identified help topic
52+
* @note added in QGIS 3.0
53+
*/
54+
static QUrl helpUrl( const QString& key );
55+
56+
private:
57+
//! Constructor
58+
QgsHelp();
59+
60+
//! Destructor
61+
~QgsHelp();
62+
63+
/** Check if given URL accessible by issuing HTTP HEAD request.
64+
* Returns true if URL accessible, false otherwise.
65+
* @param url URL to check
66+
* @note added in QGIS 3.0
67+
*/
68+
bool urlExists( const QString& url ) const;
69+
70+
static QgsHelp* sHelp;
71+
};
72+
73+
#endif // QGSHELP_H

0 commit comments

Comments
 (0)