summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Rødal <sroedal@trolltech.com>2008-06-26 10:11:48 +0200
committerSamuel Rødal <sroedal@trolltech.com>2008-06-26 10:11:48 +0200
commit9d65781bfbccda2ce65af5c088a9b203e3500da3 (patch)
tree494d53fa1ad8e6bb60df86bb3425c4d8792e317b
parent88ebfcad57721d201af3314f84739205adde3843 (diff)
Replace combobox with file dialog.
-rw-r--r--model.cpp5
-rw-r--r--model.h5
-rw-r--r--openglscene.cpp43
-rw-r--r--openglscene.h17
4 files changed, 37 insertions, 33 deletions
diff --git a/model.cpp b/model.cpp
index 5bc38ff..48e714d 100644
--- a/model.cpp
+++ b/model.cpp
@@ -6,9 +6,10 @@
#include <QtOpenGL>
-Model::Model(const QString &filename)
+Model::Model(const QString &filePath)
+ : m_fileName(QFileInfo(filePath).fileName())
{
- QFile file(filename);
+ QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
return;
diff --git a/model.h b/model.h
index ed759b7..be7881a 100644
--- a/model.h
+++ b/model.h
@@ -1,6 +1,7 @@
#ifndef MODEL_H
#define MODEL_H
+#include <QString>
#include <QVector>
#include <math.h>
@@ -11,15 +12,17 @@ class Model
{
public:
Model() {}
- Model(const QString &filename);
+ Model(const QString &filePath);
void render(bool wireframe = false, bool normals = false) const;
+ QString fileName() const { return m_fileName; }
int faces() const { return m_pointIndices.size() / 3; }
int edges() const { return m_edgeIndices.size() / 2; }
int points() const { return m_points.size(); }
private:
+ QString m_fileName;
QVector<Point3d> m_points;
QVector<Point3d> m_normals;
QVector<int> m_edgeIndices;
diff --git a/openglscene.cpp b/openglscene.cpp
index 8d6791d..b274cd9 100644
--- a/openglscene.cpp
+++ b/openglscene.cpp
@@ -29,19 +29,15 @@ OpenGLScene::OpenGLScene()
, m_model(0)
, m_lastTime(0)
, m_angularMomentum(0, 40, 0)
- , m_dir(QLatin1String("models"), QLatin1String("*.obj"), QDir::Size | QDir::Reversed, QDir::Files)
{
QWidget *controls = createDialog(tr("Controls"));
- controls->layout()->addWidget(new QLabel(tr("Model:")));
-
- m_models = new QComboBox;
- connect(m_models, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(loadModel(const QString &)));
+ m_modelButton = new QPushButton(tr("Load model"));
+ connect(m_modelButton, SIGNAL(pressed()), this, SLOT(loadModel()));
#ifndef QT_NO_CONCURRENT
connect(&m_modelLoader, SIGNAL(finished()), this, SLOT(modelLoaded()));
#endif
- m_models->addItems(m_dir.entryList());
- controls->layout()->addWidget(m_models);
+ controls->layout()->addWidget(m_modelButton);
QCheckBox *wireframe = new QCheckBox(tr("Render as wireframe"));
connect(wireframe, SIGNAL(toggled(bool)), this, SLOT(enableWireframe(bool)));
@@ -68,7 +64,7 @@ OpenGLScene::OpenGLScene()
QWidget *statistics = createDialog(tr("Model info"));
statistics->layout()->setMargin(20);
- for (int i = 0; i < 3; ++i) {
+ for (int i = 0; i < 4; ++i) {
m_labels[i] = new QLabel;
statistics->layout()->addWidget(m_labels[i]);
}
@@ -91,7 +87,7 @@ OpenGLScene::OpenGLScene()
pos += QPointF(0, 10 + rect.height());
}
- setModel(new Model);
+ loadModel(QLatin1String("qt.obj"));
m_time.start();
}
@@ -105,6 +101,9 @@ void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ if (!m_model)
+ return;
+
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
@@ -139,19 +138,24 @@ void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
QTimer::singleShot(20, this, SLOT(update()));
}
-static Model *loadModel(const QString &filename)
+static Model *loadModel(const QString &filePath)
+{
+ return new Model(filePath);
+}
+
+void OpenGLScene::loadModel()
{
- return new Model(filename);
+ loadModel(QFileDialog::getOpenFileName(0, tr("Choose model"), QString(), QLatin1String("*.obj")));
}
-void OpenGLScene::loadModel(const QString &filename)
+void OpenGLScene::loadModel(const QString &filePath)
{
- m_models->setEnabled(false);
+ m_modelButton->setEnabled(false);
QApplication::setOverrideCursor(Qt::BusyCursor);
#ifndef QT_NO_CONCURRENT
- m_modelLoader.setFuture(QtConcurrent::run(::loadModel, m_dir.filePath(filename)));
+ m_modelLoader.setFuture(QtConcurrent::run(::loadModel, filePath));
#else
- setModel(::loadModel(m_dir.filePath(filename)));
+ setModel(::loadModel(filePath));
modelLoaded();
#endif
}
@@ -161,7 +165,7 @@ void OpenGLScene::modelLoaded()
#ifndef QT_NO_CONCURRENT
setModel(m_modelLoader.result());
#endif
- m_models->setEnabled(true);
+ m_modelButton->setEnabled(true);
QApplication::restoreOverrideCursor();
}
@@ -170,9 +174,10 @@ void OpenGLScene::setModel(Model *model)
delete m_model;
m_model = model;
- m_labels[0]->setText(tr("Points: %0").arg(m_model->points()));
- m_labels[1]->setText(tr("Edges: %0").arg(m_model->edges()));
- m_labels[2]->setText(tr("Faces: %0").arg(m_model->faces()));
+ m_labels[0]->setText(tr("File: %0").arg(m_model->fileName()));
+ m_labels[1]->setText(tr("Points: %0").arg(m_model->points()));
+ m_labels[2]->setText(tr("Edges: %0").arg(m_model->edges()));
+ m_labels[3]->setText(tr("Faces: %0").arg(m_model->faces()));
update();
}
diff --git a/openglscene.h b/openglscene.h
index 2602b95..fe0339e 100644
--- a/openglscene.h
+++ b/openglscene.h
@@ -3,19 +3,14 @@
#include "point3d.h"
-#include <QDir>
#include <QGraphicsScene>
+#include <QLabel>
#include <QTime>
#ifndef QT_NO_CONCURRENT
#include <QFutureWatcher>
#endif
-QT_BEGIN_NAMESPACE
-class QComboBox;
-class QLabel;
-QT_END_NAMESPACE
-
class Model;
class OpenGLScene : public QGraphicsScene
@@ -33,7 +28,8 @@ public slots:
void setLightPosition(int pos);
void setModelColor();
void setBackgroundColor();
- void loadModel(const QString &model);
+ void loadModel();
+ void loadModel(const QString &filePath);
void modelLoaded();
protected:
@@ -66,13 +62,12 @@ private:
Point3d m_angularMomentum;
Point3d m_accumulatedMomentum;
+ QLabel *m_labels[4];
+ QWidget *m_modelButton;
+
#ifndef QT_NO_CONCURRENT
QFutureWatcher<Model *> m_modelLoader;
#endif
- QComboBox *m_models;
- QDir m_dir;
-
- QLabel *m_labels[3];
};
#endif