~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/ListView.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 2001 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 version 2 of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
    KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
 
20
    Please do not commit any changes without consulting me first. Thanks!
 
21
 
 
22
    $Id: ListView.cc,v 1.12 2003/08/31 18:03:15 wheeler Exp $
 
23
*/
 
24
 
 
25
#include <ctype.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
 
 
29
#include <config.h>
 
30
#include <qdom.h>
 
31
 
 
32
#include <kcolorbutton.h>
 
33
#include <kdebug.h>
 
34
#include <kglobal.h>
 
35
#include <klocale.h>
 
36
#include <kmessagebox.h>
 
37
#include <ksgrd/ColorPicker.h>
 
38
#include <ksgrd/SensorManager.h>
 
39
#include <ksgrd/StyleEngine.h>
 
40
 
 
41
#include "ListView.h"
 
42
#include "ListView.moc"
 
43
#include "ListViewSettings.h"
 
44
 
 
45
PrivateListViewItem::PrivateListViewItem(PrivateListView *parent)
 
46
        : QListViewItem(parent)
 
47
{
 
48
        _parent = parent;
 
49
}
 
50
 
 
51
int PrivateListViewItem::compare( QListViewItem *item, int col, bool ascending ) const
 
52
{
 
53
  int type = ((PrivateListView*)listView())->columnType( col );
 
54
 
 
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 ) );
 
58
    if ( prev < next )
 
59
      return -1;
 
60
    else if ( prev == next )
 
61
      return 0;
 
62
    else
 
63
      return 1;
 
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 ) );
 
67
    if ( prev < next )
 
68
      return -1;
 
69
    else
 
70
      return 1;
 
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;
 
77
    if ( prev < next )
 
78
      return -1;
 
79
    else if ( prev == next )
 
80
      return 0;
 
81
    else
 
82
      return 1;
 
83
  } else if ( type == PrivateListView::DiskStat ) {
 
84
    QString prev = key( col, ascending );
 
85
    QString next = item->key( col, ascending );
 
86
    QString prevKey, nextKey;
 
87
    
 
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() );
 
92
        break;
 
93
      }
 
94
 
 
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() );
 
99
        break;
 
100
      }
 
101
 
 
102
    return prevKey.compare( nextKey );
 
103
  } else
 
104
    return key( col, ascending ).localeAwareCompare( item->key( col, ascending ) );
 
105
}
 
106
 
 
107
PrivateListView::PrivateListView(QWidget *parent, const char *name)
 
108
        : QListView(parent, name)
 
109
{
 
110
        QColorGroup cg = colorGroup();
 
111
 
 
112
        cg.setColor(QColorGroup::Link, KSGRD::Style->firstForegroundColor());
 
113
        cg.setColor(QColorGroup::Text, KSGRD::Style->secondForegroundColor());
 
114
        cg.setColor(QColorGroup::Base, KSGRD::Style->backgroundColor());
 
115
 
 
116
        setPalette(QPalette(cg, cg, cg));
 
117
}
 
118
 
 
119
void PrivateListView::update(const QString& answer)
 
120
{
 
121
        clear();
 
122
 
 
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 ) );
 
132
      else
 
133
                          item->setText(j, records[j]);
 
134
    }
 
135
 
 
136
                insertItem(item);
 
137
        }
 
138
}
 
139
 
 
140
int PrivateListView::columnType( uint pos ) const
 
141
{
 
142
  if ( pos >= mColumnTypes.count() )
 
143
    return 0;
 
144
 
 
145
  if ( mColumnTypes[ pos ] == "d" || mColumnTypes[ pos ] == "D" )
 
146
    return Int;
 
147
  else if ( mColumnTypes[ pos ] == "f" || mColumnTypes[ pos ] == "F" )
 
148
    return Float;
 
149
  else if ( mColumnTypes[ pos ] == "t" )
 
150
    return Time;
 
151
  else if ( mColumnTypes[ pos ] == "M" )
 
152
    return DiskStat;
 
153
  else
 
154
    return Text;
 
155
}
 
156
 
 
157
void PrivateListView::removeColumns(void)
 
158
{
 
159
        for (int i = columns() - 1; i >= 0; --i)
 
160
                removeColumn(i);
 
161
}
 
162
 
 
163
void PrivateListView::addColumn(const QString& label, const QString& type)
 
164
{
 
165
        QListView::addColumn( label );
 
166
  int col = columns() - 1;
 
167
 
 
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);
 
178
        else
 
179
        {
 
180
                kdDebug(1215) << "Unknown type " << type << " of column " << label
 
181
                                  << " in ListView!" << endl;
 
182
                return;
 
183
        }
 
184
 
 
185
  mColumnTypes.append( type );
 
186
 
 
187
        /* Just use some sensible default values as initial setting. */
 
