DemoMode.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <cmath>
00027 # include <float.h>
00028 # include <climits>
00029 # include <QTimer>
00030 #include <Inventor/nodes/SoCamera.h>
00031 #endif
00032 
00033 #include "DemoMode.h"
00034 #include "ui_DemoMode.h"
00035 
00036 #include "Application.h"
00037 #include "Document.h"
00038 #include "MainWindow.h"
00039 #include "View3DInventor.h"
00040 #include "View3DInventorViewer.h"
00041 #include <Base/Tools.h>
00042 
00043 using namespace Gui::Dialog;
00044 
00045 /* TRANSLATOR Gui::Dialog::DemoMode */
00046 
00047 DemoMode::DemoMode(QWidget* parent, Qt::WFlags fl)
00048   : QDialog(0, fl|Qt::WindowStaysOnTopHint), viewAxis(0,0,-1), ui(new Ui_DemoMode)
00049 {
00050     // create widgets
00051     ui->setupUi(this);
00052     timer = new QTimer(this);
00053     timer->setInterval(1000 * ui->timeout->value());
00054     connect(timer, SIGNAL(timeout()), this, SLOT(onAutoPlay()));
00055     oldvalue = ui->angleSlider->value();
00056 }
00057 
00059 DemoMode::~DemoMode()
00060 {
00061     delete ui;
00062 }
00063 
00064 void DemoMode::reset()
00065 {
00066     on_fullscreen_toggled(false);
00067     on_stopButton_clicked();
00068     ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
00069         ("User parameter:BaseApp/Preferences/View");
00070     hGrp->Notify("UseAutoRotation");
00071 }
00072 
00073 void DemoMode::accept()
00074 {
00075     reset();
00076     QDialog::accept();
00077 }
00078 
00079 void DemoMode::reject()
00080 {
00081     reset();
00082     QDialog::reject();
00083 }
00084 
00085 Gui::View3DInventor* DemoMode::activeView() const
00086 {
00087     Document* doc = Application::Instance->activeDocument();
00088     if (doc) {
00089         MDIView* view = doc->getActiveView();
00090         if (doc && view->isDerivedFrom(Gui::View3DInventor::getClassTypeId())) {
00091             return static_cast<Gui::View3DInventor*>(view);
00092         }
00093     }
00094 
00095     return 0;
00096 }
00097 
00098 float DemoMode::getSpeed(int v) const
00099 {
00100     float speed = ((float)v)/50.0f; // let 2.0 be the maximum speed
00101     return speed;
00102 }
00103 
00104 SbVec3f DemoMode::getDirection(Gui::View3DInventor* view) const
00105 {
00106     SoCamera* cam = view->getViewer()->getCamera();
00107     if (!cam) return this->viewAxis;
00108     SbRotation rot = cam->orientation.getValue();
00109     SbRotation inv = rot.inverse();
00110     SbVec3f vec(this->viewAxis);
00111     inv.multVec(vec, vec);
00112     if (vec.length() < FLT_EPSILON)
00113         vec = this->viewAxis;
00114     vec.normalize();
00115     return vec;
00116 }
00117 
00118 void DemoMode::on_angleSlider_valueChanged(int v)
00119 {
00120     Gui::View3DInventor* view = activeView();
00121     if (view) {
00122         SoCamera* cam = view->getViewer()->getCamera();
00123         if (!cam) return;
00124         float angle = Base::toRadians<float>(/*90-v*/v-this->oldvalue);
00125         SbRotation rot(SbVec3f(-1,0,0),angle);
00126         reorientCamera(cam ,rot);
00127         this->oldvalue = v;
00128         if (view->getViewer()->isAnimating()) {
00129             startAnimation(view);
00130         }
00131     }
00132 }
00133 
00134 void DemoMode::reorientCamera(SoCamera * cam, const SbRotation & rot)
00135 {
00136     // Find global coordinates of focal point.
00137     SbVec3f direction;
00138     cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
00139     SbVec3f focalpoint = cam->position.getValue() +
00140                          cam->focalDistance.getValue() * direction;
00141 
00142     // Set new orientation value by accumulating the new rotation.
00143     cam->orientation = rot * cam->orientation.getValue();
00144 
00145     // Reposition camera so we are still pointing at the same old focal point.
00146     cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
00147     cam->position = focalpoint - cam->focalDistance.getValue() * direction;
00148 }
00149 
00150 void DemoMode::on_speedSlider_valueChanged(int v)
00151 {
00152     Gui::View3DInventor* view = activeView();
00153     if (view && view->getViewer()->isAnimating()) {
00154         startAnimation(view);
00155     }
00156 }
00157 
00158 void DemoMode::on_playButton_clicked()
00159 {
00160     Gui::View3DInventor* view = activeView();
00161     if (view) {
00162         if (!view->getViewer()->isAnimating()) {
00163             SoCamera* cam = view->getViewer()->getCamera();
00164             if (cam) {
00165                 SbRotation rot = cam->orientation.getValue();
00166                 SbVec3f vec(0,-1,0);
00167                 rot.multVec(vec, this->viewAxis);
00168             }
00169         }
00170 
00171         startAnimation(view);
00172     }
00173 }
00174 
00175 void DemoMode::on_stopButton_clicked()
00176 {
00177     Gui::View3DInventor* view = activeView();
00178     if (view)
00179         view->getViewer()->stopAnimating();
00180 }
00181 
00182 void DemoMode::on_fullscreen_toggled(bool on)
00183 {
00184     Gui::View3DInventor* view = activeView();
00185     if (view) {
00186         view->setCurrentViewMode(on ? MDIView::/*TopLevel*/FullScreen : MDIView::Child);
00187         this->activateWindow();
00188     }
00189 }
00190 
00191 void DemoMode::on_timeout_valueChanged(int v)
00192 {
00193     timer->setInterval(v*1000);
00194 }
00195 
00196 void DemoMode::onAutoPlay()
00197 {
00198     Gui::View3DInventor* view = activeView();
00199     if (view && !view->getViewer()->isAnimating()) {
00200         startAnimation(view);
00201     }
00202 }
00203 
00204 void DemoMode::startAnimation(Gui::View3DInventor* view)
00205 {
00206     if (!view->getViewer()->isAnimationEnabled())
00207         view->getViewer()->setAnimationEnabled(true);
00208     view->getViewer()->startAnimating(getDirection(view), 
00209         getSpeed(ui->speedSlider->value()));
00210 }
00211 
00212 void DemoMode::on_timerCheck_toggled(bool on)
00213 {
00214     if (on)
00215         timer->start();
00216     else
00217         timer->stop();
00218 }
00219 
00220 void DemoMode::changeEvent(QEvent *e)
00221 {
00222     if (e->type() == QEvent::LanguageChange)
00223         ui->retranslateUi(this);
00224     QDialog::changeEvent(e);
00225 }
00226 
00227 #include "moc_DemoMode.cpp"

Generated on Wed Nov 23 19:00:05 2011 for FreeCAD by  doxygen 1.6.1