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
&index
)
202 if ( !index
.isValid() )
205 if ( index
.row() < 0 || index
.row() >= mSensors
.count() )
208 mSensors
[ index
.row() ] = sensor
;
210 emit
dataChanged( index
, index
);
213 void SensorModel::removeSensor( const QModelIndex
&index
)
215 if ( !index
.isValid() )
218 if ( index
.row() < 0 || index
.row() >= mSensors
.count() )
221 beginRemoveRows( QModelIndex(), index
.row(), index
.row());
222 int id
= mSensors
[index
.row() ].id();
225 mSensors
.removeAt( index
.row() );
226 for(int i
= 0; i
< mSensors
.count(); i
++) {
227 if(mSensors
[i
].id() > id
)
228 mSensors
[i
].setId(mSensors
[i
].id()-1);
234 void SensorModel::moveDownSensor(const QModelIndex
&sindex
)
236 int row
= sindex
.row();
237 if(row
>= mSensors
.count()) return;
238 mSensors
.move(row
, row
+1);
240 for( int i
= 0; i
< columnCount(); i
++)
241 changePersistentIndex(index(row
, i
), index(row
+1, i
));
243 emit
dataChanged(sindex
, index(row
+1, columnCount()-1));
245 void SensorModel::moveUpSensor(const QModelIndex
&sindex
)
247 int row
= sindex
.row();
249 mSensors
.move(row
, row
-1);
250 for( int i
= 0; i
< columnCount(); i
++)
251 changePersistentIndex(index(row
, i
), index(row
-1, i
));
252 emit
dataChanged(sindex
, index(row
-1, columnCount()-1));
254 QList
<int> SensorModel::deleted() const
259 void SensorModel::clearDeleted()
263 QList
<int> SensorModel::order() const
266 for(int i
= 0; i
< mSensors
.count(); i
++)
268 newOrder
.append(mSensors
[i
].id());
273 void SensorModel::resetOrder() {
274 //Renumber the items 3, 2, 1, 0 etc
275 for(int i
= 0; i
< mSensors
.count(); i
++)
277 mSensors
[i
].setId(i
);
282 SensorModelEntry
SensorModel::sensor( const QModelIndex
&index
) const
284 if ( !index
.isValid() || index
.row() >= mSensors
.count() || index
.row() < 0 )
285 return SensorModelEntry();
287 return mSensors
[ index
.row() ];
290 void SensorModel::setHasLabel( bool hasLabel
)
292 mHasLabel
= hasLabel
;
297 #include "SensorModel.moc"