examples/Qt/qtbluetoothapp/bluetoothappwindow.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 "bluetoothappwindow.h"
00035 #include "bluetoothdiscovery.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 BluetoothAppWindow::BluetoothAppWindow(QWidget *parent) :
00043     QMainWindow(parent)
00044 {
00045     addMenu();
00046     initializeWindow();
00047     
00048     mBluetoothDiscovery = new BluetoothDiscovery(this);
00049     
00050     //Connect slots for update of new devices, discovery starting and completion, and errors
00051     connect(mBluetoothDiscovery,SIGNAL(showMessage(bool , QString )),this, SLOT(showMessage(bool , QString )));
00052     connect(mBluetoothDiscovery,SIGNAL(changeMenu(int)),this, SLOT(changeMenu(int)));
00053 }
00054 
00055 /*
00056  * Add items to the menu of the main window.
00057  */
00058 void BluetoothAppWindow::addMenu() 
00059 {
00060     // Add menu item, establish connection for it and set it invisible.
00061     mDiscoverDevices = new QAction("Discover devices",this);
00062     connect(mDiscoverDevices, SIGNAL(triggered()), this, SLOT(discoverDevices()));
00063     menuBar()->addAction(mDiscoverDevices);
00064     
00065     mDiscoverServices = new QAction("Discover services",this);
00066     connect(mDiscoverServices, SIGNAL(triggered()), this, SLOT(discoverServices()));
00067     menuBar()->addAction(mDiscoverServices);
00068     mDiscoverServices->setVisible(false);
00069     
00070     mStopDiscovery= new QAction("Stop Discovery",this);
00071     connect(mStopDiscovery, SIGNAL(triggered()), this, SLOT(stopDiscovery()));
00072     menuBar()->addAction(mStopDiscovery);
00073 
00074     mSendMessage = new QAction("Send Message",this);
00075     connect(mSendMessage, SIGNAL(triggered()), this, SLOT(sendMessage()));
00076     menuBar()->addAction(mSendMessage);
00077     
00078     mStartSlave = new QAction("Start Slave",this);
00079     connect(mStartSlave, SIGNAL(triggered()), this, SLOT(startSlave()));
00080     menuBar()->addAction(mStartSlave);
00081      
00082     mConnectDevices = new QAction("Connect devices",this);
00083     connect(mConnectDevices, SIGNAL(triggered()), this, SLOT(connectDevices()));
00084     menuBar()->addAction(mConnectDevices);
00085     
00086     mStopSlave = new QAction("Stop Slave",this);
00087     connect(mStopSlave, SIGNAL(triggered()), this, SLOT(stopSlave()));
00088     menuBar()->addAction(mStopSlave);
00089     
00090     mDisconnectDevices = new QAction("Disconnect devices",this);
00091     connect(mDisconnectDevices, SIGNAL(triggered()), this, SLOT(disconnectDevices()));
00092     menuBar()->addAction(mDisconnectDevices);
00093     
00094     mShowConnectedDevices = new QAction("Show ConnectedDevices",this);
00095     connect(mShowConnectedDevices, SIGNAL(triggered()), this, SLOT(showConnectedDevices()));
00096     menuBar()->addAction(mShowConnectedDevices); 
00097     
00098     mClearText = new QAction("Clear Text",this);
00099     connect(mClearText, SIGNAL(triggered()), this, SLOT(clearText()));
00100     menuBar()->addAction(mClearText); 
00101     
00102     initializeMenu();
00103     
00104 }
00105 
00106 // Initialize the Menu Items
00107 void BluetoothAppWindow::initializeMenu() 
00108 {
00109     mDiscoverDevices->setVisible(true);
00110     mDiscoverServices->setVisible(false);
00111     mStopDiscovery->setVisible(false);
00112     mStartSlave->setVisible(true);
00113     mStopSlave->setVisible(false);
00114     mConnectDevices->setVisible(false);
00115     mDisconnectDevices->setVisible(false);
00116     mShowConnectedDevices->setVisible(false);
00117     mSendMessage->setVisible(false);
00118 }
00119 
00120 /*
00121  * Add a text edit widget to the mainwindow.
00122  * Output text is displayed in this text edit widget.
00123  */
00124 void BluetoothAppWindow::initializeWindow() 
00125 {    
00126     mRemoteDevicesList = new QTextEdit();
00127     mRemoteDevicesList->setText("All the remote devices \n"
00128             "detected by bluetooth are listed here");
00129     mRemoteDevicesList->setWordWrapMode(QTextOption::WordWrap);
00130     // this is a read-only text.
00131     mRemoteDevicesList->setReadOnly(true);
00132     setCentralWidget(mRemoteDevicesList);
00133 }
00134 
00135 // Discover the devices available in the bluetooth range.
00136 void BluetoothAppWindow::discoverDevices() 
00137 {
00138     mBluetoothDiscovery->discoverDevices();
00139 }
00140 
00141 // Discover the services offered by the devices.
00142 void BluetoothAppWindow::discoverServices() 
00143 {
00144     mBluetoothDiscovery->discoverServices();
00145 }
00146 
00147 // Stop the ongoing discovery of devices.
00148 void BluetoothAppWindow::stopDiscovery() 
00149 {
00150     mDiscoverDevices->setVisible(true);
00151     mStopDiscovery->setVisible(false);
00152     mBluetoothDiscovery->stopDiscovery();
00153 }
00154 
00155 // Send a message to the connected devices.
00156 void BluetoothAppWindow::sendMessage() 
00157 {
00158     mBluetoothDiscovery->sendMessage();
00159 }
00160 
00161 // Set the device in the listening mode to receive data.
00162 void BluetoothAppWindow::startSlave() 
00163 {
00164     mStartSlave->setVisible(false);
00165     mBluetoothDiscovery->startSlave();
00166 }
00167 
00168 // Connect to devices with necessary services.
00169 void BluetoothAppWindow::connectDevices() 
00170 {
00171     mBluetoothDiscovery->connectDevices();
00172 }
00173 
00174 // Disconnect from the listening mode, Stop the slave mode.
00175 void BluetoothAppWindow::stopSlave() 
00176 {
00177     mStartSlave->setVisible(true);
00178     mBluetoothDiscovery->stopSlave();
00179 }
00180 
00181 // Disconnect from the connected devices.
00182 void BluetoothAppWindow::disconnectDevices() 
00183 {
00184     mBluetoothDiscovery->disconnectDevices();
00185 }
00186 
00187 // Show the connected devices.
00188 void BluetoothAppWindow::showConnectedDevices() 
00189 {
00190     mBluetoothDiscovery->showConnectedDevices();
00191 }
00192 
00193 // Clear the text in window.
00194 void BluetoothAppWindow::clearText() 
00195 {
00196     mDisplayText.clear();
00197     mRemoteDevicesList->setText(mDisplayText);
00198 }
00199 
00200 // Change the menu options.
00201 void BluetoothAppWindow::changeMenu(int aMenuOptions) 
00202 {
00203   switch(aMenuOptions)  
00204       {
00205       case BluetoothAppWindow::EStopDeviceDiscovery:
00206           mDiscoverDevices->setVisible(false);
00207           mStopDiscovery->setVisible(true);
00208           break;
00209       case BluetoothAppWindow::EDeviceDiscoveryComplete:
00210           mDiscoverDevices->setVisible(true);
00211           mStopDiscovery->setVisible(false);
00212           break;          
00213       case BluetoothAppWindow::EHasDevices:
00214           mDiscoverServices->setVisible(true);
00215           mStartSlave->setVisible(true);
00216           mConnectDevices->setVisible(false);
00217           mSendMessage->setVisible(false);
00218           mShowConnectedDevices->setVisible(false);
00219           mDisconnectDevices->setVisible(false);
00220           break;
00221       case BluetoothAppWindow::EHasServices:
00222           mConnectDevices->setVisible(true);
00223           mSendMessage->setVisible(false);
00224           mShowConnectedDevices->setVisible(false);
00225           mDisconnectDevices->setVisible(false);
00226           break;
00227       case BluetoothAppWindow::EHasConnections:
00228           mSendMessage->setVisible(true);
00229           mSendMessage->setVisible(true);
00230           mShowConnectedDevices->setVisible(true);
00231           mDisconnectDevices->setVisible(true);
00232           mConnectDevices->setVisible(false);
00233           break;
00234       case BluetoothAppWindow::EStopSlave:
00235           mStopSlave->setVisible(true);
00236           break;
00237       case BluetoothAppWindow::ESendMessage:
00238           mSendMessage->setVisible(true);
00239           break; 
00240       }
00241 }
00242 
00243 
00244 // Display the output on the screen.
00245 void BluetoothAppWindow::showMessage(bool aDrawLine,QString aDisplayMsg)
00246 { 
00247    if(aDrawLine){
00248        mDisplayText.append("............................\n");    
00249    }
00250    mDisplayText.append(aDisplayMsg);
00251    // write the text in the scroll area.
00252    mRemoteDevicesList->setText(mDisplayText);
00253 }

Generated by  doxygen 1.6.2