Pause now works
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 9 May 2011 19:30:01 +0000 (22:30 +0300)
committerHeli Hyvättinen <heli@pantteri.(none)>
Mon, 9 May 2011 19:30:01 +0000 (22:30 +0300)
main.cpp [new file with mode: 0644]
mainwindow.cpp [new file with mode: 0644]
mainwindow.h [new file with mode: 0644]
orientationcontrol2pix.qrc [new file with mode: 0644]
orientationcontrolledgraphicspixmapobject.cpp [new file with mode: 0644]
orientationcontrolledgraphicspixmapobject.h [new file with mode: 0644]
rotationcontrol2.pro [new file with mode: 0644]

diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..5360a7c
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,15 @@
+#include <QtGui/QApplication>
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+#if defined(Q_WS_S60)
+    w.showMaximized();
+#else
+    w.show();
+#endif
+
+    return a.exec();
+}
diff --git a/mainwindow.cpp b/mainwindow.cpp
new file mode 100644 (file)
index 0000000..8502b60
--- /dev/null
@@ -0,0 +1,83 @@
+#include "mainwindow.h"
+#include <QPixmap>
+#include <QTimer>
+#include <QDebug>
+#include <QAction>
+#include <QMenuBar>
+
+
+
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent)
+{
+    paused_ = false;
+
+    pScene_ = new QGraphicsScene ();
+    pView_  = new QGraphicsView ();
+
+    QPixmap waves (":/pix/meri.png");
+    pScene_->setBackgroundBrush(QBrush(waves));
+
+    pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/tursas.png"));
+    pScene_->addItem(pTursas_);
+
+    pView_->setScene(pScene_);
+    setCentralWidget(pView_);
+
+
+    //the boundaries of the scene are set to match the size of the view window, which is not
+    //available in the constructor --> timer needed
+    QTimer::singleShot(60,this,SLOT(initializeBoundaries()));
+
+
+    QAction * pPauseAction = new QAction(tr("Pause"),this);
+    pPauseAction->setCheckable(true);
+    addAction(pPauseAction);
+    connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
+    menuBar()->addAction(pPauseAction);
+
+}
+
+MainWindow::~MainWindow()
+{
+
+}
+
+void MainWindow::initializeBoundaries()
+{
+        //the boundaries of the scene are set to match the size of the view window, and
+        //the view is set to show exactly the whole scene area
+
+    QPoint topleft (0,0);
+    QSize windowsize = pView_->size();
+    QRectF rectangle (topleft,windowsize);
+
+
+    pScene_->setSceneRect(rectangle);
+    pView_->setSceneRect(rectangle);
+    pTursas_->setBoundaries(rectangle);
+    pTursas_->startMoving();
+
+    qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
+}
+
+void MainWindow::pause(bool paused)
+{
+//    qDebug() << "pause pressed " << paused;
+    if (paused_ == paused)
+            return;
+
+    paused_ = paused;
+
+    if (paused == false)
+    {
+ //       qDebug() << "starting to move again";
+        pTursas_->startMoving();
+    }
+
+    else
+    {
+        qDebug("about to stop movement");
+        pTursas_->stopMoving();
+    }
+}
diff --git a/mainwindow.h b/mainwindow.h
new file mode 100644 (file)
index 0000000..58d2a4e
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QtGui/QMainWindow>
+#include <QGraphicsView>
+#include "orientationcontrolledgraphicspixmapobject.h"
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = 0);
+    ~MainWindow();
+
+public slots:
+    void initializeBoundaries();
+    void pause(bool paused);
+
+private:
+
+QGraphicsScene * pScene_;
+QGraphicsView * pView_;
+OrientationControlledGraphicsPixmapObject * pTursas_;
+bool paused_;
+
+
+
+
+};
+
+#endif // MAINWINDOW_H
diff --git a/orientationcontrol2pix.qrc b/orientationcontrol2pix.qrc
new file mode 100644 (file)
index 0000000..b40ac7f
--- /dev/null
@@ -0,0 +1,6 @@
+<RCC>
+    <qresource prefix="/pix">
+        <file>meri.png</file>
+        <file>tursas.png</file>
+    </qresource>
+</RCC>
diff --git a/orientationcontrolledgraphicspixmapobject.cpp b/orientationcontrolledgraphicspixmapobject.cpp
new file mode 100644 (file)
index 0000000..5b3b4b1
--- /dev/null
@@ -0,0 +1,71 @@
+#include "orientationcontrolledgraphicspixmapobject.h"
+#include <QDebug>
+#include <QGraphicsScene>
+
+//OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
+//    QObject(), QGraphicsPixmapItem (parent)
+//{
+
+//}
+
+OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject(QPixmap pixmap, QGraphicsItem *parent) :
+    QObject(), QGraphicsPixmapItem (pixmap, parent)
+{
+
+    connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
+
+
+
+}
+
+void OrientationControlledGraphicsPixmapObject::startMoving()
+{
+    rotationSensor_.start();
+    qDebug() << "started the sensor";
+    qDebug() << rotationSensor_.isActive();
+}
+
+
+void OrientationControlledGraphicsPixmapObject::stopMoving()
+{
+    rotationSensor_.stop();
+    qDebug () << "trying to stop the sensor";
+}
+
+void OrientationControlledGraphicsPixmapObject::readRotationSensor()
+{
+    if (!scene()) //no movement if this item does not belong to a scene
+        return;
+
+    QRect sceneRectangle = scene()->sceneRect().toRect();
+
+
+    QRotationReading* pSensorData = rotationSensor_.reading();
+
+    int deltay = pSensorData->x(); //yes, you need the "x" value from the sensor for "y" direction in the scene...
+    int deltax = pSensorData->y(); //...and vice versa
+
+
+ //   qDebug() << deltax << " " << deltay;
+
+    int newx = x() + deltax/15;
+    int newy = y() + deltay/15;
+
+
+//    qDebug() << sceneRectangle.left() << sceneRectangle.right();
+
+
+    setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
+    setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
+
+}
+
+
+void OrientationControlledGraphicsPixmapObject::setBoundaries(QRectF boundaryrect)
+{
+    boundaryrect_ = boundaryrect;
+
+}
+
+
+
diff --git a/orientationcontrolledgraphicspixmapobject.h b/orientationcontrolledgraphicspixmapobject.h
new file mode 100644 (file)
index 0000000..da7c3ed
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef ORIENTATIONCONTROLLEDGRAPHICSPIXMAPOBJECT_H
+#define ORIENTATIONCONTROLLEDGRAPHICSPIXMAPOBJECT_H
+
+#include <QGraphicsPixmapItem>
+#include <QRotationSensor>
+
+QTM_USE_NAMESPACE
+
+
+class OrientationControlledGraphicsPixmapObject : public QObject, public QGraphicsPixmapItem
+{
+    Q_OBJECT
+public:
+//    explicit OrientationControlledGraphicsPixmapObject(QGraphicsItem *parent = 0);
+    explicit OrientationControlledGraphicsPixmapObject(QPixmap pixmap = 0, QGraphicsItem *parent = 0);
+
+signals:
+
+public slots:
+    void startMoving();
+    void stopMoving();
+    void readRotationSensor();
+    void setBoundaries(QRectF boundaryrect);
+
+
+private:
+
+    QRotationSensor rotationSensor_;
+
+    QRectF boundaryrect_;
+
+};
+
+#endif // ORIENTATIONCONTROLLEDGRAPHICSPIXMAPOBJECT_H
diff --git a/rotationcontrol2.pro b/rotationcontrol2.pro
new file mode 100644 (file)
index 0000000..cc13161
--- /dev/null
@@ -0,0 +1,40 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2011-05-03T17:35:02
+#
+#-------------------------------------------------
+
+QT       += core gui
+
+TARGET = rotationcontrol2
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+        mainwindow.cpp \
+    orientationcontrolledgraphicspixmapobject.cpp
+
+HEADERS  += mainwindow.h \
+    orientationcontrolledgraphicspixmapobject.h
+
+CONFIG += mobility
+MOBILITY = sensors
+
+symbian {
+    TARGET.UID3 = 0xe3f4bbc2
+    # TARGET.CAPABILITY += 
+    TARGET.EPOCSTACKSIZE = 0x14000
+    TARGET.EPOCHEAPSIZE = 0x020000 0x800000
+}
+
+unix:!symbian {
+    maemo5 {
+        target.path = /opt/usr/bin
+    } else {
+        target.path = /usr/local/bin
+    }
+    INSTALLS += target
+}
+
+RESOURCES += \
+    orientationcontrol2pix.qrc