QGIS API Documentation  2.17.0-Master (3a3b9ab7)
qgscontexthelp.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscontexthelp.cpp
3  Display context help for a dialog
4  -------------------
5  begin : 2005-06-19
6  copyright : (C) 2005 by Gary E.Sherman
7  email : sherman at mrcc.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include <QString>
20 #include <QProcess>
21 #include <QTextStream>
22 
23 #include "qgscontexthelp.h"
24 #include "qgsmessagelog.h"
25 #include "qgsapplication.h"
26 #include "qgslogger.h"
27 
28 
29 QgsContextHelp *QgsContextHelp::gContextHelp = nullptr; // Singleton instance
30 
31 void QgsContextHelp::run( const QString& context )
32 {
33  if ( !gContextHelp )
34  {
35  // Create singleton instance if it does not exist
36  gContextHelp = new QgsContextHelp();
37  }
38 
39  gContextHelp->showContext( context );
40 }
41 
42 QgsContextHelp::QgsContextHelp()
43 {
44  mProcess = start();
45 }
46 
47 QgsContextHelp::~QgsContextHelp()
48 {
49  delete mProcess;
50 }
51 
52 QProcess *QgsContextHelp::start()
53 {
54  // Get the path to the help viewer
56  QgsDebugMsg( QString( "Help path is %1" ).arg( helpPath ) );
57 
58  QProcess *process = new QProcess;
59 
60  // Delete this object if the process terminates
61  connect( process, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( processExited() ) );
62 
63  connect( process, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( error( QProcess::ProcessError ) ) );
64 
65 #ifdef Q_OS_WIN
67  {
68  process->setEnvironment( QStringList() << QString( "PATH=%1;%2" ).arg( getenv( "PATH" ) ).arg( QApplication::applicationDirPath() ) );
69  }
70 #endif
71 
72  process->start( helpPath, QStringList() );
73 
74  return process;
75 }
76 
77 void QgsContextHelp::error( QProcess::ProcessError error )
78 {
79  QgsMessageLog::logMessage( tr( "Error starting help viewer [%1]" ).arg( error ), tr( "Context help" ) );
80 }
81 
82 void QgsContextHelp::showContext( const QString& context )
83 {
84  init();
85 
86  QString helpContents = gContextHelpTexts.value( context,
87  tr( "<h3>Oops! QGIS can't find help for this form.</h3>"
88  "The help file for %1 was not found for your language<br>"
89  "If you would like to create it, contact the QGIS development team"
90  ).arg( context ) );
91 
93  helpContents = "<head><style>" + myStyle + "</style></head><body>" + helpContents + "</body>\nEOH\n";
94 
95  mProcess->write( helpContents.toUtf8() );
96 }
97 
98 void QgsContextHelp::processExited()
99 {
100  // Delete this object if the process terminates
101  delete gContextHelp;
102  gContextHelp = nullptr;
103 }
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static bool isRunningFromBuildDir()
Indicates whether running from build directory (not installed)
static QString reportStyleSheet()
get a standard css style sheet for reports.
QString tr(const char *sourceText, const char *disambiguation, int n)
static void run(const QString &context)
Provides a context based help browser for a dialog.
static QString helpAppPath()
Returns the path to the help application.
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void setEnvironment(const QStringList &environment)
const T value(const Key &key) const
qint64 write(const char *data, qint64 maxSize)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString applicationDirPath()
void start(const QString &program, const QStringList &arguments, QFlags< QIODevice::OpenModeFlag > mode)