fw4spl
Bundles/ui/guiQt/src/guiQt/Plugin.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 "guiQt/Plugin.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGuiQt/App.hpp>
12 #include <fwGuiQt/WorkerQt.hpp>
13 
14 #include <fwRuntime/operations.hpp>
15 #include <fwRuntime/profile/Profile.hpp>
16 #include <fwRuntime/utils/GenericExecutableFactoryRegistrar.hpp>
17 
18 #include <fwServices/macros.hpp>
19 #include <fwServices/registry/ActiveWorkers.hpp>
20 
21 #include <QFile>
22 #include <QResource>
23 #include <QString>
24 #include <QStyleFactory>
25 #include <QTextStream>
26 
27 #include <functional>
28 
29 namespace guiQt
30 {
31 //-----------------------------------------------------------------------------
32 
33 static ::fwRuntime::utils::GenericExecutableFactoryRegistrar<Plugin> registrar("::guiQt::Plugin");
34 
35 //-----------------------------------------------------------------------------
36 
37 Plugin::~Plugin() noexcept
38 {
39 }
40 
41 //-----------------------------------------------------------------------------
42 
44 {
45  ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::profile::getCurrentProfile();
46  SLM_ASSERT("Profile is not initialized", profile);
47  int& argc = profile->getRawArgCount();
48  char** argv = profile->getRawParams();
49 
50  m_workerQt = ::fwGuiQt::getQtWorker(argc, argv);
51 
53 
54  m_workerQt->post( std::bind( &Plugin::loadStyleSheet, this ) );
55 
56  ::fwRuntime::profile::getCurrentProfile()->setRunCallback(std::bind(&Plugin::run, this));
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 void Plugin::stop() noexcept
62 {
63  if(m_workerQt)
64  {
65  m_workerQt->stop();
66  m_workerQt.reset();
67  }
68 }
69 
70 //-----------------------------------------------------------------------------
71 
72 void setup()
73 {
74  ::fwRuntime::profile::getCurrentProfile()->setup();
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 int Plugin::run() noexcept
80 {
81  m_workerQt->post( std::bind( &setup ) );
82  m_workerQt->getFuture().wait(); // This is required to start WorkerQt loop
83 
84  ::fwRuntime::profile::getCurrentProfile()->cleanup();
85  int result = ::boost::any_cast<int>(m_workerQt->getFuture().get());
86 
88  m_workerQt.reset();
89 
90  return result;
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 void Plugin::loadStyleSheet()
96 {
97  if( this->getBundle()->hasParameter("resource") )
98  {
99  const std::string resourceFile = this->getBundle()->getParameterValue("resource");
100  const auto path = fwRuntime::getBundleResourceFilePath(resourceFile);
101 
102  const bool resourceLoaded = QResource::registerResource(path.string().c_str());
103  SLM_ASSERT("Cannot load resources '"+resourceFile+"'.", resourceLoaded);
104  }
105 
106  if( this->getBundle()->hasParameter("style") )
107  {
108  const std::string style = this->getBundle()->getParameterValue("style");
109  qApp->setStyle(QStyleFactory::create(QString::fromStdString(style)));
110  }
111 
112  if( this->getBundle()->hasParameter("stylesheet") )
113  {
114  const std::string stylesheetFile = this->getBundle()->getParameterValue("stylesheet");
115  const auto path = fwRuntime::getBundleResourceFilePath(stylesheetFile);
116 
117  QFile data(QString::fromStdString(path.string()));
118  QString style;
119  if(data.open(QFile::ReadOnly))
120  {
121  QTextStream styleIn(&data);
122  style = styleIn.readAll();
123  data.close();
124  qApp->setStyleSheet(style);
125  }
126  }
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 } // namespace guiQt
GUIQT_API void start()
Notifies the plugin about its start.
static FWSERVICES_API void setDefaultWorker(::fwThread::Worker::sptr worker)
Register the default active worker.
GUIQT_API ~Plugin() noexcept
destructor
static FWSERVICES_API ActiveWorkers::sptr getDefault()
Returns an instance of ActiveWorkers.
#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
FWRUNTIME_API std::shared_ptr< Bundle > getBundle() const noexcept override
Retrieves the bundle the executable originates from.
FWRUNTIME_API::boost::filesystem::path getBundleResourceFilePath(const std::string &bundleIdentifier, const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a path relative to the bundle having the specified identifier...
Definition: operations.cpp:106
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
GUIQT_API void stop() noexcept
Notifies the plugin about its stop.