Added model & delegate
This commit is contained in:
114
bcvaluedelegate.cpp
Normal file
114
bcvaluedelegate.cpp
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
#include <QSlider>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
#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>()) {
|
||||||
|
BCValue bc = value.value<BCValue>();
|
||||||
|
|
||||||
|
// 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<BCValue>())
|
||||||
|
return QStyledItemDelegate::createEditor(parent, option, index);
|
||||||
|
|
||||||
|
BCValue bc = rawData.value<BCValue>();
|
||||||
|
|
||||||
|
// 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<BCValue>();
|
||||||
|
|
||||||
|
QSlider *slider = editor->findChild<QSlider*>("slider");
|
||||||
|
QLabel *lblUnit = editor->findChild<QLabel*>("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<QSlider*>("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);
|
||||||
|
}
|
||||||
26
bcvaluedelegate.h
Normal file
26
bcvaluedelegate.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// BCValueDelegate.h
|
||||||
|
#ifndef BCVALUEDELEGATE_H
|
||||||
|
#define BCVALUEDELEGATE_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <QStyledItemDelegate>
|
||||||
|
|
||||||
|
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
|
||||||
95
bcvaluemodel.cpp
Normal file
95
bcvaluemodel.cpp
Normal file
@@ -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 <info@bigxionflasher.org>
|
||||||
|
@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.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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<double>()) {
|
||||||
|
item.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_valueList[index.row()] = item;
|
||||||
|
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
63
bcvaluemodel.h
Normal file
63
bcvaluemodel.h
Normal file
@@ -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 <info@bigxionflasher.org>
|
||||||
|
@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 <QAbstractTableModel>
|
||||||
|
|
||||||
|
#include <bcvalue.h>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user