| 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 |
#ifdef _MSC_VER |
| 34 |
#pragma warning(disable : 4267) |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "QuarterWidgetP.h" |
| 38 |
#include "QuarterWidget.h" |
| 39 |
#include "eventhandlers/EventFilter.h" |
| 40 |
|
| 41 |
#include <QActionGroup> |
| 42 |
#include <QApplication> |
| 43 |
#include <QCursor> |
| 44 |
#include <QMenu> |
| 45 |
#include <QOpenGLContext> |
| 46 |
#include <QOpenGLWidget> |
| 47 |
|
| 48 |
#include <Inventor/SoEventManager.h> |
| 49 |
#include <Inventor/actions/SoSearchAction.h> |
| 50 |
#include <Inventor/elements/SoGLCacheContextElement.h> |
| 51 |
#include <Inventor/lists/SbList.h> |
| 52 |
#include <Inventor/misc/SoContextHandler.h> |
| 53 |
#include <Inventor/nodes/SoCamera.h> |
| 54 |
#include <Inventor/nodes/SoNode.h> |
| 55 |
#include <Inventor/scxml/SoScXMLStateMachine.h> |
| 56 |
#include <Inventor/C/glue/gl.h> |
| 57 |
|
| 58 |
#include "ContextMenu.h" |
| 59 |
#include "NativeEvent.h" |
| 60 |
#include "QuarterP.h" |
| 61 |
|
| 62 |
|
| 63 |
using namespace SIM::Coin3D::Quarter; |
| 64 |
|
| 65 |
class QuarterWidgetP_cachecontext { |
| 66 |
public: |
| 67 |
uint32_t id; |
| 68 |
SbList <const QOpenGLWidget *> widgetlist; |
| 69 |
}; |
| 70 |
|
| 71 |
static SbList <QuarterWidgetP_cachecontext *> * cachecontext_list = nullptr; |
| 72 |
|
| 73 |
QuarterWidgetP::QuarterWidgetP(QuarterWidget * masterptr, const QOpenGLWidget * sharewidget) |
| 74 |
: master(masterptr), |
| 75 |
scene(nullptr), |
| 76 |
eventfilter(nullptr), |
| 77 |
interactionmode(nullptr), |
| 78 |
sorendermanager(nullptr), |
| 79 |
soeventmanager(nullptr), |
| 80 |
initialsorendermanager(false), |
| 81 |
initialsoeventmanager(false), |
| 82 |
headlight(nullptr), |
| 83 |
cachecontext(nullptr), |
| 84 |
contextmenuenabled(true), |
| 85 |
autoredrawenabled(true), |
| 86 |
interactionmodeenabled(false), |
| 87 |
clearzbuffer(true), |
| 88 |
clearwindow(true), |
| 89 |
addactions(true), |
| 90 |
processdelayqueue(true), |
| 91 |
currentStateMachine(nullptr), |
| 92 |
device_pixel_ratio(1.0), |
| 93 |
transparencytypegroup(nullptr), |
| 94 |
stereomodegroup(nullptr), |
| 95 |
rendermodegroup(nullptr), |
| 96 |
contextmenu(nullptr) |
| 97 |
{ |
| 98 |
this->cachecontext = findCacheContext(masterptr, sharewidget); |
| 99 |
|
| 100 |
// FIXME: Centralize this as only one custom event filter can be |
| 101 |
// added to an application. (20101019 handegar) |
| 102 |
#ifdef HAVE_SPACENAV_LIB // fixes #0001970 |
| 103 |
qApp->setEventFilter(QuarterWidgetP::nativeEventFilter); |
| 104 |
#endif |
| 105 |
} |
| 106 |
|
| 107 |
QuarterWidgetP::~QuarterWidgetP() |
| 108 |
{ |
| 109 |
QOpenGLWidget* glMaster = static_cast<QOpenGLWidget*>(this->master->viewport()); |
| 110 |
removeFromCacheContext(this->cachecontext, glMaster); |
| 111 |
delete this->contextmenu; |
| 112 |
} |
| 113 |
|
| 114 |
SoCamera * |
| 115 |
QuarterWidgetP::searchForCamera(SoNode * root) |
| 116 |
{ |
| 117 |
SoSearchAction sa; |
| 118 |
sa.setInterest(SoSearchAction::FIRST); |
| 119 |
sa.setType(SoCamera::getClassTypeId()); |
| 120 |
sa.apply(root); |
| 121 |
|
| 122 |
if (sa.getPath()) { |
| 123 |
SoNode * node = sa.getPath()->getTail(); |
| 124 |
if (node && node->isOfType(SoCamera::getClassTypeId())) { |
| 125 |
return (SoCamera *) node; |
| 126 |
} |
| 127 |
} |
| 128 |
return nullptr; |
| 129 |
} |
| 130 |
|
| 131 |
uint32_t |
| 132 |
QuarterWidgetP::getCacheContextId() const |
| 133 |
{ |
| 134 |
return this->cachecontext->id; |
| 135 |
} |
| 136 |
|
| 137 |
QuarterWidgetP_cachecontext * |
| 138 |
QuarterWidgetP::findCacheContext(QuarterWidget * widget, const QOpenGLWidget * sharewidget) |
| 139 |
{ |
| 140 |
if (!cachecontext_list) { |
| 141 |
// FIXME: static memory leak |
| 142 |
cachecontext_list = new SbList <QuarterWidgetP_cachecontext*>; |
| 143 |
} |
| 144 |
for (int i = 0; i < cachecontext_list->getLength(); i++) { |
| 145 |
QuarterWidgetP_cachecontext * cachecontext = (*cachecontext_list)[i]; |
| 146 |
|
| 147 |
for (int j = 0; j < cachecontext->widgetlist.getLength(); j++) { |
| 148 |
if (cachecontext->widgetlist[j] == sharewidget) { |
| 149 |
cachecontext->widgetlist.append(static_cast<const QOpenGLWidget*>(widget->viewport())); |
| 150 |
return cachecontext; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
QuarterWidgetP_cachecontext * cachecontext = new QuarterWidgetP_cachecontext; |
| 155 |
cachecontext->id = SoGLCacheContextElement::getUniqueCacheContext(); |
| 156 |
cachecontext->widgetlist.append(static_cast<const QOpenGLWidget*>(widget->viewport())); |
| 157 |
cachecontext_list->append(cachecontext); |
| 158 |
|
| 159 |
return cachecontext; |
| 160 |
} |
| 161 |
|
| 162 |
void |
| 163 |
QuarterWidgetP::removeFromCacheContext(QuarterWidgetP_cachecontext * context, const QOpenGLWidget * widget) |
| 164 |
{ |
| 165 |
context->widgetlist.removeItem((const QOpenGLWidget*) widget); |
| 166 |
|
| 167 |
if (context->widgetlist.getLength() == 0) { // last context in this share group? |
| 168 |
assert(cachecontext_list); |
| 169 |
|
| 170 |
for (int i = 0; i < cachecontext_list->getLength(); i++) { |
| 171 |
if ((*cachecontext_list)[i] == context) { |
| 172 |
QOpenGLContext* glcontext = widget->context(); |
| 173 |
if (glcontext) { |
| 174 |
// set the context while calling destructingContext() (might trigger OpenGL calls) |
| 175 |
if (glcontext->isValid()) { |
| 176 |
const_cast<QOpenGLWidget*> (widget)->makeCurrent(); |
| 177 |
} |
| 178 |
// fetch the cc_glglue context instance as a workaround for a bug fixed in Coin r12818 |
| 179 |
(void) cc_glglue_instance(context->id); |
| 180 |
} |
| 181 |
cachecontext_list->removeFast(i); |
| 182 |
SoContextHandler::destructingContext(context->id); |
| 183 |
if (glcontext) { |
| 184 |
if (glcontext->isValid()) { |
| 185 |
const_cast<QOpenGLWidget*> (widget)->doneCurrent(); |
| 186 |
} |
| 187 |
} |
| 188 |
delete context; |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void |
| 196 |
QuarterWidgetP::replaceGLWidget(const QOpenGLWidget * newviewport) |
| 197 |
{ |
| 198 |
QOpenGLWidget* oldviewport = static_cast<QOpenGLWidget*>(this->master->viewport()); |
| 199 |
cachecontext->widgetlist.removeItem(oldviewport); |
| 200 |
cachecontext->widgetlist.append(newviewport); |
| 201 |
} |
| 202 |
|
| 203 |
/*! |
| 204 |
|
| 205 |
*/ |
| 206 |
void |
| 207 |
QuarterWidgetP::rendercb(void * userdata, SoRenderManager *) |
| 208 |
{ |
| 209 |
QuarterWidget * thisp = static_cast<QuarterWidget *>(userdata); |
| 210 |
|
| 211 |
if (thisp->pimpl->autoredrawenabled) { |
| 212 |
thisp->redraw(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void |
| 217 |
QuarterWidgetP::prerendercb(void * userdata, SoRenderManager * manager) |
| 218 |
{ |
| 219 |
Q_UNUSED(manager); |
| 220 |
QuarterWidgetP * thisp = static_cast<QuarterWidgetP *>(userdata); |
| 221 |
SoEventManager * evman = thisp->soeventmanager; |
| 222 |
assert(evman); |
| 223 |
for (int c = 0; c < evman->getNumSoScXMLStateMachines(); ++c) { |
| 224 |
SoScXMLStateMachine * statemachine = evman->getSoScXMLStateMachine(c); |
| 225 |
statemachine->preGLRender(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void |
| 230 |
QuarterWidgetP::postrendercb(void * userdata, SoRenderManager * manager) |
| 231 |
{ |
| 232 |
Q_UNUSED(manager); |
| 233 |
QuarterWidgetP * thisp = static_cast<QuarterWidgetP *>(userdata); |
| 234 |
SoEventManager * evman = thisp->soeventmanager; |
| 235 |
assert(evman); |
| 236 |
for (int c = 0; c < evman->getNumSoScXMLStateMachines(); ++c) { |
| 237 |
SoScXMLStateMachine * statemachine = evman->getSoScXMLStateMachine(c); |
| 238 |
statemachine->postGLRender(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
QuarterWidgetP::statechangecb(void * userdata, ScXMLStateMachine * statemachine, const char * stateid, SbBool enter, SbBool) |
| 244 |
{ |
| 245 |
Q_UNUSED(statemachine); |
| 246 |
static const SbName contextmenurequest("contextmenurequest"); |
| 247 |
QuarterWidgetP * thisp = static_cast<QuarterWidgetP *>(userdata); |
| 248 |
assert(thisp && thisp->master); |
| 249 |
if (enter) { |
| 250 |
SbName state(stateid); |
| 251 |
if (thisp->contextmenuenabled && state == contextmenurequest) { |
| 252 |
thisp->contextMenu()->exec(thisp->eventfilter->globalMousePosition()); |
| 253 |
} |
| 254 |
if (QuarterP::statecursormap->contains(state)) { |
| 255 |
QCursor cursor = QuarterP::statecursormap->value(state); |
| 256 |
thisp->master->setCursor(cursor); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
#define ADD_ACTION(enum, text, group, parent, list) \ |
| 262 |
do { \ |
| 263 |
QAction * action = new QAction(text, parent); \ |
| 264 |
action->setCheckable(true); \ |
| 265 |
action->setData(enum); \ |
| 266 |
action->setObjectName(text); \ |
| 267 |
action->setActionGroup(group); \ |
| 268 |
list.append(action); \ |
| 269 |
} while (0) |
| 270 |
|
| 271 |
|
| 272 |
QList<QAction *> |
| 273 |
QuarterWidgetP::transparencyTypeActions() const |
| 274 |
{ |
| 275 |
if (this->transparencytypeactions.isEmpty()) { |
| 276 |
this->transparencytypegroup = new QActionGroup(this->master); |
| 277 |
ADD_ACTION(QuarterWidget::NONE, "none", transparencytypegroup, this->master, this->transparencytypeactions); |
| 278 |
ADD_ACTION(QuarterWidget::SCREEN_DOOR, "screen door", transparencytypegroup, this->master, this->transparencytypeactions); |
| 279 |
ADD_ACTION(QuarterWidget::ADD, "add", transparencytypegroup, this->master, this->transparencytypeactions); |
| 280 |
ADD_ACTION(QuarterWidget::DELAYED_ADD, "delayed add", transparencytypegroup, this->master, this->transparencytypeactions); |
| 281 |
ADD_ACTION(QuarterWidget::SORTED_OBJECT_ADD, "sorted object add", transparencytypegroup, this->master, this->transparencytypeactions); |
| 282 |
ADD_ACTION(QuarterWidget::BLEND, "blend", transparencytypegroup, this->master, this->transparencytypeactions); |
| 283 |
ADD_ACTION(QuarterWidget::DELAYED_BLEND, "delayed blend", transparencytypegroup, this->master, this->transparencytypeactions); |
| 284 |
ADD_ACTION(QuarterWidget::SORTED_OBJECT_BLEND, "sorted object blend", transparencytypegroup, this->master, this->transparencytypeactions); |
| 285 |
ADD_ACTION(QuarterWidget::SORTED_OBJECT_SORTED_TRIANGLE_ADD, "sorted object sorted triangle add", transparencytypegroup, this->master, this->transparencytypeactions); |
| 286 |
ADD_ACTION(QuarterWidget::SORTED_OBJECT_SORTED_TRIANGLE_BLEND, "sorted object sorted triangle blend", transparencytypegroup, this->master, this->transparencytypeactions); |
| 287 |
ADD_ACTION(QuarterWidget::SORTED_LAYERS_BLEND, "sorted layers blend", transparencytypegroup, this->master, this->transparencytypeactions); |
| 288 |
} |
| 289 |
return this->transparencytypeactions; |
| 290 |
} |
| 291 |
|
| 292 |
QList<QAction *> |
| 293 |
QuarterWidgetP::stereoModeActions() const |
| 294 |
{ |
| 295 |
if (this->stereomodeactions.isEmpty()) { |
| 296 |
this->stereomodegroup = new QActionGroup(this->master); |
| 297 |
ADD_ACTION(QuarterWidget::MONO, "mono", stereomodegroup, this->master, stereomodeactions); |
| 298 |
ADD_ACTION(QuarterWidget::ANAGLYPH, "anaglyph", stereomodegroup, this->master, stereomodeactions); |
| 299 |
ADD_ACTION(QuarterWidget::QUAD_BUFFER, "quad buffer", stereomodegroup, this->master, stereomodeactions); |
| 300 |
ADD_ACTION(QuarterWidget::INTERLEAVED_ROWS, "interleaved rows", stereomodegroup, this->master, stereomodeactions); |
| 301 |
ADD_ACTION(QuarterWidget::INTERLEAVED_COLUMNS, "interleaved columns", stereomodegroup, this->master, stereomodeactions); |
| 302 |
} |
| 303 |
return this->stereomodeactions; |
| 304 |
} |
| 305 |
|
| 306 |
QList<QAction *> |
| 307 |
QuarterWidgetP::renderModeActions() const |
| 308 |
{ |
| 309 |
if (this->rendermodeactions.isEmpty()) { |
| 310 |
this->rendermodegroup = new QActionGroup(this->master); |
| 311 |
ADD_ACTION(QuarterWidget::AS_IS, "as is", rendermodegroup, this->master, rendermodeactions); |
| 312 |
ADD_ACTION(QuarterWidget::WIREFRAME, "wireframe", rendermodegroup, this->master, rendermodeactions); |
| 313 |
ADD_ACTION(QuarterWidget::WIREFRAME_OVERLAY, "wireframe overlay", rendermodegroup, this->master, rendermodeactions); |
| 314 |
ADD_ACTION(QuarterWidget::POINTS, "points", rendermodegroup, this->master, rendermodeactions); |
| 315 |
ADD_ACTION(QuarterWidget::HIDDEN_LINE, "hidden line", rendermodegroup, this->master, rendermodeactions); |
| 316 |
ADD_ACTION(QuarterWidget::BOUNDING_BOX, "bounding box", rendermodegroup, this->master, rendermodeactions); |
| 317 |
} |
| 318 |
return this->rendermodeactions; |
| 319 |
} |
| 320 |
|
| 321 |
#undef ADD_ACTION |
| 322 |
|
| 323 |
QMenu * |
| 324 |
QuarterWidgetP::contextMenu() |
| 325 |
{ |
| 326 |
if (!this->contextmenu) { |
| 327 |
this->contextmenu = new ContextMenu(this->master); |
| 328 |
} |
| 329 |
|
| 330 |
return this->contextmenu->getMenu(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
bool |
| 335 |
QuarterWidgetP::nativeEventFilter(void * message, long * result) |
| 336 |
{ |
| 337 |
#ifdef HAVE_SPACENAV_LIB |
| 338 |
XEvent * event = (XEvent *) message; |
| 339 |
if (event->type == ClientMessage) { |
| 340 |
// FIXME: I don't really like this, but the original XEvent will |
| 341 |
// die before reaching the destination within the Qt system. To |
| 342 |
// avoid this, we'll have to make a copy. We should try to find a |
| 343 |
// workaround for this. (20101020 handegar) |
| 344 |
|
| 345 |
// The copy will automatically be deleted when the NativeEvent dies. |
| 346 |
XEvent * copy = (XEvent *) malloc(sizeof(XEvent)); |
| 347 |
memcpy(copy, event, sizeof(XEvent)); |
| 348 |
NativeEvent * ne = new NativeEvent(copy); |
| 349 |
|
| 350 |
qApp->postEvent(QApplication::focusWidget(), ne); |
| 351 |
return true; |
| 352 |
} |
| 353 |
#else |
| 354 |
Q_UNUSED(message); |
| 355 |
Q_UNUSED(result); |
| 356 |
#endif // HAVE_SPACENAV_LIB |
| 357 |
|
| 358 |
return false; |
| 359 |
} |
| 360 |
|