examples/Qt/sensorgesture/sensorgestureexample/sgexamplewidget.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation
00006 **
00007 **
00008 ** $QT_BEGIN_LICENSE:BSD$
00009 ** You may use this file under the terms of the BSD license as follows:
00010 **
00011 ** "Redistribution and use in source and binary forms, with or without
00012 ** modification, are permitted provided that the following conditions are
00013 ** met:
00014 **   * Redistributions of source code must retain the above copyright
00015 **     notice, this list of conditions and the following disclaimer.
00016 **   * Redistributions in binary form must reproduce the above copyright
00017 **     notice, this list of conditions and the following disclaimer in
00018 **     the documentation and/or other materials provided with the
00019 **     distribution.
00020 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00021 **     the names of its contributors may be used to endorse or promote
00022 **     products derived from this software without specific prior written
00023 **     permission.
00024 **
00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00036 ** $QT_END_LICENSE$
00037 **
00038 ****************************************************************************/
00039 
00040 // System includes
00041 #include <QPlainTextEdit>
00042 #include <XQSensorGestureManager>
00043 #include <QDeclarativePropertyMap>
00044 #include <QSound>
00045 #include <QLabel>
00046 
00047 // User includes
00048 #include "sgexamplewidget.h"
00049 
00050 //Sound file to notify gesture recognition. 
00051 const QString soundFile = "z:/data/sounds/digital/alarm.aac";
00052 //Gesture type that would be used by the example application
00053 const QString gestureType = "com.nokia.gesture.cover" ;
00054 
00058 SGExampleWidget::SGExampleWidget(QWidget *parent) :
00059     QWidget(parent), mSensorGestureManager(0)
00060 { 
00061     // initializing the text view and make it read only     
00062     mIntoductionMessage = new QPlainTextEdit(this);   
00063     mIntoductionMessage->setReadOnly(true);    
00064   
00065     mApplicationTitle = new QLabel("SensureGesture Example");
00066     mApplicationTitle->setFrameStyle(QFrame::Box | QFrame::Plain);
00067     mApplicationTitle->setLineWidth(2);
00068     
00069     // creating and setting the vertical layout and adding the text view to the layout    
00070     QVBoxLayout *vLayout = new QVBoxLayout() ; 
00071     vLayout->addWidget(mApplicationTitle);
00072     vLayout->addWidget(mIntoductionMessage);
00073     setLayout(vLayout);    
00074     
00075     // creating the sound player and setting it play once    
00076     mSound = new QSound(soundFile);
00077     mSound->setLoops(1);   
00078     
00079     //Start using the framework
00080     startGestureRecognition();          
00081 }
00082 
00087 void SGExampleWidget::startGestureRecognition()
00088 {    
00089     //Start or/and connect to the sensor gesture framework   
00090     mSensorGestureManager = new XQSensorGestureManager(this);
00091     
00092     writeMsg("\nConnected to the SG framework.") ;
00093 
00094     //Check the availiblity of the required plugin    
00095     if(mSensorGestureManager->isGestureTypeAvailable(gestureType)) {
00096         writeMsg(gestureType + " is available.") ;
00097       
00098         //If plugin is available, start it  
00099         if (mSensorGestureManager->start(gestureType)) {   
00100             writeMsg(gestureType + " is started.") ;       
00101                      
00102             writeMsg("\nThe cover gesture is recognized when the phone is in horizontal position, " \
00103                     "facing up and covered.");
00104             
00105             //Connect the framework signal to the local slot
00106             connect(mSensorGestureManager,SIGNAL(recognized(QString, QDeclarativePropertyMap)),
00107                     this, SLOT(handleGestureRecognition(QString, QDeclarativePropertyMap)));
00108         }
00109     }
00110     else {
00111         writeMsg(gestureType + " is not available.") ;
00112     }    
00113 }
00114 
00118 SGExampleWidget::~SGExampleWidget()
00119 {
00120     // Deleting mSound 
00121     mSound = NULL;
00122     delete mSound;    
00123     
00124     // Deleting the sensor gesture manager after stopping it if it is started
00125     if(mSensorGestureManager){ 
00126         if (mSensorGestureManager->isStarted(gestureType) == true)
00127         {
00128         mSensorGestureManager->stop(gestureType);  //optional
00129         }
00130         delete mSensorGestureManager ;
00131     }
00132 }
00133 
00137 void SGExampleWidget::handleGestureRecognition(const QString &gestureType,
00138                                        const QDeclarativePropertyMap &attributes)
00139 {
00140     Q_UNUSED(gestureType);
00141     Q_UNUSED(attributes);
00142    
00143     // Play the sound and display the messagebox when the cover gesture is detected.
00144     if (!mSound->isFinished()) {
00145         mSound->stop();
00146     }
00147     mSound->play();
00148     
00149     // The location and the style of the message box are set.     
00150     mNotificationMsgBox.setText("Cover gesture detected");
00151     mNotificationMsgBox.show();    
00152     mNotificationMsgBox.exec();
00153 }
00154 
00158 void SGExampleWidget::writeMsg(QString msg)
00159 {
00160     qDebug() << msg;
00161     if (mIntoductionMessage) 
00162     {
00163         mIntoductionMessage->appendPlainText(msg);
00164     }
00165 }

Generated by  doxygen 1.6.2