|
| 1 | +/*========================================================================= |
| 2 | +
|
| 3 | + Program: Visualization Toolkit |
| 4 | + Module: QVTKInteractor.cxx |
| 5 | +
|
| 6 | + Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen |
| 7 | + All rights reserved. |
| 8 | + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
| 9 | +
|
| 10 | + This software is distributed WITHOUT ANY WARRANTY; without even |
| 11 | + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 12 | + PURPOSE. See the above copyright notice for more information. |
| 13 | +
|
| 14 | +=========================================================================*/ |
| 15 | +/* |
| 16 | + * Copyright 2004 Sandia Corporation. |
| 17 | + * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive |
| 18 | + * license for use of this work by or on behalf of the |
| 19 | + * U.S. Government. Redistribution and use in source and binary forms, with |
| 20 | + * or without modification, are permitted provided that this Notice and any |
| 21 | + * statement of authorship are reproduced on all copies. |
| 22 | + */ |
| 23 | + |
| 24 | +/*======================================================================== |
| 25 | + For general information about using VTK and Qt, see: |
| 26 | + http://www.trolltech.com/products/3rdparty/vtksupport.html |
| 27 | +=========================================================================*/ |
| 28 | + |
| 29 | +#ifdef _MSC_VER |
| 30 | +// Disable warnings that Qt headers give. |
| 31 | +#pragma warning(disable:4127) |
| 32 | +#pragma warning(disable:4512) |
| 33 | +#endif |
| 34 | + |
| 35 | +#include "QVTKInteractor.h" |
| 36 | +#include "QVTKInteractorInternal.h" |
| 37 | + |
| 38 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 39 | +# include "vtkTDxWinDevice.h" |
| 40 | +#endif |
| 41 | + |
| 42 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 43 | +# include "vtkTDxMacDevice.h" |
| 44 | +#endif |
| 45 | + |
| 46 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 47 | +# include "vtkTDxUnixDevice.h" |
| 48 | +#endif |
| 49 | + |
| 50 | +#include <QEvent> |
| 51 | +#include <QSignalMapper> |
| 52 | +#include <QTimer> |
| 53 | +#include <QResizeEvent> |
| 54 | + |
| 55 | +#include "vtkObjectFactory.h" |
| 56 | +#include "vtkCommand.h" |
| 57 | +#include "vtkRenderWindow.h" |
| 58 | + |
| 59 | +QVTKInteractorInternal::QVTKInteractorInternal(QVTKInteractor* p) |
| 60 | + : Parent(p) |
| 61 | +{ |
| 62 | + this->SignalMapper = new QSignalMapper(this); |
| 63 | + QObject::connect(this->SignalMapper, SIGNAL(mapped(int)), this, SLOT(TimerEvent(int)) ); |
| 64 | +} |
| 65 | + |
| 66 | +QVTKInteractorInternal::~QVTKInteractorInternal() |
| 67 | +{ |
| 68 | +} |
| 69 | + |
| 70 | +void QVTKInteractorInternal::TimerEvent(int id) |
| 71 | +{ |
| 72 | + Parent->TimerEvent(id); |
| 73 | +} |
| 74 | + |
| 75 | +/*! allocation method for Qt/VTK interactor |
| 76 | + */ |
| 77 | +vtkStandardNewMacro(QVTKInteractor); |
| 78 | + |
| 79 | +/*! constructor for Qt/VTK interactor |
| 80 | + */ |
| 81 | +QVTKInteractor::QVTKInteractor() |
| 82 | +{ |
| 83 | + this->Internal = new QVTKInteractorInternal(this); |
| 84 | + |
| 85 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 86 | + this->Device=vtkTDxWinDevice::New(); |
| 87 | +#endif |
| 88 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 89 | + this->Device=vtkTDxMacDevice::New(); |
| 90 | +#endif |
| 91 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 92 | + this->Device=0; |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +void QVTKInteractor::Initialize() |
| 97 | +{ |
| 98 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 99 | + if(this->UseTDx) |
| 100 | + { |
| 101 | + // this is QWidget::winId(); |
| 102 | + HWND hWnd=static_cast<HWND>(this->GetRenderWindow()->GetGenericWindowId()); |
| 103 | + if(!this->Device->GetInitialized()) |
| 104 | + { |
| 105 | + this->Device->SetInteractor(this); |
| 106 | + this->Device->SetWindowHandle(hWnd); |
| 107 | + this->Device->Initialize(); |
| 108 | + } |
| 109 | + } |
| 110 | +#endif |
| 111 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 112 | + if(this->UseTDx) |
| 113 | + { |
| 114 | + if(!this->Device->GetInitialized()) |
| 115 | + { |
| 116 | + this->Device->SetInteractor(this); |
| 117 | + // Do not initialize the device here. |
| 118 | + } |
| 119 | + } |
| 120 | +#endif |
| 121 | + this->Initialized = 1; |
| 122 | + this->Enable(); |
| 123 | +} |
| 124 | + |
| 125 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 126 | +// ---------------------------------------------------------------------------- |
| 127 | +vtkTDxUnixDevice *QVTKInteractor::GetDevice() |
| 128 | +{ |
| 129 | + return this->Device; |
| 130 | +} |
| 131 | + |
| 132 | +// ---------------------------------------------------------------------------- |
| 133 | +void QVTKInteractor::SetDevice(vtkTDxDevice *device) |
| 134 | +{ |
| 135 | + if(this->Device!=device) |
| 136 | + { |
| 137 | + this->Device=static_cast<vtkTDxUnixDevice *>(device); |
| 138 | + } |
| 139 | +} |
| 140 | +#endif |
| 141 | + |
| 142 | + |
| 143 | +/*! start method for interactor |
| 144 | + */ |
| 145 | +void QVTKInteractor::Start() |
| 146 | +{ |
| 147 | + vtkErrorMacro(<<"QVTKInteractor cannot control the event loop."); |
| 148 | +} |
| 149 | + |
| 150 | +/*! terminate the application |
| 151 | + */ |
| 152 | +void QVTKInteractor::TerminateApp() |
| 153 | +{ |
| 154 | + // we are in a GUI so let's terminate the GUI the normal way |
| 155 | + //qApp->exit(); |
| 156 | +} |
| 157 | + |
| 158 | +// ---------------------------------------------------------------------------- |
| 159 | +void QVTKInteractor::StartListening() |
| 160 | +{ |
| 161 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 162 | + if(this->Device->GetInitialized() && !this->Device->GetIsListening()) |
| 163 | + { |
| 164 | + this->Device->StartListening(); |
| 165 | + } |
| 166 | +#endif |
| 167 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 168 | + if(this->UseTDx && !this->Device->GetInitialized()) |
| 169 | + { |
| 170 | + this->Device->Initialize(); |
| 171 | + } |
| 172 | +#endif |
| 173 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 174 | + if(this->UseTDx && this->Device!=0) |
| 175 | + { |
| 176 | + this->Device->SetInteractor(this); |
| 177 | + } |
| 178 | +#endif |
| 179 | +} |
| 180 | + |
| 181 | +// ---------------------------------------------------------------------------- |
| 182 | +void QVTKInteractor::StopListening() |
| 183 | +{ |
| 184 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 185 | + if(this->Device->GetInitialized() && this->Device->GetIsListening()) |
| 186 | + { |
| 187 | + this->Device->StopListening(); |
| 188 | + } |
| 189 | +#endif |
| 190 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 191 | + if(this->UseTDx && this->Device->GetInitialized()) |
| 192 | + { |
| 193 | + this->Device->Close(); |
| 194 | + } |
| 195 | +#endif |
| 196 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 197 | + if(this->UseTDx && this->Device!=0) |
| 198 | + { |
| 199 | + // this assumes that a outfocus event is emitted prior |
| 200 | + // a infocus event on another widget. |
| 201 | + this->Device->SetInteractor(0); |
| 202 | + } |
| 203 | +#endif |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +/*! handle timer event |
| 208 | + */ |
| 209 | +void QVTKInteractor::TimerEvent(int timerId) |
| 210 | +{ |
| 211 | + if ( !this->GetEnabled() ) |
| 212 | + { |
| 213 | + return; |
| 214 | + } |
| 215 | + this->InvokeEvent(vtkCommand::TimerEvent, (void*)&timerId); |
| 216 | + |
| 217 | + if(this->IsOneShotTimer(timerId)) |
| 218 | + { |
| 219 | + this->DestroyTimer(timerId); // 'cause our Qt timers are always repeating |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +/*! constructor |
| 224 | + */ |
| 225 | +QVTKInteractor::~QVTKInteractor() |
| 226 | +{ |
| 227 | + delete this->Internal; |
| 228 | +#if defined(VTK_USE_TDX) && defined(Q_OS_WIN) |
| 229 | + this->Device->Delete(); |
| 230 | +#endif |
| 231 | +#if defined(VTK_USE_TDX) && defined(Q_OS_MAC) |
| 232 | + this->Device->Delete(); |
| 233 | +#endif |
| 234 | +#if defined(VTK_USE_TDX) && (defined(Q_WS_X11) || defined(Q_OS_LINUX)) |
| 235 | + this->Device=0; |
| 236 | +#endif |
| 237 | +} |
| 238 | + |
| 239 | +/*! create Qt timer with an interval of 10 msec. |
| 240 | + */ |
| 241 | +int QVTKInteractor::InternalCreateTimer(int timerId, int vtkNotUsed(timerType), unsigned long duration) |
| 242 | +{ |
| 243 | + QTimer* timer = new QTimer(this->Internal); |
| 244 | + timer->start(duration); |
| 245 | + this->Internal->SignalMapper->setMapping(timer, timerId); |
| 246 | + QObject::connect(timer, SIGNAL(timeout()), this->Internal->SignalMapper, SLOT(map())); |
| 247 | + int platformTimerId = timer->timerId(); |
| 248 | + this->Internal->Timers.insert(QVTKInteractorInternal::TimerMap::value_type(platformTimerId, timer)); |
| 249 | + return platformTimerId; |
| 250 | +} |
| 251 | + |
| 252 | +/*! destroy timer |
| 253 | + */ |
| 254 | +int QVTKInteractor::InternalDestroyTimer(int platformTimerId) |
| 255 | +{ |
| 256 | + QVTKInteractorInternal::TimerMap::iterator iter = this->Internal->Timers.find(platformTimerId); |
| 257 | + if(iter != this->Internal->Timers.end()) |
| 258 | + { |
| 259 | + iter->second->stop(); |
| 260 | + iter->second->deleteLater(); |
| 261 | + this->Internal->Timers.erase(iter); |
| 262 | + return 1; |
| 263 | + } |
| 264 | + return 0; |
| 265 | +} |
0 commit comments