CustusX  2023.01.05-dev+develop.0da12
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxHelpEngine.h"
13 
14 #include <QHelpEngine>
15 #include "cxDataLocations.h"
16 #include <iostream>
17 #include "cxTypeConversions.h"
18 #include <QFileInfo>
19 #include <QApplication>
20 #include <QWidget>
21 #include "cxLogger.h"
22 #include "cxProfile.h"
23 #include <QDir>
24 #include <QTimer>
25 
26 //#define DEBUG_HELP_SYSTEM // turn on to output help system focus information
27 
28 namespace cx
29 {
30 
32 {
33  QDir().mkpath(profile()->getPath()); // otherwise setupData() fails sometimes
34  QString helpFile = profile()->getPath() + "/cx_user_doc.qhc";
35  helpEngine = new QHelpEngine(helpFile, NULL);
36 
37  this->setupDataWithWarning();
38  this->setupDocFile();
39  this->setupDataWithWarning();
40 
41  connect(qApp, SIGNAL(focusObjectChanged(QObject*)), this, SLOT(focusObjectChanged(QObject*)));
42  connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
43 
44  QTimer::singleShot(100, this, SLOT(setInitialPage()));
45 }
46 
48 {
49  delete helpEngine;
50 }
51 
52 void HelpEngine::setupDocFile()
53 {
54  QString docFile = DataLocations::getDocPath()+"/cx_user_doc.qch";
55  if(!QFile(docFile).exists())
56  reportWarning(QString("HelpEngine: Cannot find docFile: %1").arg(docFile));
57  helpEngine->unregisterDocumentation(helpEngine->namespaceName(docFile));
58  if(!helpEngine->registerDocumentation(docFile))
59  reportWarning(QString("HelpEngine: Documentation registration failed: %1").arg(helpEngine->error()));
60 }
61 
62 void HelpEngine::setupDataWithWarning()
63 {
64  bool success = helpEngine->setupData();
65  // had problems here before mkdir was called on the qhc path
66  if (!success)
67  CX_LOG_WARNING() << QString("Help engine setup failed with error [%1]").arg(helpEngine->error());
68 }
69 
70 void HelpEngine::setInitialPage()
71 {
72  emit keywordActivated("user_doc_overview");
73 }
74 
75 void HelpEngine::focusChanged(QWidget * old, QWidget * now)
76 {
77  if (!now)
78  return;
79 }
80 
81 void HelpEngine::focusObjectChanged(QObject* newFocus)
82 {
83  if (!newFocus)
84  return;
85 #ifdef DEBUG_HELP_SYSTEM
86  CX_LOG_CHANNEL_INFO("HELP_DB") << QString("**Focus on [%1]: %2").arg(newFocus->objectName()).arg(dynamic_cast<QWidget*>(newFocus)->windowTitle());
87 #endif
88  QString keyword = this->findBestMatchingKeyword(newFocus);
89  if (!keyword.isEmpty())
90  {
91 #ifdef DEBUG_HELP_SYSTEM
92  CX_LOG_CHANNEL_INFO("HELP_DB") << QString(" Found keyword [%1]").arg(keyword);
93 #endif
94  emit keywordActivated(keyword);
95  }
96 }
97 
98 bool HelpEngine::isBreakChar(QChar c) const
99 {
100  return c.isDigit() || c.isUpper();
101 }
102 
103 bool HelpEngine::isBreakChar(QString text, int index) const
104 {
105  if (!this->isBreakChar(text[index]))
106  return false;
107 
108  bool prev = true;
109  if (index>0)
110  prev = this->isBreakChar(text[index-1]);
111 
112  bool next = true;
113  if (index+1<text.size())
114  next = this->isBreakChar(text[index+1]);
115 
116  if (!prev || !next)
117  return true;
118 
119  return false;
120 }
121 
122 QString HelpEngine::findBestMatchingKeyword(QObject* object)
123 {
124  while (object)
125  {
126  QString id = object->objectName();
127 #ifdef DEBUG_HELP_SYSTEM
128  CX_LOG_CHANNEL_DEBUG("HELP_DB") << QString(" examining [%1](%2)")
129  .arg(id)
130  .arg(object->metaObject()->className());
131 #endif
132  if (id.contains("help_widget"))
133  return "";
134 
135  QMap<QString, QUrl> links = this->engine()->linksForIdentifier(id);
136  if (!links.empty())
137  {
138  return id;
139  }
140 
141  object = object->parent();
142  }
143  return "";
144 }
145 
146 
147 }
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:108
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
QHelpEngine * engine()
Definition: cxHelpEngine.h:44
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
#define CX_LOG_CHANNEL_DEBUG(channel)
Definition: cxLogger.h:107
#define CX_LOG_WARNING
Definition: cxLogger.h:98
static QString getDocPath()
return path to folder containing documentation files
Namespace for all CustusX production code.
void keywordActivated(QString)