OpenWalnut  1.5.0dev
WUIQtWidgetBase.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 WUIQTWIDGETBASE_H
26 #define WUIQTWIDGETBASE_H
27 
28 #include <memory>
29 
30 #include <QWidget>
31 
32 #include "../WMainWindow.h"
33 #include "core/common/WCondition.h"
34 
35 /**
36  * Implementation of \ref WUIWidgetBase. Not really. It is an base class for all WUIWidgetBase implementations to comfortably allow thread-save
37  * GUI ops.
38  */
40 {
41 public:
42  /**
43  * Convenience typedef for a std::shared_ptr< WUIQtWidgetBase >.
44  */
45  typedef std::shared_ptr< WUIQtWidgetBase > SPtr;
46 
47  /**
48  * Convenience typedef for a std::shared_ptr< const WUIQtWidgetBase >.
49  */
50  typedef std::shared_ptr< const WUIQtWidgetBase > ConstSPtr;
51 
52  /**
53  * Default constructor.
54  *
55  * \param mainWindow the main window instance
56  * \param parent the parent widget. Can be NULL.
57  */
58  WUIQtWidgetBase( WMainWindow* mainWindow, WUIQtWidgetBase::SPtr parent );
59 
60  /**
61  * Destructor.
62  */
63  virtual ~WUIQtWidgetBase();
64 
65  /**
66  * The title as QString.
67  *
68  * \return the title.
69  */
70  virtual QString getTitleQString() const = 0;
71 
72  /**
73  * The widget was created and can be used.
74  *
75  * \return true if the widget is valid.
76  */
77  virtual bool isReal();
78 
79  /**
80  * Show this widget if not yet visible.
81  */
82  virtual void show();
83 
84  /**
85  * Hide/show this widget. Unlike close(), you can show the widget again using show().
86  *
87  * \param visible false to hide widget
88  */
89  virtual void setVisible( bool visible = true );
90 
91  /**
92  * Check if the widget is hidden or not.
93  *
94  * \return true if visible.
95  */
96  virtual bool isVisible() const;
97 
98  /**
99  * Realize the widget. This method blocks until the GUI thread created the widget.
100  *
101  * \param abortCondition a condition enforcing abort of widget creation.
102  */
103  virtual void realize( WCondition::SPtr abortCondition = WCondition::SPtr() );
104 
105  /**
106  * Handle shutdown. This includes notification of the creating module and closing the widget. Can be called from any thread.
107  * Implement in your implementation.
108  */
109  virtual void close() = 0;
110 
111  /**
112  * Get the widget representation. Can be NULL if not yet created.
113  *
114  * \return the widget
115  */
116  QWidget* getWidget() const;
117 
118  /**
119  * Get the parent as Qt widget. Can be NULL.
120  *
121  * \return the parent
122  */
123  QWidget* getParentAsQtWidget() const;
124 
125  /**
126  * Parent widget. Can be NULL.
127  *
128  * \return the parent
129  */
131 
132  /**
133  * Implement \ref WUIWidgetBase::addAction.
134  *
135  * \param group the property to use.
136  * \param icon the icon to use. Consider a minimum size of 32x32.
137  */
138  virtual void addAction( WPropGroup group, WGEImage::SPtr icon = WGEImage::SPtr() );
139 
140  /**
141  * Implement \ref WUIWidgetBase::addAction.
142  *
143  * \param trigger the property to use.
144  * \param icon the icon to use. Consider a minimum size of 32x32.
145  */
146  virtual void addAction( WPropTrigger trigger, WGEImage::SPtr icon = WGEImage::SPtr() );
147 
148  /**
149  * Implement \ref WUIWidgetBase::addAction.
150  *
151  * \param toggle the property to use.
152  * \param icon the icon to use. Consider a minimum size of 32x32.
153  */
154  virtual void addAction( WPropBool toggle, WGEImage::SPtr icon = WGEImage::SPtr() );
155 protected:
156  /**
157  * Realize the widget. This method blocks until the GUI thread created the widget. Called from within the GUI thread! So you can safely do Qt
158  * stuff.
159  */
160  virtual void realizeImpl() = 0;
161 
162  /**
163  * Show this widget if not yet visible. Called in GUI Thread (GT).
164  */
165  virtual void showGT();
166 
167  /**
168  * Hide/show this widget. Unlike close(), you can show the widget again using show(). Called in GUI Thread (GT).
169  *
170  * \param visible false to hide widget
171  */
172  virtual void setVisibleGT( bool visible = true );
173 
174  /**
175  * Check if the widget is hidden or not. Called in GUI Thread (GT).
176  *
177  * \return true if visible.
178  */
179  virtual bool isVisibleGT() const;
180 
181  /**
182  * Close the widget. When done, the widget can be safely deleted.
183  */
184  virtual void closeImpl();
185 
186  /**
187  * Close the widget. When done, the widget can be safely deleted. Called in GUI Thread (GT).
188  */
189  virtual void closeGT();
190 
191  /**
192  * Cleanup the GUI. Do not delete m_widget, or your content widget. This is done by WUIQtWidgetBase. This method allows you to free resources
193  * that are not automatically freed by the Qt delete mechanism.
194  */
195  virtual void cleanUpGT() = 0;
196 
197  /**
198  * The main window instance.
199  */
201 
202  /**
203  * The widget representing this abstract UI element.
204  */
205  QWidget* m_widget;
206 
207  /**
208  * Parent widget. Can be NULL.
209  */
211 
212  /**
213  * Returns the parent to use for your implementation in \ref realizeImpl. It is important to understand that this always returns a parent,
214  * regardless of m_parent being NULL or not. This is the parent of your QWidget.
215  *
216  * \return the parent
217  */
218  QWidget* getCompellingQParent() const;
219 
220  /**
221  * Check if the widget is embedded into another WUI widget.
222  *
223  * \return true if there is a WUIQtWidgetBase parent
224  */
225  bool hasUIParent() const;
226 
227  /**
228  * This method can be used if you just create some QWidget and do not want to take care about embedding the content in a dock if there is no
229  * parent, setting size constraints and similar. This method handles this. It is also aware of QDckWidgets. This means it does not embed
230  * them if you create your own QDockWidgets. It then just sets the defaults and registers it at WMainWindow.
231  *
232  * \param content your widget to config and embed
233  *
234  * \return the container (usually WQtDockWidget) or NULL if the content is used stand-alone.
235  */
236  QWidget* embedContent( QWidget* content );
237 
238  /**
239  * Implement \ref WUIWidgetBase::addAction.
240  *
241  * \param group the property to use.
242  * \param icon the icon to use. Consider a minimum size of 32x32.
243  */
244  virtual void addActionGroupGT( WPropGroup group, WGEImage::SPtr icon = WGEImage::SPtr() );
245 
246  /**
247  * Implement \ref WUIWidgetBase::addAction.
248  *
249  * \param trigger the property to use.
250  * \param icon the icon to use. Consider a minimum size of 32x32.
251  */
252  virtual void addActionTriggerGT( WPropTrigger trigger, WGEImage::SPtr icon = WGEImage::SPtr() );
253 
254  /**
255  * Implement \ref WUIWidgetBase::addAction.
256  *
257  * \param toggle the property to use.
258  * \param icon the icon to use. Consider a minimum size of 32x32.
259  */
260  virtual void addActionBoolGT( WPropBool toggle, WGEImage::SPtr icon = WGEImage::SPtr() );
261 private:
262  /**
263  * Forwards call from a boost function to the virtual realizeImpl method
264  */
265  void realizeGT();
266 
267  /**
268  * Returns m_widget as WQtDockWidget if possible. NULL if not.
269  *
270  * \return the dock or NULL
271  */
273 };
274 
275 #endif // WUIQTWIDGETBASE_H
276 
std::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:48
std::shared_ptr< WGEImage > SPtr
Convenience typedef for a std::shared_ptr< WGEImage >.
Definition: WGEImage.h:48
This class contains the main window and the layout of the widgets within the window.
Definition: WMainWindow.h:66
Advanced QDockWidget.
Definition: WQtDockWidget.h:50
Implementation of WUIWidgetBase.
QWidget * m_widget
The widget representing this abstract UI element.
virtual void addActionTriggerGT(WPropTrigger trigger, WGEImage::SPtr icon=WGEImage::SPtr())
Implement WUIWidgetBase::addAction.
virtual QString getTitleQString() const =0
The title as QString.
QWidget * getCompellingQParent() const
Returns the parent to use for your implementation in realizeImpl.
virtual bool isReal()
The widget was created and can be used.
virtual void show()
Show this widget if not yet visible.
virtual void realize(WCondition::SPtr abortCondition=WCondition::SPtr())
Realize the widget.
std::shared_ptr< const WUIQtWidgetBase > ConstSPtr
Convenience typedef for a std::shared_ptr< const WUIQtWidgetBase >.
virtual void showGT()
Show this widget if not yet visible.
virtual bool isVisibleGT() const
Check if the widget is hidden or not.
bool hasUIParent() const
Check if the widget is embedded into another WUI widget.
WUIQtWidgetBase::SPtr m_parent
Parent widget.
std::shared_ptr< WUIQtWidgetBase > SPtr
Convenience typedef for a std::shared_ptr< WUIQtWidgetBase >.
virtual void cleanUpGT()=0
Cleanup the GUI.
virtual void closeGT()
Close the widget.
WMainWindow * m_mainWindow
The main window instance.
virtual void setVisible(bool visible=true)
Hide/show this widget.
WUIQtWidgetBase(WMainWindow *mainWindow, WUIQtWidgetBase::SPtr parent)
Default constructor.
WQtDockWidget * asDockWidget()
Returns m_widget as WQtDockWidget if possible.
virtual bool isVisible() const
Check if the widget is hidden or not.
virtual void addAction(WPropGroup group, WGEImage::SPtr icon=WGEImage::SPtr())
Implement WUIWidgetBase::addAction.
virtual ~WUIQtWidgetBase()
Destructor.
QWidget * getWidget() const
Get the widget representation.
void realizeGT()
Forwards call from a boost function to the virtual realizeImpl method.
virtual void close()=0
Handle shutdown.
virtual void closeImpl()
Close the widget.
virtual void realizeImpl()=0
Realize the widget.
QWidget * getParentAsQtWidget() const
Get the parent as Qt widget.
QWidget * embedContent(QWidget *content)
This method can be used if you just create some QWidget and do not want to take care about embedding ...
WUIQtWidgetBase::SPtr getQtParent() const
Parent widget.
virtual void addActionGroupGT(WPropGroup group, WGEImage::SPtr icon=WGEImage::SPtr())
Implement WUIWidgetBase::addAction.
virtual void addActionBoolGT(WPropBool toggle, WGEImage::SPtr icon=WGEImage::SPtr())
Implement WUIWidgetBase::addAction.
virtual void setVisibleGT(bool visible=true)
Hide/show this widget.