QVariant SensorModel::data( const QModelIndex &index, int role ) const { if ( !index.isValid() ) return QVariant(); if ( index.row() >= mSensors.count() || index.row() < 0 ) return QVariant(); SensorModelEntry sensor = mSensors[ index.row() ]; if ( role == Qt::DisplayRole ) { switch ( index.column() ) { case 0: return sensor.hostName(); break; case 1: return sensor.sensorName(); break; case 2: return sensor.unit(); break; case 3: return sensor.status(); break; case 4: return sensor.label(); break; } } else if ( role == Qt::DecorationRole ) { if ( index.column() == 1 ) { if ( sensor.color().isValid() ) { QPixmap pm( 12, 12 ); pm.fill( sensor.color() ); return pm; } } } return QVariant(); }
void FancyPlotterSettings::editSensor() { if ( !mView->selectionModel() ) return; const QModelIndex index = mView->selectionModel()->currentIndex(); if ( !index.isValid() ) return; SensorModelEntry sensor = mModel->sensor( index ); KColorDialog dialog(this, true); connect(&dialog, SIGNAL(colorSelected(QColor)), this, SLOT(setColorForSelectedItem(QColor))); QColor color = sensor.color(); dialog.setColor(color); int result = dialog.exec(); if ( result == KColorDialog::Accepted ) sensor.setColor( dialog.color() ); //If it's not accepted, make sure we set the color back to how it was mModel->setSensor( sensor, index ); }