From 8c9b92b51c8962e3992ad91580798c02f7c65761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Tue, 24 Jun 2008 16:32:55 +0200 Subject: Fix bug where choosing cancel in color dialog causes color to become black. --- openglscene.cpp | 43 +++++++++++++++++++++++-------------------- openglscene.h | 5 ++++- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/openglscene.cpp b/openglscene.cpp index c80d7b8..6c63939 100644 --- a/openglscene.cpp +++ b/openglscene.cpp @@ -23,8 +23,8 @@ public: private slots: void loadModel(const QString &model); void modelLoaded(); - void setModelColor(bool showDialog = true); - void setBackgroundColor(bool showDialog = true); + void setModelColor(); + void setBackgroundColor(); private: OpenGLScene *m_scene; @@ -32,17 +32,12 @@ private: QFutureWatcher m_modelLoader; #endif QComboBox *m_models; - - QColor m_modelColor; - QColor m_backgroundColor; QDir m_dir; }; Controls::Controls(OpenGLScene *scene) : m_scene(scene) , m_models(new QComboBox) - , 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_dir(QLatin1String("models")) { QVBoxLayout *layout = new QVBoxLayout(this); @@ -81,12 +76,10 @@ Controls::Controls(OpenGLScene *scene) QPushButton *colorButton = new QPushButton(tr("Choose model color")); connect(colorButton, SIGNAL(pressed()), this, SLOT(setModelColor())); layout->addWidget(colorButton); - setModelColor(false); QPushButton *backgroundButton = new QPushButton(tr("Choose background color")); connect(backgroundButton, SIGNAL(pressed()), this, SLOT(setBackgroundColor())); layout->addWidget(backgroundButton); - setBackgroundColor(false); } Model *loadModel(const QString &filename) @@ -115,26 +108,26 @@ void Controls::modelLoaded() QApplication::restoreOverrideCursor(); } -void Controls::setModelColor(bool showDialog) +void Controls::setModelColor() { - if (showDialog) - m_modelColor = QColorDialog::getColor(m_modelColor); - - m_scene->setModelColor(m_modelColor); + const QColor color = QColorDialog::getColor(m_scene->modelColor()); + if (color.isValid()) + m_scene->setModelColor(color); } -void Controls::setBackgroundColor(bool showDialog) +void Controls::setBackgroundColor() { - if (showDialog) - m_backgroundColor = QColorDialog::getColor(m_backgroundColor); - - m_scene->setBackgroundColor(m_backgroundColor); + const QColor color = QColorDialog::getColor(m_scene->backgroundColor()); + if (color.isValid()) + m_scene->setBackgroundColor(color); } OpenGLScene::OpenGLScene() : m_wireframeEnabled(true) , m_normalsEnabled(false) , m_distance(1.4f) + , 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) @@ -176,7 +169,6 @@ void OpenGLScene::drawBackground(QPainter *painter, const QRectF &) m_rotation += m_angularMomentum * (delta / 1000.0); m_lastTime += delta; - glLoadIdentity(); glTranslatef(0, 0, -m_distance); glRotatef(m_rotation.x, 1, 0, 0); glRotatef(m_rotation.y, 0, 1, 0); @@ -231,6 +223,17 @@ void OpenGLScene::setBackgroundColor(const QColor &color) update(); } +QColor OpenGLScene::backgroundColor() const +{ + return m_backgroundColor; +} + +QColor OpenGLScene::modelColor() const +{ + return m_modelColor; +} + + void OpenGLScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QGraphicsScene::mouseMoveEvent(event); diff --git a/openglscene.h b/openglscene.h index e7b0619..c9283ab 100644 --- a/openglscene.h +++ b/openglscene.h @@ -17,6 +17,9 @@ public: void drawBackground(QPainter *painter, const QRectF &rect); + QColor backgroundColor() const; + QColor modelColor() const; + public slots: void setModel(Model *model); void enableWireframe(bool enabled); @@ -38,8 +41,8 @@ private: float m_lightPos; float m_distance; - QColor m_backgroundColor; QColor m_modelColor; + QColor m_backgroundColor; Model *m_model; -- cgit v1.2.3