Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / ksysguard / processui / ReniceDlg.cc
blobbddd202023b285d0cf66db61601cbe414ce683d5
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
5 Copyright (c) 2007 John Tapsell <tapsell@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
25 #include <klocale.h>
26 #include <kdebug.h>
28 #include "ReniceDlg.moc"
29 #include <QListWidget>
30 #include <QButtonGroup>
31 #include "ui_ReniceDlgUi.h"
32 #include "processcore/process.h"
34 ReniceDlg::ReniceDlg(QWidget* parent, const QStringList& processes, int currentCpuPrio, int currentCpuSched, int currentIoPrio, int currentIoSched )
35 : KDialog( parent )
37 setObjectName( "Renice Dialog" );
38 setModal( true );
39 setCaption( i18n("Renice Process") );
40 setButtons( Ok | Cancel );
41 showButtonSeparator( true );
43 connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
45 if(currentIoSched == KSysGuard::Process::None) {
46 // CurrentIoSched == 0 means that the priority is set automatically.
47 // Using the formula given by the linux kernel Documentation/block/ioprio
48 currentIoPrio = (currentCpuPrio+20)/5;
50 if(currentIoSched == (int)KSysGuard::Process::BestEffort && currentIoPrio == (currentCpuPrio+20)/5) {
51 // Unfortunately, in linux you can't ever set a process back to being None. So we fake it :)
52 currentIoSched = KSysGuard::Process::None;
54 ioniceSupported = (currentIoPrio != -2);
57 QWidget *widget = new QWidget(this);
58 setMainWidget(widget);
59 ui = new Ui_ReniceDlgUi();
60 ui->setupUi(widget);
61 ui->listWidget->insertItems(0, processes);
63 cpuScheduler = new QButtonGroup(this);
64 cpuScheduler->addButton(ui->radioNormal, (int)KSysGuard::Process::Other);
65 cpuScheduler->addButton(ui->radioBatch, (int)KSysGuard::Process::Batch);
66 cpuScheduler->addButton(ui->radioFIFO, (int)KSysGuard::Process::Fifo);
67 cpuScheduler->addButton(ui->radioRR, (int)KSysGuard::Process::RoundRobin);
68 if(currentCpuSched >= 0) { //negative means none of these
69 QAbstractButton *sched = cpuScheduler->button(currentCpuSched);
70 if(sched)
71 sched->setChecked(true); //Check the current scheduler
73 cpuScheduler->setExclusive(true);
75 ioScheduler = new QButtonGroup(this);
76 ioScheduler->addButton(ui->radioIONormal, (int)KSysGuard::Process::None);
77 ioScheduler->addButton(ui->radioIdle, (int)KSysGuard::Process::Idle);
78 ioScheduler->addButton(ui->radioRealTime, (int)KSysGuard::Process::RealTime);
79 ioScheduler->addButton(ui->radioBestEffort, (int)KSysGuard::Process::BestEffort);
80 if(currentIoSched >= 0) { //negative means none of these
81 QAbstractButton *iosched = ioScheduler->button(currentIoSched);
82 if(iosched)
83 iosched->setChecked(true); //Check the current io scheduler
86 ioScheduler->setExclusive(true);
88 if(ioniceSupported)
89 ui->sliderIO->setValue(currentIoPrio);
90 ui->sliderCPU->setValue(currentCpuPrio);
92 ui->imgCPU->setPixmap( KIcon("cpu").pixmap(128, 128) );
93 ui->imgIO->setPixmap( KIcon("drive-harddisk").pixmap(128, 128) );
95 newCPUPriority = 40;
97 connect(cpuScheduler, SIGNAL(buttonClicked(int)), this, SLOT(updateUi()));
98 connect(ioScheduler, SIGNAL(buttonClicked(int)), this, SLOT(updateUi()));
99 connect(ui->sliderCPU, SIGNAL(valueChanged(int)), this, SLOT(cpuSliderChanged(int)));
100 connect(ui->sliderIO, SIGNAL(valueChanged(int)), this, SLOT(ioSliderChanged(int)));
102 updateUi();
105 void ReniceDlg::ioSliderChanged(int value) {
106 ui->sliderIO->setToolTip(QString::number(value));
108 void ReniceDlg::cpuSliderChanged(int value) {
109 if(cpuScheduler->checkedId() == (int)KSysGuard::Process::Other || cpuScheduler->checkedId() == (int)KSysGuard::Process::Batch) {
110 if( ioScheduler->checkedId() == -1 || ioScheduler->checkedId() == (int)KSysGuard::Process::None) {
111 //ionice is 'Normal', thus automatically calculated based on cpunice
112 ui->sliderIO->setValue((value+20)/5);
115 ui->sliderCPU->setToolTip(QString::number(value));
118 void ReniceDlg::updateUi() {
119 bool cpuPrioEnabled = ( cpuScheduler->checkedId() != -1);
120 bool ioPrioEnabled = ( ioniceSupported && ioScheduler->checkedId() != -1 && ioScheduler->checkedId() != (int)KSysGuard::Process::Idle && ioScheduler->checkedId() != (int)KSysGuard::Process::None);
122 ui->sliderCPU->setEnabled(cpuPrioEnabled);
123 ui->lblCpuLow->setEnabled(cpuPrioEnabled);
124 ui->lblCpuHigh->setEnabled(cpuPrioEnabled);
126 ui->sliderIO->setEnabled(ioPrioEnabled);
127 ui->lblIOLow->setEnabled(ioPrioEnabled);
128 ui->lblIOHigh->setEnabled(ioPrioEnabled);
130 setSliderRange();
131 cpuSliderChanged(ui->sliderCPU->value());
132 ioSliderChanged(ui->sliderIO->value());
135 void ReniceDlg::setSliderRange() {
136 if(cpuScheduler->checkedId() == (int)KSysGuard::Process::Other || cpuScheduler->checkedId() == (int)KSysGuard::Process::Batch) {
137 //The slider is setting the priority, so goes from 19 to -20. We cannot actually do this with a slider, so instead we go from -19 to 20, and negate later
138 if(ui->sliderCPU->value() > 20) ui->sliderCPU->setValue(20);
139 ui->sliderCPU->setInvertedAppearance(true);
140 ui->sliderCPU->setMinimum(-19);
141 ui->sliderCPU->setMaximum(20);
142 ui->sliderCPU->setTickInterval(5);
143 } else {
144 if(ui->sliderCPU->value() < 1) ui->sliderCPU->setValue(1);
145 ui->sliderCPU->setInvertedAppearance(false);
146 ui->sliderCPU->setMinimum(1);
147 ui->sliderCPU->setMaximum(99);
148 ui->sliderCPU->setTickInterval(12);
152 void ReniceDlg::slotOk()
154 newCPUPriority = ui->sliderCPU->value();
155 newIOPriority = ui->sliderIO->value();
156 newCPUSched = cpuScheduler->checkedId();
157 newIOSched = ioScheduler->checkedId();