File: Qt5Widget.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (182 lines) | stat: -rw-r--r-- 4,460 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#if defined(WITH_QT5)

#include "Qt5Widget.h"
#include "Qt5InputManager.h"
#include "../MainApplication.h"

#if defined(WITH_GLEW)
#	include "Qt5GfxDevice.h"
#endif

#include <QCoreApplication>
#include <QResizeEvent>

namespace nCine::Backends
{
	Qt5Widget::Qt5Widget(QWidget* parent, std::unique_ptr<IAppEventHandler>(*createAppEventHandler)(), int argc, char** argv)
		: QOpenGLWidget(parent), application_(static_cast<MainApplication&>(theApplication())),
			createAppEventHandler_(createAppEventHandler), isInitialized_(false), shouldUpdate_(true)
	{
		setFocusPolicy(Qt::StrongFocus);
		setMouseTracking(true);
		QObject::connect(this, SIGNAL(frameSwapped()), this, SLOT(autoUpdate()));

		//ASSERT(createAppEventHandler_);
		application_.qt5Widget_ = this;
		application_.init(createAppEventHandler_, argc, argv);
		application_.setAutoSuspension(false);

		const int width = application_.appConfiguration().resolution.x;
		const int height = application_.appConfiguration().resolution.y;
		QRect rect = geometry();
		rect.setWidth(application_.appConfiguration().resolution.x);
		rect.setHeight(application_.appConfiguration().resolution.y);
		setGeometry(rect);

		if (!application_.appCfg_.isResizable) {
			setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
			setMinimumSize(width, height);
			setMaximumSize(width, height);
		}
	}

	Qt5Widget::~Qt5Widget()
	{
		shutdown();
	}

	IAppEventHandler& Qt5Widget::appEventHandler()
	{
		return *application_.appEventHandler_;
	}

	bool Qt5Widget::event(QEvent* event)
	{
		Qt5InputManager* inputManager = static_cast<Qt5InputManager*>(&application_.inputManager());

		if (event->type() == QEvent::FocusIn) {
			application_.setFocus(true);
		} else if (event->type() == QEvent::FocusOut) {
			application_.setFocus(false);
		}

		switch (event->type()) {
			case QEvent::FocusIn:
			case QEvent::FocusOut:
			case QEvent::KeyPress:
			case QEvent::KeyRelease:
			case QEvent::MouseButtonPress:
			case QEvent::MouseButtonRelease:
			case QEvent::MouseMove:
			case QEvent::TouchBegin:
			case QEvent::TouchUpdate:
			case QEvent::TouchEnd:
			case QEvent::Wheel:
				if (inputManager) {
					if (inputManager->handler()) {
						makeCurrent();
					}
					const bool result = inputManager->event(event);
					if (inputManager->handler()) {
						doneCurrent();
					}
					return result;
				}
				return false;
			case QEvent::Resize: {
				makeCurrent();
				const QSize size = static_cast<QResizeEvent*>(event)->size();
				application_.resizeScreenViewport(size.width(), size.height());
				doneCurrent();
				return QWidget::event(event);
			}
			case QEvent::Close: {
				const bool shouldQuit = inputManager ? inputManager->shouldQuitOnRequest() : true;
				if (shouldQuit) {
					makeCurrent();
					shutdown();
					doneCurrent();
				} else {
					static_cast<QCloseEvent*>(event)->ignore();
				}
				return true;
			}
			default:
				return QWidget::event(event);
		}
	}

	void Qt5Widget::initializeGL()
	{
		Qt5GfxDevice& gfxDevice = static_cast<Qt5GfxDevice&>(*application_.gfxDevice_);

#if defined(WITH_GLEW)
		gfxDevice.initGlew();
#endif
		application_.initCommon();
		gfxDevice.resetTextureBinding();
		isInitialized_ = true;
	}

	void Qt5Widget::resizeGL(int w, int h)
	{
		if (isInitialized_) {
			Qt5GfxDevice& gfxDevice = static_cast<Qt5GfxDevice&>(*application_.gfxDevice_);
			gfxDevice.setResolution(w, h);
			gfxDevice.resetTextureBinding();
		}
	}

	void Qt5Widget::paintGL()
	{
		if (isInitialized_) {
			if (!application_.shouldQuit()) {
				application_.run();
			} else {
				shutdown();
				QCoreApplication::quit();
			}
		}
	}

	QSize Qt5Widget::minimumSizeHint() const
	{
		if (application_.appConfiguration().isResizable) {
			return QSize(-1, -1);
		}

		if (isInitialized_) {
			return QSize(application_.width(), application_.height());
		} else {
			return QSize(application_.appCfg_.resolution.x, application_.appCfg_.resolution.y);
		}
	}

	QSize Qt5Widget::sizeHint() const
	{
		if (isInitialized_) {
			return QSize(application_.width(), application_.height());
		} else {
			return QSize(application_.appCfg_.resolution.x, application_.appCfg_.resolution.y);
		}
	}

	void Qt5Widget::autoUpdate()
	{
		if (shouldUpdate_) {
			update();
		}
	}

	void Qt5Widget::shutdown()
	{
		if (isInitialized_) {
			application_.shutdownCommon();
			application_.qt5Widget_ = nullptr;
			isInitialized_ = false;
		}
		disconnect(SIGNAL(frameSwapped()));
	}
}

#endif