dtor first
[personal-kdebase.git] / workspace / ksysguard / gui / SensorDisplayLib / SensorModel.cc
blob555cafda5c6c89c49ff4dd2ebaddce7139f65d3d
1 /*
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>
24 #include <klocale.h>
26 #include "SensorModel.h"
28 void SensorModelEntry::setId( int id )
30 mId = id;
33 int SensorModelEntry::id() const
35 return mId;
38 void SensorModelEntry::setHostName( const QString &hostName )
40 mHostName = hostName;
43 QString SensorModelEntry::hostName() const
45 return mHostName;
48 void SensorModelEntry::setSensorName( const QString &sensorName )
50 mSensorName = sensorName;
53 QString SensorModelEntry::sensorName() const
55 return mSensorName;
58 void SensorModelEntry::setLabel( const QString &label )
60 mLabel = label;
63 QString SensorModelEntry::label() const
65 return mLabel;
68 void SensorModelEntry::setUnit( const QString &unit )
70 mUnit = unit;
73 QString SensorModelEntry::unit() const
75 return mUnit;
78 void SensorModelEntry::setStatus( const QString &status )
80 mStatus = status;
83 QString SensorModelEntry::status() const
85 return mStatus;
88 void SensorModelEntry::setColor( const QColor &color )
90 mColor = color;
93 QColor SensorModelEntry::color() const
95 return mColor;
98 SensorModel::SensorModel( QObject *parent )
99 : QAbstractTableModel( parent ), mHasLabel( false )
103 int SensorModel::columnCount( const QModelIndex& ) const
105 if ( mHasLabel )
106 return 5;
107 else
108 return 4;
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() )
119 return QVariant();
121 if ( index.row() >= mSensors.count() || index.row() < 0 )
122 return QVariant();
124 SensorModelEntry sensor = mSensors[ index.row() ];
126 if ( role == Qt::DisplayRole ) {
127 switch ( index.column() ) {
128 case 0:
129 return sensor.hostName();
130 break;
131 case 1:
132 return sensor.sensorName();
133 break;
134 case 2:
135 return sensor.unit();
136 break;
137 case 3:
138 return sensor.status();
139 break;
140 case 4:
141 return sensor.label();
142 break;
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() );
150 return pm;
155 return QVariant();
158 QVariant SensorModel::headerData( int section, Qt::Orientation orientation, int role ) const
160 if ( orientation == Qt::Vertical )
161 return QVariant();
163 if ( role == Qt::DisplayRole ) {
164 switch ( section ) {
165 case 0:
166 return i18n( "Host" );
167 break;
168 case 1:
169 return i18n( "Sensor" );
170 break;
171 case 2:
172 return i18n( "Unit" );
173 break;
174 case 3:
175 return i18n( "Status" );
176 break;
177 case 4:
178 return i18n( "Label" );
179 break;
180 default:
181 return QVariant();
185 return QVariant();
188 void SensorModel::setSensors( const SensorModelEntry::List &sensors )
190 mSensors = sensors;
192 emit layoutChanged();
195 SensorModelEntry::List SensorModel::sensors() const
197 return mSensors;
200 void SensorModel::setSensor( const SensorModelEntry &sensor, const QModelIndex &index )
202 if ( !index.isValid() )
203 return;
205 if ( index.row() < 0 || index.row() >= mSensors.count() )
206 return;
208 mSensors[ index.row() ] = sensor;
210 emit dataChanged( index, index );
213 void SensorModel::removeSensor( const QModelIndex &index )
215 if ( !index.isValid() )
216 return;
218 if ( index.row() < 0 || index.row() >= mSensors.count() )
219 return;
221 beginRemoveRows( QModelIndex(), index.row(), index.row());
222 int id = mSensors[index.row() ].id();
223 mDeleted.append(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);
230 endRemoveRows();
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();
248 if(row <= 0) return;
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
256 return mDeleted;
259 void SensorModel::clearDeleted()
261 mDeleted.clear();
263 QList<int> SensorModel::order() const
265 QList<int> newOrder;
266 for(int i = 0; i < mSensors.count(); i++)
268 newOrder.append(mSensors[i].id());
270 return newOrder;
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);
279 reset();
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"