delegate cleanups

This commit is contained in:
2025-09-10 07:27:09 +02:00
parent cbe8b92582
commit a0064b2566
4 changed files with 49 additions and 42 deletions

View File

@@ -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<int>(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<QWidget*>(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<QIcon>(index.data(Qt::DecorationRole));
comboOption.currentIcon = qvariant_cast<QIcon>(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);
}

View File

@@ -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;

View File

@@ -21,10 +21,10 @@
</Battery>
<Battery BatteryID="#2 BYD 02" FriendlyName="@BatteryName" BatteryName="02 BYD T02 Stackable" Manufacturer="BYD" Capacity="9000" Yield="94" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#3 BYD 03" FriendlyName="@BatteryName" BatteryName="03 BYD T01 Stackable" Manufacturer="BYD" Capacity="4500" Yield="86" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#4 BYD 04" FriendlyName="@BatteryName" BatteryName="04 BYD T02 Stackable" Manufacturer="BYD" Capacity="9000" Yield="98" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#4 BYD 04" FriendlyName="@BatteryName" BatteryName="04 BYD T02 Stackable" Manufacturer="BYD" Capacity="9000" Yield="91" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#5 GroWatt 05 G2K" FriendlyName="@BatteryName" BatteryName="05 BYD T01 Stackable" Manufacturer="GroWatt" Capacity="4500" Yield="94" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#6 GroWatt 06 G4K" FriendlyName="@BatteryName" BatteryName="06 BYD T02 Stackable" Manufacturer="GroWatt" Capacity="9000" Yield="49" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#7 Pyne 07 G4K" FriendlyName="@BatteryName" BatteryName="07 Pyne K7 Stackable" Manufacturer="PyNe" Capacity="9000" Yield="49" MaxCurrent="120" MaxVolt="48"/>
<Battery BatteryID="#7 Pyne 07 G4K" FriendlyName="@BatteryName" BatteryName="07 Pyne K7 Stackable" Manufacturer="PyNe" Capacity="9000" Yield="68" MaxCurrent="120" MaxVolt="48"/>
</Components>
<IrgendWasAnderes>

View File

@@ -18,7 +18,7 @@
<PlainType RenderStyle="PlainStyle" EditorType="LineEditType" ItemFlags="IsEnabled|IsEditable|IsSelectable"/>
<ValueType RenderStyle="FormattedStyle" EditorType="LineEditType" ItemFlags="IsEnabled|IsEditable|IsSelectable" UnitType="Coulomb"/>
<CheckableType RenderStyle="FormattedStyle" EditorType="LineEditType" ItemFlags="IsEnabled|IsEditable|IsSelectable" />
<PercentageType RenderStyle="ColorBarStyle" EditorType="ColorBarType" ItemFlags="IsEnabled|IsEditable|IsSelectable"/>
<PercentageType RenderStyle="ColorBarStyle" EditorType="ColorBarType" ItemFlags="IsEnabled|IsEditable|IsSelectable" UnitType="%"/>
<ChoiceType RenderStyle="ComboBoxStyle" EditorType="ComboBoxType" ItemFlags="IsEnabled|IsEditable|IsSelectable" FixedChoices="2000|4000|6000|8000" UnitType="W"/>
<IntValueType RenderStyle="SpinBoxStyle" EditorType="SpinBoxType" ItemFlags="IsEnabled|IsEditable|IsSelectable"/>
</ItemTypes>