Added timer controlled item
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Sat, 14 May 2011 13:23:22 +0000 (16:23 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Sat, 14 May 2011 13:23:22 +0000 (16:23 +0300)
Mostly untested though.

mainwindow.cpp
orientationcontrol2pix.qrc
rotationcontrol2.pro
timercontrolledtursas.cpp [new file with mode: 0644]
timercontrolledtursas.h [new file with mode: 0644]

index 9c8605f..968ebeb 100644 (file)
@@ -1,4 +1,5 @@
 #include "mainwindow.h"
+#include "timercontrolledtursas.h"
 #include <QPixmap>
 #include <QTimer>
 #include <QDebug>
@@ -45,6 +46,13 @@ MainWindow::MainWindow(QWidget *parent)
     pRock2->moveBy(80,80);
     pRock2->setData(0,"rock");
 
+    TimerControlledTursas * pMustekala = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
+    pScene_->addItem(pMustekala);
+
+    pMustekala->startMoving();
+
+
+
 
 }
 
index a47e75d..27a9a3a 100644 (file)
@@ -4,5 +4,6 @@
         <file>tursas.png</file>
         <file>kari.png</file>
         <file>aave.png</file>
+        <file>laiva.png</file>
     </qresource>
 </RCC>
index cc13161..4dac436 100644 (file)
@@ -12,10 +12,12 @@ TEMPLATE = app
 
 SOURCES += main.cpp\
         mainwindow.cpp \
-    orientationcontrolledgraphicspixmapobject.cpp
+    orientationcontrolledgraphicspixmapobject.cpp \
+    timercontrolledtursas.cpp
 
 HEADERS  += mainwindow.h \
-    orientationcontrolledgraphicspixmapobject.h
+    orientationcontrolledgraphicspixmapobject.h \
+    timercontrolledtursas.h
 
 CONFIG += mobility
 MOBILITY = sensors
diff --git a/timercontrolledtursas.cpp b/timercontrolledtursas.cpp
new file mode 100644 (file)
index 0000000..187d5f1
--- /dev/null
@@ -0,0 +1,72 @@
+#include "timercontrolledtursas.h"
+#include <QGraphicsScene>
+#include <QDebug>
+
+
+TimerControlledTursas::TimerControlledTursas(QPixmap pixmap, int speed, QGraphicsItem* parent) :
+    QObject(), QGraphicsPixmapItem(pixmap, parent)
+{
+    setSpeed(speed);
+    direction_ = S;
+    connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
+}
+
+void TimerControlledTursas::startMoving()
+{
+    timer_.start();
+}
+
+void TimerControlledTursas::stopMoving()
+{
+    timer_.stop();
+}
+
+void TimerControlledTursas::setSpeed(int speed)
+{
+    timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
+}
+
+void TimerControlledTursas::move()
+{
+
+    if (!scene()) //no movement if this item does not belong to a scene
+        return;
+
+    int oldx = x();
+    int oldy = y();
+
+    int newx = oldx;
+    int newy = oldy;
+
+    if (direction_ == E || direction_ == SE || direction_ == NE)
+    {
+        newx++;
+    }
+
+    if (direction_ == W || direction_ == SW || direction_ == NW)
+    {
+        newx--;
+    }
+
+    if (direction_ == S || direction_ == SE || direction_ == SW)
+    {
+        newy++;
+    }
+
+    if (direction_ == N || direction_ == NE || direction_ == NW)
+    {
+        newy--;
+    }
+
+
+    //These three lines are identical with the orientationcontrolled version - should there be a common base class with a function to handle this?
+
+    QRect sceneRectangle = scene()->sceneRect().toRect();
+    setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
+    setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
+
+
+
+}
+
+
diff --git a/timercontrolledtursas.h b/timercontrolledtursas.h
new file mode 100644 (file)
index 0000000..f82d22c
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef TIMERCONTROLLEDTURSAS_H
+#define TIMERCONTROLLEDTURSAS_H
+
+#include <QObject>
+#include <QGraphicsPixmapItem>
+#include <QTimer>
+
+class TimerControlledTursas : public QObject, public QGraphicsPixmapItem
+{
+    Q_OBJECT
+public:
+    explicit TimerControlledTursas(QPixmap pixmap = QPixmap(), int speed = 1, QGraphicsItem *parent = 0);
+
+
+signals:
+
+public slots:
+
+    void startMoving();
+    void stopMoving();
+
+    /*! Intended to be used internally by connecting to a timer
+      */
+    void move();
+
+    /*! Sets the movement speed of the item
+      @param speed given in pixels per second
+      */
+    void setSpeed(int speed);
+
+
+ private:
+
+    QTimer timer_;
+
+    enum direction {N, NE, E, SE, S, SW, W, NW};
+
+    direction direction_;
+
+};
+
+#endif // TIMERCONTROLLEDTURSAS_H