summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Rødal <sroedal@trolltech.com>2008-06-23 13:16:36 +0200
committerSamuel Rødal <sroedal@trolltech.com>2008-06-23 13:16:36 +0200
commitbf6b869c9f0fc2d4e1279234eb5be248d9c9d129 (patch)
tree8bcb3b4192313cb52553e20a12c1391f23512edd
parent4ee8805b00b0b8f630563d995883c8f04280c841 (diff)
Rename: GraphicsScene -> OpenGLScene
-rw-r--r--main.cpp17
-rw-r--r--openglexample.pro4
-rw-r--r--openglscene.cpp (renamed from graphicsscene.cpp)47
-rw-r--r--openglscene.h (renamed from graphicsscene.h)8
4 files changed, 46 insertions, 30 deletions
diff --git a/main.cpp b/main.cpp
index 18cff27..d2702c7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,17 +1,30 @@
-#include "graphicsscene.h"
+#include "openglscene.h"
#include <QtOpenGL>
+class GraphicsView : public QGraphicsView
+{
+protected:
+ void resizeEvent(QResizeEvent *event) {
+ if (scene())
+ scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
+ QGraphicsView::resizeEvent(event);
+ }
+};
+
int main(int argc, char **argv)
{
QApplication app(argc, argv);
- QGraphicsView view(new GraphicsScene);
+ GraphicsView view;
view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
+ view.setScene(new OpenGLScene);
view.show();
+ view.resize(1024, 768);
+
return app.exec();
}
diff --git a/openglexample.pro b/openglexample.pro
index 55f3025..859dd79 100644
--- a/openglexample.pro
+++ b/openglexample.pro
@@ -8,7 +8,7 @@ DEPENDPATH += .
INCLUDEPATH += .
# Input
-HEADERS += graphicsscene.h point3d.h model.h
-SOURCES += main.cpp model.cpp graphicsscene.cpp
+HEADERS += openglscene.h point3d.h model.h
+SOURCES += main.cpp model.cpp openglscene.cpp
QT += opengl
diff --git a/graphicsscene.cpp b/openglscene.cpp
index 01d1d4e..f0c4c79 100644
--- a/graphicsscene.cpp
+++ b/openglscene.cpp
@@ -1,4 +1,4 @@
-#include "graphicsscene.h"
+#include "openglscene.h"
#include "model.h"
@@ -13,7 +13,7 @@ class Controls : public QGroupBox
Q_OBJECT
public:
- Controls(GraphicsScene *scene);
+ Controls(OpenGLScene *scene);
private slots:
void loadModel(const QString &model);
@@ -22,7 +22,7 @@ private slots:
void setBackgroundColor(bool showDialog = true);
private:
- GraphicsScene *m_scene;
+ OpenGLScene *m_scene;
QFutureWatcher<Model *> m_modelLoader;
QComboBox *m_models;
@@ -30,7 +30,7 @@ private:
QRgb m_backgroundColor;
};
-Controls::Controls(GraphicsScene *scene)
+Controls::Controls(OpenGLScene *scene)
: m_scene(scene)
, m_models(new QComboBox)
, m_modelColor(qRgb(180, 100, 255))
@@ -119,7 +119,7 @@ void Controls::setBackgroundColor(bool showDialog)
m_scene->setBackgroundColor(m_backgroundColor);
}
-GraphicsScene::GraphicsScene()
+OpenGLScene::OpenGLScene()
: m_wireframeEnabled(true)
, m_normalsEnabled(false)
, m_autoRotate(true)
@@ -140,11 +140,9 @@ GraphicsScene::GraphicsScene()
QGraphicsProxyWidget *item = addWidget(controls);
item->translate(10, 10);
item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
-
- setSceneRect(QRect(0, 0, 1024, 768));
}
-void GraphicsScene::updateMatrix()
+void OpenGLScene::updateMatrix()
{
if (!QGLContext::currentContext())
return;
@@ -158,8 +156,13 @@ void GraphicsScene::updateMatrix()
glPopMatrix();
}
-void GraphicsScene::drawBackground(QPainter *painter, const QRectF &)
+void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
{
+ if (!QGLContext::currentContext()) {
+ qWarning() << "OpenGLScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view";
+ return;
+ }
+
painter->save();
glClearColor(qRed(m_backgroundColor)/255.0f, qGreen(m_backgroundColor)/255.0f, qBlue(m_backgroundColor)/255.0f, 1);
@@ -200,42 +203,42 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &)
QTimer::singleShot(20, this, SLOT(update()));
}
-void GraphicsScene::setModel(Model *model)
+void OpenGLScene::setModel(Model *model)
{
delete m_model;
m_model = model;
update();
}
-void GraphicsScene::enableAutoRotate(bool enabled)
+void OpenGLScene::enableAutoRotate(bool enabled)
{
m_autoRotate = enabled;
update();
}
-void GraphicsScene::enableWireframe(bool enabled)
+void OpenGLScene::enableWireframe(bool enabled)
{
m_wireframeEnabled = enabled;
}
-void GraphicsScene::enableNormals(bool enabled)
+void OpenGLScene::enableNormals(bool enabled)
{
m_normalsEnabled = enabled;
}
-void GraphicsScene::setLightPosition(int pos)
+void OpenGLScene::setLightPosition(int pos)
{
m_lightPos = pos * 0.05;
update();
}
-void GraphicsScene::setModelColor(QRgb color)
+void OpenGLScene::setModelColor(QRgb color)
{
m_modelColor = color;
update();
}
-void GraphicsScene::setBackgroundColor(QRgb color)
+void OpenGLScene::setBackgroundColor(QRgb color)
{
m_backgroundColor = color;
update();
@@ -256,7 +259,7 @@ static Point3d spherical(const QPointF &point, qreal w, qreal h)
return p;
}
-void GraphicsScene::updateRotation(const QPointF &last, const QPointF &current)
+void OpenGLScene::updateRotation(const QPointF &last, const QPointF &current)
{
Point3d pos = spherical(current, width(), height());
Point3d lastPos = spherical(last, width(), height());
@@ -281,7 +284,7 @@ void GraphicsScene::updateRotation(const QPointF &last, const QPointF &current)
}
}
-void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+void OpenGLScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseMoveEvent(event);
if (event->isAccepted() || !m_rotating)
@@ -291,7 +294,7 @@ void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
event->accept();
}
-void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
+void OpenGLScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mousePressEvent(event);
if (event->isAccepted())
@@ -301,7 +304,7 @@ void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
m_rotating = true;
}
-void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+void OpenGLScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseReleaseEvent(event);
if (event->isAccepted())
@@ -312,7 +315,7 @@ void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
}
-void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event)
+void OpenGLScene::wheelEvent(QGraphicsSceneWheelEvent *event)
{
QGraphicsScene::wheelEvent(event);
if (event->isAccepted())
@@ -323,4 +326,4 @@ void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event)
update();
}
-#include "graphicsscene.moc"
+#include "openglscene.moc"
diff --git a/graphicsscene.h b/openglscene.h
index 9618cf7..15ce85c 100644
--- a/graphicsscene.h
+++ b/openglscene.h
@@ -1,5 +1,5 @@
-#ifndef GRAPHICSSCENE_H
-#define GRAPHICSSCENE_H
+#ifndef OPENGLSCENE_H
+#define OPENGLSCENE_H
#include "point3d.h"
@@ -8,12 +8,12 @@
class Model;
-class GraphicsScene : public QGraphicsScene
+class OpenGLScene : public QGraphicsScene
{
Q_OBJECT
public:
- GraphicsScene();
+ OpenGLScene();
void drawBackground(QPainter *painter, const QRectF &rect);