QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QTcpSocket>
22 #include <QTextStream>
23 
24 #include "qgscontexthelp.h"
25 #include "qgsmessagelog.h"
26 #include "qgsapplication.h"
27 #include "qgslogger.h"
28 
29 
30 QgsContextHelp *QgsContextHelp::gContextHelp = 0; // Singleton instance
31 
32 void QgsContextHelp::run( QString context )
33 {
34  if ( !gContextHelp )
35  {
36  // Create singleton instance if it does not exist
38  }
39 
40  gContextHelp->showContext( context );
41 }
42 
44 {
45  mProcess = start();
46 }
47 
49 {
50  delete mProcess;
51 }
52 
54 {
55  // Get the path to the help viewer
56  QString helpPath = QgsApplication::helpAppPath();
57  QgsDebugMsg( QString( "Help path is %1" ).arg( helpPath ) );
58 
59  QProcess *process = new QProcess;
60 
61  // Delete this object if the process terminates
62  connect( process, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( processExited() ) );
63 
64  // Delete the process if the application quits
65  connect( qApp, SIGNAL( aboutToQuit() ), process, SLOT( terminate() ) );
66 
67  connect( process, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( error( QProcess::ProcessError ) ) );
68 
69 #ifdef Q_OS_WIN
71  {
72  process->setEnvironment( QStringList() << QString( "PATH=%1;%2" ).arg( getenv( "PATH" ) ).arg( QApplication::applicationDirPath() ) );
73  }
74 #endif
75 
76  process->start( helpPath, QStringList() );
77 
78  return process;
79 }
80 
81 void QgsContextHelp::error( QProcess::ProcessError error )
82 {
83  QgsMessageLog::logMessage( tr( "Error starting help viewer [%1]" ).arg( error ), tr( "Context help" ) );
84 }
85 
86 void QgsContextHelp::showContext( QString context )
87 {
88  init();
89 
90  QString helpContents = gContextHelpTexts.value( context,
91  tr( "<h3>Oops! QGIS can't find help for this form.</h3>"
92  "The help file for %1 was not found for your language<br>"
93  "If you would like to create it, contact the QGIS development team"
94  ).arg( context ) );
95 
96  QString myStyle = QgsApplication::reportStyleSheet();
97  helpContents = "<head><style>" + myStyle + "</style></head><body>" + helpContents + "</body>\nEOH\n";
98 
99  mProcess->write( helpContents.toUtf8() );
100 }
101 
103 {
104  // Delete this object if the process terminates
105  delete gContextHelp;
106  gContextHelp = NULL;
107 }