not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / ksysguard / processui / ReniceDlg.cc
blob628d992323f4d2210a4af2c900c80d9d10f9bdc2
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 );
42 previous_cpuscheduler = 0;
44 connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
46 if(currentIoSched == KSysGuard::Process::None) {
47 // CurrentIoSched == 0 means that the priority is set automatically.
48 // Using the formula given by the linux kernel Documentation/block/ioprio
49 currentIoPrio = (currentCpuPrio+20)/5;
51 if(currentIoSched == (int)KSysGuard::Process::BestEffort && currentIoPrio == (currentCpuPrio+20)/5) {
52 // Unfortunately, in linux you can't ever set a process back to being None. So we fake it :)
53 currentIoSched = KSysGuard::Process::None;
55 ioniceSupported = (currentIoPrio != -2);
58 QWidget *widget = new QWidget(this);
59 setMainWidget(widget);
60 ui = new Ui_ReniceDlgUi();
61 ui->setupUi(widget);
62 ui->listWidget->insertItems(0, processes);
64 cpuScheduler = new QButtonGroup(this);
65 cpuScheduler->addButton(ui->radioNormal, (int)KSysGuard::Process::Other);
66 cpuScheduler->addButton(ui->radioBatch, (int)KSysGuard::Process::Batch);
67 cpuScheduler->addButton(ui->radioFIFO, (int)KSysGuard::Process::Fifo);
68 cpuScheduler->addButton(ui->radioRR, (int)KSysGuard::Process::RoundRobin);
69 if(currentCpuSched >= 0) { //negative means none of these
70 QAbstractButton *sched = cpuScheduler->button(currentCpuSched);
71 if(sched) {
72 sched->setChecked(true); //Check the current scheduler
73 previous_cpuscheduler = currentCpuSched;
76 cpuScheduler->setExclusive(true);
78 ioScheduler = new QButtonGroup(this);
79 ioScheduler->addButton(ui->radioIONormal, (int)KSysGuard::Process::None);
80 ioScheduler->addButton(ui->radioIdle, (int)KSysGuard::Process::Idle);
81 ioScheduler->addButton(ui->radioRealTime, (int)KSysGuard::Process::RealTime);
82 ioScheduler->addButton(ui->radioBestEffort, (int)KSysGuard::Process::BestEffort);
83 if(currentIoSched >= 0) { //negative means none of these
84 QAbstractButton *iosched = ioScheduler->button(currentIoSched);
85 if(iosched)
86 iosched->setChecked(true); //Check the current io scheduler
89 ioScheduler->setExclusive(true);
91 if(ioniceSupported)
92 ui->sliderIO->setValue(currentIoPrio);
93 ui->sliderCPU->setValue(currentCpuPrio);
95 ui->imgCPU->setPixmap( KIcon("cpu").pixmap(128, 128) );
96 ui->imgIO->setPixmap( KIcon("drive-harddisk").pixmap(128, 128) );
98 newCPUPriority = 40;
100 connect(cpuScheduler, SIGNAL(buttonClicked(int)), this, SLOT(cpuSchedulerChanged(int)));
101 connect(ioScheduler, SIGNAL(buttonClicked(int)), this, SLOT(updateUi()));
102 connect(ui->sliderCPU, SIGNAL(valueChanged(int)), this, SLOT(cpuSliderChanged(int)));
103 connect(ui->sliderIO, SIGNAL(valueChanged(int)), this, SLOT(ioSliderChanged(int)));
105 updateUi();
108 void ReniceDlg::ioSliderChanged(int value) {
109 ui->sliderIO->setToolTip(QString::number(value));
112 void ReniceDlg::cpuSchedulerChanged(int value) {
113 if(value != previous_cpuscheduler) {
114 if( (value == (int)KSysGuard::Process::Other || value == KSysGuard::Process::Batch) &&
115 (previous_cpuscheduler == (int)KSysGuard::Process::Fifo || previous_cpuscheduler == (int)KSysGuard::Process::RoundRobin)) {
116 int slider = -ui->sliderCPU->value() * 2 / 5 + 20;
117 setSliderRange();
118 ui->sliderCPU->setValue( slider );
119 } else if( (previous_cpuscheduler == (int)KSysGuard::Process::Other || previous_cpuscheduler == KSysGuard::Process::Batch) &&
120 (value == (int)KSysGuard::Process::Fifo || value == (int)KSysGuard::Process::RoundRobin)) {
121 int slider = (-ui->sliderCPU->value() + 20) * 5 / 2;
122 setSliderRange();
123 ui->sliderCPU->setValue( slider );
126 previous_cpuscheduler = value;
127 updateUi();
130 void ReniceDlg::cpuSliderChanged(int value) {
131 if(cpuScheduler->checkedId() == (int)KSysGuard::Process::Other || cpuScheduler->checkedId() == (int)KSysGuard::Process::Batch) {
132 if( ioScheduler->checkedId() == -1 || ioScheduler->checkedId() == (int)KSysGuard::Process::None) {
133 //ionice is 'Normal', thus automatically calculated based on cpunice
134 ui->sliderIO->setValue((value+20)/5);
137 ui->sliderCPU->setToolTip(QString::number(value));
140 void ReniceDlg::updateUi() {
141 bool cpuPrioEnabled = ( cpuScheduler->checkedId() != -1);
142 bool ioPrioEnabled = ( ioniceSupported && ioScheduler->checkedId() != -1 && ioScheduler->checkedId() != (int)KSysGuard::Process::Idle && ioScheduler->checkedId() != (int)KSysGuard::Process::None);
144 ui->sliderCPU->setEnabled(cpuPrioEnabled);
145 ui->lblCpuLow->setEnabled(cpuPrioEnabled);
146 ui->lblCpuHigh->setEnabled(cpuPrioEnabled);
148 ui->sliderIO->setEnabled(ioPrioEnabled);
149 ui->lblIOLow->setEnabled(ioPrioEnabled);
150 ui->lblIOHigh->setEnabled(ioPrioEnabled);
152 setSliderRange();
153 cpuSliderChanged(ui->sliderCPU->value());
154 ioSliderChanged(ui->sliderIO->value());
157 void ReniceDlg::setSliderRange() {
158 if(cpuScheduler->checkedId() == (int)KSysGuard::Process::Other || cpuScheduler->checkedId() == (int)KSysGuard::Process::Batch) {
159 //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
160 if(ui->sliderCPU->value() > 20) ui->sliderCPU->setValue(20);
161 ui->sliderCPU->setInvertedAppearance(true);
162 ui->sliderCPU->setMinimum(-19);
163 ui->sliderCPU->setMaximum(20);
164 ui->sliderCPU->setTickInterval(5);
165 } else {
166 if(ui->sliderCPU->value() < 1) ui->sliderCPU->setValue(1);
167 ui->sliderCPU->setInvertedAppearance(false);
168 ui->sliderCPU->setMinimum(1);
169 ui->sliderCPU->setMaximum(99);
170 ui->sliderCPU->setTickInterval(12);
174 void ReniceDlg::slotOk()
176 newCPUPriority = ui->sliderCPU->value();
177 newIOPriority = ui->sliderIO->value();
178 newCPUSched = cpuScheduler->checkedId();
179 newIOSched = ioScheduler->checkedId();