loadgen/ui/hb/src/mainview.cpp
changeset 17 4f2773374eff
child 31 e7a04a6385be
equal deleted inserted replaced
15:e11368ed4880 17:4f2773374eff
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mainview.h"
       
    19 #include "enginewrapper.h"
       
    20 #include "notifications.h"
       
    21 #include "loadgen.hrh"
       
    22 #include <hblabel.h>
       
    23 #include <hbmenu.h>
       
    24 #include <hbaction.h>
       
    25 #include <hbmessagebox.h>
       
    26 #include <hbmainwindow.h>
       
    27 #include <hbapplication.h>
       
    28 //#include <hbcommonnote.h>
       
    29 //#include <hbconfirmationquery.h>
       
    30 #include <hbprogressdialog.h>
       
    31 #include <hbtextedit.h>
       
    32 #include <hblistview.h>
       
    33 #include <hbselectiondialog.h>
       
    34 
       
    35 #include <QGraphicsLinearLayout>
       
    36 #include <QStandardItemModel>
       
    37 #include <QStandardItem>
       
    38 #include <QSignalMapper>
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 
       
    42 MainView::MainView(HbMainWindow &mainWindow) :
       
    43 					  mMainWindow(mainWindow),
       
    44 					  mEngineWrapper(0),
       
    45 					  mIsLoadListEmpty(true),
       
    46 					  mStopAll(NULL),
       
    47 					  mSuspendAll(NULL),
       
    48 					  mResumeAll(NULL)
       
    49 					  
       
    50 {
       
    51 }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 
       
    55 MainView::~MainView()
       
    56 {
       
    57     if (mEngineWrapper != 0) {
       
    58         delete mEngineWrapper;
       
    59     }
       
    60     delete mListModel;    
       
    61     delete mListView;
       
    62 	delete mSm;	
       
    63 }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 
       
    67 void MainView::init(HbApplication &app)
       
    68 {
       
    69 	this->setTitle("Load Gener.");
       
    70     mEngineWrapper = new EngineWrapper(mMainWindow, *this);
       
    71     int error = mEngineWrapper->init();
       
    72     Q_ASSERT_X(error == 1, "LoadGen", "Engine initialization failed");
       
    73     createMenu(app);
       
    74 	loadListIinit();
       
    75 }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 
       
    79 void MainView::createMenu(HbApplication &app)
       
    80 {
       
    81     HbMenu *menu = this->menu();
       
    82 	
       
    83     // signal mapper for identifying the different command actions  
       
    84 	// in executeCommand() handler function:
       
    85     mSm = new QSignalMapper(this);
       
    86     connect(mSm, SIGNAL(mapped(int)), this, SLOT(executeMenuCommand(int)));
       
    87 
       
    88     if (menu != NULL) {
       
    89 		// Temp work-around for menu item bug in Orbit Wk38,
       
    90 		// sub menu cannot contain more than 6 functional action items!
       
    91 		// two sub menus created to fix this problem.
       
    92 		mLoadSubMenu1 = menu->addMenu("New loads");
       
    93 		mLoadSubMenu2 = menu->addMenu("More new loads");
       
    94 		mCpuLoadAction = mLoadSubMenu1->addAction("CPU load");
       
    95 		connect(mCpuLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
    96 		mSm->setMapping(mCpuLoadAction, ELoadGenCmdNewLoadCPULoad);
       
    97 
       
    98 		mMemoryEatLoadAction = mLoadSubMenu1->addAction("Eat memory");
       
    99 		connect(mMemoryEatLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   100 		mSm->setMapping(mMemoryEatLoadAction, ELoadGenCmdNewLoadEatMemory);
       
   101 
       
   102 		mPhoneCallLoadAction = mLoadSubMenu1->addAction("Phone calls");
       
   103 		connect(mPhoneCallLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   104 		mSm->setMapping(mPhoneCallLoadAction, ELoadGenCmdNewLoadPhoneCall);
       
   105 
       
   106 		mMessagesLoadAction = mLoadSubMenu1->addAction("Messages");
       
   107 		connect(mMessagesLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   108 		mSm->setMapping(mMessagesLoadAction, ELoadGenCmdNewLoadMessages);
       
   109 
       
   110 		mNetConnLoadAction = mLoadSubMenu1->addAction("Network conn.");
       
   111 		connect(mNetConnLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   112 		mSm->setMapping(mNetConnLoadAction, ELoadGenCmdNewLoadNetConn);
       
   113 	
       
   114 		mKeyPressLoadAction = mLoadSubMenu2->addAction("Key presses");
       
   115 		connect(mKeyPressLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   116 		mSm->setMapping(mKeyPressLoadAction, ELoadGenCmdNewLoadKeyPress);
       
   117 
       
   118 		mAppsLoadAction = mLoadSubMenu2->addAction("Applications");
       
   119 		connect(mAppsLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   120 		mSm->setMapping(mAppsLoadAction, ELoadGenCmdNewLoadApplications);
       
   121 		
       
   122 		mPhotoCapturesLoadAction = mLoadSubMenu2->addAction("Photo captures");
       
   123 		connect(mPhotoCapturesLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   124 		mSm->setMapping(mPhotoCapturesLoadAction, ELoadGenCmdNewLoadPhotoCaptures);
       
   125 
       
   126 		mBtActionsLoadAction = mLoadSubMenu2->addAction("Bluetooth actions");
       
   127 		connect(mBtActionsLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   128 		mSm->setMapping(mBtActionsLoadAction, ELoadGenCmdNewLoadBluetooth);
       
   129 
       
   130 		mPointerEventsLoadAction = mLoadSubMenu2->addAction("Pointer events");
       
   131 		connect(mPointerEventsLoadAction, SIGNAL( triggered() ), mSm, SLOT(map()));
       
   132 		mSm->setMapping(mPointerEventsLoadAction, ELoadGenCmdNewLoadPointerEvent);
       
   133 		
       
   134 		mActionPerfMon = menu->addAction("Launch PerfMon");
       
   135 		connect(mActionPerfMon, SIGNAL(triggered()), this, SLOT( launchPerfMon()));
       
   136 		
       
   137 		mActionAbout = menu->addAction("About");
       
   138 		connect(mActionAbout, SIGNAL(triggered()), this, SLOT( showAboutPopup()));
       
   139 
       
   140 		mActionExit = menu->addAction("Exit");
       
   141 		connect(mActionExit, SIGNAL(triggered()), &app, SLOT(quit()));
       
   142 
       
   143 		// menu dynamic update
       
   144 		connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));        		
       
   145 	}
       
   146 }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 
       
   150 void MainView::loadListIinit()
       
   151 {
       
   152     // Create list view and model for it
       
   153     mListView = new HbListView(this);
       
   154 	mListModel = new QStandardItemModel(this);
       
   155 	mListView->setModel(mListModel); 
       
   156 	mListView->setSelectionMode(HbListView::NoSelection);
       
   157 	mListView->setVisible( false ); //HighlightMode(HbItemHighlight::Hidden);
       
   158 
       
   159 	QStandardItem *item = new QStandardItem(QString("No loads activated. (Select new load from menu to generate new load)"));
       
   160     mListModel->setItem(0, item);
       
   161 
       
   162     connect(mListView, 
       
   163 			SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), 
       
   164 			this, 
       
   165 			SLOT(handleLoadListEvent(HbAbstractViewItem*, QPointF)));	
       
   166 
       
   167 	
       
   168     // Create layout and add list view there:
       
   169     QGraphicsLinearLayout *mainlayout = new QGraphicsLinearLayout(Qt::Vertical, this);
       
   170     mainlayout->addItem(mListView);
       
   171 	mainlayout->setAlignment(mListView, Qt::AlignCenter);
       
   172     setLayout(mainlayout);
       
   173 }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 
       
   177 void MainView::showAboutPopup()
       
   178 {
       
   179     Notifications::about();
       
   180 }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 
       
   184 void MainView::executeMenuCommand(int cmd)
       
   185 {
       
   186 	mEngineWrapper->startNewLoad(cmd);
       
   187 }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 
       
   191 void MainView::handleLoadListEvent(HbAbstractViewItem */*listViewItem*/, const QPointF &/*coords*/)
       
   192 {
       
   193     const QStringList items = (QStringList() << "Stop" << "Resume/Suspend" << "Edit");
       
   194     HbSelectionDialog *dlg = new HbSelectionDialog();
       
   195     dlg->setAttribute(Qt::WA_DeleteOnClose);
       
   196     dlg->setStringItems(items);
       
   197     dlg->setSelectionMode(HbAbstractItemView::SingleSelection);
       
   198     dlg->open(this, SLOT(ItemActionPopupClosed(HbAction*)));
       
   199 }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 
       
   203 void MainView::ItemActionPopupClosed(HbAction* action)
       
   204 {
       
   205     HbSelectionDialog *dlg = static_cast<HbSelectionDialog*>(sender());
       
   206     if(!action && dlg->selectedItems().count()){
       
   207         int userSelectionIndex = dlg->selectedItems().at(0).toInt();   
       
   208         // all operations for selected list item only. Query selected load list item index from model.
       
   209         if (userSelectionIndex == 0) {
       
   210             // stop selected load from the load list:
       
   211             stopLoad(false);
       
   212         }
       
   213         else if (userSelectionIndex == 1) {
       
   214             // Suspend or Resume
       
   215             mEngineWrapper->suspendOrResumeSelectedOrHighlightedItems();
       
   216         }
       
   217         else {
       
   218             // current selected row number from the load list. 
       
   219             int selectedRow = mListView->selectionModel()->currentIndex().row();
       
   220             // edit load setttings:
       
   221             mEngineWrapper->loadEdit(selectedRow);
       
   222         }
       
   223     }
       
   224 }
       
   225 
       
   226 // --------------------------------------------------------------------------
       
   227 
       
   228 void MainView::stopAllLoads()
       
   229 {
       
   230 	stopLoad(true);
       
   231 }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 
       
   235 bool MainView::stopLoad(bool stopAll)
       
   236 {
       
   237 	int success = false;
       
   238 	if (mListView != NULL) {
       
   239 		if (stopAll == true) {
       
   240 			success = mEngineWrapper->stopLoad(stopAll);
       
   241 			mIsLoadListEmpty = true;
       
   242 		}
       
   243 		else {
       
   244 			success = mEngineWrapper->stopLoad(stopAll);
       
   245 		}
       
   246 	}
       
   247 	return success;
       
   248 }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 
       
   252 void MainView::updateMenu()
       
   253 {
       
   254 	if (mIsLoadListEmpty != true) {
       
   255 		if (mStopAll == NULL) {
       
   256 			mStopAll = new HbAction("Stop all", menu());
       
   257 			connect(mStopAll, SIGNAL(triggered()), this, SLOT(stopAllLoads()));
       
   258 			menu()->insertAction(mActionAbout, mStopAll);
       
   259 		}
       
   260 		else{
       
   261             mStopAll->setVisible(true);
       
   262 		}
       
   263 		if (mSuspendAll == NULL) {
       
   264 			mSuspendAll = new HbAction("Suspend all", menu());
       
   265 			connect(mSuspendAll, SIGNAL(triggered()), this, SLOT(suspendAllLoads()));
       
   266 			menu()->insertAction(mActionAbout, mSuspendAll);
       
   267 		}
       
   268 		else{
       
   269             mSuspendAll->setVisible(true);
       
   270         }
       
   271 		if (mResumeAll == NULL) {
       
   272 			mResumeAll = new HbAction("Resume all", menu());
       
   273 			connect(mResumeAll, SIGNAL(triggered()), this, SLOT(resumeAllLoads()));
       
   274 			menu()->insertAction(mActionAbout, mResumeAll);
       
   275 		}
       
   276 		else{
       
   277             mResumeAll->setVisible(true);
       
   278         }
       
   279 	}
       
   280 	else if (mIsLoadListEmpty == true) {
       
   281 		if (mStopAll != NULL && mStopAll->text() == "Stop all") {
       
   282             mStopAll->setVisible(false);
       
   283 		}
       
   284 		if (mSuspendAll != NULL && mSuspendAll->text() == "Suspend all") {
       
   285             mSuspendAll->setVisible(false);
       
   286 		}
       
   287 		if (mResumeAll != NULL && mResumeAll->text() == "Resume all") {
       
   288             mSuspendAll->setVisible(false);
       
   289 		}		
       
   290 	}
       
   291 }
       
   292 
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 
       
   296 int MainView::currentItemIndex()
       
   297 {
       
   298 	return mListView->selectionModel()->currentIndex().row();
       
   299 }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 
       
   303 void MainView::setCurrentItemIndex(int index) 
       
   304 {
       
   305     QModelIndex newIndex = mListView->model()->index(index, 0);
       
   306     mListView->selectionModel()->setCurrentIndex(newIndex, 
       
   307                                                 QItemSelectionModel::SelectCurrent);
       
   308 	//mListView->setCurrentIndex( index ); //setCurrentRow(index);
       
   309 }	
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 
       
   313 void MainView::launchPerfMon()
       
   314 {
       
   315 	mEngineWrapper->launchPerfMonApp();
       
   316 }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 
       
   320 QList<int> MainView::listSelectionIndexes()
       
   321     {
       
   322 	QList<int> indices;
       
   323     if (mListView != NULL) {
       
   324 		QItemSelectionModel *selectionModel = mListView->selectionModel();
       
   325 		if (selectionModel->hasSelection()) {
       
   326 			QModelIndexList modelIndexes = selectionModel->selectedIndexes();
       
   327 			QModelIndex index;
       
   328 			foreach (index, modelIndexes) {
       
   329 				indices.append(index.row());
       
   330 			}
       
   331 		}
       
   332     }
       
   333     return indices;
       
   334 	}
       
   335 
       
   336 // --------------------------------------------------------------------------------------------
       
   337 
       
   338 void MainView::suspendAllLoads()
       
   339 {
       
   340 	mEngineWrapper->suspendAllLoadItems();
       
   341 }
       
   342 
       
   343 // ---------------------------------------------------------------------------
       
   344 	
       
   345 void MainView::resumeAllLoads()
       
   346 {
       
   347 	mEngineWrapper->resumeAllLoadItems();
       
   348 }
       
   349 
       
   350 void MainView::clearListSelection()
       
   351 {
       
   352     if (mListView != NULL) {
       
   353 		mListView->clearSelection();
       
   354 	}
       
   355 }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 
       
   359 void MainView::setLoadListData(QStringList& items)
       
   360 {
       
   361 	QString loadItem;
       
   362 	QString iconName;
       
   363 
       
   364 	if (mListView) {
       
   365 		mListView->setSelectionMode(HbListView::MultiSelection);
       
   366 		mListView->setVisible(true); //setHighlightMode(HbItemHighlight::AlwaysVisible);
       
   367 		if(mListModel->rowCount())
       
   368 			{
       
   369 			mListModel->removeRows(0,mListModel->rowCount());
       
   370 			}
       
   371 //		mListModel->clear();
       
   372 	}
       
   373 	if (items.count() > 0) {
       
   374 		mIsLoadListEmpty = false;
       
   375 	}
       
   376 	else if (items.count() == 0) {
       
   377 		mIsLoadListEmpty = true;
       
   378 	}
       
   379 	for (int i = 0; i < items.count(); i++) {
       
   380 		QStandardItem *item = new QStandardItem();
       
   381 		loadItem = items.at(i);
       
   382 		if (loadItem.startsWith("1")) {
       
   383 			iconName = ":/paused.svg";
       
   384 		}
       
   385 		else {
       
   386 			iconName = ":/active.svg";
       
   387 		}
       
   388 		// remove icon indicator info from string
       
   389 		// to show in load in load list
       
   390 		loadItem.remove(0,2); 
       
   391 		item->setText(loadItem);
       
   392 		item->setIcon(QIcon(iconName));
       
   393 		// set new load to correct row in load list
       
   394 		mListModel->insertRow(i, item);
       
   395 	}
       
   396 }	
       
   397 
       
   398 // ---------------------------------------------------------------------------