Engauge Digitizer  2
HelpWindow.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "HelpBrowser.h"
8 #include "HelpWindow.h"
9 #include "Logger.h"
10 #include <QApplication>
11 #include <QDir>
12 #include <QFileInfo>
13 #include <QHelpContentWidget>
14 #include <QHelpEngine>
15 #include <QHelpIndexWidget>
16 #include <QSplitter>
17 #include <QTabWidget>
18 
19 const int MIN_WIDTH = 600;
20 const int MIN_HEIGHT = 600;
21 
22 HelpWindow::HelpWindow(QWidget *parent) :
23  QDockWidget (parent)
24 {
25  setMinimumWidth (MIN_WIDTH);
26  setMinimumHeight (MIN_HEIGHT);
27 
28 #if !defined(OSX_DEBUG) && !defined(OSX_RELEASE)
29  QHelpEngine *helpEngine = new QHelpEngine (helpPath());
30  helpEngine->setupData();
31 
32  QTabWidget *tabs = new QTabWidget;
33  tabs->addTab (helpEngine->contentWidget(),
34  tr ("Contents"));
35  tabs->addTab (helpEngine->indexWidget(),
36  tr ("Index"));
37 
38  HelpBrowser *browser = new HelpBrowser (helpEngine);
39 
40  // URL is constructed from <namespace>, <virtualFolder> and <file> in engauge.qhp. If this line shows
41  // the error message 'QTextBrowser: No document for qthelp...' then one of the following applies:
42  // (1) the qhc file has not been built and put into the bin/documentation folder
43  // (2) in qtcreator the build is getting put into some directory other than engauge-digitizer/bin
44  browser->setSource (QUrl ("qthelp://engaugedigitizer.net/doc/index.html"));
45 
46  connect (helpEngine->contentWidget (), SIGNAL (linkActivated (QUrl)), browser, SLOT (setSource (QUrl)));
47  connect (helpEngine->indexWidget (), SIGNAL (linkActivated (QUrl, QString)), browser, SLOT (setSource (QUrl)));
48 
49  QSplitter *splitter = new QSplitter (Qt::Horizontal);
50  splitter->insertWidget (0, tabs);
51  splitter->insertWidget (1, browser);
52 
53  setWidget (splitter);
54 #endif
55 }
56 
57 #if !defined(OSX_DEBUG) && !defined(OSX_RELEASE)
58 QString HelpWindow::helpPath() const
59 {
60  // Possible locations of help file. Each entry is first tried as is, and then with
61  // applicationDirPath as a prefix. Each entry should probably start with a slash. This
62  // search approach offers some flexibility in the help file location
63  QStringList paths;
64 #ifdef HELPDIR
65 #define QUOTE(string) _QUOTE(string)
66 #define _QUOTE(string) #string
67  QString path = QString ("%1/engauge.qhc")
68  .arg (QUOTE (HELPDIR));
69  paths << path;
70 #endif
71  paths << "/documentation/engauge.qhc";
72  paths << "/../share/doc/engauge-digitizer/engauge.qhc";
73  paths << "/usr/share/engauge-digitizer-doc/engauge.qhc";
74 
75  QStringList::iterator itr;
76  for (itr = paths.begin(); itr != paths.end(); itr++) {
77 
78  QString pathAsIs = *itr;
79 
80  QFileInfo fileAsIs (pathAsIs);
81  if (fileAsIs.exists()) {
82  return pathAsIs;
83  }
84 
85  QString pathWithPrefix = QApplication::applicationDirPath() + pathAsIs;
86 
87  QFileInfo fileWithPrefix (pathWithPrefix);
88  if (fileWithPrefix.exists()) {
89  return pathWithPrefix;
90  }
91  }
92 
93  return ""; // Empty file, since help file was never found, will simply result in empty help contents
94 }
95 #endif
HelpWindow(QWidget *parent)
Single constructor.
Definition: HelpWindow.cpp:22
Text browser with resource loading enhanced for use as help text browser.
Definition: HelpBrowser.h:15