mirror of
https://github.com/Fishwaldo/qt-openzwave.git
synced 2025-07-23 21:48:21 +00:00
153 lines
6.4 KiB
C++
153 lines
6.4 KiB
C++
#include <QPainter>
|
|
#include <QDebug>
|
|
#include <QApplication>
|
|
#include "qtozw_itemdelegate.h"
|
|
#include "qtozwvalueidmodel.h"
|
|
|
|
QTOZW_ItemDelegate::QTOZW_ItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
void QTOZW_ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
QModelIndex typeIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Type);
|
|
QModelIndex flagsIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::ValueFlags);
|
|
QBitArray flags = flagsIndex.data().value<QBitArray>();
|
|
bool readOnly = flags.at(QTOZW_ValueIds::ValueIDFlags::ReadOnly);
|
|
|
|
switch (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>()) {
|
|
case QTOZW_ValueIds::ValueIdTypes::List: {
|
|
QTOZW_ValueIDList val = index.data().value<QTOZW_ValueIDList>();
|
|
QStyleOptionComboBox comboBoxOption;
|
|
comboBoxOption.rect = option.rect;
|
|
comboBoxOption.state = option.state;
|
|
if (readOnly) {
|
|
comboBoxOption.state |= QStyle::State_ReadOnly;
|
|
}
|
|
comboBoxOption.currentText = val.selectedItem;
|
|
|
|
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
|
|
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
|
|
|
|
break;
|
|
}
|
|
case QTOZW_ValueIds::ValueIdTypes::Bool: {
|
|
QStyleOptionButton cbOption;
|
|
cbOption.rect = option.rect;
|
|
cbOption.state |= index.data().value<bool>() ? QStyle::State_On : QStyle::State_Off;
|
|
cbOption.state |= QStyle::State_Enabled;
|
|
if (readOnly)
|
|
cbOption.state |= QStyle::State_ReadOnly;
|
|
QApplication::style()->drawControl(QStyle::CE_CheckBox, &cbOption, painter);
|
|
|
|
break;
|
|
}
|
|
#if 0
|
|
case QTOZW_ValueIds::ValueIdTypes::Int:
|
|
case QTOZW_ValueIds::ValueIdTypes::Byte:
|
|
case QTOZW_ValueIds::ValueIdTypes::Short: {
|
|
QSpinBox sb;
|
|
sb.setValue(index.data().toInt());
|
|
sb.resize(option.rect.size());
|
|
this->m_spinBox->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
|
|
|
|
|
|
this->m_spinBox->setValue(index.data().toInt());
|
|
this->m_spinBox->setEnabled(!readOnly);
|
|
this->m_spinBox->resize(option.rect.size());
|
|
int min = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Min).data().toInt();
|
|
int max = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Max).data().toInt();
|
|
if (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>() == QTOZW_ValueIds::ValueIdTypes::Int) {
|
|
max = qMin(max, std::numeric_limits<int>::max());
|
|
} else if (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>() == QTOZW_ValueIds::ValueIdTypes::Byte) {
|
|
max = qMin(static_cast<uint8_t>(max), std::numeric_limits<uint8_t>::max());
|
|
} else if (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>() == QTOZW_ValueIds::ValueIdTypes::Short) {
|
|
max = qMin(static_cast<uint16_t>(max), std::numeric_limits<uint16_t>::max());
|
|
}
|
|
this->m_spinBox->setRange(min, max);
|
|
painter->save();
|
|
painter->translate(option.rect.topLeft());
|
|
this->m_spinBox->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
|
|
painter->restore();
|
|
break;
|
|
}
|
|
#endif
|
|
default: {
|
|
return QStyledItemDelegate::paint(painter, option, index);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
QSize QTOZW_ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(index);
|
|
QModelIndex typeIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Type);
|
|
switch (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>()) {
|
|
case QTOZW_ValueIds::ValueIdTypes::List: {
|
|
QTOZW_ValueIDList val = index.data().value<QTOZW_ValueIDList>();
|
|
QStyleOptionComboBox comboBoxOption;
|
|
comboBoxOption.rect = option.rect;
|
|
comboBoxOption.state = option.state;
|
|
comboBoxOption.currentText = val.selectedItem;
|
|
return QSize(QFontMetrics(option.font).width(val.selectedItem), comboBoxOption.rect.height());
|
|
}
|
|
case QTOZW_ValueIds::ValueIdTypes::Bool: {
|
|
QStyleOptionButton cbOption;
|
|
cbOption.rect = option.rect;
|
|
cbOption.state |= index.data().value<bool>() ? QStyle::State_On : QStyle::State_Off;
|
|
cbOption.state |= QStyle::State_Enabled;
|
|
return cbOption.rect.size();
|
|
}
|
|
default:
|
|
return QStyledItemDelegate::sizeHint(option, index);
|
|
}
|
|
}
|
|
|
|
QWidget *QTOZW_ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
QModelIndex typeIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Type);
|
|
switch (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>()) {
|
|
case QTOZW_ValueIds::ValueIdTypes::List: {
|
|
QComboBox *editor = new QComboBox(parent);
|
|
editor->setAutoFillBackground(true);
|
|
connect(editor, &QComboBox::currentTextChanged, this, &QTOZW_ItemDelegate::commitAndCloseEditor);
|
|
return editor;
|
|
}
|
|
|
|
|
|
default:
|
|
return QStyledItemDelegate::createEditor(parent, option, index);
|
|
}
|
|
}
|
|
|
|
void QTOZW_ItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(index);
|
|
editor->setGeometry(option.rect);
|
|
}
|
|
|
|
void QTOZW_ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
|
{
|
|
QModelIndex typeIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Type);
|
|
switch (typeIndex.data().value<QTOZW_ValueIds::ValueIdTypes>()) {
|
|
case QTOZW_ValueIds::ValueIdTypes::List: {
|
|
QComboBox *cb = static_cast<QComboBox*>(editor);
|
|
QTOZW_ValueIDList val = index.data().value<QTOZW_ValueIDList>();
|
|
cb->addItems(val.labels);
|
|
cb->setCurrentText(val.selectedItem);
|
|
cb->showPopup();
|
|
break;
|
|
}
|
|
default:
|
|
QStyledItemDelegate::setEditorData(editor, index);
|
|
}
|
|
}
|
|
|
|
void QTOZW_ItemDelegate::commitAndCloseEditor() {
|
|
QWidget *editor = qobject_cast<QWidget *>(sender());
|
|
emit commitData(editor);
|
|
emit closeEditor(editor);
|
|
}
|
|
|