Back to index

kdeartwork  4.3.2
pendulum.h
Go to the documentation of this file.
00001 /*============================================================================
00002  *
00003  * KPendulum screen saver for KDE
00004  *
00005  * The screen saver displays a physically realistic simulation of a two-part
00006  * pendulum.
00007  *
00008  * Copyright (C) 2004 Georg Drenkhahn, Georg.Drenkhahn@gmx.net
00009  *
00010  * This program is free software; you can redistribute it and/or modify it under
00011  * the terms of the GNU General Public License as published by the Free Software
00012  * Foundation; either version 2 of the License or (at your option) version 3 or
00013  * any later version accepted by the membership of KDE e.V. (or its successor
00014  * approved by the membership of KDE e.V.), which shall act as a proxy defined
00015  * in Section 14 of version 3 of the license.
00016  *
00017  * This program is distributed in the hope that it will be useful, but WITHOUT
00018  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00020  * details.
00021  *
00022  *============================================================================*/
00023 
00024 #ifndef PENDULUM_H
00025 #define PENDULUM_H
00026 
00027 // STL headers
00028 #include <valarray>
00029 
00030 // Qt headers
00031 #include <QWidget>
00032 #include <QTimer>
00033 #include <QGLWidget>
00034 
00035 // GL headers
00036 #include <GL/glu.h>
00037 #include <GL/gl.h>
00038 
00039 // KDE headers
00040 #include <kscreensaver.h>
00041 
00042 #include "rkodesolver.h"
00043 
00044 // KPendulumSetupUi
00045 #include "ui_pendulumcfg.h"
00046 
00047 //--------------------------------------------------------------------
00048 
00050 class PendulumOdeSolver : public RkOdeSolver<double,4>
00051 {
00052   public:
00063    PendulumOdeSolver(
00064       const double&   t,
00065       const double&   dt,
00066       const Vector4d& y,
00067       const double&   eps,
00068       const double&   m1,
00069       const double&   m2,
00070       const double&   l1,
00071       const double&   l2,
00072       const double&   g);
00073 
00074   protected:
00079    Vector4d f(const double& x, const Vector4d& y) const;
00080 
00081   private:
00084    const double m_A, m_B1, m_B, m_C, m_D, m_E, m_M;
00085 };
00086 
00087 
00088 //--------------------------------------------------------------------
00089 
00093 class PendulumGLWidget : public QGLWidget
00094 {
00095    Q_OBJECT
00096 
00097   public:
00100    PendulumGLWidget(QWidget* parent=0);
00102    ~PendulumGLWidget(void);
00103 
00106    void setEyePhi(double phi);
00110    void setAngles(const double& q1, const double& q2);
00114    void setMasses(const double& m1, const double& m2);
00118    void setLengths(const double& l1, const double& l2);
00119 
00120    /* accessors for colour settings */
00121 
00124    void setBarColor(const QColor& c);
00127    inline QColor barColor(void) const {return m_barColor;}
00130    void setM1Color(const QColor& c);
00133    inline QColor m1Color(void) const {return m_m1Color;}
00136    void setM2Color(const QColor& c);
00139    inline QColor m2Color(void) const {return m_m2Color;}
00140 
00141   protected:
00143    virtual void paintGL();
00145    virtual void resizeGL(int w, int h);
00147    virtual void initializeGL();
00148 
00149   private: // Private attributes
00151    GLfloat m_eyeR;
00153    double  m_eyeTheta;
00155    double  m_eyePhi;
00157    GLfloat m_lightR;
00159    double  m_lightTheta;
00161    double  m_lightPhi;
00162 
00164    GLfloat m_ang1;
00166    GLfloat m_ang2;
00167 
00169    GLfloat m_sqrtm1;
00171    GLfloat m_sqrtm2;
00172 
00174    GLfloat m_l1;
00176    GLfloat m_l2;
00177 
00179    GLUquadricObj* const m_quadM1;
00180 
00182    QColor m_barColor;
00184    QColor m_m1Color;
00186    QColor m_m2Color;
00187 };
00188 
00189 //--------------------------------------------------------------------
00190 
00194 class KPendulumSaver : public KScreenSaver
00195 {
00196    Q_OBJECT
00197 
00198   public:
00204    KPendulumSaver(WId drawable);
00208    ~KPendulumSaver();
00209 
00211    void readSettings();
00215    void initData();
00216 
00217    /* accessors for PendulumGLWidget member variables */
00218 
00220    void setBarColor(const QColor& c);
00222    QColor barColor(void) const;
00223 
00225    void setM1Color(const QColor& c);
00227    QColor m1Color(void) const;
00228 
00230    void setM2Color(const QColor& c);
00232    QColor m2Color(void) const;
00233 
00234    /* accessors for own member variables */
00235 
00238    void setMassRatio(const double& massRatio);
00241    inline double massRatio(void) const
00242       {
00243          return m_massRatio;
00244       }
00245 
00248    void setLengthRatio(const double& lengthRatio);
00251    inline double lengthRatio(void) const
00252       {
00253          return m_lengthRatio;
00254       }
00255 
00257    void setG(const double& g);
00259    inline double g(void) const
00260       {
00261          return m_g;
00262       }
00263 
00265    void setE(const double& E);
00267    inline double E(void) const
00268       {
00269          return m_E;
00270       }
00271 
00274    void setPersChangeInterval(const unsigned int& persChangeInterval);
00277    inline unsigned int persChangeInterval(void) const
00278       {
00279          return m_persChangeInterval;
00280       }
00281 
00282    /* public static class member variables */
00283 
00284    static const QColor sm_barColorDefault;
00285    static const QColor sm_m1ColorDefault;
00286    static const QColor sm_m2ColorDefault;
00287 
00290    static const double sm_massRatioLimitUpper;
00291    static const double sm_massRatioLimitLower;
00292    static const double sm_massRatioDefault;
00293 
00296    static const double sm_lengthRatioLimitLower;
00297    static const double sm_lengthRatioLimitUpper;
00298    static const double sm_lengthRatioDefault;
00299 
00302    static const double sm_gLimitLower;
00303    static const double sm_gLimitUpper;
00304    static const double sm_gDefault;
00305 
00308    static const double sm_ELimitLower;
00309    static const double sm_ELimitUpper;
00310    static const double sm_EDefault;
00311 
00314    static const unsigned int sm_persChangeIntervalLimitLower;
00315    static const unsigned int sm_persChangeIntervalLimitUpper;
00316    static const unsigned int sm_persChangeIntervalDefault;
00317 
00318   public slots:
00320    void doTimeStep();
00323    void resizeGlArea(QResizeEvent* e);
00324 
00325   private:
00328    static const unsigned int sm_deltaT;
00330    static const double       sm_eyePhiDefault;
00331 
00333    PendulumOdeSolver* m_solver;
00335    PendulumGLWidget*  m_glArea;
00337    QTimer*            m_timer;
00338 
00339    // persistent configurtion settings
00340 
00343    double       m_massRatio;
00347    double       m_lengthRatio;
00350    double       m_g;
00354    double       m_E;
00358    unsigned int m_persChangeInterval;
00359 };
00360 
00361 //--------------------------------------------------------------------
00362 
00366 class KPendulumSetup : public QDialog, public Ui::KPendulumSetupUi
00367 {
00368    Q_OBJECT
00369 
00370   public:
00376    KPendulumSetup(QWidget* parent = 0);
00380    ~KPendulumSetup(void);
00381 
00382   public slots:
00384    void okButtonClickedSlot(void);
00386    void aboutButtonClickedSlot(void);
00387 
00390    void mEditLostFocusSlot(void);
00393    void lEditLostFocusSlot(void);
00396    void gEditLostFocusSlot(void);
00399    void eEditLostFocusSlot(void);
00403    void persChangeEnteredSlot(int t);
00404 
00407    void barColorButtonClickedSlot(void);
00410    void m1ColorButtonClickedSlot(void);
00413    void m2ColorButtonClickedSlot(void);
00414 
00415   private:
00418    KPendulumSaver* m_saver;
00419 };
00420 
00421 #endif