Fixed slider painting, part III
This commit is contained in:
@@ -99,6 +99,15 @@ void BCValueDelegate::setEditorData(QWidget *editor, const QModelIndex& index) c
|
||||
|
||||
}
|
||||
|
||||
void BCValueDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
Q_UNUSED(index)
|
||||
|
||||
QRect sliderRect = clipToSliderRect( option.rect );
|
||||
editor->setGeometry(sliderRect); // Slider nur über Progress Bar
|
||||
qDebug() << " --- Hier: " << sliderRect;
|
||||
//return QStyledItemDelegate::updateEditorGeometry(editor,option,index);
|
||||
}
|
||||
void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
|
||||
{
|
||||
if( index.column() == 1)
|
||||
@@ -119,30 +128,118 @@ void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c
|
||||
|
||||
void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
// Standard-Zeichnen (Text, Hintergrund, Selection) durchführen
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
int row = index.row();
|
||||
int row = index.row();
|
||||
|
||||
if( index.column() != 1 )
|
||||
return;
|
||||
if( row<0 || row >= _valueList.size() )
|
||||
return;
|
||||
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
const BCValue& bcValue = *(_valueList[ index.row()].get());
|
||||
|
||||
|
||||
if( bcValue.valueType() == BCValue::ValueType::Bool )
|
||||
{
|
||||
opt.text = "fitz!";
|
||||
qDebug() << " -- YES: " << opt.text;
|
||||
opt.text = bcValue.rawValue() == 1 ? "Yes" : "No";
|
||||
}
|
||||
|
||||
// Standard-Zeichnen (Text, Hintergrund, Selection) durchführen
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
|
||||
|
||||
if( !bcValue.isReadOnly() )
|
||||
{
|
||||
// Wir zeichnen boolean Values an toggle switches
|
||||
if( bcValue.valueType() == BCValue::ValueType::Bool )
|
||||
paintPlainToggleSwitch(painter, option, bcValue);
|
||||
paintPlainToggleSwitch(painter, opt, bcValue);
|
||||
else
|
||||
paintPlainSliderIndicator(painter, option.rect, bcValue.calcMinMaxRatio() );
|
||||
paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio() );
|
||||
}
|
||||
|
||||
if(_rowOpacities.contains(row))
|
||||
paintHighlightRow(painter,option,index.row());
|
||||
paintHighlightRow(painter, opt,index.row());
|
||||
|
||||
}
|
||||
|
||||
void BCValueDelegate::paintBooleanValue( QPainter *painter, const QStyleOptionViewItem& option, const BCValue& bcValue ) const
|
||||
{
|
||||
/*
|
||||
* // 1. Den anzuzeigenden Text aus dem Model holen
|
||||
QString text = index.data(Qt::DisplayRole).toString();
|
||||
|
||||
// Wenn du vorher z. B. '0' oder '1' durch "Aktiv" oder "Inaktiv" ersetzen willst,
|
||||
// könntest du das hier tun:
|
||||
// text = index.data(Qt::DisplayRole).toBool() ? "Aktiv" : "Inaktiv";
|
||||
|
||||
// 2. Das Rechteck für den Text berechnen
|
||||
// Wir nehmen das gesamte Rechteck der Zelle und schneiden links Platz für die Checkbox ab.
|
||||
// Angenommen, Checkbox + linkes Padding benötigen ca. 30 Pixel:
|
||||
QRect textRect = option.rect;
|
||||
textRect.setLeft(textRect.left() + 30);
|
||||
|
||||
// 3. Die korrekte Textfarbe ermitteln (extrem wichtig für die UX!)
|
||||
// Wenn die Zeile markiert ist (Selected), muss der Text meist weiß sein,
|
||||
// ansonsten schwarz (oder je nach System-Theme).
|
||||
QPalette::ColorRole textRole = (option.state & QStyle::State_Selected)
|
||||
? QPalette::HighlightedText
|
||||
: QPalette::Text;
|
||||
|
||||
// 4. Den Text nativ durch den Style zeichnen lassen
|
||||
QApplication::style()->drawItemText(
|
||||
painter,
|
||||
textRect,
|
||||
Qt::AlignLeft | Qt::AlignVCenter, // Ausrichtung innerhalb von textRect
|
||||
option.palette, // Farbpalette der View übernehmen
|
||||
option.state & QStyle::State_Enabled, // Prüfen, ob die Zelle klickbar/aktiv ist
|
||||
text, // Der zu zeichnende String
|
||||
textRole // Die ermittelte Farb-Rolle
|
||||
);
|
||||
*/
|
||||
// 1. Standard-Optionen für die Zelle holen
|
||||
QStyleOptionViewItem opt = option;
|
||||
//initStyleOption(&opt, index);
|
||||
|
||||
// 2. Den originalen Text ('0' oder '1') ausblenden, damit er nicht hinter der Checkbox steht
|
||||
opt.text = "";
|
||||
|
||||
// Zuerst den Hintergrund der Zelle zeichnen (wichtig für Selektions-Farben etc.)
|
||||
// Wir übergeben nullptr für das Widget, da QStyledItemDelegate ohnehin unabhängig zeichnet
|
||||
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, nullptr);
|
||||
|
||||
// 3. Den tatsächlichen Wert aus dem Model auslesen
|
||||
bool isChecked = bcValue.rawValue() == 1;
|
||||
|
||||
// 4. Optionen für die Checkbox konfigurieren
|
||||
QStyleOptionButton cbOpt;
|
||||
cbOpt.state = isChecked ? QStyle::State_On : QStyle::State_Off;
|
||||
cbOpt.state |= QStyle::State_Enabled; // Damit die Checkbox nicht ausgegraut/deaktiviert aussieht
|
||||
|
||||
// 5. Größe und Position berechnen (Zentriert in der Zelle)
|
||||
// Wir fragen das System nach der korrekten Größe für eine Checkbox
|
||||
QRect checkRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &cbOpt, nullptr);
|
||||
|
||||
// Checkbox exakt in der Mitte des zur Verfügung stehenden Zellenbereichs platzieren
|
||||
cbOpt.rect = QStyle::alignedRect(option.direction, Qt::AlignLeft| Qt::AlignVCenter, checkRect.size(), option.rect);
|
||||
|
||||
// 6. Die native Checkbox zeichnen
|
||||
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &cbOpt, painter, nullptr);
|
||||
}
|
||||
|
||||
QRect BCValueDelegate::clipToSliderRect( const QRect& rect, int buttonOffset ) const
|
||||
{
|
||||
return rect.adjusted
|
||||
(
|
||||
rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar)
|
||||
0, // Oben: kein Offset
|
||||
-buttonOffset, // Rechts: 8px Padding
|
||||
0 // Unten: kein Offset
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Zeichnet der 'Sliderindicator', also den Anteil des Gesamtwerts als Fortschrittsbalken
|
||||
@@ -150,8 +247,29 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio
|
||||
|
||||
void BCValueDelegate::paintPlainSliderIndicator(QPainter* painter, const QRect& rect, double ratio )const
|
||||
{
|
||||
QRect sliderRect = BCValueSlider::clipToSliderRect( rect );
|
||||
BCValueSlider::paintSliderIndicator(painter, sliderRect, ratio );
|
||||
QRect sliderRect = clipToSliderRect( rect, 35 );
|
||||
//BCValueSliderOld::paintSliderIndicator(painter, sliderRect, ratio );
|
||||
// Kleinen Slider-Indikator zeichnen
|
||||
painter->save();
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QRect barRect = sliderRect;
|
||||
|
||||
int yOffset = sliderRect.height()/2;
|
||||
barRect.setY( rect.y() + yOffset - 3 );
|
||||
barRect.setHeight( 6);
|
||||
|
||||
// Mini Progress Bar: der Gesamtbereich
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(QColor(0xE0E0E0));
|
||||
painter->drawRoundedRect(barRect, 4, 4);
|
||||
|
||||
// Mini Progress Bar: der Wertebereich
|
||||
barRect.setWidth( ratio * barRect.width() );
|
||||
painter->setBrush(QColor(0x0078D4));
|
||||
painter->drawRoundedRect(barRect, 4, 4);
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void BCValueDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, int row) const
|
||||
@@ -182,14 +300,6 @@ void BCValueDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionVie
|
||||
}
|
||||
|
||||
|
||||
void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const
|
||||
{
|
||||
Q_UNUSED(index)
|
||||
QRect baseRect = option.rect.adjusted( 0,0,0 + BCValueSlider::cToolIconOffset, 0 );
|
||||
QRect sliderRect = BCValueSlider::clipToSliderRect( baseRect );
|
||||
editor->setGeometry(sliderRect); // Slider nur über Progress Bar
|
||||
}
|
||||
|
||||
void BCValueDelegate::paintPlainToggleSwitch(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user