examples/Qt/qtnfctags/nfctagswindow.cpp

00001 /*
00002  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 #include <QMenuBar>
00031 #include <QTextEdit>
00032 #include <QAction>
00033 
00034 #include "nfctagswindow.h"
00035 #include "nfctagsdiscovery.h"
00036 
00037 /*
00038  * Initialize the window for the application, establish the 
00039  * necessary connections.
00040  * Create the wrapper class and establish its connections.
00041  */
00042 NfcTagsWindow::NfcTagsWindow(QWidget *parent) :
00043     QMainWindow(parent),mDiscovery(false)
00044 {
00045     addMenu();
00046     initializeWindow();
00047     
00048     mNfcTagsDiscovery = new NfcTagsDiscovery();
00049     
00050     //Connect slots for update of new devices, discovery starting and completion, and errors
00051     connect(mNfcTagsDiscovery,SIGNAL(showMessage(QString)),this, SLOT(showMessage(QString)));
00052     connect(mNfcTagsDiscovery,SIGNAL(changeMenu(int)),this, SLOT(changeMenu(int)));
00053     connect(mNfcTagsDiscovery,SIGNAL(clearText()),this, SLOT(clearText()));
00054 }
00055 
00056 /*
00057  * Add items to the menu of the main window.
00058  */
00059 void NfcTagsWindow::addMenu() 
00060 {
00061     // Add menu item, establish connection for it.
00062     mStartTagDiscovery = new QAction("Start Tag Discovery",this);
00063     connect(mStartTagDiscovery, SIGNAL(triggered()), this, SLOT(startTagDiscovery()));
00064     menuBar()->addAction(mStartTagDiscovery);
00065 
00066     
00067     mStopTagDiscovery = new QAction("Stop Tag Discovery",this);
00068     connect(mStopTagDiscovery, SIGNAL(triggered()), this, SLOT(stopTagDiscovery()));
00069     menuBar()->addAction(mStopTagDiscovery);
00070     // Set to invisible.
00071     mStopTagDiscovery->setVisible(false);
00072     
00073     mStartNdefDiscovery = new QAction("Start Ndef Discovery",this);
00074     connect(mStartNdefDiscovery, SIGNAL(triggered()), this, SLOT(startNdefDiscovery()));
00075     menuBar()->addAction(mStartNdefDiscovery);
00076 
00077     
00078     mStopNdefDiscovery = new QAction("Stop Ndef Discovery",this);
00079     connect(mStopNdefDiscovery, SIGNAL(triggered()), this, SLOT(stopNdefDiscovery()));
00080     menuBar()->addAction(mStopNdefDiscovery);  
00081     mStopNdefDiscovery->setVisible(false);
00082 }
00083 
00084 // Close the application.
00085 void NfcTagsWindow::closeEvent(QCloseEvent *event) 
00086 {
00087     if(mDiscovery){
00088     // Stop Tag/NDEF discovery
00089     mNfcTagsDiscovery->stopTagDiscovery();
00090     }
00091 }
00092 
00093 /*
00094  * Add a text edit widget to the main window.
00095  * Output text is displayed in this text edit widget.
00096  */
00097 void NfcTagsWindow::initializeWindow() 
00098 {    
00099     mDisplayInfo = new QTextEdit();
00100     mDisplayInfo->setText("Detected nfc tags information is \n"
00101             "displayed here.");
00102     mDisplayInfo->setWordWrapMode(QTextOption::WordWrap);
00103     // This is a read-only text.
00104     mDisplayInfo->setReadOnly(true);
00105     setCentralWidget(mDisplayInfo);
00106 }
00107 
00108 // Start tag discovery.
00109 void NfcTagsWindow::startTagDiscovery() 
00110 {
00111     mDiscovery = true;
00112     mNfcTagsDiscovery->startTagDiscovery();
00113 }
00114 
00115 // Stop tag discovery.
00116 void NfcTagsWindow::stopTagDiscovery() 
00117 {
00118     mDiscovery = false;
00119     mNfcTagsDiscovery->stopTagDiscovery();
00120 }
00121 
00122 // Search for NDEF messages.
00123 void NfcTagsWindow::startNdefDiscovery() 
00124 {
00125     mDiscovery = true;
00126     mNfcTagsDiscovery->startNdefDiscovery();
00127 }
00128 
00129 // Stop NDEF discovery.
00130 void NfcTagsWindow::stopNdefDiscovery() 
00131 {
00132     mDiscovery = false;
00133     mNfcTagsDiscovery->stopNdefDiscovery();
00134 }
00135 
00136 void NfcTagsWindow::changeMenu(int option) 
00137 {
00138    switch(option) 
00139        {
00140        case 0:
00141            mStartTagDiscovery->setVisible(false);
00142            mStopTagDiscovery->setVisible(true);
00143            mStartNdefDiscovery->setVisible(false);
00144            mStopNdefDiscovery->setVisible(false);
00145            break;
00146            
00147        case 1:
00148            mStartTagDiscovery->setVisible(false);
00149            mStopTagDiscovery->setVisible(false);
00150            mStartNdefDiscovery->setVisible(false);
00151            mStopNdefDiscovery->setVisible(true);
00152            break;
00153        
00154        case 2:
00155             mStartTagDiscovery->setVisible(false);
00156             mStopTagDiscovery->setVisible(false);
00157             mStartNdefDiscovery->setVisible(false);
00158             mStopNdefDiscovery->setVisible(true);
00159             break;
00160 
00161        case 3:
00162        case 4:
00163            mStartTagDiscovery->setVisible(true);
00164            mStopTagDiscovery->setVisible(false);
00165            mStartNdefDiscovery->setVisible(true);
00166            mStopNdefDiscovery->setVisible(false);
00167            break;
00168        };
00169    
00170 }
00171 
00172 
00173 // Clear the text in the window.
00174 void NfcTagsWindow::clearText() 
00175 {
00176     mText.clear();
00177     mDisplayInfo->setText(mText);
00178 }
00179 
00180 // Show the message on the window.
00181 void NfcTagsWindow::showMessage(QString aDisplayMsg)
00182 { 
00183    mText.append(aDisplayMsg);
00184    // Write the text to the text edit item.
00185    mDisplayInfo->setText(mText);
00186 }

Generated by  doxygen 1.6.2