188
        QFontMetrics fm = fontMetrics();
 
189
        setColumnWidth(col, fm.width(label) + 10);
 
190
}
 
191
 
 
192
ListView::ListView(QWidget* parent, const char* name, const QString& title, int, int)
 
193
        : KSGRD::SensorDisplay(parent, name, title)
 
194
{
 
195
        setBackgroundColor(KSGRD::Style->backgroundColor());
 
196
 
 
197
        monitor = new PrivateListView( frame() );
 
198
        Q_CHECK_PTR(monitor);
 
199
        monitor->setSelectionMode(QListView::NoSelection);
 
200
        monitor->setItemMargin(2);
 
201
 
 
202
        setMinimumSize(50, 25);
 
203
 
 
204
        setPlotterWidget(monitor);
 
205
 
 
206
        setModified(false);
 
207
}
 
208
 
 
209
bool
 
210
ListView::addSensor(const QString& hostName, const QString& sensorName, const QString& sensorType, const QString& title)
 
211
{
 
212
        if (sensorType != "listview")
 
213
                return (false);
 
214
 
 
215
        registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
 
216
 
 
217
        setTitle(title);
 
218
 
 
219
        /* To differentiate between answers from value requests and info
 
220
         * requests we use 100 for info requests. */
 
221
        sendRequest(hostName, sensorName + "?", 100);
 
222
 
 
223
        setModified(true);
 
224
        return (true);
 
225
}
 
226
 
 
227
void
 
228
ListView::updateList()
 
229
{
 
230
        sendRequest(sensors().at(0)->hostName(), sensors().at(0)->name(), 19);
 
231
}
 
232
 
 
233
void
 
234
ListView::answerReceived(int id, const QString& answer)
 
235
{
 
236
        /* We received something, so the sensor is probably ok. */
 
237
        sensorError(id, false);
 
238
 
 
239
        switch (id)
 
240
        {
 
241
                case 100: {
 
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)
 
246
                        {
 
247
                                kdDebug(1215) << "wrong number of lines" << endl;
 
248
                                return;
 
249
                        }
 
250
                        KSGRD::SensorTokenizer headers(lines[0], '\t');
 
251
                        KSGRD::SensorTokenizer colTypes(lines[1], '\t');
 
252
 
 
253
                        /* remove all columns from list */
 
254
                        monitor->removeColumns();
 
255
 
 
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]);
 
260
                        break;
 
261
                }
 
262
                case 19: {
 
263
                        monitor->update(answer);
 
264
                        break;
 
265
                }
 
266
        }
 
267
}
 
268
 
 
269
void
 
270
ListView::resizeEvent(QResizeEvent*)
 
271
{
 
272
        frame()->setGeometry(0, 0, width(), height());
 
273
        monitor->setGeometry(10, 20, width() - 20, height() - 30);
 
274
}
 
275
 
 
276
bool
 
277
ListView::restoreSettings(QDomElement& element)
 
278
{
 
279
        addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "listview" : element.attribute("sensorType")), element.attribute("title"));
 
280
 
 
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()));
 
285
 
 
286
        monitor->setPalette(QPalette(colorGroup, colorGroup, colorGroup));
 
287
 
 
288
        SensorDisplay::restoreSettings(element);
 
289
 
 
290
        setModified(false);
 
291
 
 
292
        return (true);
 
293
}
 
294
 
 
295
bool
 
296
ListView::saveSettings(QDomDocument& doc, QDomElement& element, bool save)
 
297
{
 
298
        element.setAttribute("hostName", sensors().at(0)->hostName());
 
299
        element.setAttribute("sensorName", sensors().at(0)->name());
 
300
        element.setAttribute("sensorType", sensors().at(0)->type());
 
301
 
 
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));
 
306
 
 
307
        SensorDisplay::saveSettings(doc, element);
 
308
 
 
309
        if (save)
 
310
                setModified(false);
 
311
 
 
312
        return (true);
 
313
}
 
314
 
 
315
void
 
316
ListView::configureSettings()
 
317
{
 
318
        lvs = new ListViewSettings(this, "ListViewSettings");
 
319
        Q_CHECK_PTR(lvs);
 
320
        connect(lvs, SIGNAL(applyClicked()), SLOT(applySettings()));
 
321
 
 
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());
 
327
 
 
328
        if (lvs->exec())
 
329
                applySettings();
 
330
 
 
331
        delete lvs;
 
332
        lvs = 0;
 
333
}
 
334
 
 
335
void
 
336
ListView::applySettings()
 
337
{
 
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));
 
343
 
 
344
        setTitle(lvs->title());
 
345
 
 
346
        setModified(true);
 
347
}
 
348
 
 
349
void
 
350
ListView::applyStyle()
 
351
{
 
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));
 
357
 
 
358
        setModified(true);
 
359
}