Files
xtree/src/items/xqitemdelegate.cpp

328 lines
9.4 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>
#include <QPainter>
#include <QHeaderView>
#include <QCommonStyle>
#include <xqitemdelegate.h>
#include <xqtreetable.h>
#include <xqitemtype.h>
#include <xqviewmodel.h>
2025-09-04 17:01:01 +02:00
//! firz
2025-08-22 22:57:06 +02:00
class XQItemEditorFactory : public QItemEditorFactory
{
public:
XQItemEditorFactory()
{
registerEditor(XQItem::LineEditType, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItem::ComboBoxType, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItem::PickerType, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItem::ProgressBarType, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItem::SpinBoxType, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItem::CustomEditorType, new QStandardItemEditorCreator<QLineEdit>());
/*
registerEditor(XQItem::LineEditStyle, new QStandardItemEditorCreator<QLineEdit>());
registerEditor(XQItemType::ComboBoxStyle, new QStandardItemEditorCreator<QComboBox>());
//registerEditor(XQItemType::ProgressBarStyle, new QStandardItemEditorCreator<QProgressBar>());
registerEditor(XQItemType::SpinBoxStyle, new QStandardItemEditorCreator<QSpinBox>());
*/
/*
registerEditor(XQItem::etDoubleSpinType, new QStandardItemEditorCreator<QDoubleSpinBox>());
registerEditor(XQItemItemTypes::etDoubleSpinType, new QStandardItemEditorCreator<QDoubleSpinBox>());
registerEditor(XQItemItemTypes::etIPAddressType, new QStandardItemEditorCreator<NTIpAddressEdit>());
registerEditor(XQItemItemTypes::etLineEditBrowser, new QStandardItemEditorCreator<NTFileSelectLine>());
*/
}
};
2025-09-04 17:01:01 +02:00
//! firz
2025-08-22 22:57:06 +02:00
XQItemDelegate::XQItemDelegate( XQViewModel& modelView)
: _modelView{modelView}
{
static XQItemEditorFactory s_EditorFactory;
setItemEditorFactory(&s_EditorFactory);
}
2025-09-04 17:01:01 +02:00
//! firz
2025-08-22 22:57:06 +02:00
XQTreeTable* XQItemDelegate::treeTable() const
{
return _modelView.treeTable();
}
XQItem& XQItemDelegate::xqItemFromIndex( const QModelIndex& index ) const
{
return _modelView.xqItemFromIndex( index );
}
2025-09-04 17:01:01 +02:00
//! firz
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-08-22 22:57:06 +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
//case XQItem::ProgressBarStyle :
// return drawProgressBarStyle( painter, option, index );
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;
XQItem& item = xqItemFromIndex( index );
// 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
{
int progress = index.data(XQItem::ContentRole ).toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
QApplication::style()->drawControl(QStyle::CE_ProgressBar,&progressBarOption, painter);
}
2025-09-04 17:01:01 +02:00
//! firz
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);
// 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 = index.data(Qt::DisplayRole).toString();
// 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-04 17:01:01 +02:00
//! firz
2025-08-22 22:57:06 +02:00
void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
// xx_fix!
//int value = index.data(XQItem::ContentRole ).toInt();
QStyleOptionSpinBox spinBoxOption;
spinBoxOption.rect = option.rect;
/*
spinBoxOption.text = QString::number(value);
spinBoxOption.textAlignment = Qt::AlignCenter;
spinBoxOption.textVisible = true;
*/
QApplication::style()->drawComplexControl(QStyle::CC_SpinBox,&spinBoxOption, painter);
}
2025-09-04 17:01:01 +02:00
//! firz
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;
//return QStyledItemDelegate::createEditor( parent, option, index );
2025-08-31 14:38:11 +02:00
return itemEditorFactory()->createEditor(edType, parent);
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-04 17:01:01 +02:00
case XQItemType::ComboBoxType :
{
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
comboBox->setModel( item.fixedChoices());
comboBox->setCurrentText( item.data().toString() );
comboBox->showPopup();
return;
}
2025-08-22 22:57:06 +02:00
2025-09-04 17:01:01 +02:00
default:
2025-08-27 14:06:31 +02:00
2025-09-04 17:01:01 +02:00
// 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-27 14:06:31 +02:00
}
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);
}