Files
BionxControl/bcvaluemodel.cpp

209 lines
4.8 KiB
C++

/***************************************************************************
BionxControl
© 2025 -2026 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
#include <bcvaluemodel.h>
/**
* @brief Konstruktor, ohne Besonderheiten
* @param parent Das Elternobject
*/
BCValueModel::BCValueModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
/**
* @brief Gibt die interne Werteliste als const ref zurück
* @return Die WerteListe
*/
const BCValueList& BCValueModel::getValueList() const
{
return _valueList;
}
/**
* @brief Nimmt eine Werteliste in Besitz.
* @param newValueList Die Wertelist. Nach dem Aufruf leer.
*/
void BCValueModel::takeValueList(BCValueList& newValueList)
{
beginResetModel();
// hier nehmen wir die valueList in Besitz.
_valueList = std::exchange(newValueList, {} );
endResetModel();
}
/**
* @brief SLOT, der aufgerufen wird, wenn sich ein Wert und/oder der Zustand eines Wertes geändert hat.
* Emitted 'dataChanged' um die zuständige View upzudaten.
*
* @param row Der Index des geänderten Wertes in der Liste
* @param state Der neue state des Wertes
* @param newValue Der neue sichtbare Zahlenwert, formatiert als QString, optionall
*/
void BCValueModel::updateValue(int row, BCValue::Flags newState, uint32_t rawValue )
{
if( row > -1 && row < _valueList.size() )
{
const BCValue& value = *(_valueList[row].get());
BCValue::Flags newFlags1 = BCValue::Flag::NoFlag;
BCValue::Flags newFlags2 = newState;
// Obacht hier!
//value.valueFlags = state;
value.setRawValue( rawValue );
QModelIndex idx = index(row,1);
// wir schicken auf jeden fall einen update request
emit dataChanged(idx, idx, {Qt::DisplayRole, Qt::EditRole});
}
}
/**
* @brief Gibt die Zeilenanzahl zurück
* @param parent Der Elternindex
* @return die Zeilenanzahl
*/
int BCValueModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return _valueList.size();
}
/**
* @brief Gibt die Spaltenanzahl zurück
* @param parent Der Elternindex
* @return die Spaltenanzahl
*/
int BCValueModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return 2;
}
/**
* @brief Gibt die Model-Daten zurück.
*/
QVariant BCValueModel::data(const QModelIndex& index, int role) const
{
Q_UNUSED(role)
int row = index.row();
int col = index.column();
bool wrongRole = ( role != Qt::DisplayRole && role != Qt::EditRole);
if (wrongRole || !index.isValid() || row >= _valueList.size() )
return QVariant();
const BCValue& value = *(_valueList.at( row ).get());
if( col == 0 )
return value.getLabel();
if( col == 1)
{
if( role == Qt::DisplayRole )
return QString("%1 %2").arg( value.formatValue(), value.getUnitLabel());
return value.formatValue();
}
return QVariant();
}
Qt::ItemFlags BCValueModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flag = Qt::NoItemFlags|Qt::ItemNeverHasChildren;
// die label spalte lässt sich nicht editieren
if (!index.isValid() || index.column() == 0 )
return flag;
int row = index.row();
if( row>-1 && row<_valueList.size() )
{
const BCValue& bcValue = *_valueList[row].get();
flag = bcValue.isReadOnly() ? flag : flag|Qt::ItemIsEditable|Qt::ItemIsEnabled;
}
return flag;
}
bool BCValueModel::setData(const QModelIndex& index, const QVariant& variant, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
BCValuePtr value = _valueList[index.row()];
qDebug() << "--- YES! " << variant.toInt();
// Wir erwarten hier nur den Value-Teil (vom Slider/Editor)
// Checken ob Int oder Double
if (variant.canConvert<int>())
{
qDebug() << "--- new VALUE: " << variant.toInt();
if( variant.toInt() == 42)
{
qDebug() << "--- YES! " << variant.toInt();
//emit makeSimonHappy();
}
// QUARK!
value->setRawValue( variant.toInt() );
}
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
return true;
}
return false;
}