OpenWalnut  1.5.0dev
WQtGLDockWidget.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 <iostream>
26 #include <string>
27 
28 #include <boost/function.hpp>
29 #include <boost/lambda/lambda.hpp>
30 
31 #include <QAction>
32 #include <QWidgetAction>
33 #include <QDockWidget>
34 #include <QVBoxLayout>
35 #include <QHBoxLayout>
36 #include <QToolButton>
37 #include <QToolBar>
38 
39 #include "core/graphicsEngine/WGEViewerEffect.h"
40 
41 #include "WQtGui.h"
42 #include "WMainWindow.h"
43 
44 #include "WSettingAction.h"
45 
46 #include "guiElements/WQtDockWidget.h"
47 #include "controlPanel/WQtPropertyGroupWidget.h"
48 
49 #include "WQtGLDockWidget.h"
50 
51 
52 WQtGLDockWidget::WQtGLDockWidget( QString viewTitle, QString dockTitle, QWidget* parent, WGECamera::ProjectionMode projectionMode,
53  const QWidget* shareWidget ):
54  WQtDockWidget( dockTitle, parent ),
55  m_dockTitle( dockTitle ),
56  m_saveViewerSettings( true )
57 {
58  setObjectName( QString( "GL - " ) + dockTitle );
59 
60  // NOTE: do not remove this. When creating custom widgets using the OSG manipulators, a too small size here (or even no min size) causes the
61  // cull visitor to do crap ... unknown reason ...
62  setMinimumSize( 50, 50 );
63 
64  // the panel contains all other widgets, including the gl widget
65  // This allows adding other widgets to certain docks
66  m_panel = new QWidget( this );
67  m_layout = new QVBoxLayout;
68  m_panel->setLayout( m_layout );
69 
70  m_glWidget = new WQtGLWidget( viewTitle.toStdString(), this, projectionMode, shareWidget );
71  m_layout->addWidget( m_glWidget );
72  m_layout->setContentsMargins( 0, 0, 0, 0 );
73 
74  setWidget( m_panel );
75 
76  // we need to know whether the dock is visible or not
77  connect( this, SIGNAL( visibilityChanged( bool ) ), this, SLOT( handleVisibilityChange( bool ) ) );
78 
79  // all view docks have a screen capture object
80  m_screenCapture = new WQtGLScreenCapture( this );
81 
82  // screen capture trigger
83  QWidgetAction* screenCaptureWidgetAction = new QWidgetAction( this );
84  screenCaptureWidgetAction->setDefaultWidget( m_screenCapture );
85  QMenu* screenCaptureMenu = new QMenu();
86  screenCaptureMenu->addAction( screenCaptureWidgetAction );
87  QToolButton* screenShotBtn = new QToolButton( parent );
88  screenShotBtn->setDefaultAction( m_screenCapture->getScreenshotTrigger() );
89  screenShotBtn->setPopupMode( QToolButton::MenuButtonPopup );
90  screenShotBtn->setMenu( screenCaptureMenu );
91 
92  // camera presets
93  m_presetBtn = new QToolButton( parent );
94  m_presetBtn->setDefaultAction( m_glWidget->getCameraResetAction() );
96  m_presetBtn->setPopupMode( QToolButton::MenuButtonPopup );
97 
98  // add them to the title
99  addTitleButton( screenShotBtn );
101 
102  addTitleProperty( m_glWidget->getViewer()->getProperties() );
103 }
104 
106 {
107  // cleanup
108  delete m_screenCapture;
109  m_screenCapture = NULL;
110 }
111 
113 {
114  if( m_screenCapture )
115  {
117  }
119 
120  // optional:
122  {
123  // visit the properties and save in QSettings. You cannot bind QSettings::setValue directly as the parameters need to be cast to QString and
124  // QVariant, which does not happen implicitly
125  m_glWidget->getViewer()->getProperties()->visitAsString( &WMainWindow::setSetting, m_dockTitle.toStdString() );
126  }
127 }
128 
130 {
133 
135  {
136  // do not forget to load the config properties of the viewer
137  WMainWindow::getSettings().beginGroup( m_dockTitle );
138  QStringList keys = WMainWindow::getSettings().allKeys();
139  // iterate all the keys in the group of this viewer. QSettings does not implement a visitor mechanism, thus we iterate manually.
140  for( QStringList::const_iterator it = keys.constBegin(); it != keys.constEnd(); ++it )
141  {
142  std::string value = WMainWindow::getSettings().value( *it ).toString().toStdString();
143  std::string key = ( *it ).toStdString();
144 
145  // NOTE: findProperty does not throw an exception, but setAsString.
146  WPropertyBase::SPtr prop = m_glWidget->getViewer()->getProperties()->findProperty( key );
147  if( prop )
148  {
149  // just in case something is going wrong (faulty setting): cannot cast string to property type. Be kind and ignore it.
150  try
151  {
152  prop->setAsString( value );
153  }
154  catch( ... )
155  {
156  // ignore faulty/old settings
157  }
158  }
159  }
160  WMainWindow::getSettings().endGroup();
161  }
162 }
163 
165 {
166  m_saveViewerSettings = enable;
167 }
168 
170 {
171  return m_saveViewerSettings;
172 }
173 
175 {
176  return m_glWidget;
177 }
178 
180 {
181  m_glWidget->getViewer()->handleVisibilityChange( visible );
182 }
183 
184 void WQtGLDockWidget::closeEvent( QCloseEvent *event )
185 {
186  event->accept();
187  m_glWidget->setPaused( true );
188  WQtDockWidget::closeEvent( event );
189 }
190 
191 void WQtGLDockWidget::showEvent( QShowEvent* event )
192 {
193  m_glWidget->setPaused( false );
194  WQtDockWidget::showEvent( event );
195 }
196 
198 {
199  return m_screenCapture;
200 }
201 
202 void WQtGLDockWidget::forceGLWidgetSize( size_t w, size_t h )
203 {
204  wlog::debug( "WQtGLDockWidget" ) << "Forcing GLWidget to be " << w << "x" << h;
205  m_glWidget->setFixedSize( w, h );
206 }
207 
209 {
210  m_glWidget->setMinimumSize( 0, 0 );
211  m_glWidget->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
212  m_panel->setMinimumSize( 0, 0 );
213  m_panel->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
214  this->setMinimumSize( 0, 0 );
215  this->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
216 }
217 
218 const QString& WQtGLDockWidget::getDockTitle() const
219 {
220  return m_dockTitle;
221 }
222 
224 {
225  if( m_screenCapture )
226  {
227  m_screenCapture->setWindowFlags( Qt::Tool );
228  m_screenCapture->show();
229  }
230 }
231 
233 {
234  if( getCameraPresetMenu()->isEmpty() )
235  {
236  m_presetBtn->setMenu( NULL );
237  }
238  else
239  {
240  m_presetBtn->setMenu( getCameraPresetMenu() );
241  }
242 }
243 
245 {
247 }
ProjectionMode
List of possible camera modes.
Definition: WGECamera.h:44
static void setSetting(std::string key, std::string value)
Set a setting specified by a key to a given value.
static QSettings & getSettings()
Returns the settings object.
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
Advanced QDockWidget.
Definition: WQtDockWidget.h:50
virtual void closeEvent(QCloseEvent *event)
Called whenever a close event is received.
virtual void saveSettings()
Save settings.
virtual void addTitleButton(QToolButton *button)
Add the given tool button to the titlebar.
virtual void restoreSettings()
Restore settings.
virtual void addTitleProperty(WPropTrigger prop, WGEImage::SPtr icon=WGEImage::SPtr())
Add a property to the title of this dock.
WQtGLWidget * getGLWidget() const
Gets the contained GL widget instance.
WQtGLWidget * m_glWidget
My GL widget.
virtual void saveSettings()
Save settings.
QToolButton * m_presetBtn
Camera presets.
const QString & getDockTitle() const
Return the title of the view/dock.
QWidget * m_panel
The panel widget using m_layout.
QVBoxLayout * m_layout
Layout of this widget.
bool getSaveViewerSettings() const
Check whether the automatic saving of WGEViewer properties is enabled.
WQtGLDockWidget(QString viewTitle, QString dockTitle, QWidget *parent, WGECamera::ProjectionMode projectionMode=WGECamera::ORTHOGRAPHIC, const QWidget *shareWidget=0)
default constructor
WQtGLScreenCapture * getScreenCapture()
Adds a screen capture dock using this view's screen capture callback.
void handleVisibilityChange(bool visible)
If the dock widget changes its visibility.
void setSaveViewerSettings(bool enable=true)
Allow turning the automatic save and restore of viewer-settings.
bool m_saveViewerSettings
If true, the saveSettings method also saves the WGEViewer properties.
virtual void restoreSettings()
Restore settings.
virtual ~WQtGLDockWidget()
destructor.
void openScreenCaptureConfig()
Open screen capture config options.
void updateCameraPresetButton()
Update camera button.
WQtGLScreenCapture * m_screenCapture
Manager for screen capturing of this view.
virtual void closeEvent(QCloseEvent *event)
Called whenever a close event is received.
QString m_dockTitle
The view name and dock title.
void forceGLWidgetSize(size_t w, size_t h)
Forces the GL widget to have the desired size.
QMenu * getCameraPresetMenu() const
Get the menu used for camera presets.
virtual void showEvent(QShowEvent *event)
Called whenever the widget gets opened.
void restoreGLWidgetSize()
Restores the GL widget size if it was fixed with forceMainGLWidgetSize() previously.
This class is a screen recorder adapter in QT.
virtual void saveSettings()
Save settings.
virtual void restoreSettings()
Restore settings.
QAction * getScreenshotTrigger() const
Returns the trigger used for screenshotting.
A widget containing an open gl display area.
Definition: WQtGLWidget.h:54
std::shared_ptr< WGEViewer > getViewer() const
Get the included viewer.
QAction * getCameraResetAction()
The action to trigger a camera reset.
QMenu * getCameraPresetsMenu()
The presets menu.
void setPaused(bool pause=true)
Pause rendering.
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331