Added model & delegate

This commit is contained in:
2025-12-17 16:26:22 +01:00
parent 8813904e2b
commit 09ff2f00bb
4 changed files with 298 additions and 0 deletions

114
bcvaluedelegate.cpp Normal file
View 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);
}