292 lines
8.6 KiB
C++
292 lines
8.6 KiB
C++
/***************************************************************************
|
|
|
|
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 <xqtreeview.h>
|
|
#include <xqitemtype.h>
|
|
#include <xqmodel.h>
|
|
|
|
|
|
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>());
|
|
*/
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
XQItemDelegate::XQItemDelegate( XQModel& modelView)
|
|
: _modelView{modelView}
|
|
{
|
|
static XQItemEditorFactory s_EditorFactory;
|
|
setItemEditorFactory(&s_EditorFactory);
|
|
}
|
|
|
|
|
|
XQTreeView* XQItemDelegate::treeView() const
|
|
{
|
|
return _modelView.treeView();
|
|
}
|
|
|
|
|
|
XQItem& XQItemDelegate::xqItemFromIndex( const QModelIndex& index ) const
|
|
{
|
|
return _modelView.xqItemFromIndex( index );
|
|
}
|
|
|
|
|
|
|
|
QWidget* XQItemDelegate::prepareHeaderOption(const QStyleOptionViewItem& option, const QModelIndex& index, QStyleOptionHeader& headerOption) const
|
|
{
|
|
// use the header as "parent" for style init
|
|
QWidget* srcWidget = treeView()->header();
|
|
headerOption.initFrom(srcWidget);
|
|
headerOption.text = index.data(Qt::DisplayRole).toString();
|
|
headerOption.rect = option.rect;
|
|
headerOption.styleObject = option.styleObject;
|
|
// __ch: reduce inner offset when painting
|
|
headerOption.textAlignment |= Qt::AlignVCenter;
|
|
|
|
return srcWidget;
|
|
|
|
}
|
|
|
|
void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
{
|
|
if( !index.isValid() )
|
|
qDebug() << " index DEAD!";
|
|
|
|
XQItem& item = xqItemFromIndex( index );
|
|
if( item.isValid() )
|
|
{
|
|
|
|
switch( item.renderStyle() )
|
|
{
|
|
case XQItem::HeaderStyle :
|
|
return drawHeaderStyle( painter, option, index );
|
|
|
|
case XQItem::ComboBoxStyle :
|
|
return drawComboBoxStyle( painter, option, index );
|
|
|
|
case XQItem::HiddenStyle :
|
|
return;
|
|
|
|
//case XQItem::ProgressBarStyle :
|
|
// return drawProgressBarStyle( painter, option, index );
|
|
|
|
|
|
default:
|
|
break;
|
|
} // switch
|
|
|
|
}
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
}
|
|
|
|
|
|
void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
{
|
|
QStyleOptionHeader headerOption;
|
|
QWidget* srcWidget = prepareHeaderOption(option, index, headerOption);
|
|
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();
|
|
}
|
|
}
|
|
|
|
void XQItemDelegate::drawProgressBarStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
void XQItemDelegate::drawComboBoxStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
|
|
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();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
QSize XQItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
return QStyledItemDelegate::sizeHint(option, index);
|
|
}
|
|
|
|
|
|
|
|
QWidget* XQItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
{
|
|
|
|
return QStyledItemDelegate::createEditor( parent, option, index );
|
|
|
|
int editorType = XQItem::xqItemFromIndex(index).editorType();
|
|
QWidget* editor = itemEditorFactory()->createEditor(editorType, parent);
|
|
if( editor )
|
|
{
|
|
return editor;
|
|
}
|
|
|
|
}
|
|
|
|
void XQItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
|
{
|
|
|
|
XQItem& item = xqItemFromIndex( index );
|
|
qDebug() << " --- ZZZ: setEditorData: " << item.text();
|
|
switch( item.editorType() )
|
|
{
|
|
case XQItemType::ComboBoxType :
|
|
{
|
|
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
|
|
comboBox->setModel( item.fixedChoices());
|
|
comboBox->setCurrentText( item.data().toString() );
|
|
comboBox->showPopup();
|
|
return;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
QStyledItemDelegate::setEditorData(editor, index);
|
|
}
|
|
|
|
void XQItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
|
{
|
|
|
|
XQItem& item = xqItemFromIndex( index );
|
|
|
|
switch( item.editorType() )
|
|
{
|
|
|
|
case XQItem::ComboBoxType :
|
|
{
|
|
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
|
|
return;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
QStyledItemDelegate::setModelData(editor, model, index);
|
|
}
|
|
|
|
void XQItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
{
|
|
//qDebug() << " --- update Editor Geometry";
|
|
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
|
|
}
|