/
canvaswindow.cpp
209 lines (173 loc) · 5.71 KB
/
canvaswindow.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file canvaswindow.cpp
* Canvas window implementation. @ingroup base
*
* @authors Copyright © 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; either version 2 of the License, or (at your
* option) any later version. 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. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QApplication>
#include <QGLFormat>
#include <QMoveEvent>
#include <de/Log>
#include <de/c_wrapper.h>
#include "de_platform.h"
#include "con_main.h"
#include "gl_main.h"
#include "canvaswindow.h"
#include <assert.h>
struct CanvasWindow::Instance
{
Canvas* canvas;
Canvas* recreated;
void (*moveFunc)(CanvasWindow&);
bool mouseWasTrapped;
Instance() : canvas(0), moveFunc(0), mouseWasTrapped(false) {}
};
CanvasWindow::CanvasWindow(QWidget *parent)
: QMainWindow(parent)
{
d = new Instance;
// Create the drawing canvas for this window.
setCentralWidget(d->canvas = new Canvas); // takes ownership
// All input goes to the canvas.
d->canvas->setFocus();
}
CanvasWindow::~CanvasWindow()
{
delete d;
}
void CanvasWindow::initCanvasAfterRecreation(Canvas& canvas)
{
CanvasWindow* self = dynamic_cast<CanvasWindow*>(canvas.parentWidget());
assert(self);
LOG_DEBUG("About to replace Canvas %p with %p")
<< de::dintptr(self->d->canvas) << de::dintptr(self->d->recreated);
// Take the existing callbacks of the old canvas.
self->d->recreated->useCallbacksFrom(*self->d->canvas);
// Switch the central widget. This will delete the old canvas automatically.
self->setCentralWidget(self->d->recreated);
self->d->canvas = self->d->recreated;
self->d->recreated = 0;
// Set up the basic GL state for the new canvas.
self->d->canvas->makeCurrent();
GL_Init2DState();
self->d->canvas->doneCurrent();
self->d->canvas->updateGL();
// Reacquire the focus.
self->d->canvas->setFocus();
if(self->d->mouseWasTrapped)
{
self->d->canvas->trapMouse();
}
LOG_DEBUG("Canvas replaced with %p") << de::dintptr(self->d->canvas);
}
void CanvasWindow::recreateCanvas()
{
// Update the GL format for subsequently created Canvases.
setDefaultGLFormat();
#if 0
canvas().setFormat(QGLFormat::defaultFormat());
LOG_DEBUG("Updated Canvas GL format.");
#else
/// @todo Instead of recreating, there is also the option of modifying the
/// existing QGLContext -- however, changing its format causes it to be
/// reset. We are doing it this way because we wish to retain the current
/// GL context's texture objects by sharing them with the new Canvas.
/// @todo See if reseting the QGLContext actually causes all textures to be
/// lost. If not, it would be better to just change the format and
/// reinitialize the context state.
// We'll re-trap the mouse after the new canvas is ready.
d->mouseWasTrapped = canvas().isMouseTrapped();
canvas().trapMouse(false);
// Create the replacement Canvas. Once it's created and visible, we'll
// finish the switch-over.
d->recreated = new Canvas(this, d->canvas);
d->recreated->setInitFunc(initCanvasAfterRecreation);
d->recreated->setGeometry(d->canvas->geometry());
d->recreated->show();
LOG_DEBUG("Canvas recreated, old one still exists");
#endif
}
Canvas& CanvasWindow::canvas()
{
assert(d->canvas != 0);
return *d->canvas;
}
bool CanvasWindow::ownsCanvas(Canvas *c) const
{
if(!c) return false;
return (d->canvas == c || d->recreated == c);
}
void CanvasWindow::setMoveFunc(void (*func)(CanvasWindow&))
{
d->moveFunc = func;
}
bool CanvasWindow::event(QEvent* ev)
{
if(ev->type() == QEvent::ActivationChange)
{
//LOG_DEBUG("CanvasWindow: Forwarding QEvent::KeyRelease, Qt::Key_Alt");
QKeyEvent keyEvent = QKeyEvent(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier);
return QApplication::sendEvent(&canvas(), &keyEvent);
}
return QMainWindow::event(ev);
}
void CanvasWindow::closeEvent(QCloseEvent* ev)
{
/// @todo autosave and quit?
Con_Execute(CMDS_DDAY, "quit", true, false);
ev->ignore();
}
void CanvasWindow::moveEvent(QMoveEvent *ev)
{
QMainWindow::moveEvent(ev);
if(d->moveFunc)
{
d->moveFunc(*this);
}
}
void CanvasWindow::hideEvent(QHideEvent* ev)
{
QMainWindow::hideEvent(ev);
LOG_DEBUG("CanvasWindow: hide event (hidden:%b)") << isHidden();
}
void CanvasWindow::setDefaultGLFormat() // static
{
// Configure the GL settings for all subsequently created canvases.
QGLFormat fmt;
fmt.setDepthBufferSize(16);
fmt.setStencilBufferSize(8);
fmt.setDoubleBuffer(true);
if(CommandLine_Exists("-novsync") || !Con_GetByte("vid-vsync"))
{
fmt.setSwapInterval(0); // vsync off
}
else
{
fmt.setSwapInterval(1);
}
if(CommandLine_Exists("-nofsaa") || !Con_GetByte("vid-fsaa"))
{
fmt.setSampleBuffers(false);
}
else
{
fmt.setSampleBuffers(true); // multisampling on (default: highest available)
//fmt.setSamples(4);
}
QGLFormat::setDefaultFormat(fmt);
}