~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/SensorModel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
 
 
22
#include <QtGui/QPixmap>
 
23
 
 
24
#include <klocale.h>
 
25
 
 
26
#include "SensorModel.h"
 
27
 
 
28
void SensorModelEntry::setId( int id )
 
29
{
 
30
  mId = id;
 
31
}
 
32
 
 
33
int SensorModelEntry::id() const
 
34
{
 
35
  return mId;
 
36
}
 
37
 
 
38
void SensorModelEntry::setHostName( const QString &hostName )
 
39
{
 
40
  mHostName = hostName;
 
41
}
 
42
 
 
43
QString SensorModelEntry::hostName() const
 
44
{
 
45
  return mHostName;
 
46
}
 
47
 
 
48
void SensorModelEntry::setSensorName( const QString &sensorName )
 
49
{
 
50
  mSensorName = sensorName;
 
51
}
 
52
 
 
53
QString SensorModelEntry::sensorName() const
 
54
{
 
55
  return mSensorName;
 
56
}
 
57
 
 
58
void SensorModelEntry::setLabel( const QString &label )
 
59
{
 
60
  mLabel = label;
 
61
}
 
62
 
 
63
QString SensorModelEntry::label() const
 
64
{
 
65
  return mLabel;
 
66
}
 
67
 
 
68
void SensorModelEntry::setUnit( const QString &unit )
 
69
{
 
70
  mUnit = unit;
 
71
}
 
72
 
 
73
QString SensorModelEntry::unit() const
 
74
{
 
75
  return mUnit;
 
76
}
 
77
 
 
78
void SensorModelEntry::setStatus( const QString &status )
 
79
{
 
80
  mStatus = status;
 
81
}
 
82
 
 
83
QString SensorModelEntry::status() const
 
84
{
 
85
  return mStatus;
 
86
}
 
87
 
 
88
void SensorModelEntry::setColor( const QColor &color )
 
89
{
 
90
  mColor = color;
 
91
}
 
92
 
 
93
QColor SensorModelEntry::color() const
 
94
{
 
95
  return mColor;
 
96
}
 
97
 
 
98
SensorModel::SensorModel( QObject *parent )
 
99
  : QAbstractTableModel( parent ), mHasLabel( false )
 
100
{
 
101
}
 
102
 
 
103
int SensorModel::columnCount( const QModelIndex& ) const
 
104
{
 
105
  if ( mHasLabel )
 
106
    return 5;
 
107
  else
 
108
    return 4;
 
109
}
 
110
 
 
111
int SensorModel::rowCount( const QModelIndex& ) const
 
112
{
 
113
  return mSensors.count();
 
114
}
 
115
 
 
116
QVariant SensorModel::data( const QModelIndex &index, int role ) const
 
117
{
 
118
  if ( !index.isValid() )
 
119
    return QVariant();
 
120
 
 
121
  if ( index.row() >= mSensors.count() || index.row() < 0 )
 
122
    return QVariant();
 
123
 
 
124
  SensorModelEntry sensor = mSensors[ index.row() ];
 
125
 
 
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;
 
143
    }
 
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() );
 
149
 
 
150
        return pm;
 
151
      }
 
152
    }
 
153
  }
 
154
 
 
155
  return QVariant();
 
156
}
 
157
 
 
158
QVariant SensorModel::headerData( int section, Qt::Orientation orientation, int role ) const
 
159
{
 
160
  if ( orientation == Qt::Vertical )
 
161
    return QVariant();
 
162
 
 
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();
 
182
    }
 
183
  }
 
184
 
 
185
  return QVariant();
 
186
}
 
187
 
 
188
void SensorModel::setSensors( const SensorModelEntry::List &sensors )
 
189
{
 
190
  mSensors = sensors;
 
191
 
 
192
  emit layoutChanged();
 
193
}
 
194
 
 
195
SensorModelEntry::List SensorModel::sensors() const
 
196
{
 
197
  return mSensors;
 
198
}
 
199
 
 
200
void SensorModel::setSensor( const SensorModelEntry &sensor, const QModelIndex &sindex )
 
201
{
 
202
  if ( !sindex.isValid() )
 
203
    return;
 
204
 
 
205
  int row = sindex.row();
 
206
  if ( row < 0 || row >= mSensors.count() )
 
207
    return;
 
208
 
 
209
  mSensors[row] = sensor;
 
210
 
 
211
  emit dataChanged( index(row,0), index(row, columnCount()-1));
 
212
}
 
213
 
 
214
void SensorModel::removeSensor( const QModelIndex &index )
 
215
{
 
216
  if ( !index.isValid() )
 
217
    return;
 
218
 
 
219
  if ( index.row() < 0 || index.row() >= mSensors.count() )
 
220
    return;
 
221
 
 
222
  beginRemoveRows( QModelIndex(), index.row(), index.row());
 
223
    int id = mSensors[index.row() ].id();
 
224
    mDeleted.append(id);
 
225
 
 
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);
 
230
    }
 
231
  endRemoveRows();
 
232
 
 
233
}
 
234
 
 
235
void SensorModel::moveDownSensor(const QModelIndex &sindex)
 
236
{
 
237
  int row = sindex.row();
 
238
  if(row >= mSensors.count()) return;
 
239
  mSensors.move(row, row+1);
 
240
  
 
241
  for( int i = 0; i < columnCount(); i++)
 
242
    changePersistentIndex(index(row, i), index(row+1, i));
 
243
 
 
244
  emit dataChanged(sindex, index(row+1, columnCount()-1));
 
245
}
 
246
void SensorModel::moveUpSensor(const QModelIndex &sindex)
 
247
{
 
248
  int row = sindex.row();
 
249
  if(row <= 0) return;
 
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));
 
254
}
 
255
QList<int> SensorModel::deleted() const
 
256
{
 
257
  return mDeleted;
 
258
}
 
259
 
 
260
void SensorModel::clearDeleted()
 
261
{
 
262
  mDeleted.clear();
 
263
}
 
264
QList<int> SensorModel::order() const
 
265
{
 
266
  QList<int> newOrder;
 
267
  for(int i = 0; i < mSensors.count(); i++)
 
268
  {
 
269
    newOrder.append(mSensors[i].id());
 
270
  }
 
271
  return newOrder;
 
272
 
 
273
}
 
274
void SensorModel::resetOrder() {
 
275
  //Renumber the items 3, 2, 1, 0  etc
 
276
  for(int i = 0; i < mSensors.count(); i++)
 
277
  {
 
278
    mSensors[i].setId(i);
 
279
  }
 
280
  reset();
 
281
}
 
282
 
 
283
SensorModelEntry SensorModel::sensor( const QModelIndex &index ) const
 
284
{
 
285
  if ( !index.isValid() || index.row() >= mSensors.count() || index.row() < 0 )
 
286
    return SensorModelEntry();
 
287
 
 
288
  return mSensors[ index.row() ];
 
289
}
 
290
 
 
291
void SensorModel::setHasLabel( bool hasLabel )
 
292
{
 
293
  mHasLabel = hasLabel;
 
294
}
 
295
 
 
296
 
 
297
 
 
298
#include "SensorModel.moc"