From 6258d5a835a0432931bafe035da644fa61e7f1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Wed, 25 Jun 2008 19:51:38 +0200 Subject: Add statistics widget. --- model.h | 4 ++++ openglscene.cpp | 52 ++++++++++++++++++++++++++++++++++++---------------- openglscene.h | 3 +++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/model.h b/model.h index 545eedc..b1ef24e 100644 --- a/model.h +++ b/model.h @@ -15,6 +15,10 @@ public: void render(bool wireframe = false, bool normals = false) const; + int triangles() const { return m_pointIndices.size() / 3; } + int edges() const { return m_edgeIndices.size() / 2; } + int points() const { return m_points.size(); } + private: QVector m_points; QVector m_normals; diff --git a/openglscene.cpp b/openglscene.cpp index 9210aaa..2a10c18 100644 --- a/openglscene.cpp +++ b/openglscene.cpp @@ -1,5 +1,4 @@ #include "openglscene.h" - #include "model.h" #include @@ -22,9 +21,9 @@ OpenGLScene::OpenGLScene() , m_dir(QLatin1String("models"), QLatin1String("*.obj"), QDir::Size | QDir::Reversed, QDir::Files) { QWidget *controls = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint); - QVBoxLayout *layout = new QVBoxLayout(controls); + QVBoxLayout *controlsLayout = new QVBoxLayout(controls); - layout->addWidget(new QLabel(tr("Model:"))); + controlsLayout->addWidget(new QLabel(tr("Model:"))); m_models = new QComboBox; connect(m_models, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(loadModel(const QString &))); @@ -32,40 +31,56 @@ OpenGLScene::OpenGLScene() connect(&m_modelLoader, SIGNAL(finished()), this, SLOT(modelLoaded())); #endif m_models->addItems(m_dir.entryList()); - layout->addWidget(m_models); + controlsLayout->addWidget(m_models); QCheckBox *wireframe = new QCheckBox(tr("Render as wireframe")); connect(wireframe, SIGNAL(toggled(bool)), this, SLOT(enableWireframe(bool))); - layout->addWidget(wireframe); + controlsLayout->addWidget(wireframe); QCheckBox *normals = new QCheckBox(tr("Display normals vectors")); connect(normals, SIGNAL(toggled(bool)), this, SLOT(enableNormals(bool))); - layout->addWidget(normals); + controlsLayout->addWidget(normals); - layout->addWidget(new QLabel(tr("Light position:"))); + controlsLayout->addWidget(new QLabel(tr("Light position:"))); QSlider *lightPosition = new QSlider(Qt::Horizontal); lightPosition->setRange(-100, 100); connect(lightPosition, SIGNAL(valueChanged(int)), this, SLOT(setLightPosition(int))); - layout->addWidget(lightPosition); + controlsLayout->addWidget(lightPosition); QPushButton *colorButton = new QPushButton(tr("Choose model color")); connect(colorButton, SIGNAL(pressed()), this, SLOT(setModelColor())); - layout->addWidget(colorButton); + controlsLayout->addWidget(colorButton); QPushButton *backgroundButton = new QPushButton(tr("Choose background color")); connect(backgroundButton, SIGNAL(pressed()), this, SLOT(setBackgroundColor())); - layout->addWidget(backgroundButton); + controlsLayout->addWidget(backgroundButton); controls->setWindowOpacity(0.8); controls->setWindowTitle(tr("Controls")); - QGraphicsProxyWidget *item = addWidget(controls); - const QPointF offset = item->boundingRect().topLeft(); + QGraphicsItem *controlsItem = addWidget(controls); + const QRectF rect = controlsItem->boundingRect(); + controlsItem->translate(10 - rect.x(), 10 - rect.y()); + + QWidget *statistics = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint); + QVBoxLayout *statisticsLayout = new QVBoxLayout(statistics); + + for (int i = 0; i < 3; ++i) { + m_labels[i] = new QLabel; + statisticsLayout->addWidget(m_labels[i]); + } + + statistics->setWindowOpacity(0.8); + statistics->setWindowTitle(tr("Model info")); + statistics->resize(controls->width(), 0); - item->translate(10, 10); - item->translate(-offset.x(), -offset.y()); - item->setCacheMode(QGraphicsItem::DeviceCoordinateCache); - item->setFlag(QGraphicsItem::ItemIsMovable); + QGraphicsItem *statisticsItem = addWidget(statistics); + statisticsItem->translate(10 - rect.x(), 20 + rect.height() - rect.y()); + + foreach (QGraphicsItem *item, items()) { + item->setFlag(QGraphicsItem::ItemIsMovable); + item->setCacheMode(QGraphicsItem::DeviceCoordinateCache); + } m_time.start(); } @@ -144,6 +159,11 @@ 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("Triangles: %0").arg(m_model->triangles())); + update(); } diff --git a/openglscene.h b/openglscene.h index 9b78ea0..6e041e1 100644 --- a/openglscene.h +++ b/openglscene.h @@ -13,6 +13,7 @@ QT_BEGIN_NAMESPACE class QComboBox; +class QLabel; QT_END_NAMESPACE class Model; @@ -68,6 +69,8 @@ private: #endif QComboBox *m_models; QDir m_dir; + + QLabel *m_labels[3]; }; #endif -- cgit v1.2.3