Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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::convertToKeyword(QString id) const
144 {
145  // convert camel case strings into whitespace-separated lowercase strings:
146  // MyWidget -> my widget
147  // myFancy3DWidget2D -> my Fancy 3D Widget 2D -> my fancy 3d widget 2d
148  // myFancyDWidget -> my Fancy D Widget -> my fancy D widget
149  QString retval;
150  retval.push_back(id[0]);
151  for (int i=1; i<id.size(); ++i)
152  {
153  // break condition Q is (uppercase or digit)
154  // insert whitespace before Q
155  // ignore if previous was Q
156  if (this->isBreakChar(id, i))
157  {
158  retval.push_back("_"); // cant't get doxygenerated anchors to work properly with whitespace
159  }
160  retval.push_back(id[i]);
161  }
162  return retval.toLower();
163 }
164 
165 QString HelpEngine::findBestMatchingKeyword(QObject* object)
166 {
167  while (object)
168  {
169  QString id = this->convertToKeyword(object->objectName());
170 #ifdef DEBUG_HELP_SYSTEM
171  CX_LOG_CHANNEL_DEBUG("HELP_DB") << QString(" examining [%1](%2)")
172  .arg(id)
173  .arg(object->metaObject()->className());
174 #endif
175  if (id.contains("help_widget"))
176  return "";
177 
178  QMap<QString, QUrl> links = this->engine()->linksForIdentifier(id);
179  if (!links.empty())
180  {
181  return id;
182  }
183 
184  object = object->parent();
185  }
186  return "";
187 }
188 
189 
190 }
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:123
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
QHelpEngine * engine()
Definition: cxHelpEngine.h:65
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
#define CX_LOG_CHANNEL_DEBUG(channel)
Definition: cxLogger.h:122
#define CX_LOG_WARNING
Definition: cxLogger.h:113
static QString getDocPath()
return path to folder containing documentation files
void keywordActivated(QString)