| 1 |
/**************************************************************************\ |
| 2 |
* Copyright (c) Kongsberg Oil & Gas Technologies AS |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Neither the name of the copyright holder nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
\**************************************************************************/ |
| 32 |
|
| 33 |
/* |
| 34 |
Adjust how QuarterWidget reacts to alt key events |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <QCoreApplication> |
| 38 |
#include <QFocusEvent> |
| 39 |
#include <QKeyEvent> |
| 40 |
|
| 41 |
#include "InteractionMode.h" |
| 42 |
#include "QuarterWidget.h" |
| 43 |
|
| 44 |
|
| 45 |
using namespace SIM::Coin3D::Quarter; |
| 46 |
|
| 47 |
InteractionMode::InteractionMode(QuarterWidget * quarterwidget) |
| 48 |
: QObject(quarterwidget) |
| 49 |
{ |
| 50 |
this->quarterwidget = quarterwidget; |
| 51 |
this->altkeydown = false; |
| 52 |
this->prevcursor = QCursor(); |
| 53 |
this->prevnavstate = |
| 54 |
this->quarterwidget->getSoEventManager()->getNavigationState(); |
| 55 |
|
| 56 |
this->isenabled = true; |
| 57 |
} |
| 58 |
|
| 59 |
InteractionMode::~InteractionMode() |
| 60 |
{ |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
InteractionMode::setEnabled(bool yes) |
| 66 |
{ |
| 67 |
this->isenabled = yes; |
| 68 |
} |
| 69 |
|
| 70 |
bool |
| 71 |
InteractionMode::enabled() const |
| 72 |
{ |
| 73 |
return this->isenabled; |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
InteractionMode::setOn(bool on) |
| 78 |
{ |
| 79 |
if (!this->isenabled) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
SoEventManager * eventmanager = this->quarterwidget->getSoEventManager(); |
| 84 |
|
| 85 |
if (on) { |
| 86 |
this->altkeydown = true; |
| 87 |
this->prevnavstate = eventmanager->getNavigationState(); |
| 88 |
this->prevcursor = this->quarterwidget->cursor(); |
| 89 |
this->quarterwidget->setCursor(this->quarterwidget->stateCursor("interact")); |
| 90 |
eventmanager->setNavigationState(SoEventManager::NO_NAVIGATION); |
| 91 |
} else { |
| 92 |
this->altkeydown = false; |
| 93 |
this->quarterwidget->setCursor(this->prevcursor); |
| 94 |
eventmanager->setNavigationState(this->prevnavstate); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
bool |
| 99 |
InteractionMode::on() const |
| 100 |
{ |
| 101 |
return this->altkeydown; |
| 102 |
} |
| 103 |
|
| 104 |
bool |
| 105 |
InteractionMode::eventFilter(QObject * obj, QEvent * event) |
| 106 |
{ |
| 107 |
if (!this->isenabled) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
assert(obj == this->quarterwidget); |
| 112 |
|
| 113 |
switch (event->type()) { |
| 114 |
case QEvent::KeyPress: |
| 115 |
return this->keyPressEvent(dynamic_cast<QKeyEvent *>(event)); |
| 116 |
case QEvent::KeyRelease: |
| 117 |
return this->keyReleaseEvent(dynamic_cast<QKeyEvent *>(event)); |
| 118 |
case QEvent::FocusOut: |
| 119 |
return this->focusOutEvent(dynamic_cast<QFocusEvent *>(event)); |
| 120 |
default: |
| 121 |
return QObject::eventFilter(obj, event); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
when alt is pressed, override navigation and allow scenegraph to |
| 127 |
process events so draggers and manipulators works |
| 128 |
*/ |
| 129 |
bool |
| 130 |
InteractionMode::keyPressEvent(QKeyEvent * event) |
| 131 |
{ |
| 132 |
if (!event || |
| 133 |
!(event->key() == Qt::Key_Alt) || |
| 134 |
!(event->modifiers() & Qt::AltModifier)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
this->setOn(true); |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
bool |
| 143 |
InteractionMode::keyReleaseEvent(QKeyEvent * event) |
| 144 |
{ |
| 145 |
if (!event || !(event->key() == Qt::Key_Alt)) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
this->setOn(false); |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
if we lose focus while alt is down, send an alt-release event |
| 155 |
*/ |
| 156 |
bool |
| 157 |
InteractionMode::focusOutEvent(QFocusEvent * event) |
| 158 |
{ |
| 159 |
Q_UNUSED(event); |
| 160 |
if (this->altkeydown) { |
| 161 |
QKeyEvent keyevent(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier); |
| 162 |
return QCoreApplication::sendEvent(this->quarterwidget, &keyevent); |
| 163 |
} |
| 164 |
return false; |
| 165 |
} |