167 lines
4.7 KiB
C++
167 lines
4.7 KiB
C++
#include <QSlider>
|
|
#include <QLabel>
|
|
#include <QHBoxLayout>
|
|
#include <QWidget>
|
|
#include <QDebug>
|
|
|
|
#include "bcitemdelegate.h"
|
|
#include "bcvalue.h"
|
|
#include "qapplication.h"
|
|
|
|
|
|
BCItemDelegate::BCItemDelegate(QObject *parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
QString BCItemDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const
|
|
{
|
|
// Wir prüfen, ob im Variant unser Struct steckt
|
|
if (dataValue.canConvert<BCValue>())
|
|
{
|
|
BCValue bc = dataValue.value<BCValue>();
|
|
//qDebug() << " --- YES: " << bc.label;
|
|
// 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(dataValue, locale);
|
|
}
|
|
|
|
|
|
QWidget *BCItemDelegate::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 BCItemDelegate::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 BCItemDelegate::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 BCItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
editor->setGeometry(option.rect);
|
|
}
|
|
|
|
QSize BCItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
return QStyledItemDelegate::sizeHint(option,index);
|
|
/*
|
|
QStyleOptionViewItem opt = option;
|
|
initStyleOption(&opt, index);
|
|
opt.text = formatDisplayString(index);
|
|
|
|
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
|
|
return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), opt.widget);
|
|
*/
|
|
}
|
|
|
|
QString BCItemDelegate::formatDisplayString(const QModelIndex &index) const
|
|
{
|
|
if (!index.isValid())
|
|
return QString();
|
|
|
|
QString displayStr;
|
|
|
|
/*
|
|
QString label = index.data(BCValueListModel::LabelRole).toString();
|
|
QVariant value = index.data(BCValueListModel::ValueRole);
|
|
QString unit = index.data(BCValueListModel::UnitRole).toString();
|
|
|
|
QString valueStr = value.toString();
|
|
|
|
// Formatierung für Zahlen
|
|
bool ok;
|
|
double numValue = value.toDouble(&ok);
|
|
if (ok) {
|
|
valueStr = QString::number(numValue, 'f', 2);
|
|
}
|
|
|
|
// Zusammensetzen des Anzeige-Strings
|
|
QString displayStr = label;
|
|
if (!displayStr.isEmpty() && !valueStr.isEmpty())
|
|
displayStr += ": ";
|
|
displayStr += valueStr;
|
|
if (!unit.isEmpty())
|
|
displayStr += " " + unit;
|
|
*/
|
|
|
|
return displayStr;
|
|
}
|