OpenWalnut  1.5.0dev
WUIQtWidgetBase.cpp
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 #include <memory>
26 
27 #include <boost/function.hpp>
28 
29 #include "../WQtGui.h"
30 #include "../guiElements/WQtDockWidget.h"
31 #include "WUIQtWidgetBase.h"
32 #include "core/common/WLogger.h"
33 
35  m_mainWindow( mainWindow ),
36  m_widget( NULL ),
37  m_parent( parent )
38 {
39  // initialize members
40 }
41 
43 {
44  // cleanup
45  if( m_widget )
46  {
47  delete m_widget;
48  }
49 }
50 
51 void WUIQtWidgetBase::realize( std::shared_ptr< WCondition > abortCondition )
52 {
53  WConditionSet conditionSet;
54  conditionSet.setResetable( true, false );
55  // there are several events we want to wait for:
56  // 1) the caller of this function does not want to wait anymore:
57  if( abortCondition )
58  {
59  conditionSet.add( abortCondition );
60  }
61 
62  // 2) the execution call was done:
63  WCondition::SPtr doneNotify( new WConditionOneShot() );
64  conditionSet.add( doneNotify );
65 
66  // use the UI to do the GUI thread call
67  WQtGui::execInGUIThreadAsync( boost::bind( &WUIQtWidgetBase::realizeGT, this ), doneNotify );
68 
69  // wait ...
70  conditionSet.wait();
71 }
72 
74 {
75  // only top-level widgets will be registered
76  if( !m_parent )
77  {
79  }
80  realizeImpl();
81 }
82 
84 {
85  return ( m_widget );
86 }
87 
89 {
90  if( m_widget )
91  {
92  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::showGT, this ) );
93  }
94 }
95 
96 void WUIQtWidgetBase::setVisible( bool visible )
97 {
98  if( m_widget )
99  {
100  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::setVisibleGT, this, visible ) );
101  }
102 }
103 
105 {
106  if( m_widget )
107  {
108  return WQtGui::execInGUIThread< bool >( boost::bind( &QWidget::isVisible, m_widget ) );
109  }
110  return false;
111 }
112 
114 {
115  wlog::debug( "WUIQtWidgetBase" ) << "Close: \"" << getTitleQString().toStdString() << "\"";
116 
117  // move this to the GUI thread.
118  if( m_widget )
119  {
120  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::closeGT, this ) );
121  }
122 }
123 
125 {
126  m_widget->show();
127 }
128 
129 void WUIQtWidgetBase::setVisibleGT( bool visible )
130 {
131  m_widget->setVisible( visible );
132 }
133 
135 {
136  return m_widget->isVisible();
137 }
138 
140 {
142 
143  QDockWidget* asDock = dynamic_cast< QDockWidget* >( m_widget );
144  if( asDock )
145  {
146  //WQtGui::getSettings().setValue( m_widget->objectName() + "state", asDock->saveState() );
147  WQtGui::getSettings().setValue( m_widget->objectName() + "geometry", asDock->saveGeometry() );
148  }
149 
150  cleanUpGT();
151  delete m_widget;
152  m_widget = NULL;
153 }
154 
156 {
157  return m_widget;
158 }
159 
161 {
162  if( getQtParent() )
163  {
164  return getQtParent()->getWidget();
165  }
166  else
167  {
168  return NULL;
169  }
170 }
171 
173 {
174  return m_parent;
175 }
176 
177 QWidget* WUIQtWidgetBase::embedContent( QWidget* content )
178 {
179  QDockWidget* asDock = dynamic_cast< QDockWidget* >( content );
180 
181  // is the widget embedded somewhere?
182  if( hasUIParent() )
183  {
184  m_widget = content;
185  m_widget->setVisible( true );
186 
187  // embedded into another widget? Remove dock features
188  if( asDock )
189  {
190  asDock->setFeatures( QDockWidget::NoDockWidgetFeatures );
191  }
192 
193  // doc says we return NULL if there is no container
194  return NULL;
195  }
196  else // it is a standalone widget -> embed into dock widget
197  {
198  // NO! It already is a dock:
199  if( asDock )
200  {
201  m_widget = asDock;
202  }
203  else // add dock
204  {
205  // it is a top-level window -> use a dock
206  // create as dock widget
208  widgetDock->setObjectName( QString( "Custom Dock Window " ) + getTitleQString() );
209  widgetDock->setWidget( content );
210  m_widget = widgetDock;
211 
212  // re-use this var. It is NULL
213  asDock = widgetDock;
214  }
215 
216  m_widget->setVisible( true );
217 
218  // restore state
219  asDock->restoreGeometry( WQtGui::getSettings().value( m_widget->objectName() + "geometry", "" ).toByteArray() );
220  if( !m_mainWindow->getDefaultCustomDockAreaWidget()->restoreDockWidget( asDock ) )
221  {
223  }
224  }
225 
226  return m_widget;
227 }
228 
230 {
231  QWidget* parent = getParentAsQtWidget();
232  if( !parent )
233  {
234  parent = m_mainWindow;
235  }
236 
237  return parent;
238 }
239 
241 {
242  return ( getQtParent() != NULL );
243 }
244 
245 void WUIQtWidgetBase::addAction( WPropGroup group, WGEImage::SPtr icon )
246 {
247  // do in GUI thread:
248  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::addActionGroupGT, this, group, icon ) );
249 }
250 
251 void WUIQtWidgetBase::addAction( WPropTrigger trigger, WGEImage::SPtr icon )
252 {
253  // do in GUI thread:
254  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::addActionTriggerGT, this, trigger, icon ) );
255 }
256 
257 void WUIQtWidgetBase::addAction( WPropBool toggle, WGEImage::SPtr icon )
258 {
259  // do in GUI thread:
260  WQtGui::execInGUIThread( boost::bind( &WUIQtWidgetBase::addActionBoolGT, this, toggle, icon ) );
261 }
262 
263 void WUIQtWidgetBase::addActionGroupGT( WPropGroup group, WGEImage::SPtr icon )
264 {
265  if( asDockWidget() )
266  {
267  asDockWidget()->addTitleProperty( group, icon );
268  }
269 }
270 
271 void WUIQtWidgetBase::addActionTriggerGT( WPropTrigger trigger, WGEImage::SPtr icon )
272 {
273  if( asDockWidget() )
274  {
275  asDockWidget()->addTitleProperty( trigger, icon );
276  }
277 }
278 
279 void WUIQtWidgetBase::addActionBoolGT( WPropBool toggle, WGEImage::SPtr icon )
280 {
281  if( asDockWidget() )
282  {
283  asDockWidget()->addTitleProperty( toggle, icon );
284  }
285 }
286 
288 {
289  return dynamic_cast< WQtDockWidget* >( m_widget );
290 }
Implements a WCondition, but can be fired only ONCE.
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:44
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
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
void deregisterCustomWidget(WUIQtWidgetBase *widget)
De-register a custom widget.
void registerCustomWidget(WUIQtWidgetBase *widget)
Register a custom widget.
QMainWindow * getDefaultCustomDockAreaWidget() const
The Widget to add custom docks.
Qt::DockWidgetArea getDefaultCustomDockArea() const
The default dock area to use for adding custom docks.
Advanced QDockWidget.
Definition: WQtDockWidget.h:50
virtual void addTitleProperty(WPropTrigger prop, WGEImage::SPtr icon=WGEImage::SPtr())
Add a property to the title of this dock.
static QSettings & getSettings()
Returns the settings object.
Definition: WQtGui.cpp:394
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
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
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.
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 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.
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331