~ubuntu-branches/ubuntu/hardy/qgis/hardy

« back to all changes in this revision

Viewing changes to src/core/qgscontexthelp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/* $Id: qgscontexthelp.cpp 5628 2006-07-24 08:35:08Z g_j_m $ */
 
19
#include <iostream>
 
20
 
 
21
#include <QString>
 
22
#include <QProcess>
 
23
#include <QTcpSocket>
 
24
#include <QTextStream>
 
25
 
 
26
#include "qgscontexthelp.h"
 
27
#include "qgsapplication.h"
 
28
 
 
29
 
 
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.
 
33
 
 
34
QgsContextHelp *QgsContextHelp::gContextHelp = NULL;  // Singleton instance
 
35
 
 
36
void QgsContextHelp::run(int contextId)
 
37
{
 
38
  if (gContextHelp == NULL)
 
39
  {
 
40
    // Create singleton instance if it does not exist
 
41
    gContextHelp = new QgsContextHelp(contextId);
 
42
  }
 
43
  else
 
44
  {
 
45
    gContextHelp->showContext(contextId);
 
46
  }
 
47
}
 
48
 
 
49
QgsContextHelp::QgsContextHelp(int contextId)
 
50
{
 
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()));
 
56
#else
 
57
  // Placeholder for new process if terminating and restarting
 
58
  mNextProcess = NULL;
 
59
#endif
 
60
}
 
61
 
 
62
QgsContextHelp::~QgsContextHelp()
 
63
{
 
64
#ifdef QGSCONTEXTHELP_REUSE
 
65
  delete mSocket;
 
66
#else
 
67
  // Should be NULL here unless previous process termination failed
 
68
  delete mNextProcess;
 
69
#endif
 
70
  delete mProcess;
 
71
}
 
72
 
 
73
QProcess *QgsContextHelp::start(int contextId)
 
74
{
 
75
  // Get the path to the help viewer
 
76
  QString helpPath = QgsApplication::helpAppPath(); 
 
77
#ifdef QGISDEBUG
 
78
  std::cout << "Help path is " << helpPath.toLocal8Bit().data() << std::endl; 
 
79
#endif
 
80
 
 
81
  QString arg1;
 
82
  arg1.setNum(contextId);
 
83
  QProcess *process = new QProcess;
 
84
  process->start(helpPath, QStringList(arg1));
 
85
 
 
86
  // Delete this object if the process terminates
 
87
  connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), 
 
88
                   SLOT(processExited()));
 
89
 
 
90
  // Delete the process if the application quits
 
91
  connect(qApp, SIGNAL(aboutToQuit()), process, SLOT(terminate()));
 
92
 
 
93
  return process;
 
94
}
 
95
 
 
96
void QgsContextHelp::readPort()
 
97
{
 
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, 
 
104
                       SLOT(readPort()));
 
105
#endif
 
106
}
 
107
 
 
108
void QgsContextHelp::showContext(int contextId)
 
109
{
 
110
  // Refresh help process with new context
 
111
#ifdef QGSCONTEXTHELP_REUSE
 
112
  // Send context to process
 
113
  QTextStream os(mSocket);
 
114
  os << contextId << "\n";
 
115
#ifdef QGISDEBUG
 
116
  std::cout << "Sending help process context " << contextId << std::endl; 
 
117
#endif
 
118
#else
 
119
  // Should be NULL here unless previous process termination failed
 
120
  // (if it did fail, we abandon the process and delete the object reference)
 
121
  delete mNextProcess;
 
122
  // Start new help viewer process (asynchronous)
 
123
  mNextProcess = start(contextId);
 
124
  // Terminate existing help viewer process (asynchronous)
 
125
  mProcess->terminate();
 
126
#endif
 
127
}
 
128
 
 
129
void QgsContextHelp::processExited()
 
130
{
 
131
#ifndef QGSCONTEXTHELP_REUSE
 
132
  if (mNextProcess)
 
133
  {
 
134
    // New process becomes current process when prior process terminates
 
135
    delete mProcess;
 
136
    mProcess = mNextProcess;
 
137
    mNextProcess = NULL;
 
138
  }
 
139
  else
 
140
#endif
 
141
  {
 
142
    // Delete this object if the process terminates
 
143
    delete gContextHelp;
 
144
    gContextHelp = NULL;
 
145
  }
 
146
}