CustusX  15.3.4-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 
45 namespace cx
46 {
47 
49 {
50  QString helpFile = profile()->getPath() + "/cx_user_doc.qhc";
51  helpEngine = new QHelpEngine(helpFile, NULL);
52  bool success = helpEngine->setupData();
53  // CX_LOG_CHANNEL_DEBUG("CA") << "Regdocs loaded: " << helpEngine->registeredDocumentations().join("--");
54 
55  QString docFile = DataLocations::getDocPath()+"/cx_user_doc.qch";
56  helpEngine->registerDocumentation(docFile);
57  // CX_LOG_CHANNEL_DEBUG("CA") << "Setup help data: " << success << " - " << docFile;
58  // CX_LOG_CHANNEL_DEBUG("CA") << "Last help error: " << helpEngine->error();
59  // CX_LOG_CHANNEL_DEBUG("CA") << "Regdocs: " << helpEngine->registeredDocumentations().join("--");
60 
61  connect(qApp, SIGNAL(focusObjectChanged(QObject*)), this, SLOT(focusObjectChanged(QObject*)));
62  connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
63 }
64 
66 {
67  delete helpEngine;
68 // std::cout << "HelpEngine::~HelpEngine()" << std::endl;
69 }
70 
71 void HelpEngine::focusChanged(QWidget * old, QWidget * now)
72 {
73  if (!now)
74  return;
75  // std::cout << "new widget focus: " << now->objectName() << std::endl;
76 
77 }
78 
79 void HelpEngine::focusObjectChanged(QObject* newFocus)
80 {
81  if (!newFocus)
82  return;
83 // std::cout << "HelpEngine::focusObjectChanged " << newFocus->objectName() << " -- " << dynamic_cast<QWidget*>(newFocus)->windowTitle() << std::endl;
84  QString keyword = this->findBestMatchingKeyword(newFocus);
85  if (!keyword.isEmpty())
86  {
87 // std::cout << "******** keyword: " << keyword << std::endl;
88  emit keywordActivated(keyword);
89  }
90 }
91 
92 bool HelpEngine::isBreakChar(QChar c) const
93 {
94  return c.isDigit() || c.isUpper();
95 }
96 
97 bool HelpEngine::isBreakChar(QString text, int index) const
98 {
99  if (!this->isBreakChar(text[index]))
100  return false;
101 
102  bool prev = true;
103  if (index>0)
104  prev = this->isBreakChar(text[index-1]);
105 
106  bool next = true;
107  if (index+1<text.size())
108  next = this->isBreakChar(text[index+1]);
109 
110  if (!prev || !next)
111  return true;
112 
113  return false;
114 }
115 
116 QString HelpEngine::convertToKeyword(QString id) const
117 {
118  // convert camel case strings into whitespace-separated lowercase strings:
119  // MyWidget -> my widget
120  // myFancy3DWidget2D -> my Fancy 3D Widget 2D -> my fancy 3d widget 2d
121  // myFancyDWidget -> my Fancy D Widget -> my fancy D widget
122  QString retval;
123  retval.push_back(id[0]);
124  for (int i=1; i<id.size(); ++i)
125  {
126  // break condition Q is (uppercase or digit)
127  // insert whitespace before Q
128  // ignore if previous was Q
129  if (this->isBreakChar(id, i))
130  {
131  retval.push_back("_"); // cant't get doxygenerated anchors to work properly with whitespace
132  }
133  retval.push_back(id[i]);
134  }
135  return retval.toLower();
136 }
137 
138 QString HelpEngine::findBestMatchingKeyword(QObject* object)
139 {
140  while (object)
141  {
142  QString id = this->convertToKeyword(object->objectName());
143 // std::cout << " examining " << object->objectName() << ", keyword = " << id << std::endl;
144 
145  if (id.contains("help_widget"))
146  return "";
147 
148  QMap<QString, QUrl> links = this->engine()->linksForIdentifier(id);
149  if (!links.empty())
150  {
151  return id;
152  }
153 
154  object = object->parent();
155  }
156  return "";
157 }
158 
159 }
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
QHelpEngine * engine()
Definition: cxHelpEngine.h:64
static QString getDocPath()
return path to folder containing documentation files
void keywordActivated(QString)