You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
372 lines
11 KiB
372 lines
11 KiB
15 years ago
|
/*
|
||
|
KSysGuard, the KDE System Guard
|
||
|
|
||
|
Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
|
||
|
|
||
|
This program is free software; you can redistribute it and/or
|
||
|
modify it under the terms of version 2 of the GNU General Public
|
||
|
License as published by the Free Software Foundation.
|
||
|
|
||
|
This program 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, write to the Free Software
|
||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
|
||
|
KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
|
||
|
Please do not commit any changes without consulting me first. Thanks!
|
||
|
|
||
|
*/
|
||
|
|
||
|
#include <ctype.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include <config.h>
|
||
14 years ago
|
#include <tqdom.h>
|
||
15 years ago
|
|
||
|
#include <kcolorbutton.h>
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeglobal.h>
|
||
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <ksgrd/SensorManager.h>
|
||
|
#include <ksgrd/StyleEngine.h>
|
||
|
|
||
|
#include "ListView.h"
|
||
|
#include "ListView.moc"
|
||
|
#include "ListViewSettings.h"
|
||
|
|
||
|
PrivateListViewItem::PrivateListViewItem(PrivateListView *parent)
|
||
14 years ago
|
: TQListViewItem(parent)
|
||
15 years ago
|
{
|
||
|
_parent = parent;
|
||
|
}
|
||
|
|
||
14 years ago
|
int PrivateListViewItem::compare( TQListViewItem *item, int col, bool ascending ) const
|
||
15 years ago
|
{
|
||
|
int type = ((PrivateListView*)listView())->columnType( col );
|
||
|
|
||
|
if ( type == PrivateListView::Int ) {
|
||
12 years ago
|
int prev = (int)TDEGlobal::locale()->readNumber( key( col, ascending ) );
|
||
|
int next = (int)TDEGlobal::locale()->readNumber( item->key( col, ascending ) );
|
||
15 years ago
|
if ( prev < next )
|
||
|
return -1;
|
||
|
else if ( prev == next )
|
||
|
return 0;
|
||
|
else
|
||
|
return 1;
|
||
|
} else if ( type == PrivateListView::Float ) {
|
||
12 years ago
|
double prev = TDEGlobal::locale()->readNumber( key( col, ascending ) );
|
||
|
double next = TDEGlobal::locale()->readNumber( item->key( col, ascending ) );
|
||
15 years ago
|
if ( prev < next )
|
||
|
return -1;
|
||
|
else
|
||
|
return 1;
|
||
|
} else if ( type == PrivateListView::Time ) {
|
||
|
int hourPrev, hourNext, minutesPrev, minutesNext;
|
||
|
sscanf( key( col, ascending ).latin1(), "%d:%d", &hourPrev, &minutesPrev );
|
||
|
sscanf( item->key( col, ascending ).latin1(), "%d:%d", &hourNext, &minutesNext );
|
||
|
int prev = hourPrev * 60 + minutesPrev;
|
||
|
int next = hourNext * 60 + minutesNext;
|
||
|
if ( prev < next )
|
||
|
return -1;
|
||
|
else if ( prev == next )
|
||
|
return 0;
|
||
|
else
|
||
|
return 1;
|
||
|
} else if ( type == PrivateListView::DiskStat ) {
|
||
14 years ago
|
TQString prev = key( col, ascending );
|
||
|
TQString next = item->key( col, ascending );
|
||
|
TQString prevKey, nextKey;
|
||
15 years ago
|
|
||
|
uint counter = prev.length();
|
||
|
for ( uint i = 0; i < counter; ++i )
|
||
|
if ( prev[ i ].isDigit() ) {
|
||
|
prevKey.sprintf( "%s%016d", prev.left( i ).latin1(), prev.mid( i ).toInt() );
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
counter = next.length();
|
||
|
for ( uint i = 0; i < counter; ++i )
|
||
|
if ( next[ i ].isDigit() ) {
|
||
|
nextKey.sprintf( "%s%016d", next.left( i ).latin1(), next.mid( i ).toInt() );
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return prevKey.compare( nextKey );
|
||
|
} else
|
||
|
return key( col, ascending ).localeAwareCompare( item->key( col, ascending ) );
|
||
|
}
|
||
|
|
||
14 years ago
|
PrivateListView::PrivateListView(TQWidget *parent, const char *name)
|
||
|
: TQListView(parent, name)
|
||
15 years ago
|
{
|
||
13 years ago
|
TQColorGroup cg = colorGroup();
|
||
15 years ago
|
|
||
14 years ago
|
cg.setColor(TQColorGroup::Link, KSGRD::Style->firstForegroundColor());
|
||
|
cg.setColor(TQColorGroup::Text, KSGRD::Style->secondForegroundColor());
|
||
|
cg.setColor(TQColorGroup::Base, KSGRD::Style->backgroundColor());
|
||
15 years ago
|
|
||
14 years ago
|
setPalette(TQPalette(cg, cg, cg));
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void PrivateListView::update(const TQString& answer)
|
||
15 years ago
|
{
|
||
13 years ago
|
setUpdatesEnabled(false);
|
||
|
viewport()->setUpdatesEnabled(false);
|
||
15 years ago
|
|
||
|
int vpos = verticalScrollBar()->value();
|
||
|
int hpos = horizontalScrollBar()->value();
|
||
|
|
||
|
clear();
|
||
|
|
||
|
KSGRD::SensorTokenizer lines(answer, '\n');
|
||
|
for (uint i = 0; i < lines.count(); i++) {
|
||
|
PrivateListViewItem *item = new PrivateListViewItem(this);
|
||
|
KSGRD::SensorTokenizer records(lines[i], '\t');
|
||
|
for (uint j = 0; j < records.count(); j++) {
|
||
|
if ( mColumnTypes[ j ] == "f" )
|
||
12 years ago
|
item->setText(j, TDEGlobal::locale()->formatNumber( records[j].toFloat() ) );
|
||
15 years ago
|
else if ( mColumnTypes[ j ] == "D" )
|
||
12 years ago
|
item->setText(j, TDEGlobal::locale()->formatNumber( records[j].toDouble(), 0 ) );
|
||
15 years ago
|
else
|
||
|
item->setText(j, records[j]);
|
||
|
}
|
||
|
|
||
|
insertItem(item);
|
||
|
}
|
||
|
|
||
|
verticalScrollBar()->setValue(vpos);
|
||
|
horizontalScrollBar()->setValue(hpos);
|
||
|
|
||
13 years ago
|
viewport()->setUpdatesEnabled(true);
|
||
|
setUpdatesEnabled(true);
|
||
15 years ago
|
|
||
|
triggerUpdate();
|
||
|
}
|
||
|
|
||
|
int PrivateListView::columnType( uint pos ) const
|
||
|
{
|
||
|
if ( pos >= mColumnTypes.count() )
|
||
|
return 0;
|
||
|
|
||
|
if ( mColumnTypes[ pos ] == "d" || mColumnTypes[ pos ] == "D" )
|
||
|
return Int;
|
||
|
else if ( mColumnTypes[ pos ] == "f" || mColumnTypes[ pos ] == "F" )
|
||
|
return Float;
|
||
|
else if ( mColumnTypes[ pos ] == "t" )
|
||
|
return Time;
|
||
|
else if ( mColumnTypes[ pos ] == "M" )
|
||
|
return DiskStat;
|
||
|
else
|
||
|
return Text;
|
||
|
}
|
||
|
|
||
|
void PrivateListView::removeColumns(void)
|
||
|
{
|
||
|
for (int i = columns() - 1; i >= 0; --i)
|
||
|
removeColumn(i);
|
||
|
}
|
||
|
|
||
14 years ago
|
void PrivateListView::addColumn(const TQString& label, const TQString& type)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQListView::addColumn( label );
|
||
15 years ago
|
int col = columns() - 1;
|
||
|
|
||
|
if (type == "s" || type == "S")
|
||
|
setColumnAlignment(col, AlignLeft);
|
||
|
else if (type == "d" || type == "D")
|
||
|
setColumnAlignment(col, AlignRight);
|
||
|
else if (type == "t")
|
||
|
setColumnAlignment(col, AlignRight);
|
||
|
else if (type == "f")
|
||
|
setColumnAlignment(col, AlignRight);
|
||
|
else if (type == "M")
|
||
|
setColumnAlignment(col, AlignLeft);
|
||
|
else
|
||
|
{
|
||
|
kdDebug(1215) << "Unknown type " << type << " of column " << label
|
||
|
<< " in ListView!" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mColumnTypes.append( type );
|
||
|
|
||
|
/* Just use some sensible default values as initial setting. */
|
||
14 years ago
|
TQFontMetrics fm = fontMetrics();
|
||
15 years ago
|
setColumnWidth(col, fm.width(label) + 10);
|
||
|
}
|
||
|
|
||
14 years ago
|
ListView::ListView(TQWidget* parent, const char* name, const TQString& title, int, int)
|
||
15 years ago
|
: KSGRD::SensorDisplay(parent, name, title)
|
||
|
{
|
||
|
setBackgroundColor(KSGRD::Style->backgroundColor());
|
||
|
|
||
|
monitor = new PrivateListView( frame() );
|
||
13 years ago
|
TQ_CHECK_PTR(monitor);
|
||
14 years ago
|
monitor->setSelectionMode(TQListView::NoSelection);
|
||
15 years ago
|
monitor->setItemMargin(2);
|
||
|
|
||
|
setMinimumSize(50, 25);
|
||
|
|
||
|
setPlotterWidget(monitor);
|
||
|
|
||
|
setModified(false);
|
||
|
}
|
||
|
|
||
|
bool
|
||
14 years ago
|
ListView::addSensor(const TQString& hostName, const TQString& sensorName, const TQString& sensorType, const TQString& title)
|
||
15 years ago
|
{
|
||
|
if (sensorType != "listview")
|
||
|
return (false);
|
||
|
|
||
|
registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
|
||
|
|
||
|
setTitle(title);
|
||
|
|
||
|
/* To differentiate between answers from value requests and info
|
||
|
* requests we use 100 for info requests. */
|
||
|
sendRequest(hostName, sensorName + "?", 100);
|
||
|
sendRequest(hostName, sensorName, 19);
|
||
|
setModified(true);
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ListView::updateList()
|
||
|
{
|
||
13 years ago
|
sendRequest(sensors().at(0)->hostName(), sensors().at(0)->name(), 19);
|
||
15 years ago
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
ListView::answerReceived(int id, const TQString& answer)
|
||
15 years ago
|
{
|
||
|
/* We received something, so the sensor is probably ok. */
|
||
|
sensorError(id, false);
|
||
|
|
||
|
switch (id)
|
||
|
{
|
||
|
case 100: {
|
||
14 years ago
|
/* We have received the answer to a '?' command that contains
|
||
15 years ago
|
* the information about the table headers. */
|
||
|
KSGRD::SensorTokenizer lines(answer, '\n');
|
||
|
if (lines.count() != 2)
|
||
|
{
|
||
|
kdDebug(1215) << "wrong number of lines" << endl;
|
||
|
return;
|
||
|
}
|
||
|
KSGRD::SensorTokenizer headers(lines[0], '\t');
|
||
|
KSGRD::SensorTokenizer colTypes(lines[1], '\t');
|
||
|
|
||
|
/* remove all columns from list */
|
||
|
monitor->removeColumns();
|
||
|
|
||
|
/* add the new columns */
|
||
|
for (unsigned int i = 0; i < headers.count(); i++)
|
||
|
/* TODO: Implement translation support for header texts */
|
||
|
monitor->addColumn(headers[i], colTypes[i]);
|
||
|
break;
|
||
|
}
|
||
|
case 19: {
|
||
|
monitor->update(answer);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
ListView::resizeEvent(TQResizeEvent*)
|
||
15 years ago
|
{
|
||
|
frame()->setGeometry(0, 0, width(), height());
|
||
|
monitor->setGeometry(10, 20, width() - 20, height() - 30);
|
||
|
}
|
||
|
|
||
|
bool
|
||
14 years ago
|
ListView::restoreSettings(TQDomElement& element)
|
||
15 years ago
|
{
|
||
|
addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "listview" : element.attribute("sensorType")), element.attribute("title"));
|
||
|
|
||
13 years ago
|
TQColorGroup colorGroup = monitor->colorGroup();
|
||
|
colorGroup.setColor(TQColorGroup::Link, restoreColor(element, "gridColor", KSGRD::Style->firstForegroundColor()));
|
||
|
colorGroup.setColor(TQColorGroup::Text, restoreColor(element, "textColor", KSGRD::Style->secondForegroundColor()));
|
||
|
colorGroup.setColor(TQColorGroup::Base, restoreColor(element, "backgroundColor", KSGRD::Style->backgroundColor()));
|
||
15 years ago
|
|
||
13 years ago
|
monitor->setPalette(TQPalette(colorGroup, colorGroup, colorGroup));
|
||
15 years ago
|
|
||
|
SensorDisplay::restoreSettings(element);
|
||
|
|
||
|
setModified(false);
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
bool
|
||
14 years ago
|
ListView::saveSettings(TQDomDocument& doc, TQDomElement& element, bool save)
|
||
15 years ago
|
{
|
||
13 years ago
|
element.setAttribute("hostName", sensors().at(0)->hostName());
|
||
|
element.setAttribute("sensorName", sensors().at(0)->name());
|
||
|
element.setAttribute("sensorType", sensors().at(0)->type());
|
||
15 years ago
|
|
||
13 years ago
|
TQColorGroup colorGroup = monitor->colorGroup();
|
||
|
saveColor(element, "gridColor", colorGroup.color(TQColorGroup::Link));
|
||
|
saveColor(element, "textColor", colorGroup.color(TQColorGroup::Text));
|
||
|
saveColor(element, "backgroundColor", colorGroup.color(TQColorGroup::Base));
|
||
15 years ago
|
|
||
|
SensorDisplay::saveSettings(doc, element);
|
||
|
|
||
|
if (save)
|
||
|
setModified(false);
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ListView::configureSettings()
|
||
|
{
|
||
|
lvs = new ListViewSettings(this, "ListViewSettings");
|
||
13 years ago
|
TQ_CHECK_PTR(lvs);
|
||
14 years ago
|
connect(lvs, TQT_SIGNAL(applyClicked()), TQT_SLOT(applySettings()));
|
||
15 years ago
|
|
||
13 years ago
|
TQColorGroup colorGroup = monitor->colorGroup();
|
||
|
lvs->setGridColor(colorGroup.color(TQColorGroup::Link));
|
||
|
lvs->setTextColor(colorGroup.color(TQColorGroup::Text));
|
||
|
lvs->setBackgroundColor(colorGroup.color(TQColorGroup::Base));
|
||
15 years ago
|
lvs->setTitle(title());
|
||
|
|
||
|
if (lvs->exec())
|
||
|
applySettings();
|
||
|
|
||
|
delete lvs;
|
||
|
lvs = 0;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ListView::applySettings()
|
||
|
{
|
||
13 years ago
|
TQColorGroup colorGroup = monitor->colorGroup();
|
||
|
colorGroup.setColor(TQColorGroup::Link, lvs->gridColor());
|
||
|
colorGroup.setColor(TQColorGroup::Text, lvs->textColor());
|
||
|
colorGroup.setColor(TQColorGroup::Base, lvs->backgroundColor());
|
||
|
monitor->setPalette(TQPalette(colorGroup, colorGroup, colorGroup));
|
||
15 years ago
|
|
||
|
setTitle(lvs->title());
|
||
|
|
||
|
setModified(true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ListView::applyStyle()
|
||
|
{
|
||
13 years ago
|
TQColorGroup colorGroup = monitor->colorGroup();
|
||
|
colorGroup.setColor(TQColorGroup::Link, KSGRD::Style->firstForegroundColor());
|
||
|
colorGroup.setColor(TQColorGroup::Text, KSGRD::Style->secondForegroundColor());
|
||
|
colorGroup.setColor(TQColorGroup::Base, KSGRD::Style->backgroundColor());
|
||
|
monitor->setPalette(TQPalette(colorGroup, colorGroup, colorGroup));
|
||
15 years ago
|
|
||
|
setModified(true);
|
||
|
}
|