Files
xtree/src/items/xqitemdelegate.cpp

394 lines
12 KiB
C++
Raw Normal View History

2025-08-22 22:57:06 +02:00
/***************************************************************************
source::worx xtree
Copyright © 2024-2025 c.holzheuer
christoph.holzheuer@gmail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
***************************************************************************/
#include <QApplication>
#include <QItemEditorFactory>
#include <QLineEdit>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QProgressBar>
2025-09-08 15:42:15 +02:00
#include <QSlider>
2025-08-22 22:57:06 +02:00
#include <QPainter>
#include <QHeaderView>
#include <QCommonStyle>
#include <xqitemdelegate.h>
#include <xqtreetable.h>
#include <xqitemtype.h>
#include <xqviewmodel.h>
2025-09-09 16:22:59 +02:00
/*
#include <QStyledItemDelegate>
#include <QPainter>
#include <QApplication>
class BarDelegate : public QStyledItemDelegate {
public:
BarDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
// Standard-Zellenhintergrund zeichnen
QStyledItemDelegate::paint(painter, option, index);
// Wert aus dem Modell holen
bool ok;
int value = index.data().toInt(&ok);
if (!ok || value < 0 || value > 100)
return;
// Balkenbereich berechnen
QRect rect = option.rect.adjusted(4, 4, -4, -4); // 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->restore();
}
};
*/
2025-08-22 22:57:06 +02:00
2025-09-07 15:46:01 +02:00
//! erzeugt eine editorfactory mit den hauseigenen editortypen.
2025-09-04 17:01:01 +02:00
2025-08-22 22:57:06 +02:00
class XQItemEditorFactory : public QItemEditorFactory
{
public:
XQItemEditorFactory()
{
registerEditor(XQItem::LineEditType, new QStandardItemEditorCreator<QLineEdit>());
2025-09-07 15:46:01 +02:00
registerEditor(XQItem::ComboBoxType, new QStandardItemEditorCreator<QComboBox>());
2025-08-22 22:57:06 +02:00
registerEditor(XQItem::PickerType, new QStandardItemEditorCreator<QLineEdit>());
2025-09-09 16:22:59 +02:00
//registerEditor(XQItem::ColorBarType, new QStandardItemEditorCreator<QProgressBar>());
registerEditor(XQItem::ColorBarType, new QStandardItemEditorCreator<QSlider>());
2025-09-07 15:46:01 +02:00
registerEditor(XQItem::SpinBoxType, new QStandardItemEditorCreator<QSpinBox>());
2025-08-22 22:57:06 +02:00
registerEditor(XQItem::CustomEditorType, new QStandardItemEditorCreator<QLineEdit>());
}
};
2025-09-07 15:46:01 +02:00
//! kontruktor mit dem zusändigen viewModel
2025-08-22 22:57:06 +02:00
2025-09-07 15:46:01 +02:00
XQItemDelegate::XQItemDelegate( XQViewModel& viewModel)
: _modelView{viewModel}
2025-08-22 22:57:06 +02:00
{
static XQItemEditorFactory s_EditorFactory;
setItemEditorFactory(&s_EditorFactory);
}
2025-09-07 15:46:01 +02:00
//! gibt die interne tree table zurück
2025-09-04 17:01:01 +02:00
2025-08-22 22:57:06 +02:00
XQTreeTable* XQItemDelegate::treeTable() const
{
return _modelView.treeTable();
}
2025-09-07 15:46:01 +02:00
//! shortcut: gibt das XQItem für den gegebenen index zurück.
2025-08-22 22:57:06 +02:00
XQItem& XQItemDelegate::xqItemFromIndex( const QModelIndex& index ) const
{
return _modelView.xqItemFromIndex( index );
}
2025-09-07 15:46:01 +02:00
//! überladene paint-methode: zeichnet das item je nach render-style.
2025-09-04 17:01:01 +02:00
2025-08-22 22:57:06 +02:00
void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if( !index.isValid() )
qDebug() << " index DEAD!";
XQItem& item = xqItemFromIndex( index );
2025-08-31 14:38:11 +02:00
switch( item.renderStyle() )
2025-08-22 22:57:06 +02:00
{
2025-08-31 14:38:11 +02:00
case XQItem::HeaderStyle :
return drawHeaderStyle( painter, option, index );
2025-08-22 22:57:06 +02:00
2025-08-31 14:38:11 +02:00
case XQItem::ComboBoxStyle :
return drawComboBoxStyle( painter, option, index );
2025-09-08 22:58:52 +02:00
/*
2025-09-08 15:42:15 +02:00
case XQItem::SpinBoxStyle :
return drawSpinBoxStyle( painter, option, index );
2025-09-09 16:22:59 +02:00
case XQItem::ColorBarStyle :
2025-09-08 15:42:15 +02:00
return drawProgressBarStyle( painter, option, index );
2025-09-08 22:58:52 +02:00
*/
2025-08-31 14:38:11 +02:00
case XQItem::HiddenStyle :
return;
2025-08-22 22:57:06 +02:00
2025-08-31 14:38:11 +02:00
default:
break;
} // switch
2025-08-22 22:57:06 +02:00
QStyledItemDelegate::paint(painter, option, index);
}
2025-09-04 17:01:01 +02:00
2025-08-22 22:57:06 +02:00
//! einen section header im header-style zeichnen
void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionHeader headerOption;
2025-09-08 15:42:15 +02:00
XQItem& item = xqItemFromIndex( index );
2025-08-22 22:57:06 +02:00
// use the header as "parent" for style init
QWidget* srcWidget = treeTable();//->header();
headerOption.initFrom(srcWidget);
headerOption.text = index.data(Qt::DisplayRole).toString();
headerOption.rect = option.rect.adjusted(0,0,0,3);
headerOption.styleObject = option.styleObject;
// __ch: reduce inner offset when painting
headerOption.textAlignment |= Qt::AlignVCenter;
headerOption.icon = item.icon();
if (srcWidget != nullptr)
{
// save painter
painter->save();
//value = index.data(Qt::ForegroundRole);
//if (value.canConvert<QBrush>())
//headerOption.palette.setBrush(QPalette::Text, Qt::red );
//headerOption.palette.setBrush(QPalette::Window, Qt::red );
QCommonStyle itemStyle;
//headerOption.backgroundBrush()
//srcWidget->style()->drawControl(QStyle::CE_Header, &headerOption, painter, srcWidget);
itemStyle.drawControl(QStyle::CE_Header, &headerOption, painter, srcWidget);
// restore painter
painter->restore();
}
}
2025-09-04 17:01:01 +02:00
//! firz
void XQItemDelegate::drawProgressBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-08-22 22:57:06 +02:00
{
2025-09-08 15:42:15 +02:00
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;
2025-08-22 22:57:06 +02:00
2025-09-08 15:42:15 +02:00
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
2025-08-22 22:57:06 +02:00
}
2025-09-04 17:01:01 +02:00
2025-09-08 15:42:15 +02:00
//! Zeichnet das Item als combo box.
2025-09-04 17:01:01 +02:00
void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-08-22 22:57:06 +02:00
{
QWidget* srcWidget = qobject_cast<QWidget*>(option.styleObject);
QStyleOptionComboBox comboOption;
QStyle* comboStyle = srcWidget->style();
comboOption.initFrom(srcWidget);
2025-09-08 15:42:15 +02:00
XQItem& item = xqItemFromIndex( index );
2025-08-22 22:57:06 +02:00
// 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;
2025-09-08 15:42:15 +02:00
comboOption.currentText = item.text();//index.data(Qt::DisplayRole).toString();
2025-08-22 22:57:06 +02:00
// decoration (if any)
comboOption.currentIcon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
comboOption.iconSize = comboOption.currentIcon.actualSize(QSize(option.rect.height() - 3, option.rect.height() - 3));
// save painter
painter->save();
// draw combo
comboStyle->drawComplexControl(QStyle::CC_ComboBox, &comboOption, painter, srcWidget);
// and combobox label
comboStyle->drawControl(QStyle::CE_ComboBoxLabel, &comboOption, painter, srcWidget);
// restore painter
painter->restore();
}
2025-09-08 15:42:15 +02:00
//! Zeichnet das Item als spin box.
2025-09-04 17:01:01 +02:00
2025-08-22 22:57:06 +02:00
void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
2025-09-08 15:42:15 +02:00
qDebug() << " --- jawas? SPINBOX!";
bool isInt = false;
// Den Wert aus dem Modell holen.
QString textToShow = index.data(Qt::DisplayRole).toString();
textToShow.toInt(&isInt);
if (isInt) {
// ----- Schritt 1: Den Rahmen und die Pfeile der SpinBox zeichnen -----
QStyleOptionSpinBox spinBoxOption;
spinBoxOption.rect = option.rect;
spinBoxOption.state = option.state;
spinBoxOption.buttonSymbols = QAbstractSpinBox::UpDownArrows;
spinBoxOption.stepEnabled = QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
spinBoxOption.frame = true;
// Zeichnet den "komplexen" Teil des Steuerelements (Rahmen, Hintergrund, Pfeile)
QApplication::style()->drawComplexControl(QStyle::CC_SpinBox, &spinBoxOption, painter, nullptr);
// ----- Schritt 2: Den Text an der richtigen Position zeichnen -----
// Ermitteln, wo genau das Textfeld innerhalb des Widgets gezeichnet werden soll.
QRect textRect = QApplication::style()->subControlRect(
QStyle::CC_SpinBox, &spinBoxOption, QStyle::SC_SpinBoxEditField, nullptr
);
// Einen kleinen Innenabstand für den Text hinzufügen für besseres Aussehen.
textRect.adjust(2, 0, -2, 0);
// Den Text aus dem Modell in das ermittelte Rechteck zeichnen.
// 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);
}
2025-08-22 22:57:06 +02:00
}
2025-09-08 15:42:15 +02:00
//! Überschreibt QStyledItemDelegate::sizeHint(option, index);
2025-09-04 17:01:01 +02:00
QSize XQItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-08-22 22:57:06 +02:00
{
return QStyledItemDelegate::sizeHint(option, index);
}
2025-08-27 14:06:31 +02:00
//! Erzeugt ein editor-widget, sofern ein gültiger content-Ptr vorhanden und ein editor-Type gesetzt ist.
2025-08-22 22:57:06 +02:00
QWidget* XQItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
2025-08-27 14:06:31 +02:00
XQItem& item = xqItemFromIndex(index);
XQItem::EditorType edType = item.editorType();
2025-08-31 14:38:11 +02:00
if( edType == XQItem::NoEditorType )
2025-08-22 22:57:06 +02:00
{
2025-08-27 14:06:31 +02:00
qDebug() << "---- NO Content or NO EditorType";
return nullptr;
2025-08-22 22:57:06 +02:00
}
2025-08-27 14:06:31 +02:00
qDebug() << "---- ed type:" << XQItem::fetchEditorTypeToString( edType ) << ": " << edType;
2025-09-07 15:46:01 +02:00
QWidget* editor = itemEditorFactory()->createEditor(edType, parent);;
//return QStyledItemDelegate::createEditor( parent, option, index );
return editor;
2025-08-22 22:57:06 +02:00
}
2025-08-27 14:06:31 +02:00
2025-09-04 17:01:01 +02:00
//! Füttert einen editor mit den model-daten
2025-08-22 22:57:06 +02:00
void XQItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
XQItem& item = xqItemFromIndex( index );
2025-08-27 14:06:31 +02:00
XQItem::EditorType edType = item.editorType();
2025-09-04 17:01:01 +02:00
if( edType == XQItem::NoEditorType )
return;
switch( edType )
2025-08-22 22:57:06 +02:00
{
2025-09-07 16:22:42 +02:00
case XQItemType::ComboBoxType :
2025-09-04 17:01:01 +02:00
{
2025-09-07 16:22:42 +02:00
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
2025-09-09 16:22:59 +02:00
// wir erwarten hier ein gültiges model?
2025-09-07 16:22:42 +02:00
comboBox->setModel( item.fixedChoices());
comboBox->setCurrentText( item.data().toString() );
comboBox->showPopup();
return;
2025-08-27 14:06:31 +02:00
}
2025-08-22 22:57:06 +02:00
2025-09-07 16:22:42 +02:00
default:
// wir benutzen hier die DisplayRole wenn der Inhalt schon formatiert ist.
int role = item.renderStyle() == XQItem::FormattedStyle ? Qt::DisplayRole : Qt::EditRole;
QVariant value = index.data(role);
QByteArray userProp = editor->metaObject()->userProperty().name();
if (!userProp.isEmpty())
{
if (!value.isValid())
value = QVariant(editor->property(userProp).metaType());
editor->setProperty(userProp, value);
}
2025-08-22 22:57:06 +02:00
}
}
2025-09-04 17:01:01 +02:00
//! Schreibt die daten aus dem editor ins model zurück
void XQItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
2025-08-22 22:57:06 +02:00
{
XQItem& item = xqItemFromIndex( index );
switch( item.editorType() )
{
case XQItem::ComboBoxType :
{
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
return;
}
default:
break;
}
QStyledItemDelegate::setModelData(editor, model, index);
}
2025-09-04 17:01:01 +02:00
//! firz
2025-08-22 22:57:06 +02:00
void XQItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//qDebug() << " --- update Editor Geometry";
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}