11.08., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

qtcanvas.cpp 30.3 KB
Newer Older
schultezub's avatar
schultezub committed
1 2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
4 5 6
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8 9 10 11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library 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 Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#include "qtcanvas.h"

31 32 33
#include <QThread>
#include <QOpenGLContext>

34
namespace cgt {
schultezub's avatar
schultezub committed
35 36 37 38 39 40 41

// shared context widget
QGLWidget* QtCanvas::shareWidget_ = 0;

QtCanvas::QtCanvas(const std::string& title,
                   const ivec2& size,
                   const Buffers buffers,
42
                   QWidget* parent, bool shared, Qt::WindowFlags f, char* /*name*/)
43
    : GLCanvas(title, size, buffers)
44
    , QGLWidget(getQGLFormat(buffers), 0, (shared ? shareWidget_ : 0), f)
schultezub's avatar
schultezub committed
45 46 47 48 49 50 51 52
{
    resize(size.x, size.y);
    if (shared && shareWidget_ == 0)
        shareWidget_ = this;

    setWindowTitle(QString(title.c_str()));

    setFocusPolicy(Qt::StrongFocus);
grunau's avatar
grunau committed
53
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
schultezub's avatar
schultezub committed
54 55 56 57 58 59 60 61 62 63 64 65

    // we have our own AutoBufferSwap-mechanism (GLCanvas::setAutoFlush), so disable the one of qt
    setAutoBufferSwap(false);

    rgbaSize_ = ivec4(format().redBufferSize(),
                      format().greenBufferSize(),
                      format().blueBufferSize(),
                      format().alphaBufferSize());
    stencilSize_ = format().stencilBufferSize();
    depthSize_ = format().depthBufferSize();
    doubleBuffered_ = doubleBuffer();
    stereoViewing_ = format().stereo();
66 67

    connect(this, SIGNAL(s_sizeChangedExternally(int, int)), this, SLOT(sizeChangedExternally(int, int)));
schultezub's avatar
schultezub committed
68 69
}

70
QtCanvas::QtCanvas(QWidget* parent, bool shared, Qt::WindowFlags f, char* /*name*/)
71
    : GLCanvas()
72
    , QGLWidget(0, (shared ? shareWidget_ : 0), f)
schultezub's avatar
schultezub committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
    if (shared && shareWidget_ == 0)
        shareWidget_ = this;

    // we have our own AutoBufferSwap-mechanism (GLCanvas::setAutoFlush), so disable the one of qt
    setAutoBufferSwap(false);

    rgbaSize_ = ivec4(format().redBufferSize(),
                      format().greenBufferSize(),
                      format().blueBufferSize(),
                      format().alphaBufferSize());
    stencilSize_ = format().stencilBufferSize();
    depthSize_ = format().depthBufferSize();
    doubleBuffered_ = doubleBuffer();
    stereoViewing_ = format().stereo();
}

QtCanvas::~QtCanvas() {}

92 93 94 95 96 97 98 99 100
void QtCanvas::moveThreadAffinity(void* threadPointer) {
    QThread* qThreadPointer = static_cast<QThread*>(threadPointer);
    this->context()->moveToThread(qThreadPointer);
}

void* QtCanvas::getCurrentThreadPointer() {
    return QThread::currentThread();
}

schultezub's avatar
schultezub committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
void QtCanvas::initializeGL() {
}

void QtCanvas::resizeGL(int w, int h) {
    sizeChanged(ivec2(w, h));
}

void QtCanvas::paintGL() {
    paint();
}

void QtCanvas::repaint() {
    updateGL();
}

void QtCanvas::update() {
    QWidget::update();
}

