Files
BionxControl/bcvaluemodel.cpp

223 lines
5.0 KiB
C++
Raw Normal View History

2025-12-17 16:26:22 +01:00
/***************************************************************************
2025-12-26 14:07:55 +01:00
BionxControl
2026-01-03 23:51:14 +01:00
© 2025 -2026 christoph holzheuer
2025-12-26 14:07:55 +01:00
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>
2026-01-03 02:50:28 +01:00
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-26 15:19:33 +01:00
2025-12-17 16:26:22 +01:00
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
void BCValueModel::onValueUpdated( int row, BCValue::State state, const QString& newVisisbleValue )
2026-01-03 23:51:14 +01:00
{
2025-12-29 13:05:35 +01:00
if( row > -1 && row < _valueList.size() )
{
2026-01-03 23:51:14 +01:00
BCValue& value = *(_valueList[row].get());
2025-12-29 13:05:35 +01:00
QModelIndex idx = index(row,1);
2026-01-03 23:51:14 +01:00
2026-01-04 00:40:53 +01:00
//qDebug();
//qDebug() << " --- OLD:"<< newVisisbleValue;
//value.dumpValue();
2026-01-03 23:51:14 +01:00
value.state = state;
if( !newVisisbleValue.isEmpty() && newVisisbleValue != value.visibleValue )
2025-12-29 13:05:35 +01:00
{
2026-01-03 23:51:14 +01:00
value.visibleValue = newVisisbleValue;
2025-12-28 22:48:18 +01:00
}
2026-01-03 23:51:14 +01:00
2026-01-04 00:40:53 +01:00
//qDebug() << " --- NEW: " << newVisisbleValue;
//value.dumpValue();
2026-01-03 23:51:14 +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
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();
2026-01-03 02:50:28 +01:00
const BCValue& value = *(_valueList.at( row ).get());
2025-12-29 13:05:35 +01:00
2025-12-29 20:10:05 +01:00
if( col == 0 )
2026-01-03 02:50:28 +01:00
return value.label;
2025-12-29 20:10:05 +01:00
if( col == 1)
2025-12-24 15:43:50 +01:00
{
2025-12-29 20:10:05 +01:00
if( role == Qt::DisplayRole )
2026-01-03 02:50:28 +01:00
return QString("%1 %2").arg( value.visibleValue, value.unitLabel);
2025-12-29 20:10:05 +01:00
2026-01-03 02:50:28 +01:00
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)
{
2026-01-02 01:43:49 +01:00
BCValuePtr 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>())
{
2026-01-02 01:43:49 +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;
}