2
KSysGuard, the KDE System Guard
4
Copyright (c) 2006 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 as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
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 <QtGui/QPixmap>
26
#include "SensorModel.h"
28
void SensorModelEntry::setId( int id )
33
int SensorModelEntry::id() const
38
void SensorModelEntry::setHostName( const QString &hostName )
43
QString SensorModelEntry::hostName() const
48
void SensorModelEntry::setSensorName( const QString &sensorName )
50
mSensorName = sensorName;
53
QString SensorModelEntry::sensorName() const
58
void SensorModelEntry::setLabel( const QString &label )
63
QString SensorModelEntry::label() const
68
void SensorModelEntry::setUnit( const QString &unit )
73
QString SensorModelEntry::unit() const
78
void SensorModelEntry::setStatus( const QString &status )
83
QString SensorModelEntry::status() const
88
void SensorModelEntry::setColor( const QColor &color )
93
QColor SensorModelEntry::color() const
98
SensorModel::SensorModel( QObject *parent )
99
: QAbstractTableModel( parent ), mHasLabel( false )
103
int SensorModel::columnCount( const QModelIndex& ) const
111
int SensorModel::rowCount( const QModelIndex& ) const
113
return mSensors.count();
116
QVariant SensorModel::data( const QModelIndex &index, int role ) const
118
if ( !index.isValid() )
121
if ( index.row() >= mSensors.count() || index.row() < 0 )
124
SensorModelEntry sensor = mSensors[ index.row() ];
126
if ( role == Qt::DisplayRole ) {
127
switch ( index.column() ) {
129
return sensor.hostName();
132
return sensor.sensorName();
135
return sensor.unit();
138
return sensor.status();
141
return sensor.label();
144
} else if ( role == Qt::DecorationRole ) {
145
if ( index.column() == 1 ) {
146
if ( sensor.color().isValid() ) {
147
QPixmap pm( 12, 12 );
148
pm.fill( sensor.color() );
158
QVariant SensorModel::headerData( int section, Qt::Orientation orientation, int role ) const
160
if ( orientation == Qt::Vertical )
163
if ( role == Qt::DisplayRole ) {
166
return i18n( "Host" );
169
return i18n( "Sensor" );
172
return i18n( "Unit" );
175
return i18n( "Status" );
178
return i18n( "Label" );
188
void SensorModel::setSensors( const SensorModelEntry::List &sensors )
192
emit layoutChanged();
195
SensorModelEntry::List SensorModel::sensors() const
200
void SensorModel::setSensor( const SensorModelEntry &sensor, const QModelIndex &sindex )
202
if ( !sindex.isValid() )
205
int row = sindex.row();
206
if ( row < 0 || row >= mSensors.count() )
209
mSensors[row] = sensor;
211
emit dataChanged( index(row,0), index(row, columnCount()-1));
214
void SensorModel::removeSensor( const QModelIndex &index )
216
if ( !index.isValid() )
219
if ( index.row() < 0 || index.row() >= mSensors.count() )
222
beginRemoveRows( QModelIndex(), index.row(), index.row());
223
int id = mSensors[index.row() ].id();
226
mSensors.removeAt( index.row() );
227
for(int i = 0; i < mSensors.count(); i++) {
228
if(mSensors[i].id() > id)
229
mSensors[i].setId(mSensors[i].id()-1);
235
void SensorModel::moveDownSensor(const QModelIndex &sindex)
237
int row = sindex.row();
238
if(row >= mSensors.count()) return;
239
mSensors.move(row, row+1);
241
for( int i = 0; i < columnCount(); i++)
242
changePersistentIndex(index(row, i), index(row+1, i));
244
emit dataChanged(sindex, index(row+1, columnCount()-1));
246
void SensorModel::moveUpSensor(const QModelIndex &sindex)
248
int row = sindex.row();
250
mSensors.move(row, row-1);
251
for( int i = 0; i < columnCount(); i++)
252
changePersistentIndex(index(row, i), index(row-1, i));
253
emit dataChanged(sindex, index(row-1, columnCount()-1));
255
QList<int> SensorModel::deleted() const
260
void SensorModel::clearDeleted()
264
QList<int> SensorModel::order() const
267
for(int i = 0; i < mSensors.count(); i++)
269
newOrder.append(mSensors[i].id());
274
void SensorModel::resetOrder() {
275
//Renumber the items 3, 2, 1, 0 etc
276
for(int i = 0; i < mSensors.count(); i++)
278
mSensors[i].setId(i);
283
SensorModelEntry SensorModel::sensor( const QModelIndex &index ) const
285
if ( !index.isValid() || index.row() >= mSensors.count() || index.row() < 0 )
286
return SensorModelEntry();
288
return mSensors[ index.row() ];
291
void SensorModel::setHasLabel( bool hasLabel )
293
mHasLabel = hasLabel;
298
#include "SensorModel.moc"