From 09ff2f00bbb9a0b87894b3326d496bdf80c6e00d Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Wed, 17 Dec 2025 16:26:22 +0100 Subject: [PATCH] Added model & delegate --- bcvaluedelegate.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ bcvaluedelegate.h | 26 ++++++++++ bcvaluemodel.cpp | 95 ++++++++++++++++++++++++++++++++++++ bcvaluemodel.h | 63 ++++++++++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 bcvaluedelegate.cpp create mode 100644 bcvaluedelegate.h create mode 100644 bcvaluemodel.cpp create mode 100644 bcvaluemodel.h diff --git a/bcvaluedelegate.cpp b/bcvaluedelegate.cpp new file mode 100644 index 0000000..9a8aa9b --- /dev/null +++ b/bcvaluedelegate.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include + +#include "bcvaluedelegate.h" +#include "bcvalue.h" + + +BCValueDelegate::BCValueDelegate(QObject *parent) : QStyledItemDelegate(parent) {} + +// --- TEIL 1: DIE NORMALE ANZEIGE --- +QString BCValueDelegate::displayText(const QVariant &value, const QLocale &locale) const +{ + // Wir prüfen, ob im Variant unser Struct steckt + if (value.canConvert()) { + BCValue bc = value.value(); + + // Hier bauen wir den String zusammen, den man sieht, + // wenn KEIN Editor offen ist. + // Format: "Label: Wert Einheit" + return QString("%1: %2 %3").arg(bc.label, bc.value.toString(), "mm1"); + } + + // Fallback für normale Strings/Zahlen + return QStyledItemDelegate::displayText(value, locale); +} + +// --- TEIL 2: DER EDITOR (SLIDER) --- +QWidget *BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QVariant rawData = index.data(Qt::EditRole); + if (!rawData.canConvert()) + return QStyledItemDelegate::createEditor(parent, option, index); + + BCValue bc = rawData.value(); + + // Nur bei Integern den Slider-Editor bauen + if (bc.value.typeId() == QMetaType::Int) + { + QWidget *container = new QWidget(parent); + container->setAutoFillBackground(true); + + QHBoxLayout *layout = new QHBoxLayout(container); + layout->setContentsMargins(4, 0, 4, 0); + layout->setSpacing(10); + + // Linkes Label (Name) + QLabel *lblName = new QLabel(bc.label, container); + lblName->setFixedWidth(80); + + // Slider + QSlider *slider = new QSlider(Qt::Horizontal, container); + slider->setRange(0, 100); + slider->setObjectName("slider"); + + // Rechtes Label (Vorschau Wert + Einheit) + QLabel *lblUnit = new QLabel(container); + lblUnit->setFixedWidth(60); + lblUnit->setAlignment(Qt::AlignRight | Qt::AlignVCenter); + lblUnit->setObjectName("lblUnit"); + + layout->addWidget(lblName); + layout->addWidget(slider); + layout->addWidget(lblUnit); + + // Live-Update des Labels im Editor (aber noch kein Speichern im Model) + connect(slider, &QSlider::valueChanged, this, [=](int val){ + lblUnit->setText(QString("%1 %2").arg(val).arg("mm2")); + }); + + return container; + } + + return QStyledItemDelegate::createEditor(parent, option, index); +} + +void BCValueDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + // Daten vom Model in den Editor laden + BCValue bc = index.data(Qt::EditRole).value(); + + QSlider *slider = editor->findChild("slider"); + QLabel *lblUnit = editor->findChild("lblUnit"); + + if (slider && lblUnit) { + bool oldState = slider->blockSignals(true); + slider->setValue(bc.value.toInt()); + slider->blockSignals(oldState); + + lblUnit->setText(QString("%1 %2").arg(bc.value.toInt()).arg( "mm3")); + } else { + QStyledItemDelegate::setEditorData(editor, index); + } +} + +void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + // Daten vom Editor zurück ins Model speichern (Beim Schließen) + QSlider *slider = editor->findChild("slider"); + + if (slider) { + int value = slider->value(); + model->setData(index, value, Qt::EditRole); + } else { + QStyledItemDelegate::setModelData(editor, model, index); + } +} + +void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + editor->setGeometry(option.rect); +} diff --git a/bcvaluedelegate.h b/bcvaluedelegate.h new file mode 100644 index 0000000..8656621 --- /dev/null +++ b/bcvaluedelegate.h @@ -0,0 +1,26 @@ +// BCValueDelegate.h +#ifndef BCVALUEDELEGATE_H +#define BCVALUEDELEGATE_H + + +#include + +class BCValueDelegate : public QStyledItemDelegate +{ + Q_OBJECT + +public: + explicit BCValueDelegate(QObject *parent = nullptr); + + // WICHTIG: Zuständig für die normale Anzeige (ohne Editor) + QString displayText(const QVariant &value, const QLocale &locale) const override; + + // Zuständig für den Edit-Modus (Doppelklick) + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; +}; + + +#endif // BCVALUEDELEGATE_H diff --git a/bcvaluemodel.cpp b/bcvaluemodel.cpp new file mode 100644 index 0000000..133923f --- /dev/null +++ b/bcvaluemodel.cpp @@ -0,0 +1,95 @@ +/*************************************************************************** + + BionxControl + Copyright © 2025 christoph holzheuer + christoph.holzheuer@gmail.com + + Using: + + BigXionFlasher USB V 0.2.4 rev. 97 + © 2011-2013 by Thomas Koenig + @see www.bigxionflasher.org + + Bionx Bike Info + © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) + @see www.ts-soft.de + + mhs_can_drv.c 3.00 + © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany + Demlehner Klaus, info@mhs-elektronik.de + @see www.mhs-elektronik.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::BCValueModel(QObject *parent) : QAbstractListModel(parent) {} + +void BCValueModel::addValue(const BCValue &val) +{ + int row = _valueList.size(); + beginInsertRows(QModelIndex(), row, row); + _valueList.append(val); + endInsertRows(); +} + +void BCValueModel::setValueList(const BCValueList& valueList) +{ + beginResetModel(); + _valueList = valueList; + endResetModel(); +} + +int BCValueModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) return 0; + return _valueList.size(); +} + +QVariant BCValueModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= _valueList.size()) + return QVariant(); + + const BCValue &item = _valueList.at(index.row()); + + if (role == Qt::DisplayRole || role == Qt::EditRole) { + return QVariant::fromValue(item); + } + return QVariant(); +} + +Qt::ItemFlags BCValueModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) return Qt::NoItemFlags; + // ItemIsEditable ist Pflicht für Persistent Editors + return QAbstractListModel::flags(index) | Qt::ItemIsEditable; +} + +bool BCValueModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid() && role == Qt::EditRole) { + BCValue item = _valueList[index.row()]; + + // Wir erwarten hier nur den Value-Teil (vom Slider/Editor) + // Checken ob Int oder Double + if (value.canConvert()) { + item.value = value; + } + + _valueList[index.row()] = item; + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + return true; + } + return false; +} diff --git a/bcvaluemodel.h b/bcvaluemodel.h new file mode 100644 index 0000000..441e5fd --- /dev/null +++ b/bcvaluemodel.h @@ -0,0 +1,63 @@ +/*************************************************************************** + + BionxControl + Copyright © 2025 christoph holzheuer + christoph.holzheuer@gmail.com + + Using: + + BigXionFlasher USB V 0.2.4 rev. 97 + © 2011-2013 by Thomas Koenig + @see www.bigxionflasher.org + + Bionx Bike Info + © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) + @see www.ts-soft.de + + mhs_can_drv.c 3.00 + © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany + Demlehner Klaus, info@mhs-elektronik.de + @see www.mhs-elektronik.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 + + ***************************************************************************/ + + +#ifndef BCVALUEMODEL_H +#define BCVALUEMODEL_H + +#include + +#include + + +class BCValueModel : public QAbstractListModel +{ + Q_OBJECT + +public: + + explicit BCValueModel(QObject *parent = nullptr); + + void addValue(const BCValue &val); + void setValueList(const BCValueList& valueList); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + + // Nötig für Editierbarkeit + Qt::ItemFlags flags(const QModelIndex &index) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + +private: + + BCValueList _valueList; + +}; + +#endif // BCVALUEMODEL_H