add more spacing
[personal-kdebase.git] / workspace / ksysguard / gui / SensorDisplayLib / ListView.cc
blob3e7fa25bc1791cbeb484d2c55e513f39e8b36bf6
1 /*
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 the GNU General Public
8 License version 2 or at your option version 3 as published by
9 the Free Software Foundation.
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.
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.
22 #include <QDomElement>
24 #include <QVBoxLayout>
25 #include <QSortFilterProxyModel>
26 #include <QTime>
28 #include <kdebug.h>
29 #include <kglobal.h>
30 #include <klocale.h>
31 #include <kmessagebox.h>
32 #include <ksgrd/SensorManager.h>
33 #include "StyleEngine.h"
35 #include <QTreeView>
36 #include <QHeaderView>
37 #include "ListView.h"
38 #include "ListView.moc"
39 #include "ListViewSettings.h"
41 ListView::ListView(QWidget* parent, const QString& title, SharedSettings *workSheetSettings)
42 : KSGRD::SensorDisplay(parent, title, workSheetSettings)
44 QVBoxLayout *layout = new QVBoxLayout(this);
45 mView = new QTreeView(this);
46 //QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
47 // proxyModel->setSourceModel(&mModel);
48 mView->setModel(&mModel);
49 layout->addWidget(mView);
50 this->setLayout(layout);
52 mView->setContextMenuPolicy( Qt::CustomContextMenu );
53 connect(mView, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
54 connect(mView, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
56 mView->setAlternatingRowColors(true);
57 mView->header()->setMovable(true);
58 mView->setSelectionMode( QAbstractItemView::NoSelection );
59 mView->setUniformRowHeights(true);
60 mView->setRootIsDecorated(false);
61 mView->header()->setSortIndicatorShown(true);
62 mView->header()->setClickable(true);
63 mView->setSortingEnabled(true);
65 /* QPalette palette;
66 palette.setColor(backgroundRole(), KSGRD::Style->backgroundColor());
67 setPalette(palette);*/
69 setMinimumSize(50, 25);
71 setPlotterWidget(mView);
72 setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
73 mView->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
76 bool
77 ListView::addSensor(const QString& hostName, const QString& sensorName, const QString& sensorType, const QString& title)
79 if (sensorType != "listview")
80 return false;
81 if(sensorName.isEmpty()) return false;
83 kDebug() << "addSensor and sensorName is " << sensorName;
84 registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
86 setTitle(title);
88 /* To differentiate between answers from value requests and info
89 * requests we use 100 for info requests. */
90 sendRequest(hostName, sensorName + '?', 100);
91 sendRequest(hostName, sensorName, 19);
92 return (true);
95 void ListView::updateList()
97 for(int i = 0; i < sensors().count(); i++)
98 sendRequest(sensors().at(i)->hostName(), sensors().at(i)->name(), 19);
101 ListView::ColumnType ListView::convertColumnType(const QString &type) const
103 if ( type == "d" || type == "D" )
104 return Int;
105 else if ( type == "f" || type == "F" )
106 return Float;
107 else if ( type == "t" )
108 return Time;
109 else if ( type == "M" )
110 return DiskStat;
111 else
112 return Text;
115 void
116 ListView::answerReceived(int id, const QList<QByteArray>& answer)
118 /* We received something, so the sensor is probably ok. */
119 sensorError(id, false);
121 switch (id)
123 case 100: {
124 /* We have received the answer to a '?' command that contains
125 * the information about the table headers. */
126 if (answer.count() != 2)
128 kDebug(1215) << "wrong number of lines";
129 return;
131 KSGRD::SensorTokenizer headers(answer[0], '\t');
132 KSGRD::SensorTokenizer colTypes(answer[1], '\t');
134 /* add the new columns */
135 mModel.clear();
136 QStringList translatedHeaders;
137 for (uint i = 0; i < headers.count(); i++) {
138 translatedHeaders.append( i18nc("heading from daemon", headers[i]) );
141 for(uint i =0 ; i < colTypes.count(); i++) {
142 mColumnTypes.append(convertColumnType(colTypes[i]));
145 mModel.setHorizontalHeaderLabels(translatedHeaders);
146 //If we have some header settings to restore, we can do so now
147 if(!mHeaderSettings.isEmpty()) {
148 mView->header()->restoreState(mHeaderSettings);
149 mModel.sort( mView->header()->sortIndicatorSection(), mView->header()->sortIndicatorOrder() );
151 break;
153 case 19: {
154 for (int i = 0; i < answer.count(); i++) {
155 KSGRD::SensorTokenizer records(answer[i], '\t');
156 for (uint j = 0; j < records.count(); j++) {
157 QStandardItem *item = new QStandardItem();
158 item->setEditable(false);
159 switch( mColumnTypes[j] ) {
160 case Int:
161 item->setData(records[j].toInt(), Qt::DisplayRole);
162 break;
163 case Float:
164 item->setData(records[j].toFloat(), Qt::DisplayRole);
165 break;
166 case Time:
167 item->setData(QTime::fromString(records[j]), Qt::DisplayRole);
168 break;
169 case DiskStat:
170 case Text:
171 default:
172 item->setText(records[j]);
174 mModel.setItem(i, j, item);
177 break;
182 bool
183 ListView::restoreSettings(QDomElement& element)
185 kDebug() << "restore settings";
186 addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "listview" : element.attribute("sensorType")), element.attribute("title"));
188 //At this stage, we don't have the heading information, so we cannot setup the headers yet.
189 //Save the info, the restore later.
190 mHeaderSettings = QByteArray::fromBase64(element.attribute("treeViewHeader").toLatin1());
192 /* QPalette pal = monitor->palette();
193 pal.setColor(QPalette::Link, restoreColor(element, "gridColor",
194 KSGRD::Style->firstForegroundColor()));
195 pal.setColor(QPalette::Text, restoreColor(element, "textColor",
196 KSGRD::Style->secondForegroundColor()));
197 pal.setColor(QPalette::Base, restoreColor(element, "backgroundColor",
198 KSGRD::Style->backgroundColor()));
200 monitor->setPalette( pal );
202 SensorDisplay::restoreSettings(element);
204 return true;
207 bool
208 ListView::saveSettings(QDomDocument& doc, QDomElement& element)
210 kDebug() << "save settings";
211 if(!sensors().isEmpty()) {
212 element.setAttribute("hostName", sensors().at(0)->hostName());
213 element.setAttribute("sensorName", sensors().at(0)->name());
214 element.setAttribute("sensorType", sensors().at(0)->type());
216 kDebug() << "sensorName is " << sensors().at(0)->name();
218 /* QPalette pal = monitor->palette();
219 saveColor(element, "gridColor", pal.color(QPalette::Link));
220 saveColor(element, "textColor", pal.color(QPalette::Text));
221 saveColor(element, "backgroundColor", pal.color(QPalette::Base));
224 element.setAttribute("treeViewHeader", QString::fromLatin1(mView->header()->saveState().toBase64()));
226 SensorDisplay::saveSettings(doc, element);
227 return true;
230 void
231 ListView::configureSettings()
233 lvs = new ListViewSettings(this, "ListViewSettings");
234 Q_CHECK_PTR(lvs);
235 connect(lvs, SIGNAL(applyClicked()), SLOT(applySettings()));
237 /* QPalette pal = monitor->palette();
238 lvs->setGridColor(pal.color(QPalette::Link));
239 lvs->setTextColor(pal.color(QPalette::Text));
240 lvs->setBackgroundColor(pal.color(QPalette::Base));
241 lvs->setTitle(title());
243 if (lvs->exec())
244 applySettings();
246 delete lvs;
247 lvs = 0;
250 void
251 ListView::applySettings()
253 /* QPalette pal = monitor->palette();
254 pal.setColor(QPalette::Active, QPalette::Link, lvs->gridColor());
255 pal.setColor(QPalette::Active, QPalette::Text, lvs->textColor());
256 pal.setColor(QPalette::Active, QPalette::Base, lvs->backgroundColor());
258 pal.setColor(QPalette::Disabled, QPalette::Link, lvs->gridColor());
259 pal.setColor(QPalette::Disabled, QPalette::Text, lvs->textColor());
260 pal.setColor(QPalette::Disabled, QPalette::Base, lvs->backgroundColor());
262 pal.setColor(QPalette::Inactive, QPalette::Link, lvs->gridColor());
263 pal.setColor(QPalette::Inactive, QPalette::Text, lvs->textColor());
264 pal.setColor(QPalette::Inactive, QPalette::Base, lvs->backgroundColor());
266 monitor->setPalette( pal );
268 setTitle(lvs->title());
271 void
272 ListView::applyStyle()
274 /* QPalette pal = monitor->palette();
275 pal.setColor(QPalette::Link, KSGRD::Style->firstForegroundColor());
276 pal.setColor(QPalette::Text, KSGRD::Style->secondForegroundColor());
277 pal.setColor(QPalette::Base, KSGRD::Style->backgroundColor());
278 monitor->setPalette( pal );*/