symbianunittestui/qt/mainwindow.cpp
changeset 2 453d490c84a5
parent 1 753e33780645
child 3 6952856dc269
equal deleted inserted replaced
1:753e33780645 2:453d490c84a5
     1 /*
       
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: QT C++ main window Class.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QAction>
       
    19 #include <QMenuBar>
       
    20 #include <QMenu>
       
    21 #include <QGridLayout>
       
    22 #include <QTreeWidgetItem>
       
    23 #include <QKeyEvent>
       
    24 #include <QFile>
       
    25 #include <QLatin1String>
       
    26 #include <QApplication>
       
    27 #include "mainwindow.h"
       
    28 #include <symbianunittestversion.h>
       
    29 #include "dialogaddtests.h"
       
    30 #include "tabwidgettestrun.h"
       
    31 #include "dialogsettings.h"
       
    32 #include "dialogmsg.h"
       
    33 
       
    34 static const QString APPNAME = "symbianunittestqt";
       
    35 static const QString STYLESHEET = ":/qss/coffee.qss";
       
    36 //Symbian EKeyLeftArrow = 0x807
       
    37 static const int KeyLeftArrow = 63495;
       
    38 //Symbian EKeyRightArrow = 0x808
       
    39 static const int KeyRightArrow = 63496;
       
    40 //Symbian EKeyUpArrow = 0x809
       
    41 static const int KeyUpArrow = 63497;
       
    42 //Symbian EKeyDownArrow = 0x80a
       
    43 static const int KeyDownArrow = 63498;
       
    44 //Symbian EKeyEnter = 0x10
       
    45 static const int KeyEnter = 0x10;
       
    46 
       
    47 MainWindow::MainWindow(QWidget* parent) :
       
    48     QMainWindow(parent)
       
    49     {
       
    50     setTitle();
       
    51     QWidget* centralWidget;
       
    52     centralWidget = new QWidget(this);
       
    53     centralWidget->setContextMenuPolicy(Qt::NoContextMenu);
       
    54     setCentralWidget(centralWidget);
       
    55     load();
       
    56     }
       
    57 
       
    58 MainWindow::~MainWindow()
       
    59     {
       
    60 
       
    61     }
       
    62 
       
    63 void MainWindow::SetStyle(const QString& styleFile)
       
    64     {
       
    65     QFile file(styleFile);
       
    66     bool rst = file.open(QFile::ReadOnly);    
       
    67     if(rst)
       
    68         {
       
    69         QString styleSheet = QLatin1String(file.readAll());    
       
    70         qApp->setStyleSheet(styleSheet);    
       
    71         }
       
    72     file.close();
       
    73     }
       
    74 
       
    75 void MainWindow::popupDialogAddTests()
       
    76     {
       
    77     dlgAddTests->clear();
       
    78     dlgAddTests->showMaximized();
       
    79     }
       
    80 
       
    81 void MainWindow::popupDialogSettings()
       
    82     {
       
    83     dlgSettings->restoreSettings();
       
    84     dlgSettings->showMaximized();
       
    85     }
       
    86 
       
    87 void MainWindow::popupDialogAbout()
       
    88     {
       
    89     dlgAbout->showMsg(tr("%1 v%2.%3.%4").arg(APPNAME) .arg(
       
    90             SUT_MAJOR_VERSION) .arg(SUT_MINOR_VERSION).arg(SUT_BUILD_VERSION));
       
    91     }
       
    92 
       
    93 void MainWindow::setTitle()
       
    94     {
       
    95     this->setWindowTitle(tr("%1 v%2.%3.%4").arg(APPNAME) .arg(
       
    96             SUT_MAJOR_VERSION) .arg(SUT_MINOR_VERSION).arg(SUT_BUILD_VERSION));
       
    97     }
       
    98 
       
    99 void MainWindow::load()
       
   100     {
       
   101     createTabWidget();
       
   102     loadTabWidget();
       
   103     setupTabWidgetEventHandlers();
       
   104     createDialogs();
       
   105     setupDialogEventHandlers();
       
   106     createMenu();
       
   107     setupMenuEventHandlers();
       
   108     loadMenu();
       
   109     setupKeyEventHandlers();
       
   110     SetStyle(STYLESHEET);
       
   111     }
       
   112 
       
   113 void MainWindow::createMenu()
       
   114     {
       
   115     atnAddTests = new QAction(tr("Add Tests"), this);
       
   116     menuMarkUnmark = new QMenu(tr("Mark / Unmark"), this);
       
   117     menuMarkUnmark->setEnabled(false);
       
   118     atnMark = new QAction(tr("Mark"), menuMarkUnmark);
       
   119     atnMarkAll = new QAction(tr("Mark All"), menuMarkUnmark);
       
   120     atnUnmark = new QAction(tr("Unmark"), menuMarkUnmark);
       
   121     atnUnmarkAll = new QAction(tr("Unmark All"), menuMarkUnmark);
       
   122     atnRun = new QAction(tr("Run"), this);
       
   123     atnRun->setEnabled(false);
       
   124     atnSettings = new QAction(tr("Settings"), this);
       
   125     atnAbout = new QAction(tr("About"), this);
       
   126     atnExit = new QAction(tr("Exit"), this);
       
   127     }
       
   128 
       
   129 void MainWindow::setupMenuEventHandlers()
       
   130     {
       
   131     // add tests
       
   132     connect(atnAddTests, SIGNAL(triggered()), this,
       
   133             SLOT(popupDialogAddTests()));
       
   134     // Mark / Unmark single / all
       
   135     connect(atnMark, SIGNAL(triggered()), tabWdgtTestRun,
       
   136             SLOT(selectCurrentCase()));
       
   137     connect(atnMarkAll, SIGNAL(triggered()), tabWdgtTestRun,
       
   138             SLOT(selectAllCases()));
       
   139     connect(atnUnmark, SIGNAL(triggered()), tabWdgtTestRun,
       
   140             SLOT(deselectCurrentCase()));
       
   141     connect(atnUnmarkAll, SIGNAL(triggered()), tabWdgtTestRun,
       
   142             SLOT(deselectAllCases()));
       
   143     // Run tests
       
   144     connect(atnRun, SIGNAL(triggered()), tabWdgtTestRun, SLOT(runTests()));
       
   145     // set options to run cases
       
   146     connect(atnSettings, SIGNAL(triggered()), this,
       
   147             SLOT(popupDialogSettings()));
       
   148 
       
   149     connect(atnAbout, SIGNAL(triggered()), this, SLOT(popupDialogAbout()));
       
   150     connect(atnExit, SIGNAL(triggered()), this, SLOT(close()));
       
   151     }
       
   152 
       
   153 void MainWindow::createTabWidget()
       
   154     {
       
   155     tabWdgtTestRun = new TabWidgetTestRun(this);
       
   156     }
       
   157 
       
   158 void MainWindow::loadMenu()
       
   159     {
       
   160     menuBar()->clear();
       
   161     menuBar()->addAction(atnAddTests);
       
   162     menuMarkUnmark->addAction(atnMark);
       
   163     menuMarkUnmark->addAction(atnMarkAll);
       
   164     menuMarkUnmark->addAction(atnUnmark);
       
   165     menuMarkUnmark->addAction(atnUnmarkAll);
       
   166     menuBar()->addAction(menuMarkUnmark->menuAction());
       
   167     menuBar()->addAction(atnRun);
       
   168     menuBar()->addAction(atnSettings);
       
   169     menuBar()->addAction(atnAbout);
       
   170     menuBar()->addAction(atnExit);
       
   171     }
       
   172 
       
   173 void MainWindow::loadTabWidget()
       
   174     {
       
   175     QGridLayout* tabLayout = new QGridLayout(this);
       
   176     tabLayout->setVerticalSpacing(2);
       
   177     tabLayout->setHorizontalSpacing(2);
       
   178     tabLayout->setSpacing(2);
       
   179     tabLayout->setMargin(2);
       
   180     tabLayout->addWidget(tabWdgtTestRun, 0, 0);
       
   181     centralWidget()->setLayout(tabLayout);
       
   182     }
       
   183 
       
   184 void MainWindow::setupTabWidgetEventHandlers()
       
   185     {
       
   186     connect(tabWdgtTestRun, SIGNAL(testsAdded()), this,
       
   187             SLOT(enableMenuMarkUnmark()));
       
   188     connect(tabWdgtTestRun, SIGNAL(selectedTestsChanged(bool, bool, bool)), this,
       
   189             SLOT(setRunMarkUnmarkEnabled(bool, bool, bool)));
       
   190     // current tab changed
       
   191     connect(tabWdgtTestRun, SIGNAL(currentTabChangedToTests(bool)), this,
       
   192             SLOT(changCurrentTabToTests(bool)));
       
   193     connect(tabWdgtTestRun, SIGNAL(currentTabChangedToExecution()), this,
       
   194             SLOT(changCurrentTabToExecution()));
       
   195     }
       
   196 
       
   197 void MainWindow::createDialogs()
       
   198     {
       
   199     dlgAddTests = new DialogAddTests(this);
       
   200     dlgSettings = new DialogSettings(this);
       
   201     dlgAbout = new DialogMsg(this);
       
   202     }
       
   203 
       
   204 void MainWindow::setupDialogEventHandlers()
       
   205     {
       
   206     connect(dlgAddTests, SIGNAL(testsSaved(QStringList)), tabWdgtTestRun,
       
   207             SLOT(addTests(QStringList)));
       
   208     connect(dlgSettings, SIGNAL(settingsSaved(const Settings*)),
       
   209             tabWdgtTestRun, SLOT(saveSettings(const Settings*)));
       
   210     }
       
   211 
       
   212 void MainWindow::changCurrentTabToTests(bool hasTests)
       
   213     {
       
   214     if (hasTests)
       
   215         {
       
   216         menuMarkUnmark->setEnabled(true);
       
   217         }
       
   218     }
       
   219 
       
   220 void MainWindow::changCurrentTabToExecution()
       
   221     {
       
   222     menuMarkUnmark->setEnabled(false);
       
   223     }
       
   224 
       
   225 void MainWindow::setRunMarkUnmarkEnabled(bool noneSelected, bool allSelected, bool curSelected)
       
   226     {
       
   227     atnRun->setEnabled(!noneSelected);
       
   228     atnUnmarkAll->setEnabled(!noneSelected);
       
   229     atnUnmark->setEnabled(curSelected);
       
   230     atnMark->setEnabled(!curSelected);
       
   231     atnMarkAll->setEnabled(noneSelected || !allSelected);
       
   232     }
       
   233 
       
   234 void MainWindow::enableMenuMarkUnmark()
       
   235     {
       
   236     menuMarkUnmark->setEnabled(true);
       
   237     atnMarkAll->setEnabled(true);
       
   238     atnMark->setEnabled(true);
       
   239     atnUnmark->setEnabled(false);
       
   240     atnUnmarkAll->setEnabled(false);
       
   241     }
       
   242 
       
   243 void MainWindow::keyPressEvent(QKeyEvent* event)
       
   244     {
       
   245     // Handle arrow keys and selection key events
       
   246     switch (event->nativeVirtualKey())
       
   247         {
       
   248         case KeyLeftArrow:
       
   249             {
       
   250             // TODO
       
   251             emit this->leftArrowPressed();
       
   252             break;
       
   253             }
       
   254         case KeyRightArrow:
       
   255             {
       
   256             // TODO
       
   257             emit this->rightArrowPressed();
       
   258             break;
       
   259             }
       
   260         case KeyUpArrow:
       
   261             {
       
   262             // TODO
       
   263             emit this->upArrowPressed();
       
   264             break;
       
   265             }
       
   266         case KeyDownArrow:
       
   267             {
       
   268             // TODO
       
   269             emit this->downArrowPressed();
       
   270             break;
       
   271             }
       
   272         case KeyEnter:
       
   273             {
       
   274             // TODO
       
   275             emit this->enterPressed();
       
   276             break;
       
   277             }
       
   278         default:
       
   279             {
       
   280             break;
       
   281             }
       
   282         }
       
   283     }
       
   284 
       
   285 void MainWindow::setupKeyEventHandlers()
       
   286     {
       
   287     connect(this, SIGNAL(leftArrowPressed()), tabWdgtTestRun,
       
   288             SLOT(pressLeftArrow()));
       
   289     connect(this, SIGNAL(rightArrowPressed()), tabWdgtTestRun,
       
   290             SLOT(pressRightArrow()));
       
   291     connect(this, SIGNAL(upArrowPressed()), tabWdgtTestRun,
       
   292             SLOT(pressUpArrow()));
       
   293     connect(this, SIGNAL(downArrowPressed()), tabWdgtTestRun,
       
   294             SLOT(pressDownArrow()));
       
   295     connect(this, SIGNAL(enterPressed()), tabWdgtTestRun, SLOT(pressEnter()));
       
   296     }