1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "JobModel.h"
#include "Color.h"
#include "CustomPixmap.h"
#include "Singleton.h"
#include "XmlOptions.h"
#include <QApplication>
#include <QFontMetrics>
#include <QPalette>
//_______________________________________________________________
JobModel::JobModel( QObject* parent ):
TreeModel<Job>( parent ),
Counter( "JobModel" ),
today_( TimeStamp::now() )
{
Debug::Throw( "JobModel::JobModel.\n" );
connect( Singleton::get().application(), SIGNAL(configurationChanged()), SLOT(_updateConfiguration()) );
_updateConfiguration();
}
//__________________________________________________________________
QVariant JobModel::data( const QModelIndex& index, int role ) const
{
if( !index.isValid() ) return QVariant();
switch( role )
{
case Qt::DisplayRole:
{
const Job& job( _find( index.internalId() ).get() );
switch( index.column() )
{
case Name:
{
const QString name( job.longName().isEmpty() ? job.name():job.longName() );
return name.left( name.indexOf( ' ' ) );
}
case User: return job.user();
case Id: return QString::number(abs(job.id()));
case ParentId:
{
if( job.parentId() > 0 ) return QString::number(job.parentId());
else return QVariant();
}
case Cpu:
{
if( job.isPaused() ) return "stopped";
else {
const int value = job.cpu();
if( value > 0 ) return QString( "%1 %" ).arg(value);
else return QVariant();
}
}
case Memory:
{
const qreal value = job.memory();
if( value > 0 ) return QString( "%1 %").arg( QString::number( value, 'f', 1 ) );
else return QVariant();
}
case CpuTime:
return ( job.cpuTime()>0 ) ? job.cpuTimeString():QString();
case Priority:
return job.priority();
case Nice:
return job.nice();
case VirtualMemory:
{
const qint64 value( job.virtualMemory() );
return value > 0 ? File::rawSizeString( value ) + " K" : QString();
}
case ResidentMemory:
{
const qint64 value( job.residentMemory() );
return value > 0 ? File::rawSizeString( value ) + " K" : QString();
}
case SharedMemory:
{
const qint64 value( job.sharedMemory() );
return value > 0 ? File::rawSizeString( value ) + " K" : QString();
}
case Threads: return job.threads();
case StartTime:
if( job.startTime().isSameDay( today_ ) ) return job.startTime().toString( TimeStamp::Time );
else if( job.startTime().isSameYear( today_ ) ) return job.startTime().toString( "MMM d hh:mm" );
else return job.startTime().toString( "MMM d yyy hh:mm" );
case Command: return job.command();
default: return QVariant();
}
break;
}
case Qt::FontRole:
{
if( index.column() == Cpu )
{
const Job& job( _find( index.internalId() ).get() );
if( job.isPaused() )
{
QFont font( QApplication::font() );
font.setItalic( true );
return font;
}
}
break;
}
case Qt::ForegroundRole:
{
const Job& job( _find( index.internalId() ).get() );
if( job.id() < 0 ) return QPalette().color( QPalette::Disabled, QPalette::Text );
else return QVariant();
}
case Qt::TextAlignmentRole:
{
switch( index.column() )
{
case User:
case Memory:
case Cpu:
case Threads:
case StartTime:
return static_cast<int>(Qt::AlignCenter);
case Id:
case ParentId:
case CpuTime:
case VirtualMemory:
case ResidentMemory:
case SharedMemory:
return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
default: break;
}
break;
}
case Qt::DecorationRole:
{
if( showIcons_ && index.column() == Name )
{
const Job& job( _find( index.internalId() ).get() );
if( !job.icon().isNull() ) return job.icon();
else return emptyIcon_;
}
break;
}
default: break;
}
return QVariant();
}
//__________________________________________________________________
QVariant JobModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch( role )
{
case Qt::DisplayRole:
{
switch( section )
{
case Name: return tr( "Name" );
case User: return tr( "User" );
case Id: return tr( "Process Id " );
case ParentId: return tr( "Parent's Id " );
case Cpu: return tr( "CPU %" );
case Memory: return tr( "Memory %" );
case CpuTime: return tr( "CPU Time " );
case Priority: return tr( "Priority" );
case Nice: return tr( "Nice" );
case VirtualMemory: return tr( "Virtual Memory " );
case ResidentMemory: return tr( "Resident Memory " );
case SharedMemory: return tr( "Shared Memory " );
case Threads: return tr( "Threads" );
case StartTime: return tr( "Started" );
case Command: return tr( "Command" );
default: return QVariant();
}
break;
}
case Qt::TextAlignmentRole:
{
switch( section )
{
case User:
case Memory:
case Cpu:
case Threads:
case StartTime:
return static_cast<int>(Qt::AlignCenter);
case Id:
case ParentId:
case CpuTime:
case VirtualMemory:
case ResidentMemory:
case SharedMemory:
return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
default: return QVariant();
}
break;
}
default: return QVariant();
}
// return empty
return QVariant();
}
//__________________________________________________________
void JobModel::_updateConfiguration(void)
{
Debug::Throw( "JobModel::_updateConfiguration.\n" );
showIcons_ = XmlOptions::get().get<bool>( "SHOW_ICONS" );
// empty icon
const int iconSize( XmlOptions::get().get<unsigned int>( "LIST_ICON_SIZE" ) );
iconSize_ = QSize( iconSize, iconSize );
// create empty pixmap
emptyIcon_ = CustomPixmap( iconSize_, CustomPixmap::Transparent );
}
//___________________________________________________________
bool JobModel::SortFTor::operator() ( Job first, Job second ) const
{
if( order_ == Qt::DescendingOrder ) std::swap( first, second );
switch( type_ )
{
case Name:
{
const QString firstName( first.longName().isEmpty() ? first.name():first.longName() );
const QString secondName( second.longName().isEmpty() ? second.name():second.longName() );
if( firstName != secondName ) return firstName < secondName;
break;
}
case User:
if( first.user() != second.user() ) return first.user() < second.user();
break;
case Id:
return first.id() < second.id();
break;
case ParentId:
if( first.parentId() != second.parentId() ) return first.parentId() < second.parentId();
break;
case Cpu:
if( first.cpu() != second.cpu() ) return first.cpu() < second.cpu();
break;
case Memory:
if( first.memory() != second.memory() ) return first.memory() < second.memory();
break;
case CpuTime:
if( first.cpuTime() != second.cpuTime() ) return first.cpuTime() < second.cpuTime();
break;
case Priority:
if( first.priority() != second.priority() ) return first.priority() < second.priority();
break;
case Nice:
if( first.nice() != second.nice() ) return first.nice() < second.nice();
break;
case VirtualMemory:
if( first.virtualMemory() != second.virtualMemory() ) return first.virtualMemory() < second.virtualMemory();
break;
case ResidentMemory:
if( first.residentMemory() != second.residentMemory() ) return first.residentMemory() < second.residentMemory();
break;
case SharedMemory:
if( first.sharedMemory() != second.sharedMemory() ) return first.sharedMemory() < second.sharedMemory();
break;
case Threads:
if( first.threads() != second.threads() ) return first.threads() < second.threads();
break;
case StartTime:
if( first.startTime() != second.startTime() ) return first.startTime() < second.startTime();
break;
case Command:
if( first.command() != second.command() ) return first.command() < second.command();
break;
default:
break;
}
return first.id() < second.id();
}
|