File: ccGLWidget.h

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (140 lines) | stat: -rw-r--r-- 3,453 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#                    COPYRIGHT: CloudCompare project                     #
//#                                                                        #
//##########################################################################

#ifndef CC_GL_WIDGET_HEADER
#define CC_GL_WIDGET_HEADER

//Local
#include <ccGLWindow.h>

//Qt
#include <QWidget>

#ifdef CC_GL_WINDOW_USE_QWINDOW

//Qt
#include <QHBoxLayout>

//System
#include <assert.h>

//! Container widget for ccGLWindow
class ccGLWidget : public QWidget
{
	Q_OBJECT

public:

	ccGLWidget(ccGLWindow* window, QWidget* parent = nullptr)
		: QWidget(parent)
	{
		setLayout(new QHBoxLayout);
		layout()->setContentsMargins(0, 0, 0, 0);

		if (window)
		{
			setAssociatedWindow(window);
		}
	}

	virtual ~ccGLWidget()
	{
		if (m_associatedWindow)
		{
			m_associatedWindow->setParent(nullptr);
			m_associatedWindow->close();
		}
	}

	inline ccGLWindow* associatedWindow() const { return m_associatedWindow; }

	static ccGLWindow* FromWidget(const QWidget* widget)
	{
		const ccGLWidget* myWidget = qobject_cast<const ccGLWidget*>(widget);
		if (myWidget)
		{
			return myWidget->associatedWindow();
		}
		else
		{
			assert(false);
			return nullptr;
		}
	}

	void setAssociatedWindow(ccGLWindow* window)
	{
		if (window)
		{
			assert(layout() && layout()->count() == 0);
			QWidget* container = QWidget::createWindowContainer(window, this);
			layout()->addWidget(container);

			m_associatedWindow = window;
			m_associatedWindow->setParentWidget(container);
		}
		else
		{
			assert(false);
		}
	}

protected:
	
	ccGLWindow* m_associatedWindow;
};

#endif

inline void CreateGLWindow(ccGLWindow*& window, QWidget*& widget, bool stereoMode = false, bool silentInitialization = false)
{
	QSurfaceFormat format = QSurfaceFormat::defaultFormat();
	format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
	format.setStereo(stereoMode);

	window = new ccGLWindow(&format, nullptr, silentInitialization);

#ifdef CC_GL_WINDOW_USE_QWINDOW
	widget = new ccGLWidget(window);
#else
	widget = window;
#endif
}

inline ccGLWindow* GLWindowFromWidget(QWidget* widget)
{
#ifdef CC_GL_WINDOW_USE_QWINDOW
	ccGLWidget* myWidget = qobject_cast<ccGLWidget*>(widget);
	if (myWidget)
	{
		return myWidget->associatedWindow();
	}
#else
	ccGLWindow* myWidget = qobject_cast<ccGLWindow*>(widget);
	if (myWidget)
	{
		return myWidget;
	}
#endif
	else
	{
		assert(false);
		return nullptr;
	}
}

#endif //CC_GL_WIDGET_HEADER