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>
31 #include <kmessagebox.h>
32 #include <ksgrd/SensorManager.h>
33 #include "StyleEngine.h"
36 #include <QHeaderView>
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);
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
);
77 ListView::addSensor(const QString
& hostName
, const QString
& sensorName
, const QString
& sensorType
, const QString
& title
)
79 if (sensorType
!= "listview")
81 if(sensorName
.isEmpty()) return false;
83 kDebug() << "addSensor and sensorName is " << sensorName
;
84 registerSensor(new KSGRD::SensorProperties(hostName
, sensorName
, sensorType
, 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);
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" )
105 else if ( type
== "f" || type
== "F" )
107 else if ( type
== "t" )
109 else if ( type
== "M" )
116 ListView::answerReceived(int id
, const QList
<QByteArray
>& answer
)
118 /* We received something, so the sensor is probably ok. */
119 sensorError(id
, false);
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";
131 KSGRD::SensorTokenizer
headers(answer
[0], '\t');
132 KSGRD::SensorTokenizer
colTypes(answer
[1], '\t');
134 /* add the new columns */
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() );
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
] ) {
161 item
->setData(records
[j
].toInt(), Qt::DisplayRole
);
164 item
->setData(records
[j
].toFloat(), Qt::DisplayRole
);
167 item
->setData(QTime::fromString(records
[j
]), Qt::DisplayRole
);
172 item
->setText(records
[j
]);
174 mModel
.setItem(i
, j
, item
);
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
);
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
);
231 ListView::configureSettings()
233 lvs
= new ListViewSettings(this, "ListViewSettings");
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());
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());
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 );*/