1 #include "orientationcontrolledgraphicspixmapobject.h"
3 #include <QGraphicsScene>
5 //OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
6 // QObject(), QGraphicsPixmapItem (parent)
11 OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject(QPixmap pixmap, QGraphicsItem *parent) :
12 QObject(), QGraphicsPixmapItem (pixmap, parent)
15 connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
21 void OrientationControlledGraphicsPixmapObject::startMoving()
23 rotationSensor_.start();
24 qDebug() << "started the sensor";
25 qDebug() << rotationSensor_.isActive();
29 void OrientationControlledGraphicsPixmapObject::stopMoving()
31 rotationSensor_.stop();
32 qDebug () << "trying to stop the sensor";
35 void OrientationControlledGraphicsPixmapObject::readRotationSensor()
37 if (!scene()) //no movement if this item does not belong to a scene
40 QRect sceneRectangle = scene()->sceneRect().toRect();
43 QRotationReading* pSensorData = rotationSensor_.reading();
45 int deltay = pSensorData->x(); //yes, you need the "x" value from the sensor for "y" direction in the scene...
46 int deltax = pSensorData->y(); //...and vice versa
49 // qDebug() << deltax << " " << deltay;
54 int newx = x() + deltax/15;
55 int newy = y() + deltay/15;
58 // qDebug() << sceneRectangle.left() << sceneRectangle.right();
61 setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
62 setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
65 //handle collisions and move back to the original position if false returned
67 if (handleCollisions() == false)
76 bool OrientationControlledGraphicsPixmapObject::handleCollisions()
78 qDebug() << "I should not be here!";