Redesign data structures, part I
This commit is contained in:
171
bcvaluemodel.cpp
Normal file
171
bcvaluemodel.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/***************************************************************************
|
||||
|
||||
BionxControl
|
||||
Copyright © 2025 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>
|
||||
|
||||
|
||||
BCValueModel::BCValueModel(QObject *parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void BCValueModel::addValue(const BCDataValue& val)
|
||||
{
|
||||
int row = _valueList.size();
|
||||
beginInsertRows(QModelIndex(), row, row);
|
||||
_valueList.append(val);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void BCValueModel::setValueList(BCValueList* valueList)
|
||||
{
|
||||
beginResetModel();
|
||||
_valueList = valueList;
|
||||
endResetModel();
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
BCValueList& BCValueModel::getValueList()
|
||||
{
|
||||
return _valueList;
|
||||
}
|
||||
*/
|
||||
|
||||
int BCValueModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
return _valueList.size();
|
||||
}
|
||||
|
||||
int BCValueModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) return 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
QVariant BCValueModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
|
||||
return QVariant();
|
||||
|
||||
switch (section)
|
||||
{
|
||||
case 0:
|
||||
return QString("Bezeichnung");
|
||||
case 1:
|
||||
return QString("Wert");
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
QVariant BCValueModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
/*
|
||||
|
||||
if (!index.isValid() || index.row() >= static_cast<int>(m_items.size()))
|
||||
return QVariant();
|
||||
|
||||
const auto& item = m_items.at(index.row());
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column()) {
|
||||
case 0: return item.id;
|
||||
case 1: return item.name;
|
||||
case 2: {
|
||||
// Hier nutzen wir QLocale für das Komma!
|
||||
return QLocale(QLocale::German).toString(item.value, 'f', 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bonus: Rechtsbündig für Zahlen
|
||||
if (role == Qt::TextAlignmentRole && (index.column() == 0 || index.column() == 2)) {
|
||||
return Qt::AlignRight | Qt::AlignVCenter;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
*/
|
||||
int row = index.row();
|
||||
int col = index.column();
|
||||
if (!index.isValid() || row >= _valueList.size())
|
||||
return QVariant();
|
||||
|
||||
BCDataValue& entry = const_cast<BCDataValue&>(_valueList.at( row ));
|
||||
entry.rowInModel = row;
|
||||
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
||||
{
|
||||
if( col == 0 )
|
||||
return entry.label;
|
||||
else if( col == 1 )
|
||||
return entry.visibleValue;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
Qt::ItemFlags BCValueModel::flags(const QModelIndex& index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
|
||||
bool BCValueModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||
{
|
||||
if (index.isValid() && role == Qt::EditRole)
|
||||
{
|
||||
BCDataValue& item = _valueList[index.row()];
|
||||
|
||||
// Wir erwarten hier nur den Value-Teil (vom Slider/Editor)
|
||||
// Checken ob Int oder Double
|
||||
if (value.canConvert<double>())
|
||||
{
|
||||
item.visibleValue = value.toString();
|
||||
}
|
||||
|
||||
_valueList[index.row()] = item;
|
||||
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user