fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / ComplexSceneManagerQt / OSGCSMQT4Window.cpp
blob30374684db1c6916e8dd23092047b3e6f5e50a82
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #ifdef OSG_WITH_QT5
47 # include <QtWidgets/QApplication>
48 # include <QtWidgets/QDesktopWidget>
49 #else
50 # include <QtGui/QApplication>
51 # include <QtGui/QDesktopWidget>
52 #endif
54 #include "OSGConfig.h"
56 #include "OSGCSMQT4Window.h"
57 #include "OSGCSMQT4GLWidget_qt.h"
58 #include "OSGQT4Window.h"
59 #include "OSGComplexSceneManager.h"
61 OSG_BEGIN_NAMESPACE
63 // Documentation for this class is emitted in the
64 // OSGCSMQT4WindowBase.cpp file.
65 // To modify it, please change the .fcd file (OSGCSMQT4Window.fcd) and
66 // regenerate the base file.
68 /***************************************************************************\
69 * Class variables *
70 \***************************************************************************/
72 bool CSMQT4Window::_bRun = false;
73 QApplication *CSMQT4Window::_pApp = NULL;
75 /***************************************************************************\
76 * Class methods *
77 \***************************************************************************/
79 void CSMQT4Window::initMethod(InitPhase ePhase)
81 Inherited::initMethod(ePhase);
83 if(ePhase == TypeObject::SystemPost)
88 /***************************************************************************\
89 * Instance methods *
90 \***************************************************************************/
92 /*-------------------------------------------------------------------------*\
93 - private -
94 \*-------------------------------------------------------------------------*/
96 /*----------------------- constructors & destructors ----------------------*/
98 CSMQT4Window::CSMQT4Window(void) :
99 Inherited( ),
100 _pQTWidget(NULL)
104 CSMQT4Window::CSMQT4Window(const CSMQT4Window &source) :
105 Inherited(source),
106 _pQTWidget(NULL )
110 CSMQT4Window::~CSMQT4Window(void)
114 void CSMQT4Window::resolveLinks(void)
116 Inherited::resolveLinks();
119 void CSMQT4Window::terminateGLContext(void)
121 if(_pWindow != NULL)
123 _pWindow->terminate();
126 if(_pQTWidget != NULL)
128 delete _pQTWidget;
130 _pQTWidget = NULL;
134 /*----------------------------- class specific ----------------------------*/
136 void CSMQT4Window::changed(ConstFieldMaskArg whichField,
137 UInt32 origin,
138 BitVector details)
140 Inherited::changed(whichField, origin, details);
143 void CSMQT4Window::dump( UInt32 ,
144 const BitVector ) const
146 SLOG << "Dump CSMQT4Window NI" << std::endl;
149 void CSMQT4Window::initQtApp(void)
151 if(_pApp == NULL)
153 QApplication::setColorSpec(QApplication::CustomColor);
155 static const Char8 *appName = "testCSM";
156 static const Char8 *dispParam = "-display";
158 std::vector<const Char8 *> vArgs;
160 vArgs.push_back(appName);
162 #ifndef WIN32
163 if(this->getPrimaryDisplayString().empty() == false)
165 vArgs.push_back(dispParam);
166 vArgs.push_back(this->getPrimaryDisplayString().c_str());
168 #endif
170 static Int32 argc = vArgs.size();
171 static const Char8 **argv = &(vArgs[0]);
173 _pApp = new QApplication(argc, const_cast<Char8 **>(argv));
177 bool CSMQT4Window::init(void)
179 this->initQtApp();
181 UInt32 uiWidth;
182 UInt32 uiHeight;
184 Int32 iXPos = 0;
185 Int32 iYPos = 0;
187 const QRect screenGeometry = QApplication::desktop()->screenGeometry();
189 if(this->getXPos() > 0.f && this->getYPos() > 0.f)
191 iXPos = Int32(this->getXPos());
192 iYPos = Int32(this->getYPos());
195 if(this->getXSize() >= 1.f)
197 uiWidth = UInt32(this->getXSize());
199 else if(this->getXSize() <= 0.f)
201 uiWidth = screenGeometry.width();
203 else
205 uiWidth = UInt32(Real32(screenGeometry.width()) *
206 this->getXSize());
209 if(this->getYSize() >= 1.f)
211 uiHeight = UInt32(this->getYSize());
213 else if(this->getYSize() <= 0.f)
215 uiHeight = screenGeometry.height();
217 else
219 uiHeight = UInt32(Real32(screenGeometry.height()) *
220 this->getYSize());
225 QT4WindowUnrecPtr pQT4Window = QT4Window::create();
227 pQT4Window->setPrivateOSGContext(this->getPrivateContext());
229 _pQTWidget =
230 new CSMQT4GLWidget(
231 new OSGQGLWidget::GLContext(QGLFormat::defaultFormat()),
232 NULL,
233 NULL,
234 0 );
236 _pQTWidget->setQT4Window(this);
238 if(this->getDecorEnabled() == false)
240 _pQTWidget->setWindowFlags(Qt::FramelessWindowHint);
243 _pQTWidget->show();
244 _pQTWidget->setGeometry(iXPos, iYPos, uiWidth, uiHeight);
246 pQT4Window->setGlWidget(_pQTWidget);
248 _pApp->processEvents();
250 pQT4Window->init();
251 pQT4Window->resize(uiWidth,
252 uiHeight);
254 if(ComplexSceneManager::the() != NULL)
255 ComplexSceneManager::the()->setMainloop(&CSMQT4Window::qtMainLoop);
257 _pWindow = pQT4Window;
259 _bRun = true;
261 _pApp->processEvents();
264 Inherited::init();
266 return true;
269 void CSMQT4Window::qtMainLoop(void)
271 if(!QGLFormat::hasOpenGL() || _pApp == NULL)
273 qWarning( "This system has no OpenGL support. Exiting." );
275 return;
278 while(_bRun == true)
280 _pApp->processEvents(); // execute QT main loop
282 ComplexSceneManager::the()->frame();
284 Thread::getCurrentChangeList()->commitChangesAndClear();
287 ComplexSceneManager::the()->terminate();
289 delete _pApp;
291 _pApp = NULL;
294 OSG_END_NAMESPACE