CustusX  15.4.0-beta
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 
60  QString docFile = DataLocations::getDocPath()+"/cx_user_doc.qch";
61  helpEngine->registerDocumentation(docFile);
62 
63  this->setupDataWithWarning();
64 
65  connect(qApp, SIGNAL(focusObjectChanged(QObject*)), this, SLOT(focusObjectChanged(QObject*)));
66  connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
67 
68  QTimer::singleShot(100, this, SLOT(setInitialPage()));
69 }
70 
72 {
73  delete helpEngine;
74 }
75 
76 void HelpEngine::setupDataWithWarning()
77 {
78  bool success = helpEngine->setupData();
79  // had problems here before mkdir was called on the qhc path
80  if (!success)
81  CX_LOG_WARNING() << QString("Help engine setup failed with error [%1]").arg(helpEngine->error());
82 }
83 
84 void HelpEngine::setInitialPage()
85 {
86  emit keywordActivated("user_doc_overview");
87 }
88 
89 void HelpEngine::focusChanged(QWidget * old, QWidget * now)
90 {
91  if (!now)
92  return;
93 }
94 
95 void HelpEngine::focusObjectChanged(QObject* newFocus)
96 {
97  if (!newFocus)
98  return;
99 #ifdef DEBUG_HELP_SYSTEM
100  CX_LOG_CHANNEL_INFO("HELP_DB") << QString("**Focus on [%1]: %2").arg(newFocus->objectName()).arg(dynamic_cast<QWidget*>(newFocus)->windowTitle());
101 #endif
102  QString keyword = this->findBestMatchingKeyword(newFocus);
103  if (!keyword.isEmpty())
104  {
105 #ifdef DEBUG_HELP_SYSTEM
106  CX_LOG_CHANNEL_INFO("HELP_DB") << QString(" Found keyword [%1]").arg(keyword);
107 #endif
108  emit keywordActivated(keyword);
109  }
110 }
111 
112 bool HelpEngine::isBreakChar(QChar c) const
113 {
114  return c.isDigit() || c.isUpper();
115 }
116 
117 bool HelpEngine::isBreakChar(QString text, int index) const
118 {
119  if (!this->isBreakChar(text[index]))
120  return false;
121 
122  bool prev = true;
123  if (index>0)
124  prev = this->isBreakChar(text[index-1]);
125 
126  bool next = true;
127  if (index+1<text.size())
128  next = this->isBreakChar(text[index+1]);
129 
130  if (!prev || !next)
131  return true;
132 
133  return false;
134 }
135 
136 QString HelpEngine::convertToKeyword(QString id) const
137 {
138  // convert camel case strings into whitespace-separated lowercase strings:
139  // MyWidget -> my widget
140  // myFancy3DWidget2D -> my Fancy 3D Widget 2D -> my fancy 3d widget 2d
141  // myFancyDWidget -> my Fancy D Widget -> my fancy D widget
142  QString retval;
143  retval.push_back(id[0]);
144  for (int i=1; i<id.size(); ++i)
145  {
146  // break condition Q is (uppercase or digit)
147  // insert whitespace before Q
148  // ignore if previous was Q
149  if (this->isBreakChar(id, i))
150  {
151  retval.push_back("_"); // cant't get doxygenerated anchors to work properly with whitespace
152  }
153  retval.push_back(id[i]);
154  }
155  return retval.toLower();
156 }
157 
158 QString HelpEngine::findBestMatchingKeyword(QObject* object)
159 {
160  while (object)
161  {
162  QString id = this->convertToKeyword(object->objectName());
163 #ifdef DEBUG_HELP_SYSTEM
164  CX_LOG_CHANNEL_DEBUG("HELP_DB") << QString(" examining [%1](%2)")
165  .arg(id)
166  .arg(object->metaObject()->className());
167 #endif
168  if (id.contains("help_widget"))
169  return "";
170 
171  QMap<QString, QUrl> links = this->engine()->linksForIdentifier(id);
172  if (!links.empty())
173  {
174  return id;
175  }
176 
177  object = object->parent();
178  }
179  return "";
180 }
181 
182 
183 }
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:121
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
QHelpEngine * engine()
Definition: cxHelpEngine.h:65
#define CX_LOG_CHANNEL_DEBUG(channel)
Definition: cxLogger.h:120
#define CX_LOG_WARNING
Definition: cxLogger.h:112
static QString getDocPath()
return path to folder containing documentation files
void keywordActivated(QString)