examples/sensors/panorama/view.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 #include "view.h"
       
     2 #include "inputcontroller.h"
       
     3 #include "keycontroller.h"
       
     4 #include "accelerometercontroller.h"
       
     5 #include "magnetometercontroller.h"
       
     6 #include "rotationcontroller.h"
       
     7 #include "orientationcontroller.h"
       
     8 #include "compasscontroller.h"
       
     9 #include "tapcontroller.h"
       
    10 
       
    11 int View::m_imageWidth;
       
    12 int View::m_imageHeight;
       
    13 QString View::m_currentSensor;
       
    14 bool View::m_isToBeZoomed;
       
    15 int View::m_lightLevel = -1;
       
    16 int View::m_scaledHeight[4];
       
    17 int View::m_index = 3;
       
    18 
       
    19 View::View(QGraphicsScene *scene) :
       
    20         QGraphicsView(scene), m_timer(this) ,m_delay(30){
       
    21 
       
    22     QPixmap bgPix(":/images/koskipuisto_pieni.jpg");
       
    23     m_pix = bgPix;
       
    24     setupWindow();
       
    25     m_scaledHeight[0] = m_imageHeight*1.5;
       
    26     m_scaledHeight[1] = m_imageHeight*2;
       
    27     m_scaledHeight[2] = (int)(m_imageHeight*0.5);
       
    28     m_scaledHeight[3] = m_imageHeight;
       
    29 
       
    30     int h = height();
       
    31     int y = qAbs(m_imageHeight-h)/2;
       
    32     setSceneRect(0, y, width(), h);
       
    33 
       
    34 
       
    35     m_sensors.append(InputController::QACCELEROMETER);
       
    36     m_sensors.append(InputController::QORIENTATIONSENSOR);
       
    37     m_sensors.append(InputController::QMAGNETOMETER);
       
    38     m_sensors.append(InputController::QROTATIONSENSOR);
       
    39     m_sensors.append(InputController::QTAPSENSOR);
       
    40     m_sensors.append(InputController::QCOMPASS);
       
    41     m_sensors.append(InputController::QKEYS);
       
    42 
       
    43 
       
    44     m_menu = new QMenu(this);
       
    45     createActions();
       
    46     handleAction(NULL,InputController::QACCELEROMETER);
       
    47 
       
    48     m_timer.setSingleShot(false);
       
    49     m_timer.start(m_delay);
       
    50     connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
       
    51 
       
    52 
       
    53     m_proximitySensor.connectToBackend();
       
    54     m_proximitySensor.start();
       
    55     connect(&m_proximitySensor, SIGNAL(readingChanged()), this, SLOT(handleProximity()));
       
    56 
       
    57     m_ambientLightSensor.connectToBackend();
       
    58     m_ambientLightSensor.start();
       
    59     connect(&m_ambientLightSensor, SIGNAL(readingChanged()), this, SLOT(handleALS()));
       
    60 
       
    61 }
       
    62 
       
    63 
       
    64 View::~View(){
       
    65     m_timer.stop();
       
    66     if (m_controller) delete m_controller;
       
    67     if (m_exController) delete m_exController;
       
    68 }
       
    69 
       
    70 
       
    71 void View::resizeEvent(QResizeEvent *event)
       
    72 {
       
    73     QGraphicsView::resizeEvent(event);
       
    74     fitInView(sceneRect(), Qt::KeepAspectRatio);
       
    75 }
       
    76 
       
    77 
       
    78 
       
    79 void View::handleAction(QString oldSensor, QString newSensor){
       
    80 
       
    81     m_menu->setVisible(false);
       
    82 
       
    83     if (oldSensor==newSensor) return;
       
    84 
       
    85 
       
    86     QList<QAction*> tmpActions = m_menu->actions();
       
    87     for (int i=0; i<tmpActions.length(); i++){
       
    88         if (tmpActions.at(i)->text() ==  oldSensor){
       
    89             tmpActions.at(i)->setEnabled(true);
       
    90             continue;
       
    91         }
       
    92         if (tmpActions.at(i)->text() == newSensor){
       
    93             tmpActions.at(i)->setEnabled(false);
       
    94         }
       
    95     }
       
    96     m_currentSensor = newSensor;
       
    97     switchController(m_currentSensor);
       
    98 
       
    99 }
       
   100 
       
   101 
       
   102 
       
   103 void View::createActions()
       
   104 {
       
   105 
       
   106     for (int i=0; i<m_sensors.length();i++){
       
   107         QAction* tmp = new QAction(m_sensors.at(i), this);
       
   108 
       
   109         const QString sensor = m_sensors.at(i);
       
   110         do{
       
   111             if (sensor==InputController::QACCELEROMETER){
       
   112                 connect(tmp, SIGNAL(triggered()), this, SLOT(startAccelerometer()));
       
   113                 break;
       
   114             }
       
   115             if (sensor==InputController::QORIENTATIONSENSOR){
       
   116                 connect(tmp, SIGNAL(triggered()), this, SLOT(startOrientationSensor()));
       
   117                 break;
       
   118             }
       
   119             if (sensor==InputController::QROTATIONSENSOR){
       
   120                 connect(tmp, SIGNAL(triggered()), this, SLOT(startRotationSensor()));
       
   121                 break;
       
   122             }
       
   123             if (sensor==InputController::QMAGNETOMETER){
       
   124                 connect(tmp, SIGNAL(triggered()), this, SLOT(startMagnetometer()));
       
   125                 break;
       
   126             }
       
   127             if (sensor==InputController::QTAPSENSOR){
       
   128                 connect(tmp, SIGNAL(triggered()), this, SLOT(startTapSensor()));
       
   129                 break;
       
   130             }
       
   131             if (sensor==InputController::QCOMPASS){
       
   132                 connect(tmp, SIGNAL(triggered()), this, SLOT(startCompass()));
       
   133                 break;
       
   134             }
       
   135             if (sensor==InputController::QKEYS){
       
   136                 connect(tmp, SIGNAL(triggered()), this, SLOT(startKeys()));
       
   137                 break;
       
   138             }
       
   139         }while (true);
       
   140         m_menu->addAction(tmp);
       
   141     }
       
   142 
       
   143 }
       
   144 
       
   145 void View::startAccelerometer(){
       
   146     handleAction(m_currentSensor, InputController::QACCELEROMETER);
       
   147 }
       
   148 
       
   149 void View::startOrientationSensor(){
       
   150     handleAction(m_currentSensor, InputController::QORIENTATIONSENSOR);
       
   151 }
       
   152 
       
   153 void View::startTapSensor(){
       
   154     handleAction(m_currentSensor, InputController::QTAPSENSOR);
       
   155 }
       
   156 
       
   157 void View::startMagnetometer(){
       
   158     handleAction(m_currentSensor, InputController::QMAGNETOMETER);
       
   159 }
       
   160 
       
   161 void View::startRotationSensor(){
       
   162     handleAction(m_currentSensor, InputController::QROTATIONSENSOR);
       
   163 }
       
   164 
       
   165 void View::startKeys(){
       
   166     handleAction(m_currentSensor, InputController::QKEYS);
       
   167 }
       
   168 
       
   169 void View::startCompass(){
       
   170     handleAction(m_currentSensor, InputController::QCOMPASS);
       
   171 }
       
   172 
       
   173 
       
   174 
       
   175 
       
   176 void View::keyPressEvent(QKeyEvent *e)
       
   177 {
       
   178     if (m_currentSensor!=InputController::QKEYS) return;
       
   179 
       
   180     if (m_controller) m_controller->keyPressEvent(e);
       
   181 }
       
   182 
       
   183 
       
   184 void View::switchController(QString sensor){
       
   185 
       
   186     if (m_controller){
       
   187         delete m_controller;
       
   188         m_controller =0;
       
   189     }
       
   190 
       
   191     if (sensor==InputController::QACCELEROMETER){
       
   192         m_controller = new AccelerometerController();
       
   193         return;
       
   194     }
       
   195     if (sensor==InputController::QROTATIONSENSOR){
       
   196         m_controller = new RotationController();
       
   197         return;
       
   198     }
       
   199     if (sensor==InputController::QMAGNETOMETER){
       
   200         m_controller = new MagnetometerController();
       
   201         return;
       
   202     }
       
   203     if (sensor==InputController::QORIENTATIONSENSOR){
       
   204         m_controller = new OrientationController();
       
   205         return;
       
   206     }
       
   207     if (sensor==InputController::QTAPSENSOR){
       
   208         m_controller = new TapController();
       
   209         return;
       
   210     }
       
   211     if (sensor==InputController::QCOMPASS){
       
   212         m_controller = new CompassController();
       
   213         return;
       
   214     }
       
   215     if (sensor==InputController::QKEYS){
       
   216         m_controller = new KeyController();
       
   217         return;
       
   218     }
       
   219 
       
   220 
       
   221 
       
   222 }
       
   223 
       
   224 
       
   225 int View::checkX(int x){
       
   226     int tmpX = qAbs(x) < m_imageWidth ? x : x % m_imageWidth;
       
   227     m_mouseMode?m_exController->setX(tmpX):m_controller->setX(tmpX);
       
   228     return tmpX;
       
   229 
       
   230 }
       
   231 
       
   232 int View::checkY(int y){
       
   233     int limit = m_imageHeight-height();
       
   234     if (y<0){
       
   235         y = limit<0?-limit/2:0;
       
   236     }
       
   237     else if (limit<0){
       
   238         y=-limit/2;
       
   239     }
       
   240     else if (y > limit){y=limit;}
       
   241 
       
   242     m_mouseMode?m_exController->setY(y):m_controller->setY(y);
       
   243     return y;
       
   244 }
       
   245 
       
   246 
       
   247 
       
   248 void View::mousePressEvent ( QMouseEvent* ){
       
   249     m_mousePressTime = QTime::currentTime();
       
   250     m_exController = m_controller;
       
   251     m_mouseMode = false;
       
   252 }
       
   253 
       
   254 
       
   255 void View::mouseMoveEvent(QMouseEvent* event){
       
   256 
       
   257     // first time
       
   258     if (!m_mouseMode){
       
   259         m_mouseMode = true;
       
   260         m_eventX = event->x();
       
   261         m_eventY = event->y();
       
   262         return;
       
   263     }
       
   264 
       
   265     if (m_menu->isVisible()){
       
   266         if (m_mousePressTime.msecsTo(QTime::currentTime())>300)
       
   267             m_menu->setVisible(false);
       
   268     }
       
   269 
       
   270     int cur_x = event->x();
       
   271     int cur_y = event->y();
       
   272 
       
   273     m_dx = m_eventX - cur_x;
       
   274     m_dy = m_eventY - cur_y;
       
   275 
       
   276     m_eventX = cur_x;
       
   277     m_eventY = cur_y;
       
   278 
       
   279     update();
       
   280 
       
   281 }
       
   282 
       
   283 
       
   284 
       
   285 void View::mouseReleaseEvent(QMouseEvent* event){
       
   286     m_controller = m_exController;
       
   287     m_exController = 0;
       
   288     m_mouseMode = false;
       
   289 
       
   290 
       
   291     if (m_menu->isVisible()){
       
   292         m_menu->setVisible(false);
       
   293         return;
       
   294     }
       
   295 
       
   296     // slow press -> move
       
   297     if (m_mousePressTime.msecsTo(QTime::currentTime())>300) return;
       
   298 
       
   299     // quick press -> show menu
       
   300     int x = event->x();
       
   301     int y = event->y();
       
   302     m_menu->move(x, y);
       
   303     m_menu->setVisible(true);
       
   304 
       
   305 }
       
   306 
       
   307 
       
   308 void View::update(){
       
   309     if (!m_mouseMode)
       
   310         m_controller->updateCoordinates();
       
   311 
       
   312     if (m_menu->isVisible()){
       
   313         if (m_mousePressTime.msecsTo(QTime::currentTime())>5000)
       
   314             m_menu->setVisible(false);
       
   315     }
       
   316 
       
   317 
       
   318     int x = m_mouseMode?m_exController->getX():m_controller->getX();
       
   319     int y = m_mouseMode?m_exController->getY():m_controller->getY();
       
   320 
       
   321     x = checkX(m_mouseMode?x+m_dx:x);
       
   322     y = checkY(m_mouseMode?y+m_dy:y);
       
   323 
       
   324     setSceneRect(x, y, width(), height());
       
   325 }
       
   326 
       
   327 
       
   328 
       
   329 
       
   330 void View::handleProximity(){
       
   331 
       
   332     // close - not close - close, quite fast sequence required for zoom
       
   333     // zoom factors 0.5, 1, 2, 3 - cyclic iterations
       
   334 
       
   335     bool isClose = m_proximitySensor.reading()->close();
       
   336 
       
   337 
       
   338     //     zoom-in, once?
       
   339     if (isClose){
       
   340         if (!m_isToBeZoomed){
       
   341             m_zoomTime = QTime::currentTime();
       
   342             m_isToBeZoomed = true;
       
   343         }
       
   344         else{
       
   345             m_isToBeZoomed = false; // ready to start again
       
   346 
       
   347             if (m_zoomTime.msecsTo(QTime::currentTime())>1000) return;
       
   348             m_index = m_index==3?0:m_index+1;
       
   349             qDebug()<< " m_index="<<m_index;
       
   350             m_pix = m_pix.scaledToHeight(m_scaledHeight[m_index]);
       
   351         }
       
   352     }
       
   353 
       
   354     setupWindow();
       
   355     update();
       
   356 
       
   357 }
       
   358 
       
   359 
       
   360 void View::handleALS(){
       
   361 
       
   362     int lightLevel = m_ambientLightSensor.reading()->lightLevel();
       
   363     qDebug()<<"lightLevel="<<lightLevel;
       
   364 
       
   365     switch (lightLevel){
       
   366     case 0:
       
   367     case 1:{
       
   368             // if dark, make image "brighter"
       
   369             QPainter painter;
       
   370             painter.begin(&m_pix);
       
   371             painter.setPen(Qt::NoPen);
       
   372             QColor slightlyOpaqueWhite(255, 255, 255, 63);
       
   373             painter.fillRect(m_pix.rect(), slightlyOpaqueWhite);
       
   374             painter.end();
       
   375             break;
       
   376         }
       
   377     default:{
       
   378             if (m_lightLevel>1) break;
       
   379             QPixmap bgPix(":/images/koskipuisto_pieni.jpg");
       
   380             m_pix = bgPix.scaledToHeight(m_scaledHeight[m_index]);
       
   381             break;
       
   382         }
       
   383     }
       
   384     m_lightLevel = lightLevel;
       
   385 
       
   386 }
       
   387 
       
   388 
       
   389 void View::setupWindow(){
       
   390     m_imageWidth = m_pix.width();
       
   391     m_imageHeight = m_pix.height();
       
   392 
       
   393     setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
       
   394     setBackgroundBrush(m_pix);
       
   395     setCacheMode(QGraphicsView::CacheBackground);
       
   396     setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
       
   397     setWindowTitle("Panorama");
       
   398 
       
   399 }
       
   400 
       
   401 
       
   402