qtmobileextensions/examples/keycaptureex/keycapturetestapp.cpp
branchRCL_3
changeset 9 5d007b20cfd0
equal deleted inserted replaced
8:885c2596c964 9:5d007b20cfd0
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include <QDebug>
       
    23 #include <QApplication>
       
    24 #include <QKeyEvent>
       
    25 #include <QLabel>
       
    26 #include <QVBoxLayout>
       
    27 #include <QStackedWidget>
       
    28 #include <QImageReader>
       
    29 #include <QDebug>
       
    30 #include <QTimer>
       
    31 #include <QPushButton>
       
    32 #include <QLineEdit>
       
    33 #include <QDebug>
       
    34 #include <QString>
       
    35 #include <QCheckBox>
       
    36 #include <QPlainTextEdit>
       
    37 #include <QString>
       
    38 #include <QMenu>
       
    39 #include <QMenuBar>
       
    40 #include <QAction>
       
    41 #include <QEvent>
       
    42 
       
    43 
       
    44 #include <QFont>
       
    45 
       
    46 #include "keycapturetestapp.h"
       
    47 #include "txlogger.h"
       
    48 
       
    49 #include "mapping.h"
       
    50 #include "capturerequest.h"
       
    51 
       
    52 KeyCaptureTestApp::KeyCaptureTestApp( QWidget *parent) : QMainWindow(parent)
       
    53 {   
       
    54     TX_ENTRY
       
    55     
       
    56     qApp->installEventFilter(this);
       
    57     QCoreApplication::instance()->installEventFilter(this);
       
    58     
       
    59     
       
    60 	setWindowTitle(tr("KeyCaptureTestApp"));
       
    61     
       
    62     mKeyCapture = new XQKeyCapture();
       
    63     
       
    64     mKeysMap.insert("Up", Qt::Key_Up);
       
    65     mKeysMap.insert("Down", Qt::Key_Down);
       
    66     mKeysMap.insert("Menu", Qt::Key_Menu);
       
    67     
       
    68     
       
    69     mKeysMenu = new QMenu(this);
       
    70     foreach (QString value, mKeysMap.keys())
       
    71         mKeysMenu->addAction(value)->setData( QVariant(value) );
       
    72     
       
    73     mLongFlagsMap.insert("LongNormal", XQKeyCapture::LongNormal);
       
    74     mLongFlagsMap.insert("LongRepeatEvents", XQKeyCapture::LongRepeatEvents);
       
    75     mLongFlagsMap.insert("LongShortEventImmediately", XQKeyCapture::LongShortEventImmediately);
       
    76     mLongFlagsMap.insert("LongWaitNotApplicable", XQKeyCapture::LongWaitNotApplicable);
       
    77     mLongFlagsMap.insert("LongWaitShort", XQKeyCapture::LongWaitShort);
       
    78     
       
    79     mLongFlagsMenu = new QMenu(this);
       
    80     foreach (QString value, mLongFlagsMap.keys())
       
    81         mLongFlagsMenu->addAction(value)->setData( QVariant(value) );
       
    82     
       
    83 	QMenu *captureMenu = menuBar()->addMenu(QString("Capture"));
       
    84     connect(captureMenu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*)));
       
    85 	captureMenu->addAction(QString("Key"))->setData( QVariant(1) );
       
    86 	captureMenu->addAction(QString("Long Key"))->setData( QVariant(2) );
       
    87 	captureMenu->addAction(QString("Up and Down Key"))->setData( QVariant(3) );
       
    88 
       
    89 	QMenu *cancelCaptureMenu = menuBar()->addMenu(QString("Cancel Capture"));
       
    90     connect(cancelCaptureMenu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*)));
       
    91 	cancelCaptureMenu->addAction(QString("Cancel Key"))->setData( QVariant(4) );
       
    92 	cancelCaptureMenu->addAction(QString("Cancel Long Key"))->setData( QVariant(5) );
       
    93 	cancelCaptureMenu->addAction(QString("Cancel Up and Down Key"))->setData( QVariant(6) );
       
    94 
       
    95     QMenu *remoteMenu = menuBar()->addMenu(QString("Remote"));
       
    96 
       
    97     // *** remcon ***
       
    98     
       
    99     remoteAllOn = remoteMenu->addAction(QString("Turn on all"));
       
   100     remoteAllOff = remoteMenu->addAction(QString("Turn off all"));
       
   101     
       
   102     toggleRemoteBasic = remoteMenu->addAction(QString("Basic Remote"));
       
   103     toggleRemoteBasic->setCheckable(true);
       
   104     
       
   105     toggleRemoteCallHandlingEx = remoteMenu->addAction(QString("Call Handl. Ex Remote"));
       
   106     toggleRemoteCallHandlingEx->setCheckable(true);
       
   107     
       
   108     toggleRemoteExtEvents = remoteMenu->addAction(QString("Extended Remote Events"));
       
   109     toggleRemoteExtEvents->setCheckable(true);
       
   110 
       
   111     connect(toggleRemoteBasic, SIGNAL(toggled(bool)), this, SLOT(enableRemBasic(bool)));
       
   112     connect(toggleRemoteCallHandlingEx, SIGNAL(toggled(bool)), this, SLOT(enableRemCallHandlingEx(bool)));
       
   113     connect(toggleRemoteExtEvents, SIGNAL(toggled(bool)), this, SLOT(enableRemoteExtEvents(bool)));
       
   114 
       
   115     connect(remoteAllOn, SIGNAL(triggered(bool)), this, SLOT(remoteAll(bool)));
       
   116     connect(remoteAllOff, SIGNAL(triggered(bool)), this, SLOT(remoteNone(bool)));
       
   117     
       
   118     // *** utilities ***
       
   119 
       
   120     connect(menuBar()->addAction(QString("Clear Log")), SIGNAL(triggered()), this, SLOT(cleanLog()));
       
   121     connect(menuBar()->addAction(QString("Exit")), SIGNAL(triggered()), qApp, SLOT(quit()));
       
   122 
       
   123     QWidget *window = new QWidget;
       
   124     QVBoxLayout* layout = new QVBoxLayout;
       
   125 
       
   126 	mTextArea = new QPlainTextEdit("");
       
   127 	mTextArea->setTextInteractionFlags(Qt::NoTextInteraction);
       
   128 	QFont font = QFont(mTextArea->font());
       
   129 	font.setPixelSize(10);
       
   130 	mTextArea->setFont(font);
       
   131 	
       
   132 	layout->addWidget(mTextArea);
       
   133 	
       
   134     window->setLayout(layout);
       
   135     setCentralWidget(window);
       
   136     window->show();;
       
   137 
       
   138     mappingPtr = new Mapping();
       
   139     
       
   140     TX_EXIT
       
   141 }
       
   142 	
       
   143 KeyCaptureTestApp::~KeyCaptureTestApp()
       
   144 {
       
   145     delete mappingPtr;
       
   146 }
       
   147 
       
   148 void KeyCaptureTestApp::triggered(QAction* aAction)
       
   149 {
       
   150     CaptureRequest request(mappingPtr);
       
   151     if (!request.setType(aAction)){
       
   152         return;
       
   153     }
       
   154     if (!request.setKey(mKeysMenu->exec(), &mKeysMap)){
       
   155         return;
       
   156     }
       
   157 //TODO: add modifiersMap selection menu;
       
   158 //TODO: add modifier selection menu;
       
   159     if ( request.mRequestType == CaptureRequest::RequestTypeLongKey || request.mRequestType == CaptureRequest::RequestTypeCancelLongKey){
       
   160         if (!request.setLongFlags(mLongFlagsMenu->exec(), &mLongFlagsMap)){
       
   161             return;
       
   162         }
       
   163     }
       
   164     procesAction(request);
       
   165 }
       
   166 
       
   167 void KeyCaptureTestApp::cleanLog()
       
   168 {
       
   169     mTextArea->setPlainText("");
       
   170 }
       
   171 
       
   172 void KeyCaptureTestApp::procesAction(CaptureRequest request)
       
   173 {
       
   174     TX_ENTRY
       
   175     switch (request.mRequestType) {
       
   176         case CaptureRequest::RequestTypeKey :
       
   177             mKeyCapture->captureKey(request.mKey, request.mModifiersMap, request.mModifier );
       
   178             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   179             break;
       
   180         case CaptureRequest::RequestTypeLongKey :
       
   181             mKeyCapture->captureLongKey(request.mKey, request.mModifiersMap, request.mModifier, request.mLongFlags);
       
   182             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   183             break;
       
   184         case CaptureRequest::RequestTypeKeyUpAndDowns :
       
   185             mKeyCapture->captureKeyUpAndDowns(request.mKey, request.mModifiersMap, request.mModifier );
       
   186             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   187             break;
       
   188         case CaptureRequest::RequestTypeCancelKey :
       
   189             mKeyCapture->cancelCaptureKey(request.mKey, request.mModifiersMap, request.mModifier );
       
   190             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   191             break;
       
   192         case CaptureRequest::RequestTypeCancelLongKey :
       
   193             mKeyCapture->cancelCaptureLongKey(request.mKey, request.mModifiersMap, request.mModifier, request.mLongFlags);
       
   194             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   195             break;
       
   196         case CaptureRequest::RequestTypeCancelKeyUpAndDowns :
       
   197             mKeyCapture->cancelCaptureKeyUpAndDowns(request.mKey, request.mModifiersMap, request.mModifier );
       
   198             addTextLine(QString("%1:%2\n").arg(request.toString()).arg(mKeyCapture->errorString()));
       
   199             break;
       
   200         default:
       
   201             break;
       
   202     }
       
   203 
       
   204     TX_EXIT    
       
   205 }
       
   206 
       
   207 void KeyCaptureTestApp::addTextLine(QString aText)
       
   208 {
       
   209     TX_LOG_ARGS( QString("aText=%1").arg(aText));
       
   210 	if ( !aText.endsWith("\n"))
       
   211 		aText = aText + "\n";
       
   212 	QString msg = mTextArea->toPlainText();
       
   213 	msg = aText + msg;
       
   214 	mTextArea->setPlainText(msg);
       
   215 }
       
   216 
       
   217 bool KeyCaptureTestApp::event(QEvent *ev)
       
   218 {
       
   219     processEvent(ev);
       
   220     return QMainWindow::event(ev);
       
   221 }
       
   222 
       
   223 bool KeyCaptureTestApp::eventFilter(QObject *o, QEvent *ev)
       
   224 {
       
   225     processEvent(ev);
       
   226     return QMainWindow::eventFilter(o, ev);
       
   227 }
       
   228 
       
   229 void KeyCaptureTestApp::processEvent(QEvent *ev)
       
   230 {
       
   231     if (ev){
       
   232         if (ev->type() == QEvent::KeyPress){
       
   233            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
       
   234            QString keyName = mappingPtr->name(static_cast<Qt::Key>(keyEvent->key())); 
       
   235            
       
   236            addTextLine(QString("KeyPress:%1\n").arg(keyName));
       
   237         } else if (ev->type() == QEvent::KeyRelease){
       
   238            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
       
   239            QString keyName = mappingPtr->name(static_cast<Qt::Key>(keyEvent->key()));
       
   240            
       
   241            addTextLine(QString("KeyRelease:%1\n").arg(keyName));
       
   242         } else if (ev->type() == XQKeyCapture::remoteEventType_KeyPress()){
       
   243            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
       
   244                       
       
   245            QString keyName = mappingPtr->name(static_cast<Qt::Key>(keyEvent->key()));
       
   246                       
       
   247            addTextLine(QString("KeyPress:%1\n").arg(keyName));
       
   248            addTextLine(QString("Native virtual key:%1\n").arg((int)keyEvent->nativeVirtualKey()));
       
   249         } else if (ev->type() == XQKeyCapture::remoteEventType_KeyRelease()){
       
   250            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
       
   251                       
       
   252            QString keyName = mappingPtr->name(static_cast<Qt::Key>(keyEvent->key()));
       
   253                       
       
   254            addTextLine(QString("KeyRelease:%1\n").arg(keyName));
       
   255            addTextLine(QString("Native virtual key:%1\n").arg((int)keyEvent->nativeVirtualKey()));
       
   256         }
       
   257     }
       
   258 }
       
   259 
       
   260 void KeyCaptureTestApp::enableRemBasic(bool enable)
       
   261 {
       
   262     if (enable) {
       
   263         addTextLine("Remote Basic enabled");
       
   264         QFlags<XQKeyCapture::CapturingFlag> flags = XQKeyCapture::CaptureNone;
       
   265         if (toggleRemoteExtEvents->isChecked())
       
   266             flags = XQKeyCapture::CaptureEnableRemoteExtEvents;
       
   267         if (toggleRemoteCallHandlingEx->isChecked())
       
   268             flags |= XQKeyCapture::CaptureCallHandlingExt;
       
   269         mKeyCapture->captureRemoteKeys(flags |= XQKeyCapture::CaptureBasic);
       
   270     } else {
       
   271         addTextLine("Remote Basic disabled");
       
   272         mKeyCapture->cancelCaptureRemoteKeys(XQKeyCapture::CaptureBasic);
       
   273     }
       
   274 }
       
   275 
       
   276 void KeyCaptureTestApp::enableRemCallHandlingEx(bool enable)
       
   277 {
       
   278     if (enable) {
       
   279         addTextLine("Remote Call Handling Ext. enabled");
       
   280         QFlags<XQKeyCapture::CapturingFlag> flags = XQKeyCapture::CaptureNone;
       
   281         if (toggleRemoteExtEvents->isChecked())
       
   282             flags = XQKeyCapture::CaptureEnableRemoteExtEvents;
       
   283         if (toggleRemoteBasic->isChecked())
       
   284             flags |= XQKeyCapture::CaptureBasic;
       
   285         mKeyCapture->captureRemoteKeys(flags | XQKeyCapture::CaptureCallHandlingExt);
       
   286     } else {
       
   287         addTextLine("Remote Call Handling Ext. disabled");
       
   288         mKeyCapture->cancelCaptureRemoteKeys(XQKeyCapture::CaptureCallHandlingExt);
       
   289     }
       
   290 }
       
   291 
       
   292 void KeyCaptureTestApp::enableRemoteExtEvents(bool enable)
       
   293 {
       
   294     if (enable) {
       
   295         addTextLine("Remote Events Ext. enabled");
       
   296         QFlags<XQKeyCapture::CapturingFlag> flags = XQKeyCapture::CaptureNone;
       
   297         if (toggleRemoteCallHandlingEx->isChecked())
       
   298             flags = XQKeyCapture::CaptureCallHandlingExt;
       
   299         if (toggleRemoteBasic->isChecked())
       
   300             flags |= XQKeyCapture::CaptureBasic;
       
   301         mKeyCapture->captureRemoteKeys(flags | XQKeyCapture::CaptureEnableRemoteExtEvents);
       
   302     } else {
       
   303         addTextLine("Remote Events Ext. disabled");
       
   304         mKeyCapture->cancelCaptureRemoteKeys(XQKeyCapture::CaptureEnableRemoteExtEvents);
       
   305     }
       
   306 }
       
   307 
       
   308 void KeyCaptureTestApp::remoteAll(bool enable)
       
   309 {
       
   310     Q_UNUSED(enable);
       
   311     toggleRemoteBasic->setChecked(true);
       
   312     toggleRemoteCallHandlingEx->setChecked(true);
       
   313     toggleRemoteExtEvents->setChecked(true);
       
   314     addTextLine("Remote: enable all");
       
   315     mKeyCapture->captureRemoteKeys(XQKeyCapture::CaptureCallHandlingExt |  XQKeyCapture::CaptureBasic | 
       
   316             XQKeyCapture::CaptureEnableRemoteExtEvents);
       
   317 }
       
   318 
       
   319 void KeyCaptureTestApp::remoteNone(bool enable)
       
   320 {
       
   321     Q_UNUSED(enable);
       
   322     toggleRemoteBasic->setChecked(false);
       
   323     toggleRemoteCallHandlingEx->setChecked(false);
       
   324     toggleRemoteExtEvents->setChecked(false);
       
   325     addTextLine("Remote: disable all");
       
   326     mKeyCapture->cancelCaptureRemoteKeys(XQKeyCapture::CaptureCallHandlingExt | XQKeyCapture::CaptureBasic | 
       
   327             XQKeyCapture::CaptureEnableRemoteExtEvents);
       
   328 }