2
KSysGuard, the KDE System Guard
4
Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
20
Please do not commit any changes without consulting me first. Thanks!
22
$Id: ListView.cc,v 1.12 2003/08/31 18:03:15 wheeler Exp $
32
#include <kcolorbutton.h>
36
#include <kmessagebox.h>
37
#include <ksgrd/ColorPicker.h>
38
#include <ksgrd/SensorManager.h>
39
#include <ksgrd/StyleEngine.h>
42
#include "ListView.moc"
43
#include "ListViewSettings.h"
45
PrivateListViewItem::PrivateListViewItem(PrivateListView *parent)
46
: QListViewItem(parent)
51
int PrivateListViewItem::compare( QListViewItem *item, int col, bool ascending ) const
53
int type = ((PrivateListView*)listView())->columnType( col );
55
if ( type == PrivateListView::Int ) {
56
int prev = (int)KGlobal::locale()->readNumber( key( col, ascending ) );
57
int next = (int)KGlobal::locale()->readNumber( item->key( col, ascending ) );
60
else if ( prev == next )
64
} else if ( type == PrivateListView::Float ) {
65
double prev = KGlobal::locale()->readNumber( key( col, ascending ) );
66
double next = KGlobal::locale()->readNumber( item->key( col, ascending ) );
71
} else if ( type == PrivateListView::Time ) {
72
int hourPrev, hourNext, minutesPrev, minutesNext;
73
sscanf( key( col, ascending ).latin1(), "%d:%d", &hourPrev, &minutesPrev );
74
sscanf( item->key( col, ascending ).latin1(), "%d:%d", &hourNext, &minutesNext );
75
int prev = hourPrev * 60 + minutesPrev;
76
int next = hourNext * 60 + minutesNext;
79
else if ( prev == next )
83
} else if ( type == PrivateListView::DiskStat ) {
84
QString prev = key( col, ascending );
85
QString next = item->key( col, ascending );
86
QString prevKey, nextKey;
88
uint counter = prev.length();
89
for ( uint i = 0; i < counter; ++i )
90
if ( prev[ i ].isDigit() ) {
91
prevKey.sprintf( "%s%016d", prev.left( i ).latin1(), prev.mid( i ).toInt() );
95
counter = next.length();
96
for ( uint i = 0; i < counter; ++i )
97
if ( next[ i ].isDigit() ) {
98
nextKey.sprintf( "%s%016d", next.left( i ).latin1(), next.mid( i ).toInt() );
102
return prevKey.compare( nextKey );
104
return key( col, ascending ).localeAwareCompare( item->key( col, ascending ) );
107
PrivateListView::PrivateListView(QWidget *parent, const char *name)
108
: QListView(parent, name)
110
QColorGroup cg = colorGroup();
112
cg.setColor(QColorGroup::Link, KSGRD::Style->firstForegroundColor());
113
cg.setColor(QColorGroup::Text, KSGRD::Style->secondForegroundColor());
114
cg.setColor(QColorGroup::Base, KSGRD::Style->backgroundColor());
116
setPalette(QPalette(cg, cg, cg));
119
void PrivateListView::update(const QString& answer)
123
KSGRD::SensorTokenizer lines(answer, '\n');
124
for (uint i = 0; i < lines.count(); i++) {
125
PrivateListViewItem *item = new PrivateListViewItem(this);
126
KSGRD::SensorTokenizer records(lines[i], '\t');
127
for (uint j = 0; j < records.count(); j++) {
128
if ( mColumnTypes[ j ] == "f" )
129
item->setText(j, KGlobal::locale()->formatNumber( records[j].toFloat() ) );
130
else if ( mColumnTypes[ j ] == "D" )
131
item->setText(j, KGlobal::locale()->formatNumber( records[j].toDouble(), 0 ) );
133
item->setText(j, records[j]);
140
int PrivateListView::columnType( uint pos ) const
142
if ( pos >= mColumnTypes.count() )
145
if ( mColumnTypes[ pos ] == "d" || mColumnTypes[ pos ] == "D" )
147
else if ( mColumnTypes[ pos ] == "f" || mColumnTypes[ pos ] == "F" )
149
else if ( mColumnTypes[ pos ] == "t" )
151
else if ( mColumnTypes[ pos ] == "M" )
157
void PrivateListView::removeColumns(void)
159
for (int i = columns() - 1; i >= 0; --i)
163
void PrivateListView::addColumn(const QString& label, const QString& type)
165
QListView::addColumn( label );
166
int col = columns() - 1;
168
if (type == "s" || type == "S")
169
setColumnAlignment(col, AlignLeft);
170
else if (type == "d" || type == "D")
171
setColumnAlignment(col, AlignRight);
172
else if (type == "t")
173
setColumnAlignment(col, AlignRight);
174
else if (type == "f")
175
setColumnAlignment(col, AlignRight);
176
else if (type == "M")
177
setColumnAlignment(col, AlignLeft);
180
kdDebug(1215) << "Unknown type " << type << " of column " << label
181
<< " in ListView!" << endl;
185
mColumnTypes.append( type );
187
/* Just use some sensible default values as initial setting. */
188
QFontMetrics fm = fontMetrics();
189
setColumnWidth(col, fm.width(label) + 10);
192
ListView::ListView(QWidget* parent, const char* name, const QString& title, int, int)
193
: KSGRD::SensorDisplay(parent, name, title)
195
setBackgroundColor(KSGRD::Style->backgroundColor());
197
monitor = new PrivateListView( frame() );
198
Q_CHECK_PTR(monitor);
199
monitor->setSelectionMode(QListView::NoSelection);
200
monitor->setItemMargin(2);
202
setMinimumSize(50, 25);
204
setPlotterWidget(monitor);
210
ListView::addSensor(const QString& hostName, const QString& sensorName, const QString& sensorType, const QString& title)
212
if (sensorType != "listview")
215
registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
219
/* To differentiate between answers from value requests and info
220
* requests we use 100 for info requests. */
221
sendRequest(hostName, sensorName + "?", 100);
228
ListView::updateList()
230
sendRequest(sensors().at(0)->hostName(), sensors().at(0)->name(), 19);
234
ListView::answerReceived(int id, const QString& answer)
236
/* We received something, so the sensor is probably ok. */
237
sensorError(id, false);
242
/* We have received the answer to a '?' command that contains
243
* the information about the table headers. */
244
KSGRD::SensorTokenizer lines(answer, '\n');
245
if (lines.count() != 2)
247
kdDebug(1215) << "wrong number of lines" << endl;
250
KSGRD::SensorTokenizer headers(lines[0], '\t');
251
KSGRD::SensorTokenizer colTypes(lines[1], '\t');
253
/* remove all columns from list */
254
monitor->removeColumns();
256
/* add the new columns */
257
for (unsigned int i = 0; i < headers.count(); i++)
258
/* TODO: Implement translation support for header texts */
259
monitor->addColumn(headers[i], colTypes[i]);
263
monitor->update(answer);
270
ListView::resizeEvent(QResizeEvent*)
272
frame()->setGeometry(0, 0, width(), height());
273
monitor->setGeometry(10, 20, width() - 20, height() - 30);
277
ListView::restoreSettings(QDomElement& element)
279
addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "listview" : element.attribute("sensorType")), element.attribute("title"));
281
QColorGroup colorGroup = monitor->colorGroup();
282
colorGroup.setColor(QColorGroup::Link, restoreColor(element, "gridColor", KSGRD::Style->firstForegroundColor()));
283
colorGroup.setColor(QColorGroup::Text, restoreColor(element, "textColor", KSGRD::Style->secondForegroundColor()));
284
colorGroup.setColor(QColorGroup::Base, restoreColor(element, "backgroundColor", KSGRD::Style->backgroundColor()));
286
monitor->setPalette(QPalette(colorGroup, colorGroup, colorGroup));
288
SensorDisplay::restoreSettings(element);
296
ListView::saveSettings(QDomDocument& doc, QDomElement& element, bool save)
298
element.setAttribute("hostName", sensors().at(0)->hostName());
299
element.setAttribute("sensorName", sensors().at(0)->name());
300
element.setAttribute("sensorType", sensors().at(0)->type());
302
QColorGroup colorGroup = monitor->colorGroup();
303
saveColor(element, "gridColor", colorGroup.color(QColorGroup::Link));
304
saveColor(element, "textColor", colorGroup.color(QColorGroup::Text));
305
saveColor(element, "backgroundColor", colorGroup.color(QColorGroup::Base));
307
SensorDisplay::saveSettings(doc, element);
316
ListView::configureSettings()
318
lvs = new ListViewSettings(this, "ListViewSettings");
320
connect(lvs, SIGNAL(applyClicked()), SLOT(applySettings()));
322
QColorGroup colorGroup = monitor->colorGroup();
323
lvs->setGridColor(colorGroup.color(QColorGroup::Link));
324
lvs->setTextColor(colorGroup.color(QColorGroup::Text));
325
lvs->setBackgroundColor(colorGroup.color(QColorGroup::Base));
326
lvs->setTitle(title());
336
ListView::applySettings()
338
QColorGroup colorGroup = monitor->colorGroup();
339
colorGroup.setColor(QColorGroup::Link, lvs->gridColor());
340
colorGroup.setColor(QColorGroup::Text, lvs->textColor());
341
colorGroup.setColor(QColorGroup::Base, lvs->backgroundColor());
342
monitor->setPalette(QPalette(colorGroup, colorGroup, colorGroup));
344
setTitle(lvs->title());
350
ListView::applyStyle()
352
QColorGroup colorGroup = monitor->colorGroup();
353
colorGroup.setColor(QColorGroup::Link, KSGRD::Style->firstForegroundColor());
354
colorGroup.setColor(QColorGroup::Text, KSGRD::Style->secondForegroundColor());
355
colorGroup.setColor(QColorGroup::Base, KSGRD::Style->backgroundColor());
356
monitor->setPalette(QPalette(colorGroup, colorGroup, colorGroup));