fw4spl
ShowHelpContents.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "uiGenericQt/action/ShowHelpContents.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/Cursor.hpp>
12 #include <fwGui/dialog/MessageDialog.hpp>
13 
14 #include <fwServices/macros.hpp>
15 
16 #include <boost/filesystem/operations.hpp>
17 
18 #include <QApplication>
19 #include <QDialog>
20 #include <QHBoxLayout>
21 #include <QHelpContentWidget>
22 #include <QHelpEngine>
23 #include <QObject>
24 #include <QSplitter>
25 #include <QTextBrowser>
26 
27 namespace uiGenericQt
28 {
29 namespace action
30 {
31 
32 //------------------------------------------------------------------------------
34 class HelpBrowser : public QTextBrowser
35 {
36 public:
37  HelpBrowser(QHelpEngine* helpEngine, QWidget* parent = 0) :
38  QTextBrowser(parent),
39  helpEngine(helpEngine)
40  {
41  }
42 
43  //------------------------------------------------------------------------------
44 
45  QVariant loadResource(int type, const QUrl& url)
46  {
47  if (url.scheme() == "qthelp")
48  {
49  return QVariant(helpEngine->fileData(url));
50  }
51  else
52  {
53  return QTextBrowser::loadResource(type, url);
54  }
55  }
56 
57 private:
58  QHelpEngine* helpEngine;
59 };
60 //------------------------------------------------------------------------------
61 
63 
64 //------------------------------------------------------------------------------
65 
66 ShowHelpContents::ShowHelpContents( ) noexcept :
67  m_bServiceIsConfigured(false),
68  m_fsHelpPath("")
69 {
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 ShowHelpContents::~ShowHelpContents() noexcept
75 {
76 }
77 
78 //------------------------------------------------------------------------------
79 
80 void ShowHelpContents::info(std::ostream& _sstream )
81 {
82  _sstream << "Action for show help contents" << std::endl;
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  /*
90  * .qhp/.qch (source/binary) : Contains a table of contents,
91  * an index of items in the documentation, and a file manifest.
92  * .qhcp/.qhc (source/binary): Contains information that is used to customize
93  * the appearance and available features of Qt Assistant.
94  */
96  if( m_configuration->findConfigurationElement("filename") )
97  {
98  std::string filename = m_configuration->findConfigurationElement("filename")->getExistingAttributeValue("id");
99  m_fsHelpPath = ::boost::filesystem::path( filename );
100  m_bServiceIsConfigured = ::boost::filesystem::exists(m_fsHelpPath);
101  OSLM_WARN_IF("Help file " <<filename<< " doesn't exist", !m_bServiceIsConfigured);
102  OSLM_TRACE("Filename found " << filename );
103  }
104 }
105 
106 //------------------------------------------------------------------------------
107 
109 {
110  SLM_TRACE_FUNC();
111  SLM_ASSERT("The Help service isn't configured properly.", m_bServiceIsConfigured);
112 
113  QDialog* dialog = new QDialog(qApp->activeWindow());
114  dialog->setWindowTitle(QString("Help"));
115  QHelpEngine* helpEngine = new QHelpEngine(QString::fromStdString(m_fsHelpPath.string()), dialog);
116  if (!helpEngine->setupData())
117  {
118  OSLM_ERROR("HelpEngine error: " << helpEngine->error().toStdString());
120  messageBox.setTitle("Warning");
121  messageBox.setMessage( "Help file is missing or not correct." );
122  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
123  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
124  messageBox.show();
125  // Setup help engine information failed.
126  // qhc (Qt Help Collection) or qch (Qt Compressed Help) file is not correct.
127  }
128  else
129  {
130  QSplitter* helpPanel = new QSplitter(Qt::Horizontal);
131  HelpBrowser* helpBrowser = new HelpBrowser(helpEngine, dialog);
132  helpPanel->insertWidget(0, helpEngine->contentWidget());
133  helpPanel->insertWidget(1, helpBrowser);
134  helpPanel->setStretchFactor(1, 1);
135 
136  QHBoxLayout* hLayout = new QHBoxLayout();
137  hLayout->addWidget(helpPanel);
138  dialog->setLayout( hLayout );
139  QObject::connect(helpEngine->contentWidget(), SIGNAL(linkActivated(const QUrl &)), helpBrowser,
140  SLOT(setSource(const QUrl &)));
141 
142  dialog->exec();
143  }
144 }
145 
146 //------------------------------------------------------------------------------
147 
149 {
151 }
152 
153 //------------------------------------------------------------------------------
154 
156 {
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 } // namespace action
163 } // namespace uiGenericQt
void stopping() override
Stops action.
void configuring() override
Configuring method.
void updating() override
Show the frame.
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
The namespace uiGenericQt contains actions to show help, acknowledgments and about frame...
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Defines the generic message box for IHM. Use the Delegate design pattern.
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
This action show the help contents.
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
Base class for each data object.
UIGENERICQT_API void info(std::ostream &_sstream) override
Write information in a stream.
#define OSLM_WARN_IF(message, cond)
Definition: spyLog.hpp:267
void starting() override
Starts action.
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.