From 90b5eeb82ba164b2329ff2839948e5bbed6249ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Wed, 25 Jun 2008 18:04:27 +0200 Subject: Attempted refactor. --- openglscene.cpp | 157 +++++++++++++++++++------------------------------------- openglscene.h | 27 +++++++--- 2 files changed, 74 insertions(+), 110 deletions(-) diff --git a/openglscene.cpp b/openglscene.cpp index 06bcd0a..9210aaa 100644 --- a/openglscene.cpp +++ b/openglscene.cpp @@ -5,42 +5,24 @@ #include #include -#ifndef QT_NO_CONCURRENT -#include -#endif - #ifndef GL_MULTISAMPLE #define GL_MULTISAMPLE 0x809D #endif -class Controls : public QDialog -{ - Q_OBJECT - -public: - Controls(OpenGLScene *scene); - -private slots: - void loadModel(const QString &model); - void modelLoaded(); - void setModelColor(); - void setBackgroundColor(); - -private: - OpenGLScene *m_scene; -#ifndef QT_NO_CONCURRENT - QFutureWatcher m_modelLoader; -#endif - QComboBox *m_models; - QDir m_dir; -}; - -Controls::Controls(OpenGLScene *scene) - : QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint) - , m_scene(scene) +OpenGLScene::OpenGLScene() + : m_wireframeEnabled(false) + , m_normalsEnabled(false) + , m_distance(1.4f) + , m_lightPosition(0.0f) + , m_modelColor(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)) + , m_backgroundColor(QColor::fromCmykF(0.39, 0.39, 0.0, 0.0)) + , m_model(new Model) + , m_lastTime(0) + , m_angularMomentum(0, 40, 0) , m_dir(QLatin1String("models"), QLatin1String("*.obj"), QDir::Size | QDir::Reversed, QDir::Files) { - QVBoxLayout *layout = new QVBoxLayout(this); + QWidget *controls = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint); + QVBoxLayout *layout = new QVBoxLayout(controls); layout->addWidget(new QLabel(tr("Model:"))); @@ -53,17 +35,17 @@ Controls::Controls(OpenGLScene *scene) layout->addWidget(m_models); QCheckBox *wireframe = new QCheckBox(tr("Render as wireframe")); - connect(wireframe, SIGNAL(toggled(bool)), m_scene, SLOT(enableWireframe(bool))); + connect(wireframe, SIGNAL(toggled(bool)), this, SLOT(enableWireframe(bool))); layout->addWidget(wireframe); QCheckBox *normals = new QCheckBox(tr("Display normals vectors")); - connect(normals, SIGNAL(toggled(bool)), m_scene, SLOT(enableNormals(bool))); + connect(normals, SIGNAL(toggled(bool)), this, SLOT(enableNormals(bool))); layout->addWidget(normals); layout->addWidget(new QLabel(tr("Light position:"))); QSlider *lightPosition = new QSlider(Qt::Horizontal); lightPosition->setRange(-100, 100); - connect(lightPosition, SIGNAL(valueChanged(int)), m_scene, SLOT(setLightPosition(int))); + connect(lightPosition, SIGNAL(valueChanged(int)), this, SLOT(setLightPosition(int))); layout->addWidget(lightPosition); QPushButton *colorButton = new QPushButton(tr("Choose model color")); @@ -73,60 +55,7 @@ Controls::Controls(OpenGLScene *scene) QPushButton *backgroundButton = new QPushButton(tr("Choose background color")); connect(backgroundButton, SIGNAL(pressed()), this, SLOT(setBackgroundColor())); layout->addWidget(backgroundButton); -} -Model *loadModel(const QString &filename) -{ - return new Model(filename); -} - -void Controls::loadModel(const QString &filename) -{ - m_models->setEnabled(false); - QApplication::setOverrideCursor(Qt::BusyCursor); -#ifndef QT_NO_CONCURRENT - m_modelLoader.setFuture(QtConcurrent::run(::loadModel, m_dir.filePath(filename))); -#else - m_scene->setModel(::loadModel(m_dir.filePath(filename))); - modelLoaded(); -#endif -} - -void Controls::modelLoaded() -{ -#ifndef QT_NO_CONCURRENT - m_scene->setModel(m_modelLoader.result()); -#endif - m_models->setEnabled(true); - QApplication::restoreOverrideCursor(); -} - -void Controls::setModelColor() -{ - const QColor color = QColorDialog::getColor(m_scene->modelColor()); - if (color.isValid()) - m_scene->setModelColor(color); -} - -void Controls::setBackgroundColor() -{ - const QColor color = QColorDialog::getColor(m_scene->backgroundColor()); - if (color.isValid()) - m_scene->setBackgroundColor(color); -} - -OpenGLScene::OpenGLScene() - : m_wireframeEnabled(false) - , m_normalsEnabled(false) - , m_distance(1.4f) - , m_lightPosition(0.0f) - , m_modelColor(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)) - , m_backgroundColor(QColor::fromCmykF(0.39, 0.39, 0.0, 0.0)) - , m_model(new Model) - , m_lastTime(0) - , m_angularMomentum(0, 40, 0) -{ - Controls *controls = new Controls(this); controls->setWindowOpacity(0.8); controls->setWindowTitle(tr("Controls")); @@ -185,6 +114,32 @@ void OpenGLScene::drawBackground(QPainter *painter, const QRectF &) QTimer::singleShot(20, this, SLOT(update())); } +static Model *loadModel(const QString &filename) +{ + return new Model(filename); +} + +void OpenGLScene::loadModel(const QString &filename) +{ + m_models->setEnabled(false); + QApplication::setOverrideCursor(Qt::BusyCursor); +#ifndef QT_NO_CONCURRENT + m_modelLoader.setFuture(QtConcurrent::run(::loadModel, m_dir.filePath(filename))); +#else + setModel(::loadModel(m_dir.filePath(filename))); + modelLoaded(); +#endif +} + +void OpenGLScene::modelLoaded() +{ +#ifndef QT_NO_CONCURRENT + setModel(m_modelLoader.result()); +#endif + m_models->setEnabled(true); + QApplication::restoreOverrideCursor(); +} + void OpenGLScene::setModel(Model *model) { delete m_model; @@ -210,26 +165,22 @@ void OpenGLScene::setLightPosition(int pos) update(); } -void OpenGLScene::setModelColor(const QColor &color) +void OpenGLScene::setModelColor() { - m_modelColor = color; - update(); -} - -void OpenGLScene::setBackgroundColor(const QColor &color) -{ - m_backgroundColor = color; - update(); -} - -QColor OpenGLScene::backgroundColor() const -{ - return m_backgroundColor; + const QColor color = QColorDialog::getColor(m_modelColor); + if (color.isValid()) { + m_modelColor = color; + update(); + } } -QColor OpenGLScene::modelColor() const +void OpenGLScene::setBackgroundColor() { - return m_modelColor; + const QColor color = QColorDialog::getColor(m_backgroundColor); + if (color.isValid()) { + m_backgroundColor = color; + update(); + } } void OpenGLScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) @@ -282,5 +233,3 @@ void OpenGLScene::wheelEvent(QGraphicsSceneWheelEvent *event) event->accept(); update(); } - -#include "openglscene.moc" diff --git a/openglscene.h b/openglscene.h index c0d5c20..9b78ea0 100644 --- a/openglscene.h +++ b/openglscene.h @@ -3,9 +3,18 @@ #include "point3d.h" +#include #include #include +#ifndef QT_NO_CONCURRENT +#include +#endif + +QT_BEGIN_NAMESPACE +class QComboBox; +QT_END_NAMESPACE + class Model; class OpenGLScene : public QGraphicsScene @@ -17,16 +26,14 @@ public: void drawBackground(QPainter *painter, const QRectF &rect); - QColor backgroundColor() const; - QColor modelColor() const; - public slots: - void setModel(Model *model); void enableWireframe(bool enabled); void enableNormals(bool enabled); void setLightPosition(int pos); - void setModelColor(const QColor &color); - void setBackgroundColor(const QColor &color); + void setModelColor(); + void setBackgroundColor(); + void loadModel(const QString &model); + void modelLoaded(); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); @@ -35,6 +42,8 @@ protected: void wheelEvent(QGraphicsSceneWheelEvent * wheelEvent); private: + void setModel(Model *model); + bool m_wireframeEnabled; bool m_normalsEnabled; @@ -53,6 +62,12 @@ private: Point3d m_rotation; Point3d m_angularMomentum; Point3d m_accumulatedMomentum; + +#ifndef QT_NO_CONCURRENT + QFutureWatcher m_modelLoader; +#endif + QComboBox *m_models; + QDir m_dir; }; #endif -- cgit v1.2.3