Files
BionxControl/bcvaluemodel.cpp

245 lines
5.6 KiB
C++
Raw Normal View History

2025-12-17 16:26:22 +01:00
/***************************************************************************
2025-12-26 14:07:55 +01:00
BionxControl
Copyright © 2025 christoph holzheuer
christoph.holzheuer@gmail.com
2025-12-17 16:26:22 +01:00
2025-12-26 14:07:55 +01:00
Using:
2025-12-17 16:26:22 +01:00
2025-12-26 14:07:55 +01:00
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
2025-12-17 16:26:22 +01:00
2025-12-26 14:07:55 +01:00
Based on Bionx data type descriptions from:
2025-12-17 16:26:22 +01:00
2025-12-26 14:07:55 +01:00
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
2025-12-17 16:26:22 +01:00
2025-12-26 14:07:55 +01:00
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
2025-12-17 16:26:22 +01:00
***************************************************************************/
2025-12-26 15:19:33 +01:00
#include <bcvaluemodel.h>
2025-12-29 20:10:05 +01:00
#include <bcvaluetype.h>
2025-12-17 16:26:22 +01:00
2025-12-29 13:05:35 +01:00
/**
* @brief Konstruktor, ohne Besonderheiten
* @param parent Das Elternobject
*/
2025-12-17 16:26:22 +01:00
2025-12-26 15:19:33 +01:00
BCValueModel::BCValueModel(QObject *parent)
2025-12-24 14:44:54 +01:00
: QAbstractTableModel(parent)
{
}
2025-12-17 16:26:22 +01:00
2025-12-29 23:29:56 +01:00
/**
* @brief Setzt den Headerlabel ( == die Devicebezeichnung )
* @param headerLabel
*/
void BCValueModel::setHeaderLabel( const QString& headerLabel )
{
_headerLabel = headerLabel;
}
2025-12-26 15:19:33 +01:00
2025-12-29 13:05:35 +01:00
/**
* @brief Einen Einzelwert hinzufügen
* @param val der neue Wert
*/
2025-12-28 22:48:18 +01:00
void BCValueModel::addValue(const BCValue& val)
2025-12-17 16:26:22 +01:00
{
int row = _valueList.size();
beginInsertRows(QModelIndex(), row, row);
_valueList.append(val);
endInsertRows();
}
2025-12-26 15:19:33 +01:00
2025-12-29 13:05:35 +01:00
/**
* @brief Gibt die interne Werteliste als const ref zurück
* @return Die WerteListe
*/
2025-12-28 22:48:18 +01:00
const BCValueList& BCValueModel::getValueList() const
2025-12-26 23:09:53 +01:00
{
return _valueList;
}
2025-12-29 13:05:35 +01:00
/**
* @brief Nimmt eine Werteliste in Besitz.
* @param newValueList Die Wertelist. Nach dem Aufruf leer.
*/
2025-12-28 14:42:12 +01:00
void BCValueModel::takeValueList(BCValueList& newValueList)
2025-12-17 16:26:22 +01:00
{
beginResetModel();
2025-12-28 14:42:12 +01:00
// hier nehmen wir die valueList in Besitz.
_valueList = std::exchange(newValueList, {} );
2025-12-17 16:26:22 +01:00
endResetModel();
}
2025-12-28 22:48:18 +01:00
2025-12-29 13:05:35 +01:00
/**
* @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
*/
2025-12-28 22:48:18 +01:00
2025-12-29 13:05:35 +01:00
void BCValueModel::onValueUpdated( int row, BC::State state, const QString& newVisisbleValue )
{
2025-12-29 20:10:05 +01:00
qDebug() << " BCValueModel::onValueUpdated update: " << newVisisbleValue;
2025-12-29 13:05:35 +01:00
if( row > -1 && row < _valueList.size() )
{
const BCValue& value = _valueList[row];
QModelIndex idx = index(row,1);
value.state = state;
if( !newVisisbleValue.isEmpty() && newVisisbleValue != value.visibleValue )
{
value.visibleValue = newVisisbleValue;
2025-12-28 22:48:18 +01:00
}
2025-12-29 13:05:35 +01:00
// wir schicken auf jeden fall einen update request
emit dataChanged(idx, idx, {Qt::DisplayRole, Qt::EditRole});
2025-12-28 22:48:18 +01:00
}
}
2025-12-19 17:37:24 +01:00
2025-12-29 13:05:35 +01:00
/**
* @brief Gibt die Zeilenanzahl zurück
* @param parent Der Elternindex
* @return die Zeilenanzahl
*/
2025-12-26 15:19:33 +01:00
int BCValueModel::rowCount(const QModelIndex& parent) const
2025-12-17 16:26:22 +01:00
{
2025-12-19 17:37:24 +01:00
if (parent.isValid())
return 0;
2025-12-17 16:26:22 +01:00
return _valueList.size();
}
2025-12-29 13:05:35 +01:00
/**
* @brief Gibt die Spaltenanzahl zurück
* @param parent Der Elternindex
* @return die Spaltenanzahl
*/
int BCValueModel::columnCount(const QModelIndex& parent) const
2025-12-24 14:44:54 +01:00
{
2025-12-29 13:05:35 +01:00
if (parent.isValid())
return 0;
2025-12-24 14:44:54 +01:00
return 2;
}
2025-12-29 13:05:35 +01:00
/**
* @brief Gibt die Header-Einträge zurück
*/
2025-12-26 15:19:33 +01:00
QVariant BCValueModel::headerData(int section, Qt::Orientation orientation, int role) const
2025-12-24 14:44:54 +01:00
{
2025-12-29 23:29:56 +01:00
if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section != 0)
2025-12-24 14:44:54 +01:00
return QVariant();
2025-12-29 23:29:56 +01:00
return _headerLabel;
2025-12-24 14:44:54 +01:00
}
2025-12-29 15:44:06 +01:00
/**
2025-12-29 20:10:05 +01:00
* @brief Gibt die Model-Daten zurück.
2025-12-29 15:44:06 +01:00
*/
2025-12-26 15:19:33 +01:00
QVariant BCValueModel::data(const QModelIndex& index, int role) const
2025-12-17 16:26:22 +01:00
{
2025-12-29 15:44:06 +01:00
Q_UNUSED(role)
2025-12-29 20:10:05 +01:00
int row = index.row();
int col = index.column();
2025-12-24 15:43:50 +01:00
2025-12-29 20:10:05 +01:00
bool wrongRole = ( role != Qt::DisplayRole && role != Qt::EditRole);
if (wrongRole || !index.isValid() || row >= _valueList.size() )
2025-12-17 16:26:22 +01:00
return QVariant();
2025-12-29 20:10:05 +01:00
const BCValue& value = _valueList.at( row );
2025-12-29 13:05:35 +01:00
2025-12-29 20:10:05 +01:00
if( col == 0 )
return value.label;
if( col == 1)
2025-12-24 15:43:50 +01:00
{
2025-12-29 20:10:05 +01:00
if( role == Qt::DisplayRole )
return QString("%1 %2").arg( value.visibleValue, value.valueType->unitLabel);
return value.visibleValue;
2025-12-24 15:43:50 +01:00
}
2025-12-29 15:44:06 +01:00
2025-12-29 20:10:05 +01:00
return QVariant();
/*
Das Model Gibt hier, unabhängig von der DataRole, immer das
* gesamte BCValue Object zurück. Die Umsetzung von Status- und Typeinfromationen in GUI-Elemente
* findet nicht hier, sondern im BCDelagate statt.
// falsch! wie geben hier doch ordentlich die einzelwerte zurück
// Bonus: Rechtsbündig für Zahlen
if (role == Qt::TextAlignmentRole && (index.column() == 0 || index.column() == 2)) {
return Qt::AlignRight | Qt::AlignVCenter;
}
//return QVariant::fromValue( entry );
return QVariant();
2025-12-17 16:26:22 +01:00
2025-12-29 20:10:05 +01:00
*/
2025-12-17 16:26:22 +01:00
}
2025-12-20 01:23:57 +01:00
2025-12-26 15:19:33 +01:00
Qt::ItemFlags BCValueModel::flags(const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2025-12-20 01:23:57 +01:00
if (!index.isValid())
return Qt::NoItemFlags;
2025-12-24 14:44:54 +01:00
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
2025-12-17 16:26:22 +01:00
}
2025-12-20 01:23:57 +01:00
2025-12-26 15:19:33 +01:00
bool BCValueModel::setData(const QModelIndex& index, const QVariant& value, int role)
2025-12-17 16:26:22 +01:00
{
2025-12-29 13:05:35 +01:00
// __fix!
2025-12-20 01:23:57 +01:00
if (index.isValid() && role == Qt::EditRole)
{
2025-12-28 22:48:18 +01:00
BCValue& item = _valueList[index.row()];
2025-12-17 16:26:22 +01:00
// Wir erwarten hier nur den Value-Teil (vom Slider/Editor)
// Checken ob Int oder Double
2025-12-20 01:23:57 +01:00
if (value.canConvert<double>())
{
2025-12-24 12:11:59 +01:00
item.visibleValue = value.toString();
2025-12-17 16:26:22 +01:00
}
_valueList[index.row()] = item;
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
return true;
}
return false;
}