memspyui/ui/hb/src/memspythreadview.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     1 /*
       
     2  * Copyright (c) 2009 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 <HbAbstractViewItem>
       
    19 #include <HbMenu>
       
    20 #include <QAction>
       
    21 
       
    22 #include "memspythreadview.h"
       
    23 #include "viewmanager.h"
       
    24 
       
    25 MemSpyThreadModel::MemSpyThreadModel(EngineWrapper &engine, ProcessId processId, QObject *parent) :
       
    26 	QAbstractListModel(parent),
       
    27 	mProcessId(processId),
       
    28 	mEngine(engine),
       
    29 	mThreads(engine.getThreads(processId))
       
    30 {
       
    31 	mPriorityMap.insert(ThreadPriorityNull, tr("[Null]"));
       
    32 	mPriorityMap.insert(ThreadPriorityMuchLess, tr("[Much Less]"));
       
    33 	mPriorityMap.insert(ThreadPriorityLess, tr("[Less]"));
       
    34 	mPriorityMap.insert(ThreadPriorityNormal, tr("[Normal]"));
       
    35 	mPriorityMap.insert(ThreadPriorityMore, tr("[More]"));
       
    36 	mPriorityMap.insert(ThreadPriorityMuchMore, tr("[Much More]"));
       
    37 	mPriorityMap.insert(ThreadPriorityRealTime, tr("[Real Time]"));
       
    38 	mPriorityMap.insert(ThreadPriorityAbsoluteVeryLow, tr("[Abs. Very Low]"));
       
    39 	mPriorityMap.insert(ThreadPriorityAbsoluteLowNormal, tr("[Abs. Low Normal]"));
       
    40 	mPriorityMap.insert(ThreadPriorityAbsoluteLow, tr("[Abs. Low]"));
       
    41 	mPriorityMap.insert(ThreadPriorityAbsoluteBackgroundNormal, tr("[Abs. Background Normal]"));
       
    42 	mPriorityMap.insert(ThreadPriorityAbsoluteBackground, tr("[Abs. Background]"));
       
    43 	mPriorityMap.insert(ThreadPriorityAbsoluteForegroundNormal, tr("[Abs. Foreground Normal]"));
       
    44 	mPriorityMap.insert(ThreadPriorityAbsoluteForeground, tr("[Abs. Foreground]"));
       
    45 	mPriorityMap.insert(ThreadPriorityAbsoluteHighNormal, tr("[Abs. Hight Normal]"));
       
    46 	mPriorityMap.insert(ThreadPriorityAbsoluteHigh, tr("[Abs. High]"));
       
    47 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime1, tr("[Abs. RT 1]"));
       
    48 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime2, tr("[Abs. RT 2]"));
       
    49 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime3, tr("[Abs. RT 3]"));
       
    50 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime4, tr("[Abs. RT 4]"));
       
    51 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime5, tr("[Abs. RT 5]"));
       
    52 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime6, tr("[Abs. RT 6]"));
       
    53 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime7, tr("[Abs. RT 7]"));
       
    54 	mPriorityMap.insert(ThreadPriorityAbsoluteRealTime8, tr("[Abs. RT 8]"));
       
    55 }
       
    56 
       
    57 MemSpyThreadModel::~MemSpyThreadModel()
       
    58 {
       
    59 	qDeleteAll(mThreads);
       
    60 }
       
    61 	
       
    62 int MemSpyThreadModel::rowCount(const QModelIndex &parent) const
       
    63 {
       
    64 	Q_UNUSED(parent);
       
    65 	return mThreads.count();
       
    66 }
       
    67 	
       
    68 QVariant MemSpyThreadModel::data(const QModelIndex &index, int role) const
       
    69 {
       
    70 	if (role == Qt::DisplayRole) {
       
    71 		QStringList lines;
       
    72 		lines << mThreads.at(index.row())->name();
       
    73 		lines << mPriorityMap.value(mThreads.at(index.row())->priority(), tr("[Unknown]"));
       
    74 		return lines;
       
    75 	}
       
    76 	
       
    77 	if (role == Qt::UserRole)
       
    78 		return mThreads.at(index.row())->id();
       
    79 	
       
    80 	return QVariant();
       
    81 }
       
    82 
       
    83 void MemSpyThreadModel::refresh()
       
    84 {
       
    85     beginResetModel();
       
    86     QList<MemSpyThread*> data = mEngine.getThreads(mProcessId);
       
    87     qDeleteAll(mThreads);
       
    88     mThreads = data;
       
    89     endResetModel();
       
    90 }
       
    91 
       
    92 MemSpyThreadView::MemSpyThreadView(EngineWrapper &engine, ViewManager &viewManager) : 
       
    93 	MemSpyListView(engine, viewManager), 
       
    94 	mContextMenu(0), 
       
    95 	mPriorityMenu(0),
       
    96 	mThreadId(0),
       
    97 	mModel(0)
       
    98 {
       
    99 }
       
   100 
       
   101 MemSpyThreadView::~MemSpyThreadView()
       
   102 {
       
   103 	delete mContextMenu;
       
   104 	delete mPriorityMenu;
       
   105 }
       
   106 
       
   107 void MemSpyThreadView::initialize(const QVariantMap& params)
       
   108 {
       
   109 	ProcessId pid = qVariantValue<ProcessId>(params["pid"]);
       
   110 	setTitle(tr("Threads"));
       
   111 	
       
   112 	mProcessName = params["pname"].toString();
       
   113 	
       
   114 	MemSpyView::initialize(params);
       
   115 	
       
   116 	mModel = new MemSpyThreadModel(mEngine, pid, this);
       
   117 	mListView.setModel(mModel);
       
   118 	
       
   119 	connect(&mListView, SIGNAL(activated(QModelIndex)), this, SLOT(itemClicked(QModelIndex)));
       
   120 	connect(&mListView, SIGNAL(longPressed(HbAbstractViewItem*,QPointF)),
       
   121 	        this, SLOT(catchLongPress(HbAbstractViewItem*,QPointF)));
       
   122 	
       
   123 	mContextMenu = new HbMenu;
       
   124 	mPriorityMenu = mContextMenu->addMenu("Change Priority");
       
   125 	
       
   126 	mPriorityMenu->addAction(tr("Abs. Very Low"), this, SLOT(changePriority()));
       
   127 	mPriorityMenu->addAction(tr("Abs. Low Normal"), this, SLOT(changePriority()));
       
   128 	mPriorityMenu->addAction(tr("Abs. Low"), this, SLOT(changePriority()));
       
   129 	mPriorityMenu->addAction(tr("Abs. Background Normal"), this, SLOT(changePriority()));
       
   130 	mPriorityMenu->addAction(tr("Abs. Background"), this, SLOT(changePriority()));
       
   131 	mPriorityMenu->addAction(tr("Abs. Foreground Normal"), this, SLOT(changePriority()));
       
   132 	mPriorityMenu->addAction(tr("Abs. Foreground"), this, SLOT(changePriority()));
       
   133 	mPriorityMenu->addAction(tr("Abs. High Normal"), this, SLOT(changePriority()));
       
   134 	mPriorityMenu->addAction(tr("Abs. High"), this, SLOT(changePriority()));
       
   135 	mPriorityMenu->addAction(tr("Abs. Real Time 1"), this, SLOT(changePriority()));
       
   136 	mPriorityMenu->addAction(tr("Abs. Real Time 2"), this, SLOT(changePriority()));
       
   137 	mPriorityMenu->addAction(tr("Abs. Real Time 3"), this, SLOT(changePriority()));
       
   138 	mPriorityMenu->addAction(tr("Abs. Real Time 4"), this, SLOT(changePriority()));
       
   139 	mPriorityMenu->addAction(tr("Abs. Real Time 5"), this, SLOT(changePriority()));
       
   140 	mPriorityMenu->addAction(tr("Abs. Real Time 6"), this, SLOT(changePriority()));
       
   141 	mPriorityMenu->addAction(tr("Abs. Real Time 7"), this, SLOT(changePriority()));
       
   142 	mPriorityMenu->addAction(tr("Abs. Real Time 8"), this, SLOT(changePriority()));
       
   143 }
       
   144 
       
   145 
       
   146 bool MemSpyThreadView::isBreadCrumbVisible() const
       
   147 {
       
   148     return true;
       
   149 }
       
   150 
       
   151 QString MemSpyThreadView::getBreadCrumbText() const
       
   152 {
       
   153     return tr("Processes > %1").arg(mProcessName);
       
   154 }
       
   155 
       
   156 void MemSpyThreadView::itemClicked(const QModelIndex& index)
       
   157 {
       
   158 	QVariantMap map;
       
   159 	map["tid"] = index.data(Qt::UserRole);
       
   160 	map.insert("pname", mProcessName);
       
   161 	map.insert("tname", index.data(Qt::DisplayRole).toStringList().at(0));
       
   162 	
       
   163 	mViewManager.showView(ThreadDetailIndexView, map);
       
   164 }
       
   165 
       
   166 void MemSpyThreadView::catchLongPress(HbAbstractViewItem *item, const QPointF &coords)
       
   167 {
       
   168 	mThreadId = qVariantValue<ThreadId>(item->modelIndex().data(Qt::UserRole));
       
   169 	mContextMenu->setPreferredPos(coords);
       
   170 	mContextMenu->open();
       
   171 }
       
   172 
       
   173 void MemSpyThreadView::changePriority()
       
   174 {
       
   175 	QAction *s = qobject_cast<QAction*>(sender());
       
   176 	int index = mPriorityMenu->actions().indexOf(s);
       
   177 	
       
   178 	ThreadPriority priorities[] = {
       
   179 		ThreadPriorityAbsoluteVeryLow,
       
   180 		ThreadPriorityAbsoluteLowNormal,
       
   181 		ThreadPriorityAbsoluteLow,
       
   182 		ThreadPriorityAbsoluteBackgroundNormal,
       
   183 		ThreadPriorityAbsoluteBackground,
       
   184 		ThreadPriorityAbsoluteForegroundNormal,
       
   185 		ThreadPriorityAbsoluteForeground,
       
   186 		ThreadPriorityAbsoluteHighNormal,
       
   187 		ThreadPriorityAbsoluteHigh,
       
   188 		ThreadPriorityAbsoluteRealTime1,
       
   189 		ThreadPriorityAbsoluteRealTime2,
       
   190 		ThreadPriorityAbsoluteRealTime3,
       
   191 		ThreadPriorityAbsoluteRealTime4,
       
   192 		ThreadPriorityAbsoluteRealTime5,
       
   193 		ThreadPriorityAbsoluteRealTime6,
       
   194 		ThreadPriorityAbsoluteRealTime7, 
       
   195 		ThreadPriorityAbsoluteRealTime8 };
       
   196 	
       
   197 	mEngine.setThreadPriority(mThreadId, priorities[index]);
       
   198 	refresh();
       
   199 }
       
   200 
       
   201 void MemSpyThreadView::refresh()
       
   202 { 
       
   203     mModel->refresh(); 
       
   204 }