1 /*
2     KSysGuard, the KDE System Guard
3 
4     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
5 
6     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 
20 */
21 
22 #include <KAcceleratorManager>
23 #include <KColorButton>
24 #include <KLocalizedString>
25 
26 #include <QCheckBox>
27 #include <QDoubleSpinBox>
28 #include <QGroupBox>
29 #include <QHeaderView>
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QPushButton>
33 #include <QTreeView>
34 #include <QGridLayout>
35 #include <QInputDialog>
36 #include "DancingBarsSettings.h"
37 
DancingBarsSettings(QWidget * parent,const QString & name)38 DancingBarsSettings::DancingBarsSettings(QWidget* parent, const QString &name )
39   : KPageDialog( parent ), mModel( new SensorModel( this ) )
40 {
41   setFaceType( Tabbed );
42   setWindowTitle( i18n( "Edit BarGraph Preferences" ) );
43   setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
44   setObjectName( name );
45   setModal( false );
46 
47   mModel->setHasLabel( true );
48 
49   // Range page
50   QFrame *page = new QFrame( this );
51   addPage( page, i18n( "Range" ) );
52   QGridLayout *pageLayout = new QGridLayout( page );
53   pageLayout->setContentsMargins( 0, 0, 0, 0 );
54 
55   QGroupBox *groupBox = new QGroupBox( i18n( "Title" ), page );
56   QGridLayout *boxLayout = new QGridLayout;
57   groupBox->setLayout( boxLayout );
58 
59   mTitle = new QLineEdit( groupBox );
60   mTitle->setWhatsThis( i18n( "Enter the title of the display here." ) );
61   boxLayout->addWidget( mTitle, 0, 0 );
62 
63   pageLayout->addWidget( groupBox, 0, 0 );
64 
65   groupBox = new QGroupBox( i18n( "Display Range" ), page );
66   boxLayout = new QGridLayout;
67   groupBox->setLayout( boxLayout );
68   boxLayout->setColumnStretch( 2, 1 );
69 
70   QLabel *label = new QLabel( i18n( "Minimum value:" ), groupBox );
71   boxLayout->addWidget( label, 0, 0 );
72 
73   mMinValue = new QDoubleSpinBox(groupBox);
74   mMinValue->setRange(0, 10000);
75   mMinValue->setSingleStep(0.5);
76   mMinValue->setValue(0);
77   mMinValue->setDecimals(2);
78   mMinValue->setWhatsThis( i18n( "Enter the minimum value for the display here. If both values are 0, automatic range detection is enabled." ) );
79   boxLayout->addWidget( mMinValue, 0, 1 );
80   label->setBuddy( mMinValue );
81 
82   label = new QLabel( i18n( "Maximum value:" ), groupBox );
83   boxLayout->addWidget( label, 0, 3 );
84 
85   mMaxValue = new QDoubleSpinBox( groupBox);
86   mMaxValue->setRange(0, 10000);
87   mMaxValue->setSingleStep(0.5);
88   mMaxValue->setValue(100);
89   mMaxValue->setDecimals(2);
90   mMaxValue->setWhatsThis( i18n( "Enter the maximum value for the display here. If both values are 0, automatic range detection is enabled." ) );
91   boxLayout->addWidget( mMaxValue, 0, 4 );
92   label->setBuddy( mMaxValue );
93 
94   pageLayout->addWidget( groupBox, 1, 0 );
95 
96   pageLayout->setRowStretch( 2, 1 );
97 
98   // Alarm page
99   page = new QFrame( this );
100   addPage( page, i18n( "Alarms" ) );
101   pageLayout = new QGridLayout( page );
102   pageLayout->setContentsMargins( 0, 0, 0, 0 );
103 
104   groupBox = new QGroupBox( i18n( "Alarm for Minimum Value" ), page );
105   boxLayout = new QGridLayout;
106   groupBox->setLayout( boxLayout );
107   boxLayout->setColumnStretch( 1, 1 );
108 
109   mUseLowerLimit = new QCheckBox( i18n( "Enable alarm" ), groupBox );
110   mUseLowerLimit->setWhatsThis( i18n( "Enable the minimum value alarm." ) );
111   boxLayout->addWidget( mUseLowerLimit, 0, 0 );
112 
113   label = new QLabel( i18n( "Lower limit:" ), groupBox );
114   boxLayout->addWidget( label, 0, 2 );
115 
116   mLowerLimit = new QDoubleSpinBox(groupBox);
117   mLowerLimit->setRange(0, 10000);
118   mLowerLimit->setSingleStep(0.5);
119   mLowerLimit->setValue(0);
120   mLowerLimit->setDecimals(2);
121   mLowerLimit->setEnabled( false );
122   boxLayout->addWidget( mLowerLimit, 0, 3 );
123   label->setBuddy( mLowerLimit );
124 
125   pageLayout->addWidget( groupBox, 0, 0 );
126 
127   groupBox = new QGroupBox( i18n( "Alarm for Maximum Value" ), page );
128   boxLayout = new QGridLayout;
129   groupBox->setLayout( boxLayout );
130   boxLayout->setColumnStretch( 1, 1 );
131 
132   mUseUpperLimit = new QCheckBox( i18n( "Enable alarm" ), groupBox );
133   mUseUpperLimit->setWhatsThis( i18n( "Enable the maximum value alarm." ) );
134   boxLayout->addWidget( mUseUpperLimit, 0, 0 );
135 
136   label = new QLabel( i18n( "Upper limit:" ), groupBox );
137   boxLayout->addWidget( label, 0, 2 );
138 
139   mUpperLimit = new QDoubleSpinBox( groupBox);
140   mUpperLimit->setRange(0, 10000);
141   mUpperLimit->setSingleStep(0.5);
142   mUpperLimit->setDecimals(2);
143   mUpperLimit->setEnabled( false );
144   boxLayout->addWidget( mUpperLimit, 0, 3 );
145   label->setBuddy( mUpperLimit );
146 
147   pageLayout->addWidget( groupBox, 1, 0 );
148 
149   pageLayout->setRowStretch( 2, 1 );
150 
151   // Look page
152   page = new QFrame( this );
153   addPage( page, i18nc( "@title:tab Appearance of the bar graph", "Look" ) );
154   pageLayout = new QGridLayout( page );
155   pageLayout->setContentsMargins( 0, 0, 0, 0 );
156 
157   label = new QLabel( i18n( "Normal bar color:" ), page );
158   pageLayout->addWidget( label, 0, 0 );
159 
160   mForegroundColor = new KColorButton( page );
161   pageLayout->addWidget( mForegroundColor, 0, 1 );
162   label->setBuddy( mForegroundColor );
163 
164   label = new QLabel( i18n( "Out-of-range color:" ), page );
165   pageLayout->addWidget( label, 1, 0 );
166 
167   mAlarmColor = new KColorButton( page );
168   pageLayout->addWidget( mAlarmColor, 1, 1 );
169   label->setBuddy( mAlarmColor );
170 
171   label = new QLabel( i18n( "Background color:" ), page );
172   pageLayout->addWidget( label, 2, 0 );
173 
174   mBackgroundColor = new KColorButton( page );
175   pageLayout->addWidget( mBackgroundColor, 2, 1 );
176   label->setBuddy( mBackgroundColor );
177 
178   label = new QLabel( i18n( "Font size:" ), page );
179   pageLayout->addWidget( label, 3, 0 );
180 
181   mFontSize = new QSpinBox( page );
182   mFontSize->setValue( 9 );
183   mFontSize->setWhatsThis( i18n( "This determines the size of the font used to print a label underneath the bars. Bars are automatically suppressed if text becomes too large, so it is advisable to use a small font size here." ) );
184   pageLayout->addWidget( mFontSize, 3, 1 );
185   label->setBuddy( mFontSize );
186 
187   pageLayout->setRowStretch( 4, 1 );
188 
189   // Sensor page
190   page = new QFrame( this );
191   addPage( page, i18n( "Sensors" ) );
192   pageLayout = new QGridLayout( page );
193   pageLayout->setContentsMargins( 0, 0, 0, 0 );
194   pageLayout->setRowStretch( 2, 1 );
195 
196   mView = new QTreeView( page );
197   mView->header()->setStretchLastSection( true );
198   mView->setRootIsDecorated( false );
199   mView->setItemsExpandable( false );
200   mView->setModel( mModel );
201   mView->header()->moveSection( SensorModel::Label, 1 ); // Show the label after host (can't place it first because it's a treeview based on host)
202   pageLayout->addWidget( mView, 0, 0, 3, 1);
203 
204   mEditButton = new QPushButton( i18n( "Edit..." ), page );
205   mEditButton->setWhatsThis( i18n( "Push this button to configure the label." ) );
206   pageLayout->addWidget( mEditButton, 0, 1 );
207 
208   mRemoveButton = new QPushButton( i18n( "Delete" ), page );
209   mRemoveButton->setWhatsThis( i18n( "Push this button to delete the sensor." ) );
210   pageLayout->addWidget( mRemoveButton, 1, 1 );
211 
212   connect(mUseLowerLimit, &QCheckBox::toggled, mLowerLimit, &QDoubleSpinBox::setEnabled);
213   connect(mUseUpperLimit, &QCheckBox::toggled, mUpperLimit, &QDoubleSpinBox::setEnabled);
214   connect(mEditButton, &QPushButton::clicked, this, &DancingBarsSettings::editSensor);
215   connect(mRemoveButton, &QPushButton::clicked, this, &DancingBarsSettings::removeSensor);
216 
217   KAcceleratorManager::manage( this );
218 
219   mTitle->setFocus();
220 }
221 
~DancingBarsSettings()222 DancingBarsSettings::~DancingBarsSettings()
223 {
224 }
225 
setTitle(const QString & title)226 void DancingBarsSettings::setTitle( const QString& title )
227 {
228   mTitle->setText( title );
229 }
230 
title() const231 QString DancingBarsSettings::title() const
232 {
233   return mTitle->text();
234 }
235 
setMinValue(double min)236 void DancingBarsSettings::setMinValue( double min )
237 {
238   mMinValue->setValue( min );
239 }
240 
minValue() const241 double DancingBarsSettings::minValue() const
242 {
243   return mMinValue->value();
244 }
245 
setMaxValue(double max)246 void DancingBarsSettings::setMaxValue( double max )
247 {
248   mMaxValue->setValue( max );
249 }
250 
maxValue() const251 double DancingBarsSettings::maxValue() const
252 {
253   return mMaxValue->value();
254 }
255 
setUseLowerLimit(bool value)256 void DancingBarsSettings::setUseLowerLimit( bool value )
257 {
258   mUseLowerLimit->setChecked( value );
259 }
260 
useLowerLimit() const261 bool DancingBarsSettings::useLowerLimit() const
262 {
263   return mUseLowerLimit->isChecked();
264 }
265 
setLowerLimit(double limit)266 void DancingBarsSettings::setLowerLimit( double limit )
267 {
268   mLowerLimit->setValue( limit );
269 }
270 
lowerLimit() const271 double DancingBarsSettings::lowerLimit() const
272 {
273   return mLowerLimit->value();
274 }
275 
setUseUpperLimit(bool value)276 void DancingBarsSettings::setUseUpperLimit( bool value )
277 {
278   mUseUpperLimit->setChecked( value );
279 }
280 
useUpperLimit() const281 bool DancingBarsSettings::useUpperLimit() const
282 {
283   return mUseUpperLimit->isChecked();
284 }
285 
setUpperLimit(double limit)286 void DancingBarsSettings::setUpperLimit( double limit )
287 {
288   mUpperLimit->setValue( limit );
289 }
290 
upperLimit() const291 double DancingBarsSettings::upperLimit() const
292 {
293   return mUpperLimit->value();
294 }
295 
setForegroundColor(const QColor & color)296 void DancingBarsSettings::setForegroundColor( const QColor &color )
297 {
298   mForegroundColor->setColor( color );
299 }
300 
foregroundColor() const301 QColor DancingBarsSettings::foregroundColor() const
302 {
303   return mForegroundColor->color();
304 }
305 
setAlarmColor(const QColor & color)306 void DancingBarsSettings::setAlarmColor( const QColor &color )
307 {
308   mAlarmColor->setColor( color );
309 }
310 
alarmColor() const311 QColor DancingBarsSettings::alarmColor() const
312 {
313   return mAlarmColor->color();
314 }
315 
setBackgroundColor(const QColor & color)316 void DancingBarsSettings::setBackgroundColor( const QColor &color )
317 {
318   mBackgroundColor->setColor( color );
319 }
320 
backgroundColor() const321 QColor DancingBarsSettings::backgroundColor() const
322 {
323   return mBackgroundColor->color();
324 }
325 
setFontSize(int size)326 void DancingBarsSettings::setFontSize( int size )
327 {
328   mFontSize->setValue( size );
329 }
330 
fontSize() const331 int DancingBarsSettings::fontSize() const
332 {
333   return mFontSize->value();
334 }
335 
setSensors(const SensorModelEntry::List & list)336 void DancingBarsSettings::setSensors( const SensorModelEntry::List &list )
337 {
338   mModel->setSensors( list );
339 
340   mView->selectionModel()->setCurrentIndex( mModel->index( 0, 0 ), QItemSelectionModel::SelectCurrent |
341                                                                    QItemSelectionModel::Rows );
342 }
343 
sensors() const344 SensorModelEntry::List DancingBarsSettings::sensors() const
345 {
346   return mModel->sensors();
347 }
348 
editSensor()349 void DancingBarsSettings::editSensor()
350 {
351   if ( !mView->selectionModel() )
352     return;
353 
354   const QModelIndex index = mView->selectionModel()->currentIndex();
355   if ( !index.isValid() )
356     return;
357 
358   SensorModelEntry sensor = mModel->sensor( index );
359 
360   bool ok;
361   const QString name = QInputDialog::getText( this, i18n( "Label of Bar Graph" ),
362                                               i18n( "Enter new label:" ), QLineEdit::Normal, sensor.label(), &ok);
363   if ( ok ) {
364     sensor.setLabel( name );
365     mModel->setSensor( sensor, index );
366   }
367 }
368 
removeSensor()369 void DancingBarsSettings::removeSensor()
370 {
371   if ( !mView->selectionModel() )
372     return;
373 
374   const QModelIndex index = mView->selectionModel()->currentIndex();
375   if ( !index.isValid() )
376     return;
377 
378   deletedIds.append(index.row());
379   mModel->removeSensor( index );
380 }
381 
382 /*
383  * returns a list of IDs, which have to removed from the sensors list,
384  * perform this in the same order as the IDs are stored in this list,
385  * otherwise you have to consider, that the IDs are changing each time you removeSensor is called
386  *
387  * example:
388  * Sensor indexes: [0,1,2,3,4]
389  *
390  * deletedIds can be create like this:
391  * [0] 			--> user has deleted row 0	-->	[1,2,3,4]
392  * [0,1]		--> user has deleted row 1	--> [1,3,4]
393  * [0,1,1]		--> user has deleted row 1	--> [1,4]
394  *
395  * using the deletedIds list by a simple iteration through the list
396  * [1,2,3,4]	--> removed pos 0
397  * [1,3,4]		--> removed pos 1
398  * [1,4]		--> removed pos 1
399  *
400  */
getDeletedIds()401 QList<uint> DancingBarsSettings::getDeletedIds()
402 {
403 	return deletedIds;
404 }
405 
406 
407