Fraxinus  17.12-rc4
An IGT application
cxHelpEngine.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxHelpEngine.h"
34 
35 #include <QHelpEngine>
36 #include "cxDataLocations.h"
37 #include <iostream>
38 #include "cxTypeConversions.h"
39 #include <QFileInfo>
40 #include <QApplication>
41 #include <QWidget>
42 #include "cxLogger.h"
43 #include "cxProfile.h"
44 #include <QDir>
45 #include <QTimer>
46 
47 //#define DEBUG_HELP_SYSTEM // turn on to output help system focus information
48 
49 namespace cx
50 {
51 
53 {
54  QDir().mkpath(profile()->getPath()); // otherwise setupData() fails sometimes
55  QString helpFile = profile()->getPath() + "/cx_user_doc.qhc";
56  helpEngine = new QHelpEngine(helpFile, NULL);
57 
58  this->setupDataWithWarning();
59  this->setupDocFile();
60  this->setupDataWithWarning();
61 
62  connect(qApp, SIGNAL(focusObjectChanged(QObject*)), this, SLOT(focusObjectChanged(QObject*)));
63  connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
64 
65  QTimer::singleShot(100, this, SLOT(setInitialPage()));
66 }
67 
69 {
70  delete helpEngine;
71 }
72 
73 void HelpEngine::setupDocFile()
74 {
75  QString docFile = DataLocations::getDocPath()+"/cx_user_doc.qch";
76  if(!QFile(docFile).exists())
77  reportWarning(QString("HelpEngine: Cannot find docFile: %1").arg(docFile));
78  helpEngine->unregisterDocumentation(helpEngine->namespaceName(docFile));
79  if(!helpEngine->registerDocumentation(docFile))
80  reportWarning(QString("HelpEngine: Documentation registration failed: %1").arg(helpEngine->error()));
81 }
82 
83 void HelpEngine::setupDataWithWarning()
84 {
85  bool success = helpEngine->setupData();
86  // had problems here before mkdir was called on the qhc path
87  if (!success)
88  CX_LOG_WARNING() << QString("Help engine setup failed with error [%1]").arg(helpEngine->error());
89 }
90 
91 void HelpEngine::setInitialPage()
92 {
93  emit keywordActivated("user_doc_overview");
94 }
95 
96 void HelpEngine::focusChanged(QWidget * old, QWidget * now)
97 {
98  if (!now)
99  return;
100 }
101 
102 void HelpEngine::focusObjectChanged(QObject* newFocus)
103 {
104  if (!newFocus)
105  return;
106 #ifdef DEBUG_HELP_SYSTEM
107  CX_LOG_CHANNEL_INFO("HELP_DB") << QString("**Focus on [%1]: %2").arg(newFocus->objectName()).arg(dynamic_cast<QWidget*>(newFocus)->windowTitle());
108 #endif
109  QString keyword = this->findBestMatchingKeyword(newFocus);
110  if (!keyword.isEmpty())
111  {
112 #ifdef DEBUG_HELP_SYSTEM
113  CX_LOG_CHANNEL_INFO("HELP_DB") << QString(" Found keyword [%1]").arg(keyword);
114 #endif
115  emit keywordActivated(keyword);
116  }
117 }
118 
119 bool HelpEngine::isBreakChar(QChar c) const
120 {
121  return c.isDigit() || c.isUpper();
122 }
123 
124 bool HelpEngine::isBreakChar(QString text, int index) const
125 {
126  if (!this->isBreakChar(text[index]))
127  return false;
128 
129  bool prev = true;
130  if (index>0)
131  prev = this->isBreakChar(text[index-1]);
132 
133  bool next = true;
134  if (index+1<text.size())
135  next = this->isBreakChar(text[index+1]);
136 
137  if (!prev || !next)
138  return true;
139 
140  return false;
141 }
142 
143 QString HelpEngine::findBestMatchingKeyword(QObject* object)
144 {
145  while (object)
146  {
147  QString id = object->objectName();
148 #ifdef DEBUG_HELP_SYSTEM
149  CX_LOG_CHANNEL_DEBUG("HELP_DB") << QString(" examining [%1](%2)")
150  .arg(id)
151  .arg(object->metaObject()->className());
152 #endif
153  if (id.contains("help_widget"))
154  return "";
155 
156  QMap<QString, QUrl> links = this->engine()->linksForIdentifier(id);
157  if (!links.empty())
158  {
159  return id;
160  }
161 
162  object = object->parent();
163  }
164  return "";
165 }
166 
167 
168 }
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:129
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:181
QHelpEngine * engine()
Definition: cxHelpEngine.h:65
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
#define CX_LOG_CHANNEL_DEBUG(channel)
Definition: cxLogger.h:128
#define CX_LOG_WARNING
Definition: cxLogger.h:119
bool contains(std::string const &s, std::string const &infix)
Definition: catch.hpp:168
static QString getDocPath()
return path to folder containing documentation files
Namespace for all CustusX production code.
void keywordActivated(QString)