void QtCanvas::swap() {
    QGLWidget::swapBuffers();
122
    QGLWidget::makeCurrent();
schultezub's avatar
schultezub committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

void QtCanvas::toggleFullScreen() {
    if (fullscreen_) {
        fullscreen_ = false;
        showNormal();
    }
    else {
        showFullScreen();
        fullscreen_ = !fullscreen_;
    }
};

/*
    These functions are called by qt if there is a user-caused event, like the pressing of a
138
    mouse-button, as in this case. The press-event has to be turned into a cgt::MouseEvent and
schultezub's avatar
schultezub committed
139 140 141 142 143
    handled: Either by a custom eventloop, or by directly broadcasting it to all eventlisteners
    that are associated with this canvas. The same is true for the other event-handling
    methods.
*/
void QtCanvas::enterEvent(QEvent* /*e*/) {
144 145
    cgt::MouseEvent* enterEv = new cgt::MouseEvent(0, 0, cgt::MouseEvent::ENTER,
        cgt::MouseEvent::MODIFIER_NONE, cgt::MouseEvent::MOUSE_BUTTON_NONE, cgt::ivec2(width(), height()));
146
    eventHandler_->broadcastEvent(enterEv);
schultezub's avatar
schultezub committed
147 148 149
}

void QtCanvas::leaveEvent(QEvent* /*e*/) {
150 151
    cgt::MouseEvent* leaveEv = new cgt::MouseEvent(0, 0, cgt::MouseEvent::EXIT,
        cgt::MouseEvent::MODIFIER_NONE, cgt::MouseEvent::MOUSE_BUTTON_NONE, cgt::ivec2(width(), height()));
152
    eventHandler_->broadcastEvent(leaveEv);
schultezub's avatar
schultezub committed
153 154 155
}

void QtCanvas::mousePressEvent(QMouseEvent* e) {
156 157
    cgt::MouseEvent* prEv = new cgt::MouseEvent(e->x(), e->y(), cgt::MouseEvent::PRESSED,
        getModifier(e), getButton(e), cgt::ivec2(width(), height()));
158
    eventHandler_->broadcastEvent(prEv);
schultezub's avatar
schultezub committed
159 160 161 162
}

// See mousePressEvent
void QtCanvas::mouseReleaseEvent (QMouseEvent* e) {
163 164
    cgt::MouseEvent* relEv = new cgt::MouseEvent(e->x(), e->y(), cgt::MouseEvent::RELEASED,
        getModifier(e), getButton(e), cgt::ivec2(width(), height()));
165
    eventHandler_->broadcastEvent(relEv);
schultezub's avatar
schultezub committed
166 167 168 169
}

// See mousePressEvent
void QtCanvas::mouseMoveEvent(QMouseEvent*  e) {
170 171
    cgt::MouseEvent* movEv = new cgt::MouseEvent(e->x(), e->y(), cgt::MouseEvent::MOTION,
        getModifier(e), getButtons(e), cgt::ivec2(width(), height())); // FIXME: submit information which mouse buttons are pressed
172
    eventHandler_->broadcastEvent(movEv);
schultezub's avatar
schultezub committed
173 174 175 176
}

// See mousePressEvent
void QtCanvas::mouseDoubleClickEvent(QMouseEvent* e) {
177 178
    cgt::MouseEvent* dcEv = new cgt::MouseEvent(e->x(), e->y(), cgt::MouseEvent::DOUBLECLICK,
                                                getModifier(e), getButton(e), cgt::ivec2(width(), height()));
179
    eventHandler_->broadcastEvent(dcEv);
schultezub's avatar
schultezub committed
180 181 182 183 184
}

// See mousePressEvent
// does not distinguish correctly between UP and DOWN => works fine in voreen!
void QtCanvas::wheelEvent(QWheelEvent* e) {
185
    cgt::MouseEvent::MouseButtons b = cgt::MouseEvent::MOUSE_WHEEL_DOWN;
schultezub's avatar
schultezub committed
186
    if (e->delta() > 0)
187 188 189
        b = cgt::MouseEvent::MOUSE_WHEEL_UP;
    cgt::MouseEvent* wheelEv = new cgt::MouseEvent(e->x(),e->y(), cgt::MouseEvent::WHEEL,
                                                   getModifier(e), b, cgt::ivec2(width(), height()));
190
    eventHandler_->broadcastEvent(wheelEv);
schultezub's avatar
schultezub committed
191 192 193 194
}

// See mousePressEvent
void QtCanvas::keyPressEvent(QKeyEvent* event) {
195
    cgt::KeyEvent* ke = new cgt::KeyEvent(getKey(event->key()), getModifier(event), true);
196
    eventHandler_->broadcastEvent(ke);
schultezub's avatar
schultezub committed
197 198 199 200
}

// See mousePressEvent
void QtCanvas::keyReleaseEvent(QKeyEvent* event) {
201
    cgt::KeyEvent* ke = new cgt::KeyEvent(getKey(event->key()), getModifier(event), false);
202
    eventHandler_->broadcastEvent(ke);
schultezub's avatar
schultezub committed
203 204 205 206
}

// yes, we need this in voreen FL
void QtCanvas::timerEvent(QTimerEvent* /*e*/) {
207
    cgt::TimeEvent* te = new cgt::TimeEvent();
208
    eventHandler_->broadcastEvent(te);
schultezub's avatar
schultezub committed
209 210
}

211
cgt::MouseEvent::MouseButtons QtCanvas::getButton(QMouseEvent* e) {
schultezub's avatar
schultezub committed
212 213
    switch (e->button()) {
        case Qt::LeftButton:
214
            return cgt::MouseEvent::MOUSE_BUTTON_LEFT;
schultezub's avatar
schultezub committed
215
        case Qt::RightButton:
216
            return cgt::MouseEvent::MOUSE_BUTTON_RIGHT;
schultezub's avatar
schultezub committed
217
        case Qt::MidButton:
218
            return cgt::MouseEvent::MOUSE_BUTTON_MIDDLE;
schultezub's avatar
schultezub committed
219
        default:
220
            return cgt::MouseEvent::MOUSE_BUTTON_NONE;
schultezub's avatar
schultezub committed
221 222 223
    }
}

224 225
cgt::MouseEvent::MouseButtons QtCanvas::getButtons(QMouseEvent* e) {
    /*cgt::MouseEvent::MouseButtons*/ int buttons = cgt::MouseEvent::MOUSE_BUTTON_NONE;
schultezub's avatar
schultezub committed
226 227

    if (e->buttons() & Qt::LeftButton)
228
        buttons |= cgt::MouseEvent::MOUSE_BUTTON_LEFT;
schultezub's avatar
schultezub committed
229
    if (e->buttons() & Qt::RightButton)
230
        buttons |= cgt::MouseEvent::MOUSE_BUTTON_RIGHT;
schultezub's avatar
schultezub committed
231
    if (e->buttons() & Qt::MidButton)
232
        buttons |= cgt::MouseEvent::MOUSE_BUTTON_MIDDLE;
schultezub's avatar
schultezub committed
233

234
    return static_cast<cgt::MouseEvent::MouseButtons>(buttons);
schultezub's avatar
schultezub committed
235 236
}

237
cgt::Event::Modifier QtCanvas::getModifier(QInputEvent* e) {
schultezub's avatar
schultezub committed
238 239
    int result = 0;
    if (e->modifiers() & Qt::ControlModifier)
240
        result |= int(cgt::Event::CTRL);
schultezub's avatar
schultezub committed
241
    if (e->modifiers() & Qt::ShiftModifier)
242
        result |= int(cgt::Event::SHIFT);
schultezub's avatar
schultezub committed
243
    if (e->modifiers() & Qt::AltModifier)
244
        result |= int(cgt::Event::ALT);
schultezub's avatar
schultezub committed
245
    if (e->modifiers() & Qt::MetaModifier)
246
        result |= int(cgt::Event::META);
schultezub's avatar
schultezub committed
247
    if (e->modifiers() == Qt::NoModifier)
248 249
        result = int(cgt::Event::MODIFIER_NONE);
    return cgt::Event::Modifier(result);
schultezub's avatar
schultezub committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
}

QGLFormat QtCanvas::getQGLFormat(const Buffers buffers) {
    QGLFormat format = QGLFormat();
    format.setAlpha(buffers & GLCanvas::ALPHA_BUFFER);
    format.setDepth(buffers & GLCanvas::DEPTH_BUFFER);
    format.setDoubleBuffer(buffers & GLCanvas::DOUBLE_BUFFER);
    format.setStencil(buffers & GLCanvas::STENCIL_BUFFER);
    format.setAccum(buffers & GLCanvas::ACCUM_BUFFER);
    format.setStereo(buffers & GLCanvas::STEREO_VIEWING);
    format.setSampleBuffers(buffers & GLCanvas::MULTISAMPLING);

    return format;
}

KeyEvent::KeyCode QtCanvas::getKey(int key) {
    switch(key) {
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        case Qt::Key_Escape   : return cgt::KeyEvent::K_ESCAPE;
        case Qt::Key_Tab  : return cgt::KeyEvent::K_TAB;
            // case Qt::Key_Backtab  : return cgt::KeyEvent::K_;
        case Qt::Key_Backspace    : return cgt::KeyEvent::K_BACKSPACE;
        case Qt::Key_Return   : return cgt::KeyEvent::K_RETURN;
        case Qt::Key_Enter    : return cgt::KeyEvent::K_KP_ENTER;
        case Qt::Key_Insert   : return cgt::KeyEvent::K_INSERT;
        case Qt::Key_Delete   : return cgt::KeyEvent::K_DELETE;
        case Qt::Key_Pause    : return cgt::KeyEvent::K_PAUSE;
        case Qt::Key_Print    : return cgt::KeyEvent::K_PRINT;
        case Qt::Key_SysReq   : return cgt::KeyEvent::K_SYSREQ;
        case Qt::Key_Clear    : return cgt::KeyEvent::K_CLEAR;
        case Qt::Key_Home     : return cgt::KeyEvent::K_HOME;
        case Qt::Key_End  : return cgt::KeyEvent::K_END;
        case Qt::Key_Left     : return cgt::KeyEvent::K_LEFT;
        case Qt::Key_Up   : return cgt::KeyEvent::K_UP;
        case Qt::Key_Right    : return cgt::KeyEvent::K_RIGHT;
        case Qt::Key_Down     : return cgt::KeyEvent::K_DOWN;
        case Qt::Key_PageUp   : return cgt::KeyEvent::K_PAGEUP;
        case Qt::Key_PageDown     : return cgt::KeyEvent::K_PAGEDOWN;
287
        case Qt::Key_Shift    : return cgt::KeyEvent::K_LSHIFT;  // Qt only knows one shift, control, meta and alt
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
        case Qt::Key_Control : return cgt::KeyEvent::K_LCTRL;//...
        case Qt::Key_Meta : return cgt::KeyEvent::K_RMETA;//...
        case Qt::Key_Alt  : return cgt::KeyEvent::K_LALT;//...
        case Qt::Key_AltGr    : return cgt::KeyEvent::K_MODE;
        case Qt::Key_CapsLock     : return cgt::KeyEvent::K_CAPSLOCK;
        case Qt::Key_NumLock  : return cgt::KeyEvent::K_NUMLOCK;
        case Qt::Key_ScrollLock   : return cgt::KeyEvent::K_SCROLLOCK;
        case Qt::Key_F1   : return cgt::KeyEvent::K_F1;
        case Qt::Key_F2   : return cgt::KeyEvent::K_F2;
        case Qt::Key_F3   : return cgt::KeyEvent::K_F3;
        case Qt::Key_F4   : return cgt::KeyEvent::K_F4;
        case Qt::Key_F5   : return cgt::KeyEvent::K_F5;
        case Qt::Key_F6   : return cgt::KeyEvent::K_F6;
        case Qt::Key_F7   : return cgt::KeyEvent::K_F7;
        case Qt::Key_F8   : return cgt::KeyEvent::K_F8;
        case Qt::Key_F9   : return cgt::KeyEvent::K_F9;
        case Qt::Key_F10  : return cgt::KeyEvent::K_F10;
        case Qt::Key_F11  : return cgt::KeyEvent::K_F11;
        case Qt::Key_F12  : return cgt::KeyEvent::K_F12;
        case Qt::Key_F13  : return cgt::KeyEvent::K_F13;
        case Qt::Key_F14  : return cgt::KeyEvent::K_F14;
        case Qt::Key_F15  : return cgt::KeyEvent::K_F15;
            // case Qt::Key_F16  : return cgt::KeyEvent::K_;
            // case Qt::Key_F17  : return cgt::KeyEvent::K_;
            // case Qt::Key_F18  : return cgt::KeyEvent::K_;
            // case Qt::Key_F19  : return cgt::KeyEvent::K_;
            // case Qt::Key_F20  : return cgt::KeyEvent::K_;
            // case Qt::Key_F21  : return cgt::KeyEvent::K_;
            // case Qt::Key_F22  : return cgt::KeyEvent::K_;
            // case Qt::Key_F23  : return cgt::KeyEvent::K_;
            // case Qt::Key_F24  : return cgt::KeyEvent::K_;
            // case Qt::Key_F25  : return cgt::KeyEvent::K_;
            // case Qt::Key_F26  : return cgt::KeyEvent::K_;
            // case Qt::Key_F27  : return cgt::KeyEvent::K_;
            // case Qt::Key_F28  : return cgt::KeyEvent::K_;
            // case Qt::Key_F29  : return cgt::KeyEvent::K_;
            // case Qt::Key_F30  : return cgt::KeyEvent::K_;
            // case Qt::Key_F31  : return cgt::KeyEvent::K_;
            // case Qt::Key_F32  : return cgt::KeyEvent::K_;
            // case Qt::Key_F33  : return cgt::KeyEvent::K_;
            // case Qt::Key_F34  : return cgt::KeyEvent::K_;
            // case Qt::Key_F35  : return cgt::KeyEvent::K_;
        case Qt::Key_Super_L  : return cgt::KeyEvent::K_LSUPER;
        case Qt::Key_Super_R  : return cgt::KeyEvent::K_RSUPER;
        case Qt::Key_Menu     : return cgt::KeyEvent::K_MENU;
            // case Qt::Key_Hyper_L  : return cgt::KeyEvent::K_;
            // case Qt::Key_Hyper_R  : return cgt::KeyEvent::K_;
        case Qt::Key_Help     : return cgt::KeyEvent::K_HELP;
            // case Qt::Key_Direction_L  : return cgt::KeyEvent::K_;
            // case Qt::Key_Direction_R  : return cgt::KeyEvent::K_;
        case Qt::Key_Space    : return cgt::KeyEvent::K_SPACE;
        case Qt::Key_Exclam   : return cgt::KeyEvent::K_EXCLAIM;
            // case Qt::Key_QuoteDbl     : return cgt::KeyEvent::K_;
            // case Qt::Key_NumberSign   : return cgt::KeyEvent::K_;
        case Qt::Key_Dollar   : return cgt::KeyEvent::K_DOLLAR;
            // case Qt::Key_Percent  : return cgt::KeyEvent::K_;
        case Qt::Key_Ampersand    : return cgt::KeyEvent::K_AMPERSAND;
            // case Qt::Key_Apostrophe   : return cgt::KeyEvent::K_;
        case Qt::Key_ParenLeft    : return cgt::KeyEvent::K_LEFTPAREN;
        case Qt::Key_ParenRight   : return cgt::KeyEvent::K_RIGHTPAREN;
        case Qt::Key_Asterisk     : return cgt::KeyEvent::K_ASTERISK;
        case Qt::Key_Plus     : return cgt::KeyEvent::K_PLUS;
        case Qt::Key_Comma    : return cgt::KeyEvent::K_COMMA;
        case Qt::Key_Minus    : return cgt::KeyEvent::K_MINUS;
        case Qt::Key_Period   : return cgt::KeyEvent::K_PERIOD;
        case Qt::Key_Slash    : return cgt::KeyEvent::K_SLASH;
        case Qt::Key_0    : return cgt::KeyEvent::K_0;
        case Qt::Key_1    : return cgt::KeyEvent::K_1;
        case Qt::Key_2    : return cgt::KeyEvent::K_2;
        case Qt::Key_3    : return cgt::KeyEvent::K_3;
        case Qt::Key_4    : return cgt::KeyEvent::K_4;
        case Qt::Key_5    : return cgt::KeyEvent::K_5;
        case Qt::Key_6    : return cgt::KeyEvent::K_6;
        case Qt::Key_7    : return cgt::KeyEvent::K_7;
        case Qt::Key_8    : return cgt::KeyEvent::K_8;
        case Qt::Key_9    : return cgt::KeyEvent::K_9;
        case Qt::Key_Colon    : return cgt::KeyEvent::K_COLON;
        case Qt::Key_Semicolon    : return cgt::KeyEvent::K_SEMICOLON;
        case Qt::Key_Less     : return cgt::KeyEvent::K_LESS;
        case Qt::Key_Equal    : return cgt::KeyEvent::K_EQUALS;
        case Qt::Key_Greater  : return cgt::KeyEvent::K_GREATER;
        case Qt::Key_Question     : return cgt::KeyEvent::K_QUESTION;
        case Qt::Key_At   : return cgt::KeyEvent::K_AT;
        case Qt::Key_A    : return cgt::KeyEvent::K_A;
        case Qt::Key_B    : return cgt::KeyEvent::K_B;
        case Qt::Key_C    : return cgt::KeyEvent::K_C;
        case Qt::Key_D    : return cgt::KeyEvent::K_D;
        case Qt::Key_E    : return cgt::KeyEvent::K_E;
        case Qt::Key_F    : return cgt::KeyEvent::K_F;
        case Qt::Key_G    : return cgt::KeyEvent::K_G;
        case Qt::Key_H    : return cgt::KeyEvent::K_H;
        case Qt::Key_I    : return cgt::KeyEvent::K_I;
        case Qt::Key_J    : return cgt::KeyEvent::K_J;
        case Qt::Key_K    : return cgt::KeyEvent::K_K;
        case Qt::Key_L    : return cgt::KeyEvent::K_L;
        case Qt::Key_M    : return cgt::KeyEvent::K_M;
        case Qt::Key_N    : return cgt::KeyEvent::K_N;
        case Qt::Key_O    : return cgt::KeyEvent::K_O;
        case Qt::Key_P    : return cgt::KeyEvent::K_P;
        case Qt::Key_Q    : return cgt::KeyEvent::K_Q;
        case Qt::Key_R    : return cgt::KeyEvent::K_R;
        case Qt::Key_S    : return cgt::KeyEvent::K_S;
        case Qt::Key_T    : return cgt::KeyEvent::K_T;
        case Qt::Key_U    : return cgt::KeyEvent::K_U;
        case Qt::Key_V    : return cgt::KeyEvent::K_V;
        case Qt::Key_W    : return cgt::KeyEvent::K_W;
        case Qt::Key_X    : return cgt::KeyEvent::K_X;
        case Qt::Key_Y    : return cgt::KeyEvent::K_Y;
        case Qt::Key_Z    : return cgt::KeyEvent::K_Z;

        case Qt::Key_BracketLeft  : return cgt::KeyEvent::K_LEFTBRACKET;
        case Qt::Key_Backslash    : return cgt::KeyEvent::K_BACKSLASH;
        case Qt::Key_BracketRight     : return cgt::KeyEvent::K_RIGHTBRACKET;
            // case Qt::Key_AsciiCircum  : return cgt::KeyEvent::K_;
        case Qt::Key_Underscore   : return cgt::KeyEvent::K_UNDERSCORE;
            // case Qt::Key_QuoteLeft    : return cgt::KeyEvent::K_;
            // case Qt::Key_BraceLeft    : return cgt::KeyEvent::K_;
            // case Qt::Key_Bar  : return cgt::KeyEvent::K_;
            // case Qt::Key_BraceRight   : return cgt::KeyEvent::K_;
            // case Qt::Key_AsciiTilde   : return cgt::KeyEvent::K_;
            // case Qt::Key_nobreakspace     : return cgt::KeyEvent::K_;
            // case Qt::Key_exclamdown   : return cgt::KeyEvent::K_;
            // case Qt::Key_cent     : return cgt::KeyEvent::K_;
            // case Qt::Key_sterling     : return cgt::KeyEvent::K_;
            // case Qt::Key_currency     : return cgt::KeyEvent::K_;
            // case Qt::Key_yen  : return cgt::KeyEvent::K_;
            // case Qt::Key_brokenbar    : return cgt::KeyEvent::K_;
            // case Qt::Key_section  : return cgt::KeyEvent::K_;
            // case Qt::Key_diaeresis    : return cgt::KeyEvent::K_;
            // case Qt::Key_copyright    : return cgt::KeyEvent::K_;
            // case Qt::Key_ordfeminine  : return cgt::KeyEvent::K_;
            // case Qt::Key_guillemotleft    : return cgt::KeyEvent::K_;
            // case Qt::Key_notsign  : return cgt::KeyEvent::K_;
            // case Qt::Key_hyphen   : return cgt::KeyEvent::K_;
            // case Qt::Key_registered   : return cgt::KeyEvent::K_;
            // case Qt::Key_macron   : return cgt::KeyEvent::K_;
            // case Qt::Key_degree   : return cgt::KeyEvent::K_;
            // case Qt::Key_plusminus    : return cgt::KeyEvent::K_;
            // case Qt::Key_twosuperior  : return cgt::KeyEvent::K_;
            // case Qt::Key_threesuperior    : return cgt::KeyEvent::K_;
            // case Qt::Key_acute    : return cgt::KeyEvent::K_;
            // case Qt::Key_mu   : return cgt::KeyEvent::K_;
            // case Qt::Key_paragraph    : return cgt::KeyEvent::K_;
            // case Qt::Key_periodcentered   : return cgt::KeyEvent::K_;
            // case Qt::Key_cedilla  : return cgt::KeyEvent::K_;
            // case Qt::Key_onesuperior  : return cgt::KeyEvent::K_;
            // case Qt::Key_masculine    : return cgt::KeyEvent::K_;
            // case Qt::Key_guillemotright   : return cgt::KeyEvent::K_;
            // case Qt::Key_onequarter   : return cgt::KeyEvent::K_;
            // case Qt::Key_onehalf  : return cgt::KeyEvent::K_;
            // case Qt::Key_threequarters    : return cgt::KeyEvent::K_;
            // case Qt::Key_questiondown     : return cgt::KeyEvent::K_;
            // case Qt::Key_Agrave   : return cgt::KeyEvent::K_;
            // case Qt::Key_Aacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Acircumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Atilde   : return cgt::KeyEvent::K_;
            // case Qt::Key_Adiaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_Aring    : return cgt::KeyEvent::K_;
            // case Qt::Key_AE   : return cgt::KeyEvent::K_;
            // case Qt::Key_Ccedilla     : return cgt::KeyEvent::K_;
            // case Qt::Key_Egrave   : return cgt::KeyEvent::K_;
            // case Qt::Key_Eacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Ecircumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Ediaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_Igrave   : return cgt::KeyEvent::K_;
            // case Qt::Key_Iacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Icircumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Idiaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_ETH  : return cgt::KeyEvent::K_;
            // case Qt::Key_Ntilde   : return cgt::KeyEvent::K_;
            // case Qt::Key_Ograve   : return cgt::KeyEvent::K_;
            // case Qt::Key_Oacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Ocircumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Otilde   : return cgt::KeyEvent::K_;
            // case Qt::Key_Odiaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_multiply     : return cgt::KeyEvent::K_;
            // case Qt::Key_Ooblique     : return cgt::KeyEvent::K_;
            // case Qt::Key_Ugrave   : return cgt::KeyEvent::K_;
            // case Qt::Key_Uacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Ucircumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Udiaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_Yacute   : return cgt::KeyEvent::K_;
            // case Qt::Key_THORN    : return cgt::KeyEvent::K_;
            // case Qt::Key_ssharp   : return cgt::KeyEvent::K_;
            // case Qt::Key_division     : return cgt::KeyEvent::K_;
            // case Qt::Key_ydiaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_Multi_key    : return cgt::KeyEvent::K_;
            // case Qt::Key_Codeinput    : return cgt::KeyEvent::K_;
            // case Qt::Key_SingleCandidate  : return cgt::KeyEvent::K_;
            // case Qt::Key_MultipleCandidate    : return cgt::KeyEvent::K_;
            // case Qt::Key_PreviousCandidate    : return cgt::KeyEvent::K_;
            // case Qt::Key_Mode_switch  : return cgt::KeyEvent::K_;
            // case Qt::Key_Kanji    : return cgt::KeyEvent::K_;
            // case Qt::Key_Muhenkan     : return cgt::KeyEvent::K_;
            // case Qt::Key_Henkan   : return cgt::KeyEvent::K_;
            // case Qt::Key_Romaji   : return cgt::KeyEvent::K_;
            // case Qt::Key_Hiragana     : return cgt::KeyEvent::K_;
            // case Qt::Key_Katakana     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hiragana_Katakana    : return cgt::KeyEvent::K_;
            // case Qt::Key_Zenkaku  : return cgt::KeyEvent::K_;
            // case Qt::Key_Hankaku  : return cgt::KeyEvent::K_;
            // case Qt::Key_Zenkaku_Hankaku  : return cgt::KeyEvent::K_;
            // case Qt::Key_Touroku  : return cgt::KeyEvent::K_;
            // case Qt::Key_Massyo   : return cgt::KeyEvent::K_;
            // case Qt::Key_Kana_Lock    : return cgt::KeyEvent::K_;
            // case Qt::Key_Kana_Shift   : return cgt::KeyEvent::K_;
            // case Qt::Key_Eisu_Shift   : return cgt::KeyEvent::K_;
            // case Qt::Key_Eisu_toggle  : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul   : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Start     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_End   : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Hanja     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Jamo  : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Romaja    : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Jeonja    : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Banja     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_PreHanja  : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_PostHanja     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangul_Special   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Grave   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Acute   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Circumflex  : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Tilde   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Macron  : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Breve   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Abovedot    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Diaeresis   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Abovering   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Doubleacute     : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Caron   : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Cedilla     : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Ogonek  : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Iota    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Voiced_Sound    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Semivoiced_Sound    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Belowdot    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Hook    : return cgt::KeyEvent::K_;
            // case Qt::Key_Dead_Horn    : return cgt::KeyEvent::K_;
            // case Qt::Key_Back     : return cgt::KeyEvent::K_;
            // case Qt::Key_Forward  : return cgt::KeyEvent::K_;
            // case Qt::Key_Stop     : return cgt::KeyEvent::K_;
            // case Qt::Key_Refresh  : return cgt::KeyEvent::K_;
            // case Qt::Key_VolumeDown   : return cgt::KeyEvent::K_;
            // case Qt::Key_VolumeMute   : return cgt::KeyEvent::K_;
            // case Qt::Key_VolumeUp     : return cgt::KeyEvent::K_;
            // case Qt::Key_BassBoost    : return cgt::KeyEvent::K_;
            // case Qt::Key_BassUp   : return cgt::KeyEvent::K_;
            // case Qt::Key_BassDown     : return cgt::KeyEvent::K_;
            // case Qt::Key_TrebleUp     : return cgt::KeyEvent::K_;
            // case Qt::Key_TrebleDown   : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaPlay    : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaStop    : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaPrevious    : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaNext    : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaRecord  : return cgt::KeyEvent::K_;
            // case Qt::Key_HomePage     : return cgt::KeyEvent::K_;
            // case Qt::Key_Favorites    : return cgt::KeyEvent::K_;
            // case Qt::Key_Search   : return cgt::KeyEvent::K_;
            // case Qt::Key_Standby  : return cgt::KeyEvent::K_;
            // case Qt::Key_OpenUrl  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchMail   : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchMedia  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch0  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch1  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch2  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch3  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch4  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch5  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch6  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch7  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch8  : return cgt::KeyEvent::K_;
            // case Qt::Key_Launch9  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchA  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchB  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchC  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchD  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchE  : return cgt::KeyEvent::K_;
            // case Qt::Key_LaunchF  : return cgt::KeyEvent::K_;
            // case Qt::Key_MediaLast    : return cgt::KeyEvent::K_;
            // case Qt::Key_unknown  : return cgt::KeyEvent::K_;
            // case Qt::Key_Call     : return cgt::KeyEvent::K_;
            // case Qt::Key_Context1     : return cgt::KeyEvent::K_;
            // case Qt::Key_Context2     : return cgt::KeyEvent::K_;
            // case Qt::Key_Context3     : return cgt::KeyEvent::K_;
            // case Qt::Key_Context4     : return cgt::KeyEvent::K_;
            // case Qt::Key_Flip     : return cgt::KeyEvent::K_;
            // case Qt::Key_Hangup   : return cgt::KeyEvent::K_;
            // case Qt::Key_No   : return cgt::KeyEvent::K_;
            // case Qt::Key_Select   : return cgt::KeyEvent::K_;
            // case Qt::Key_Yes : return cgt::KeyEvent::K_;
        default: return cgt::KeyEvent::K_UNKNOWN;
schultezub's avatar
schultezub committed
579 580 581
    }
}

582
void QtCanvas::setSize(ivec2 newSize) {
583 584 585
    // pass size change command through Qt's event messaging system to enforce execution in GUI thread.
    // (setSize() may be called externally from a different thread).
    emit s_sizeChangedExternally(newSize.x, newSize.y);
586 587
}

grunau's avatar
grunau committed
588 589 590 591 592 593
QSize QtCanvas::sizeHint() const {
    const ivec2& size = getSize();

    return QSize(size.x, size.y);
}

594
void QtCanvas::acquireAsCurrentContext() {
595
    QGLWidget::makeCurrent();
596 597 598
}

void QtCanvas::releaseAsCurrentContext() {
599 600 601 602 603
    QGLWidget::doneCurrent();
}

void QtCanvas::sizeChangedExternally(int w, int h) {
    QWidget::resize(w, h);
604 605
}

606

schultezub's avatar
schultezub committed
607
} // namespace