From a0064b25668d002eef0820e3019a72dca97cdebb Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Wed, 10 Sep 2025 07:27:09 +0200 Subject: [PATCH] delegate cleanups --- src/items/xqitemdelegate.cpp | 77 ++++++++++++++++++++---------------- src/items/xqitemdelegate.h | 8 ++-- xml/modeldata1.xtr | 4 +- xml/modelsheets.xml | 2 +- 4 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/items/xqitemdelegate.cpp b/src/items/xqitemdelegate.cpp index a9c791b..e4ede87 100644 --- a/src/items/xqitemdelegate.cpp +++ b/src/items/xqitemdelegate.cpp @@ -129,20 +129,20 @@ void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option XQItem& item = xqItemFromIndex( index ); - switch( item.renderStyle() ) { case XQItem::HeaderStyle : - return drawHeaderStyle( painter, option, index ); + return drawHeaderStyle( painter, option, item ); case XQItem::ComboBoxStyle : - return drawComboBoxStyle( painter, option, index ); -/* - case XQItem::SpinBoxStyle : - return drawSpinBoxStyle( painter, option, index ); + return drawComboBoxStyle( painter, option, item ); case XQItem::ColorBarStyle : - return drawProgressBarStyle( painter, option, index ); + qDebug() << " --- JAJA: " << index.data().toString(); + return drawColorBarStyle( painter, option, item ); +/* + case XQItem::SpinBoxStyle : + return drawSpinBoxStyle( painter, option, item ); */ case XQItem::HiddenStyle : return; @@ -158,15 +158,14 @@ void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option //! einen section header im header-style zeichnen -void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const { QStyleOptionHeader headerOption; - XQItem& item = xqItemFromIndex( index ); // use the header as "parent" for style init - QWidget* srcWidget = treeTable();//->header(); + QWidget* srcWidget = treeTable();//->header(); headerOption.initFrom(srcWidget); - headerOption.text = index.data(Qt::DisplayRole).toString(); + headerOption.text = item.text(); headerOption.rect = option.rect.adjusted(0,0,0,3); headerOption.styleObject = option.styleObject; // __ch: reduce inner offset when painting @@ -193,32 +192,43 @@ void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewIt //! firz -void XQItemDelegate::drawProgressBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +void XQItemDelegate::drawColorBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const { - QStyleOptionProgressBar progressBarOption; - progressBarOption.rect = option.rect; - progressBarOption.state = option.state; - progressBarOption.direction = option.direction; - progressBarOption.fontMetrics = option.fontMetrics; - progressBarOption.palette = option.palette; - progressBarOption.styleObject = option.styleObject; - progressBarOption.minimum = 0; - progressBarOption.maximum = 100; - progressBarOption.progress = 67; // Fester Wert - progressBarOption.text = QStringLiteral("67%"); - progressBarOption.textVisible = true; - progressBarOption.textAlignment = Qt::AlignCenter; + //QStyledItemDelegate::paint(painter, option, item); - QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); + // Wert aus dem Modell holen + bool ok; + int value = item.data(Qt::EditRole).toInt(&ok); + if (!ok || value < 0 || value > 100) + return; + // Balkenbereich berechnen + QRect rect = option.rect.adjusted(2, 2, -2, -2); // etwas Padding + int barWidth = static_cast(rect.width() * (value / 100.0)); + // Balken zeichnen + painter->save(); + painter->setRenderHint(QPainter::Antialiasing); + + QRect barRect(rect.left(), rect.top(), barWidth, rect.height()); + QColor barColor = QColor(100, 180, 255); // z. B. hellblau + painter->setBrush(barColor); + painter->setPen(Qt::NoPen); + painter->drawRect(barRect); + + // Text (Zahl) zentriert zeichnen + painter->setPen(Qt::black); + //painter->drawText(rect, Qt::AlignCenter, QString::number(value)+" %"); + painter->drawText(rect, Qt::AlignCenter, item.text() ); + + painter->restore(); } //! Zeichnet das Item als combo box. -void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const { QWidget* srcWidget = qobject_cast(option.styleObject); QStyleOptionComboBox comboOption; @@ -226,15 +236,14 @@ void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionView comboOption.initFrom(srcWidget); - XQItem& item = xqItemFromIndex( index ); // set options comboOption.rect = option.rect; comboOption.state = option.state | QStyle::State_Selected | QStyle::State_Enabled; // not editable => only visual, but painter needs to know it comboOption.editable = false; - comboOption.currentText = item.text();//index.data(Qt::DisplayRole).toString(); + comboOption.currentText = item.text(); // decoration (if any) - comboOption.currentIcon = qvariant_cast(index.data(Qt::DecorationRole)); + comboOption.currentIcon = qvariant_cast(item.data(Qt::DecorationRole)); comboOption.iconSize = comboOption.currentIcon.actualSize(QSize(option.rect.height() - 3, option.rect.height() - 3)); // save painter @@ -250,12 +259,12 @@ void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionView //! Zeichnet das Item als spin box. -void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const { qDebug() << " --- jawas? SPINBOX!"; bool isInt = false; // Den Wert aus dem Modell holen. - QString textToShow = index.data(Qt::DisplayRole).toString(); + QString textToShow = item.data(Qt::DisplayRole).toString(); textToShow.toInt(&isInt); if (isInt) { @@ -285,9 +294,6 @@ void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewI // Die Flags sorgen für die korrekte Ausrichtung (rechtsbündig, vertikal zentriert). painter->drawText(textRect, Qt::AlignRight | Qt::AlignVCenter, textToShow); - } else { - // Fallback für alle anderen Datentypen. - QStyledItemDelegate::paint(painter, option, index); } } @@ -391,3 +397,4 @@ void XQItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionVie //qDebug() << " --- update Editor Geometry"; QStyledItemDelegate::updateEditorGeometry(editor, option, index); } + diff --git a/src/items/xqitemdelegate.h b/src/items/xqitemdelegate.h index b4ff029..b87a0da 100644 --- a/src/items/xqitemdelegate.h +++ b/src/items/xqitemdelegate.h @@ -46,10 +46,10 @@ public: protected: - void drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; - void drawProgressBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; - void drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; - void drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + void drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item ) const; + void drawColorBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const; + void drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const; + void drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const XQItem& item) const; XQViewModel& _modelView; diff --git a/xml/modeldata1.xtr b/xml/modeldata1.xtr index fb4b4af..60c55b9 100644 --- a/xml/modeldata1.xtr +++ b/xml/modeldata1.xtr @@ -21,10 +21,10 @@ - + - + diff --git a/xml/modelsheets.xml b/xml/modelsheets.xml index 3338b16..e09f636 100644 --- a/xml/modelsheets.xml +++ b/xml/modelsheets.xml @@ -18,7 +18,7 @@ - +