All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AssistantHelpManagerImpl.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/help/AssistantHelpManagerImpl.cpp
22 
23  \brief An implementation of HelpManager that uses QAssistant to show help files.
24 */
25 
26 // TerraLib
28 
29 // STL
30 #include <cassert>
31 
32 // Qt
33 #include <QApplication>
34 #include <QDir>
35 #include <QFileInfo>
36 #include <QLibraryInfo>
37 #include <QMainWindow>
38 #include <QMessageBox>
39 #include <QProcess>
40 
41 QWidget* GetMainWindow()
42 {
43  QWidgetList lst = qApp->topLevelWidgets();
44  QWidgetList::iterator it;
45 
46  for(it=lst.begin(); it!=lst.end(); ++it)
47  {
48  QMainWindow* main = qobject_cast<QMainWindow*>(*it);
49 
50  if(main != 0)
51  return main;
52  }
53 
54  return 0;
55 }
56 
57 
58 te::qt::widgets::AssistantHelpManagerImpl::AssistantHelpManagerImpl(const QString& collectionFile, QObject* parent)
59  : QObject(parent),
60  m_proc(0),
61  m_collectionFile(collectionFile)
62 {
63 }
64 
66 {
67  if(m_proc != 0)
68  {
69  QByteArray ba;
70 
71  bool tag = m_regDocs.size() > 1;
72 
73  for(int i = 0; i < m_regDocs.size();i++)
74  {
75  ba.append("unregister " + m_regDocs.value(i));
76 
77  if(tag)
78  ba.append((i == (m_regDocs.size()-1)) ? "\n" : ";");
79  else
80  ba.append('\n');
81  }
82 
83  m_proc->write(ba);
84 
85  m_proc->close();
86 
87  delete m_proc;
88  }
89 }
90 
92 {
93  QFileInfo info(m_collectionFile);
94 
95  if(!info.exists())
96  {
97  QMessageBox::warning(GetMainWindow(), QObject::tr("Help failure"), QObject::tr("Could not find help files"));
98  return false;
99  }
100 
101  if (!m_proc)
102  m_proc = new QProcess();
103 
104  if (m_proc->state() != QProcess::Running)
105  {
106  QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();
107  #if !defined(Q_OS_MAC)
108  app += QLatin1String("assistant");
109  #else
110  app += QLatin1String("Assistant.app/Contents/MacOS/Assistant");
111  #endif
112 
113  QStringList args;
114  args << QLatin1String("-collectionFile")
115  << m_collectionFile
116  << QLatin1String("-enableRemoteControl");
117 
118  m_proc->start(app, args);
119 
120  if (!m_proc->waitForStarted())
121  {
122  QMessageBox::critical(GetMainWindow(), QObject::tr("Simple Text Viewer"), QObject::tr("Unable to launch Qt Assistant (%1)").arg(app));
123  return false;
124  }
125  }
126 
127  return true;
128 }
129 
130 void te::qt::widgets::AssistantHelpManagerImpl::showHelp(const QString& htmRef, const QString& nSpace)
131 {
132  if (!startAssistant())
133  return;
134 
135  QString nS = (nSpace.isEmpty()) ? "dpi.inpe.br.qtwidgets" : nSpace;
136 
137  QByteArray ba;
138  ba.append("setSource qthelp://" + nS + "/doc/" + htmRef.toLocal8Bit() + '\n');
139 
140  m_proc->write(ba);
141 }
142 
144 {
145  if(m_regDocs.contains(docRef))
146  return;
147 
148  m_regDocs.append(docRef);
149 
150  QString app;
151  QStringList args;
152  QByteArray ba;
153 
154  if(m_proc == 0)
155  m_proc = new QProcess;
156 
157  ba.append(QLatin1String("register ") + docRef.toLocal8Bit() + '\n');
158  m_proc->write(ba);
159 }
160 
AssistantHelpManagerImpl(const QString &collectionFile, QObject *parent=0)
Constructor.
void showHelp(const QString &htmRef, const QString &nSpace="")
bool startAssistant()
It starts the Qt assistant help process pointing out to the given collection file.
QWidget * GetMainWindow()
An implementation of HelpManager that uses QAssistant to show help files.
int main(int argc, char *argv[])
Definition: main.cpp:24