All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AssistantHelpManagerImpl.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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 <QtCore/QDir>
34 #include <QtCore/QFileInfo>
35 #include <QtCore/QLibraryInfo>
36 #include <QtCore/QProcess>
37 #include <QtGui/QApplication>
38 #include <QtGui/QMainWindow>
39 #include <QtGui/QMessageBox>
40 
41 
42 QWidget* GetMainWindow()
43 {
44  QWidgetList lst = qApp->topLevelWidgets();
45  QWidgetList::iterator it;
46 
47  for(it=lst.begin(); it!=lst.end(); ++it)
48  {
49  QMainWindow* main = qobject_cast<QMainWindow*>(*it);
50 
51  if(main != 0)
52  return main;
53  }
54 
55  return 0;
56 }
57 
58 
59 te::qt::widgets::AssistantHelpManagerImpl::AssistantHelpManagerImpl(const QString& collectionFile, QObject* parent)
60  : QObject(parent),
61  m_proc(0),
62  m_collectionFile(collectionFile)
63 {
64 }
65 
67 {
68  if(m_proc != 0)
69  {
70  QByteArray ba;
71 
72  bool tag = m_regDocs.size() > 1;
73 
74  for(int i = 0; i < m_regDocs.size();i++)
75  {
76  ba.append("unregister " + m_regDocs.value(i));
77 
78  if(tag)
79  ba.append((i == (m_regDocs.size()-1)) ? "\n" : ";");
80  else
81  ba.append('\n');
82  }
83 
84  m_proc->write(ba);
85 
86  m_proc->close();
87 
88  delete m_proc;
89  }
90 }
91 
93 {
94  QFileInfo info(m_collectionFile);
95 
96  if(!info.exists())
97  {
98  QMessageBox::warning(GetMainWindow(), QObject::tr("Help failure"), QObject::tr("Could not find help files"));
99  return false;
100  }
101 
102  if (!m_proc)
103  m_proc = new QProcess();
104 
105  if (m_proc->state() != QProcess::Running)
106  {
107  QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();
108  #if !defined(Q_OS_MAC)
109  app += QLatin1String("assistant");
110  #else
111  app += QLatin1String("Assistant.app/Contents/MacOS/Assistant");
112  #endif
113 
114  QStringList args;
115  args << QLatin1String("-collectionFile")
116  << m_collectionFile
117  << QLatin1String("-enableRemoteControl");
118 
119  m_proc->start(app, args);
120 
121  if (!m_proc->waitForStarted())
122  {
123  QMessageBox::critical(GetMainWindow(), QObject::tr("Simple Text Viewer"), QObject::tr("Unable to launch Qt Assistant (%1)").arg(app));
124  return false;
125  }
126  }
127 
128  return true;
129 }
130 
131 void te::qt::widgets::AssistantHelpManagerImpl::showHelp(const QString& htmRef, const QString& nSpace)
132 {
133  if (!startAssistant())
134  return;
135 
136  QString nS = (nSpace.isEmpty()) ? "dpi.inpe.br.qtwidgets" : nSpace;
137 
138  QByteArray ba;
139  ba.append("setSource qthelp://" + nS + "/doc/" + htmRef.toLocal8Bit() + '\n');
140 
141  m_proc->write(ba);
142 }
143 
145 {
146  if(m_regDocs.contains(docRef))
147  return;
148 
149  m_regDocs.append(docRef);
150 
151  QString app;
152  QStringList args;
153  QByteArray ba;
154 
155  if(m_proc == 0)
156  m_proc = new QProcess;
157 
158  ba.append(QLatin1String("register ") + docRef.toLocal8Bit() + '\n');
159  m_proc->write(ba);
160 }
161 
bool startAssistant()
It starts the Qt assistant help process pointing out to the given collection file.
void showHelp(const QString &htmRef, const QString &nSpace="")
QWidget * GetMainWindow()
AssistantHelpManagerImpl(const QString &collectionFile, QObject *parent=0)
Constructor.
int main(int argc, char *argv[])
Definition: main.cpp:24
An implementation of HelpManager that uses QAssistant to show help files.