fw4spl
SShowAbout.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 <QApplication>
8 #if defined(QT_WEBKIT)
9 #include <QWebView>
10 #include <QWebPage>
11 #else
12 #include <QTextBrowser>
13 #endif
14 #include <QDesktopServices>
15 #include <QDialog>
16 #include <QFrame>
17 #include <QHBoxLayout>
18 #include <QPushButton>
19 #include <QVBoxLayout>
20 
21 #include <boost/filesystem/operations.hpp>
22 #include <fwCore/base.hpp>
23 #include <fwServices/macros.hpp>
24 #include <fwRuntime/operations.hpp>
25 
26 #include "uiGenericQt/action/SShowAbout.hpp"
27 
28 namespace uiGenericQt
29 {
30 namespace action
31 {
32 
33 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::uiGenericQt::action::SShowAbout, ::fwData::Object );
34 
35 //------------------------------------------------------------------------------
36 
37 SShowAbout::SShowAbout( ) noexcept :
38  m_bServiceIsConfigured(false),
39  m_fsAboutPath(""),
40  m_title("About"),
41  m_size(500, 300)
42 {
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 SShowAbout::~SShowAbout() noexcept
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 void SShowAbout::info(std::ostream& _sstream )
54 {
55  _sstream << "SShowAbout" << std::endl;
56 }
57 
58 //------------------------------------------------------------------------------
59 
61 {
63 
64  typedef SPTR (::fwRuntime::ConfigurationElement) ConfigurationElement;
65 
66  ConfigurationElement cfgFilename = m_configuration->findConfigurationElement("filename");
67  ConfigurationElement cfgTitle = m_configuration->findConfigurationElement("title");
68  ConfigurationElement cfgSize = m_configuration->findConfigurationElement("size");
69 
70  if(cfgFilename)
71  {
72  const std::string& filename = cfgFilename->getExistingAttributeValue("id");
73  // Convert the path from a bundle location
74  m_fsAboutPath = ::fwRuntime::getBundleResourceFilePath(filename);
75 
76  m_bServiceIsConfigured = ::boost::filesystem::exists(m_fsAboutPath);
77  SLM_WARN_IF("About file " + filename + " doesn't exist", !m_bServiceIsConfigured);
78  SLM_TRACE("Filename found '" + filename + "'");
79  }
80 
81  if(cfgTitle)
82  {
83  m_title = cfgTitle->getValue();
84  SLM_TRACE("Set title to '" + m_title + "'");
85  }
86 
87  if(cfgSize)
88  {
89  const std::string& w = cfgSize->getExistingAttributeValue("width");
90  const std::string& h = cfgSize->getExistingAttributeValue("height");
91 
92  m_size.setWidth(std::stoi(w));
93  m_size.setHeight(std::stoi(h));
94 
95  OSLM_TRACE("Set frame size to (" << m_size.width() << ", " << m_size.height() << ")");
96  }
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 {
103  SLM_TRACE_FUNC();
104  SLM_ASSERT("The service 'SShowAbout' isn't configured properly.", m_bServiceIsConfigured );
105 
106  QDialog* dialog = new QDialog(qApp->activeWindow());
107  dialog->setWindowTitle(QString::fromStdString(m_title));
108  QUrl url(QString::fromStdString(m_fsAboutPath.string()));
109 #if defined(QT_WEBKIT)
110  QWebView* htmlView = new QWebView(dialog);
111  htmlView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
112  htmlView->load( url );
113  QObject::connect( htmlView, SIGNAL(linkClicked(const QUrl &)), this, SLOT(onUrlClicked(const QUrl &)));
114 #else
115  QTextBrowser* htmlView = new QTextBrowser(dialog);
116  htmlView->setSource(url);
117  htmlView->setOpenExternalLinks(true);
118  htmlView->setMinimumSize(m_size);
119  QStringList searchPaths;
120  searchPaths.append(QString::fromStdString(m_fsAboutPath.parent_path().string()));
121  htmlView->setSearchPaths(searchPaths);
122 #endif
123  QPushButton* okButton = new QPushButton(QObject::tr("Ok"));
124  QHBoxLayout* hLayout = new QHBoxLayout();
125  hLayout->addStretch();
126  hLayout->addWidget(okButton);
127  hLayout->setContentsMargins(5, 5, 5, 5);
128 
129  QFrame* line = new QFrame(dialog);
130  line->setFrameShape(QFrame::HLine);
131  line->setFrameShadow(QFrame::Sunken);
132 
133  QVBoxLayout* layout = new QVBoxLayout();
134  layout->addWidget(htmlView, 0);
135  layout->addWidget(line, 0);
136  layout->addLayout(hLayout, 0);
137  layout->setContentsMargins(0, 0, 0, 0);
138  layout->setSpacing(0);
139  dialog->setLayout( layout );
140 
141  QObject::connect(okButton, SIGNAL(clicked()), dialog, SLOT(accept()));
142  QObject::connect(dialog, SIGNAL(accepted()), dialog, SLOT(deleteLater()));
143  dialog->setModal(true);
144  dialog->show();
145 }
146 
147 //------------------------------------------------------------------------------
148 
150 {
152 }
153 
154 //------------------------------------------------------------------------------
155 
157 {
159 }
160 
161 //------------------------------------------------------------------------------
162 
163 void SShowAbout::onUrlClicked(const QUrl& url )
164 {
165  QDesktopServices::openUrl(url);
166 }
167 
168 //------------------------------------------------------------------------------
169 
170 } // namespace action
171 } // namespace uiGenericQt
#define SPTR(_cls_)
void stopping() override
Stops action.
Definition: SShowAbout.cpp:156
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
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 configuration element class.
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
void starting() override
Starts action.
Definition: SShowAbout.cpp:149
This action show the about frame.
Definition: SShowAbout.hpp:29
void info(std::ostream &_sstream) override
Prints service info.
Definition: SShowAbout.cpp:53
#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.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
void updating() override
Shows the frame.
Definition: SShowAbout.cpp:101
void onUrlClicked(const QUrl &url)
Triggered when an URL is clicked in the about frame.
Definition: SShowAbout.cpp:163
void configuring() override
Configuring method.
Definition: SShowAbout.cpp:60
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265