1
/***************************************************************************
3
Display context help for a dialog
6
copyright : (C) 2005 by Gary E.Sherman
7
email : sherman at mrcc.com
8
***************************************************************************/
10
/***************************************************************************
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. *
17
***************************************************************************/
18
/* $Id: qgscontexthelp.cpp 5628 2006-07-24 08:35:08Z g_j_m $ */
24
#include <QTextStream>
26
#include "qgscontexthelp.h"
27
#include "qgsapplication.h"
30
// Note: QGSCONTEXTHELP_REUSE must be defined (or not) in qgscontexthelp.h.
31
// The flag determines if an existing viewer process should be reused or
32
// terminated and restarted in order to make the viewer be the top window.
34
QgsContextHelp *QgsContextHelp::gContextHelp = NULL; // Singleton instance
36
void QgsContextHelp::run(int contextId)
38
if (gContextHelp == NULL)
40
// Create singleton instance if it does not exist
41
gContextHelp = new QgsContextHelp(contextId);
45
gContextHelp->showContext(contextId);
49
QgsContextHelp::QgsContextHelp(int contextId)
51
mProcess = start(contextId);
52
#ifdef QGSCONTEXTHELP_REUSE
53
// Create socket to communicate with process
54
mSocket = new QTcpSocket(this);
55
connect(mProcess, SIGNAL(readyReadStandardoutput()), SLOT(readPort()));
57
// Placeholder for new process if terminating and restarting
62
QgsContextHelp::~QgsContextHelp()
64
#ifdef QGSCONTEXTHELP_REUSE
67
// Should be NULL here unless previous process termination failed
73
QProcess *QgsContextHelp::start(int contextId)
75
// Get the path to the help viewer
76
QString helpPath = QgsApplication::helpAppPath();
78
std::cout << "Help path is " << helpPath.toLocal8Bit().data() << std::endl;
82
arg1.setNum(contextId);
83
QProcess *process = new QProcess;
84
process->start(helpPath, QStringList(arg1));
86
// Delete this object if the process terminates
87
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
88
SLOT(processExited()));
90
// Delete the process if the application quits
91
connect(qApp, SIGNAL(aboutToQuit()), process, SLOT(terminate()));
96
void QgsContextHelp::readPort()
98
#ifdef QGSCONTEXTHELP_REUSE
99
// Get port and connect socket to process
100
QString p = mProcess->readAllStandardOutput();
101
Q_UINT16 port = p.toUShort();
102
mSocket->connectToHost("localhost", port);
103
disconnect(mProcess, SIGNAL(readyReadStandardOutput()), this,
108
void QgsContextHelp::showContext(int contextId)
110
// Refresh help process with new context
111
#ifdef QGSCONTEXTHELP_REUSE
112
// Send context to process
113
QTextStream os(mSocket);
114
os << contextId << "\n";
116
std::cout << "Sending help process context " << contextId << std::endl;
119
// Should be NULL here unless previous process termination failed
120
// (if it did fail, we abandon the process and delete the object reference)
122
// Start new help viewer process (asynchronous)
123
mNextProcess = start(contextId);
124
// Terminate existing help viewer process (asynchronous)
125
mProcess->terminate();
129
void QgsContextHelp::processExited()
131
#ifndef QGSCONTEXTHELP_REUSE
134
// New process becomes current process when prior process terminates
136
mProcess = mNextProcess;
142
// Delete this object if the process terminates