OpenWalnut  1.5.0dev
WQtGui.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WQTGUI_H
26 #define WQTGUI_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <QApplication>
33 #include <QSplashScreen>
34 #include <QtCore/QMutex>
35 #include <QtCore/QSettings>
36 #include <boost/function.hpp>
37 #include <boost/program_options.hpp>
38 
39 #include "WIconManager.h"
40 #include "abstractUI/WUIQtWidgetFactory.h"
41 #include "core/common/WDefines.h"
42 #include "core/graphicsEngine/WGraphicsEngine.h"
43 #include "core/graphicsEngine/WROI.h"
44 #include "core/kernel/WModule.h"
45 #include "core/ui/WUI.h"
46 #include "events/WDeferredCallEvent.h"
47 
48 class WMainWindow;
49 class WKernel;
50 
51 /**
52  * The QT Based GUI implementation. Implements WUI.
53  * \ingroup ui
54  */
55 class WQtGui : public WUI
56 {
57 public:
58  /**
59  * Constructor.
60  *
61  * \param options the option-variable map
62  * \param argc number of arguments given on command line.
63  * \param argv arguments given on command line.
64  */
65  WQtGui( const boost::program_options::variables_map& options, int argc, char** argv );
66 
67  /**
68  * Default destructor.
69  */
70  virtual ~WQtGui();
71 
72  /**
73  * Runs the GUI. All initialization should be done here.
74  *
75  * \return the return code.
76  */
77  virtual int run();
78 
79  /**
80  * returns a pointer to the selected module in the control panel
81  *
82  * \return the module
83  */
84  virtual std::shared_ptr< WModule > getSelectedModule();
85 
86  /**
87  * Slot gets called whenever we need to update the texture sorter.
88  */
89  virtual void slotUpdateTextureSorter();
90 
91  /**
92  * Slot gets called whenever a new module is added.
93  *
94  * \param module the module to be added
95  *
96  * \note This can be used to add datasets or other modules.
97  */
98  virtual void slotAddDatasetOrModuleToTree( std::shared_ptr< WModule > module );
99 
100  /**
101  * Slot gets called whenever a new ROI is added.
102  *
103  * \param roi the ROI to be added
104  */
105  virtual void slotAddRoiToTree( osg::ref_ptr< WROI > roi );
106 
107  /**
108  * Slot gets called whenever a ROI is removed.
109  *
110  * \param roi the ROI to be removed
111  */
112  virtual void slotRemoveRoiFromTree( osg::ref_ptr< WROI > roi );
113 
114  /**
115  * Slot gets called whenever a module switches its state to "ready".
116  *
117  * \param module the module.
118  */
119  virtual void slotActivateDatasetOrModuleInTree( std::shared_ptr< WModule > module );
120 
121  /**
122  * Slot gets called whenever a module has been removed from the root container.
123  *
124  * \param module the module
125  */
126  virtual void slotRemoveDatasetOrModuleInTree( std::shared_ptr< WModule > module );
127 
128  /**
129  * Slot gets called whenever a connector pair got connected.
130  *
131  * \param in input connector
132  * \param out output connector
133  */
134  virtual void slotConnectionEstablished( std::shared_ptr<WModuleConnector> in, std::shared_ptr<WModuleConnector> out );
135 
136  /**
137  * Slot gets called whenever a connector pair got disconnected.
138  *
139  * \param in input connector
140  * \param out output connector
141  */
142  virtual void slotConnectionClosed( std::shared_ptr<WModuleConnector> in, std::shared_ptr<WModuleConnector> out );
143 
144  /**
145  * Getter functions for the signals provided by the gui
146  * \return the signal
147  */
148  boost::signals2::signal< void( std::vector< std::string > ) >* getLoadButtonSignal();
149 
150  /**
151  * Returns the current main window instance or NULL if not existent.
152  *
153  * \return the main window instance.
154  */
155  static WMainWindow* getMainWindow();
156 
157  /**
158  * Returns the settings object.
159  *
160  * \return settings object.
161  */
162  static QSettings& getSettings();
163 
164  /**
165  * Get the icon manager of this gui instance.
166  *
167  * \return the icon manager.
168  */
169  static WIconManager* getIconManager();
170 
171  /**
172  * Returns the option map for the current instance of this GUI. This can be useful to parse further commandline parameters
173  *
174  * \return the option map
175  */
176  const boost::program_options::variables_map& getOptionMap() const;
177 
178  /**
179  * Returns the widget factory of the UI.
180  *
181  * \return the factory. Use this to create your widget instances.
182  */
184 
185  /**
186  * Call a given function from within the GUI thread. The function posts an event and waits for its execution.
187  *
188  * \param functor the function to call (you can implement structs with operator() too if you need parameters and return values)
189  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
190  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
191  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call (but still the
192  * method blocks until execution).
193  */
194  static void execInGUIThread( boost::function< void( void ) > functor, WCondition::SPtr notify = WCondition::SPtr() );
195 
196  /**
197  * Call a given function from within the GUI thread. The function posts an event and waits for its execution.
198  *
199  * \param functor the function to call (you can implement structs with operator() too if you need parameters and return values)
200  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
201  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
202  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call (but still the
203  * method blocks until execution).
204  *
205  * \return the result
206  * \tparam Result the type of the function return value.
207  */
208  template< typename Result >
209  static Result execInGUIThread( boost::function< Result( void ) > functor, WCondition::SPtr notify = WCondition::SPtr() )
210  {
211  if( !notify )
212  {
213  // the user did not specify a condition. We create our own
214  notify = WCondition::SPtr( new WConditionOneShot() );
215  }
216  Result result; // stores result
217  WDeferredCallResultEvent< Result >* ev = new WDeferredCallResultEvent< Result >( functor, &result, notify );
218  QCoreApplication::postEvent( getMainWindow(), ev );
219  notify->wait();
220  return result;
221  }
222 
223  /**
224  * Call a given function from within the GUI thread. The function posts an event and DOES NOT wait for its execution.
225  *
226  * \param functor the function to call (you can implement structs with operator() too if you need parameters and return values)
227  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
228  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
229  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call.
230  */
231  static void execInGUIThreadAsync( boost::function< void( void ) > functor, WCondition::SPtr notify = WCondition::SPtr() );
232 protected:
233  /**
234  * Called whenever a module crashes.
235  *
236  * \param module the module which has thrown the exception
237  * \param exception the exception the module has thrown
238  */
239  void moduleError( std::shared_ptr< WModule > module, const WException& exception );
240 
241 private:
242  /**
243  * Object storing certain persistent application settings.
244  */
245  static QSettings* m_settings;
246 
247  /**
248  * Main window containing all needed widgets.
249  */
251 
252  /**
253  * Graphics Engine instance.
254  */
255  std::shared_ptr< WGraphicsEngine > m_ge;
256 
257  /**
258  * The connection to the AddLog signal of the logger
259  */
260  boost::signals2::connection m_loggerConnection;
261 
262  /**
263  * Kernel instance.
264  */
265  std::shared_ptr< WKernel > m_kernel;
266 
267  const boost::program_options::variables_map& m_optionsMap; //!< Map storing the program options.
268 
269  /**
270  * If true, the next trigger of deferredLoad will actually do loading. This variable and the deferredLoad function are protected
271  * with m_deferredLoadMutex.
272  */
274 
275  /**
276  * This mutex protects the deferredLoad method from being called in parallel or twice.
277  */
279 
280  /**
281  * New log item added. Pushing event to QT's event queue.
282  *
283  * \param entry the entry added.
284  */
285  void slotAddLog( const WLogEntry& entry );
286 
287  /**
288  * This is called by the GE when the osg was set-up correctly. This triggers project and data file loading.
289  *
290  * \note can be called from an arbitrary thread. Protected by m_deferredLoadMutex.
291  */
292  void deferredLoad();
293 
294  /**
295  * The splash screen.
296  */
297  QSplashScreen* m_splash;
298 
299  /**
300  * The widget factory which handles WUI widget creation.
301  */
303 };
304 
305 #endif // WQTGUI_H
Implements a WCondition, but can be fired only ONCE.
std::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:48
Derived WDeferredCallEvent allowing result values in calls.
Basic exception handler.
Definition: WException.h:39
Manages icon access.
Definition: WIconManager.h:41
OpenWalnut kernel, managing modules and interaction between UI, GE and DataHandler.
Definition: WKernel.h:61
Represents a simple log message with some attributes.
Definition: WLogEntry.h:57
This class contains the main window and the layout of the widgets within the window.
Definition: WMainWindow.h:66
The QT Based GUI implementation.
Definition: WQtGui.h:56
virtual void slotAddDatasetOrModuleToTree(std::shared_ptr< WModule > module)
Slot gets called whenever a new module is added.
Definition: WQtGui.cpp:300
virtual std::shared_ptr< WModule > getSelectedModule()
returns a pointer to the selected module in the control panel
Definition: WQtGui.cpp:384
virtual int run()
Runs the GUI.
Definition: WQtGui.cpp:148
const boost::program_options::variables_map & getOptionMap() const
Returns the option map for the current instance of this GUI.
Definition: WQtGui.cpp:399
static QSettings & getSettings()
Returns the settings object.
Definition: WQtGui.cpp:394
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
QSplashScreen * m_splash
The splash screen.
Definition: WQtGui.h:297
virtual ~WQtGui()
Default destructor.
Definition: WQtGui.cpp:77
virtual void slotRemoveDatasetOrModuleInTree(std::shared_ptr< WModule > module)
Slot gets called whenever a module has been removed from the root container.
Definition: WQtGui.cpp:331
virtual void slotRemoveRoiFromTree(osg::ref_ptr< WROI > roi)
Slot gets called whenever a ROI is removed.
Definition: WQtGui.cpp:315
virtual void slotActivateDatasetOrModuleInTree(std::shared_ptr< WModule > module)
Slot gets called whenever a module switches its state to "ready".
Definition: WQtGui.cpp:320
static WMainWindow * m_mainWindow
Main window containing all needed widgets.
Definition: WQtGui.h:250
bool m_loadDeferredOnce
If true, the next trigger of deferredLoad will actually do loading.
Definition: WQtGui.h:273
static WIconManager * getIconManager()
Get the icon manager of this gui instance.
Definition: WQtGui.cpp:93
static void execInGUIThreadAsync(boost::function< void(void) > functor, WCondition::SPtr notify=WCondition::SPtr())
Call a given function from within the GUI thread.
Definition: WQtGui.cpp:421
WQtGui(const boost::program_options::variables_map &options, int argc, char **argv)
Constructor.
Definition: WQtGui.cpp:70
boost::signals2::connection m_loggerConnection
The connection to the AddLog signal of the logger.
Definition: WQtGui.h:260
boost::signals2::signal< void(std::vector< std::string >) > * getLoadButtonSignal()
Getter functions for the signals provided by the gui.
Definition: WQtGui.cpp:389
virtual void slotConnectionEstablished(std::shared_ptr< WModuleConnector > in, std::shared_ptr< WModuleConnector > out)
Slot gets called whenever a connector pair got connected.
Definition: WQtGui.cpp:342
std::shared_ptr< WGraphicsEngine > m_ge
Graphics Engine instance.
Definition: WQtGui.h:255
static QSettings * m_settings
Object storing certain persistent application settings.
Definition: WQtGui.h:245
std::shared_ptr< WKernel > m_kernel
Kernel instance.
Definition: WQtGui.h:265
virtual void slotUpdateTextureSorter()
Slot gets called whenever we need to update the texture sorter.
Definition: WQtGui.cpp:283
static Result execInGUIThread(boost::function< Result(void) > functor, WCondition::SPtr notify=WCondition::SPtr())
Call a given function from within the GUI thread.
Definition: WQtGui.h:209
virtual void slotConnectionClosed(std::shared_ptr< WModuleConnector > in, std::shared_ptr< WModuleConnector > out)
Slot gets called whenever a connector pair got disconnected.
Definition: WQtGui.cpp:363
void deferredLoad()
This is called by the GE when the osg was set-up correctly.
Definition: WQtGui.cpp:98
virtual WUIWidgetFactory::SPtr getWidgetFactory() const
Returns the widget factory of the UI.
Definition: WQtGui.cpp:404
QMutex m_deferredLoadMutex
This mutex protects the deferredLoad method from being called in parallel or twice.
Definition: WQtGui.h:278
const boost::program_options::variables_map & m_optionsMap
Map storing the program options.
Definition: WQtGui.h:267
WUIQtWidgetFactory::SPtr m_widgetFactory
The widget factory which handles WUI widget creation.
Definition: WQtGui.h:302
void slotAddLog(const WLogEntry &entry)
New log item added.
Definition: WQtGui.cpp:289
static void execInGUIThread(boost::function< void(void) > functor, WCondition::SPtr notify=WCondition::SPtr())
Call a given function from within the GUI thread.
Definition: WQtGui.cpp:409
void moduleError(std::shared_ptr< WModule > module, const WException &exception)
Called whenever a module crashes.
Definition: WQtGui.cpp:82
virtual void slotAddRoiToTree(osg::ref_ptr< WROI > roi)
Slot gets called whenever a new ROI is added.
Definition: WQtGui.cpp:310
std::shared_ptr< WUIQtWidgetFactory > SPtr
Convenience typedef for a std::shared_ptr< WUIQtWidgetFactory >.
std::shared_ptr< WUIWidgetFactory > SPtr
Convenience typedef for a std::shared_ptr< WUIWidgetFactory >.
This class prescribes the interface to the UI.
Definition: WUI.h:51