| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <limits> |
| 24 |
|
| 25 |
#include <QCursor> |
| 26 |
#include <QTimer> |
| 27 |
#include <Inventor/nodes/SoCamera.h> |
| 28 |
|
| 29 |
#include <Base/Tools.h> |
| 30 |
|
| 31 |
#include "DemoMode.h" |
| 32 |
#include "ui_DemoMode.h" |
| 33 |
#include "Application.h" |
| 34 |
#include "Command.h" |
| 35 |
#include "Document.h" |
| 36 |
#include "View3DInventor.h" |
| 37 |
#include "View3DInventorViewer.h" |
| 38 |
|
| 39 |
|
| 40 |
using namespace Gui::Dialog; |
| 41 |
|
| 42 |
/* TRANSLATOR Gui::Dialog::DemoMode */ |
| 43 |
|
| 44 |
DemoMode::DemoMode(QWidget* /*parent*/, Qt::WindowFlags fl) |
| 45 |
: QDialog(nullptr, fl | Qt::WindowStaysOnTopHint) |
| 46 |
, viewAxis(0, 0, -1) |
| 47 |
, ui(new Ui_DemoMode) |
| 48 |
{ |
| 49 |
// create widgets |
| 50 |
ui->setupUi(this); |
| 51 |
setupConnections(); |
| 52 |
ui->playButton->setCheckable(true); |
| 53 |
|
| 54 |
timer = new QTimer(this); |
| 55 |
timer->setInterval(1000 * ui->timeout->value()); |
| 56 |
connect(timer, &QTimer::timeout, this, &DemoMode::onAutoPlay); |
| 57 |
oldvalue = ui->angleSlider->value(); |
| 58 |
|
| 59 |
wasHidden = false; |
| 60 |
showHideTimer = new QTimer(this); |
| 61 |
showHideTimer->setInterval(5000); |
| 62 |
connect(showHideTimer, &QTimer::timeout, this, &DemoMode::hide); |
| 63 |
} |
| 64 |
|
| 65 |
/** Destroys the object and frees any allocated resources */ |
| 66 |
DemoMode::~DemoMode() |
| 67 |
{ |
| 68 |
delete ui; |
| 69 |
} |
| 70 |
|
| 71 |
void DemoMode::setupConnections() |
| 72 |
{ |
| 73 |
// clang-format off |
| 74 |
connect(ui->playButton, &QPushButton::clicked, |
| 75 |
this, &DemoMode::onPlayButtonToggled); |
| 76 |
connect(ui->fullscreen, &QCheckBox::toggled, |
| 77 |
this, &DemoMode::onFullscreenToggled); |
| 78 |
connect(ui->timerCheck, &QCheckBox::toggled, |
| 79 |
this, &DemoMode::onTimerCheckToggled); |
| 80 |
connect(ui->speedSlider, &QSlider::valueChanged, |
| 81 |
this, &DemoMode::onSpeedSliderValueChanged); |
| 82 |
connect(ui->angleSlider, &QSlider::valueChanged, |
| 83 |
this, &DemoMode::onAngleSliderValueChanged); |
| 84 |
connect(ui->timeout, qOverload<int>(&QSpinBox::valueChanged), |
| 85 |
this, &DemoMode::onTimeoutValueChanged); |
| 86 |
// clang-format on |
| 87 |
} |
| 88 |
|
| 89 |
void DemoMode::reset() |
| 90 |
{ |
| 91 |
onFullscreenToggled(false); |
| 92 |
Gui::View3DInventor* view = activeView(); |
| 93 |
if (view) { |
| 94 |
view->getViewer()->stopAnimating(); |
| 95 |
} |
| 96 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 97 |
"User parameter:BaseApp/Preferences/View" |
| 98 |
); |
| 99 |
hGrp->Notify("UseNavigationAnimations"); |
| 100 |
} |
| 101 |
|
| 102 |
void DemoMode::accept() |
| 103 |
{ |
| 104 |
reset(); |
| 105 |
QDialog::accept(); |
| 106 |
} |
| 107 |
|
| 108 |
void DemoMode::reject() |
| 109 |
{ |
| 110 |
reset(); |
| 111 |
QDialog::reject(); |
| 112 |
} |
| 113 |
|
| 114 |
bool DemoMode::eventFilter(QObject* obj, QEvent* event) |
| 115 |
{ |
| 116 |
if (event->type() == QEvent::MouseMove) { |
| 117 |
if (ui->fullscreen->isChecked()) { |
| 118 |
QPoint point = QCursor::pos() - oldPos; |
| 119 |
if (point.manhattanLength() > 5) { |
| 120 |
show(); |
| 121 |
showHideTimer->start(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return QDialog::eventFilter(obj, event); |
| 126 |
} |
| 127 |
|
| 128 |
void DemoMode::showEvent(QShowEvent*) |
| 129 |
{ |
| 130 |
if (this->wasHidden) { |
| 131 |
this->move(this->pnt); |
| 132 |
} |
| 133 |
this->wasHidden = false; |
| 134 |
} |
| 135 |
|
| 136 |
void DemoMode::hideEvent(QHideEvent*) |
| 137 |
{ |
| 138 |
this->pnt = this->pos(); |
| 139 |
this->wasHidden = true; |
| 140 |
this->oldPos = QCursor::pos(); |
| 141 |
showHideTimer->stop(); |
| 142 |
} |
| 143 |
|
| 144 |
Gui::View3DInventor* DemoMode::activeView() const |
| 145 |
{ |
| 146 |
if (Document* doc = Application::Instance->activeDocument()) { |
| 147 |
return freecad_cast<Gui::View3DInventor*>(doc->getActiveView()); |
| 148 |
} |
| 149 |
|
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
|
| 153 |
float DemoMode::getSpeed(int v) const |
| 154 |
{ |
| 155 |
float speed = (static_cast<float>(v)) / 10.0f; // let 10.0 be the maximum speed |
| 156 |
return speed; |
| 157 |
} |
| 158 |
|
| 159 |
SbVec3f DemoMode::getDirection(Gui::View3DInventor* view) const |
| 160 |
{ |
| 161 |
SoCamera* cam = view->getViewer()->getSoRenderManager()->getCamera(); |
| 162 |
if (!cam) { |
| 163 |
return this->viewAxis; |
| 164 |
} |
| 165 |
SbRotation rot = cam->orientation.getValue(); |
| 166 |
SbRotation inv = rot.inverse(); |
| 167 |
SbVec3f vec(this->viewAxis); |
| 168 |
inv.multVec(vec, vec); |
| 169 |
if (vec.length() < std::numeric_limits<float>::epsilon()) { |
| 170 |
vec = this->viewAxis; |
| 171 |
} |
| 172 |
vec.normalize(); |
| 173 |
return vec; |
| 174 |
} |
| 175 |
|
| 176 |
void DemoMode::onAngleSliderValueChanged(int v) |
| 177 |
{ |
| 178 |
Gui::View3DInventor* view = activeView(); |
| 179 |
if (view) { |
| 180 |
SoCamera* cam = view->getViewer()->getSoRenderManager()->getCamera(); |
| 181 |
if (!cam) { |
| 182 |
return; |
| 183 |
} |
| 184 |
auto angle = Base::toRadians<float>(/*90-v*/ v - this->oldvalue); |
| 185 |
SbRotation rot(SbVec3f(-1, 0, 0), angle); |
| 186 |
reorientCamera(cam, rot); |
| 187 |
this->oldvalue = v; |
| 188 |
if (view->getViewer()->isSpinning()) { |
| 189 |
startAnimation(view); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void DemoMode::reorientCamera(SoCamera* cam, const SbRotation& rot) |
| 195 |
{ |
| 196 |
// Find global coordinates of focal point. |
| 197 |
SbVec3f direction; |
| 198 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 199 |
SbVec3f focalpoint = cam->position.getValue() + cam->focalDistance.getValue() * direction; |
| 200 |
|
| 201 |
// Set new orientation value by accumulating the new rotation. |
| 202 |
cam->orientation = rot * cam->orientation.getValue(); |
| 203 |
|
| 204 |
// Reposition camera so we are still pointing at the same old focal point. |
| 205 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 206 |
cam->position = focalpoint - cam->focalDistance.getValue() * direction; |
| 207 |
} |
| 208 |
|
| 209 |
void DemoMode::onSpeedSliderValueChanged(int v) |
| 210 |
{ |
| 211 |
Q_UNUSED(v); |
| 212 |
Gui::View3DInventor* view = activeView(); |
| 213 |
if (view && view->getViewer()->isSpinning()) { |
| 214 |
startAnimation(view); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void DemoMode::onPlayButtonToggled(bool pressed) |
| 219 |
{ |
| 220 |
Gui::View3DInventor* view = activeView(); |
| 221 |
if (view) { |
| 222 |
if (pressed) { |
| 223 |
if (!view->getViewer()->isSpinning()) { |
| 224 |
SoCamera* cam = view->getViewer()->getSoRenderManager()->getCamera(); |
| 225 |
if (cam) { |
| 226 |
SbRotation rot = cam->orientation.getValue(); |
| 227 |
SbVec3f vec(0, -1, 0); |
| 228 |
rot.multVec(vec, this->viewAxis); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
startAnimation(view); |
| 233 |
ui->playButton->setText(tr("Stop")); |
| 234 |
} |
| 235 |
else { |
| 236 |
view->getViewer()->stopAnimating(); |
| 237 |
ui->playButton->setText(tr("Play")); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void DemoMode::onFullscreenToggled(bool on) |
| 243 |
{ |
| 244 |
Gui::View3DInventor* view = activeView(); |
| 245 |
if (view) { |
| 246 |
CommandManager& rcCmdMgr = Application::Instance->commandManager(); |
| 247 |
Command* cmd = rcCmdMgr.getCommandByName("Std_ViewDockUndockFullscreen"); |
| 248 |
if (cmd) { |
| 249 |
cmd->invoke(on ? MDIView::FullScreen : MDIView::Child); |
| 250 |
} |
| 251 |
this->activateWindow(); |
| 252 |
ui->playButton->setChecked(false); |
| 253 |
} |
| 254 |
if (on) { |
| 255 |
qApp->installEventFilter(this); |
| 256 |
showHideTimer->start(); |
| 257 |
} |
| 258 |
else { |
| 259 |
qApp->removeEventFilter(this); |
| 260 |
showHideTimer->stop(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void DemoMode::onTimeoutValueChanged(int v) |
| 265 |
{ |
| 266 |
timer->setInterval(v * 1000); |
| 267 |
} |
| 268 |
|
| 269 |
void DemoMode::onAutoPlay() |
| 270 |
{ |
| 271 |
Gui::View3DInventor* view = activeView(); |
| 272 |
if (view && !view->getViewer()->isSpinning()) { |
| 273 |
ui->playButton->setChecked(true); |
| 274 |
startAnimation(view); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void DemoMode::startAnimation(Gui::View3DInventor* view) |
| 279 |
{ |
| 280 |
view->getViewer()->startSpinningAnimation(getDirection(view), getSpeed(ui->speedSlider->value())); |
| 281 |
} |
| 282 |
|
| 283 |
void DemoMode::onTimerCheckToggled(bool on) |
| 284 |
{ |
| 285 |
if (on) { |
| 286 |
timer->start(); |
| 287 |
} |
| 288 |
else { |
| 289 |
timer->stop(); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void DemoMode::changeEvent(QEvent* e) |
| 294 |
{ |
| 295 |
if (e->type() == QEvent::LanguageChange) { |
| 296 |
ui->retranslateUi(this); |
| 297 |
} |
| 298 |
QDialog::changeEvent(e); |
| 299 |
} |
| 300 |
|
| 301 |
#include "moc_DemoMode.cpp" |