#include "channelsettingdelegate.h" #include "Common/External/Core/PaintHelper/painthelper.h" #include "Common/External/Core/UpdateSkinStyle/lhstylemanager.h" #include #include #include #include ChannelSettingDelegate::ChannelSettingDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QColor ChannelSettingDelegate::GetTextColor(const QStyleOptionViewItem &option, const QModelIndex &index) const { QModelIndex rowIndex = index.model()->index(index.row(), 0); if(!index.model()->data(rowIndex, Qt::UserRole).canConvert()) { QColor c = option.state.testFlag(QStyle::State_Selected)?QColor(255, 255, 255):QColor(213, 213, 213); return c; } QColor colorText; if(LHStyleManager::Instance()->GetCurSkinStyle() == eLightStyle) { colorText = QColor(58, 63, 99); } else if(LHStyleManager::Instance()->GetCurSkinStyle() == eDeepStyle) { colorText = QColor(210, 210, 210); } return colorText; } void ChannelSettingDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { PainterEx *painterEx = static_cast(painter); QString text = index.data().toString(); //获取字体 FontEx font(qApp->font().family(), 14, false); font.setBold(option.state.testFlag(QStyle::State_None)); painterEx->setFont(font); QRect rectText = option.rect.adjusted(0, 0, 0, 0); if(index.column() == 0) { rectText = QRect(QPoint(rectText.left() + 12, rectText.top()), rectText.size()); } painterEx->DrawText(rectText, text, GetTextColor(option, index), Qt::AlignLeft|Qt::AlignVCenter); } QWidget* ChannelSettingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.column() == 1) { CustomComboBox *tableComBox = new CustomComboBox(parent); tableComBox->installEventFilter(parent); tableComBox->addItem("不设置"); for(const stMonitorPin &chn : qAsConst(m_listMonitorPin)) { QVariant variant; variant.setValue(chn); tableComBox->addItem(chn.strMName, variant); } tableComBox->setCurrentIndex(0); return tableComBox; } return QStyledItemDelegate::createEditor(parent,option,index); } void ChannelSettingDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { if(index.column() == 1) { QModelIndex rowIndex = index.model()->index(index.row(), 0); if(!index.model()->data(rowIndex, Qt::UserRole).canConvert()) return; stMeterInfo* outData = index.model()->data(rowIndex, Qt::UserRole).value(); CustomComboBox* box = static_cast(editor); //通过监控点id获取最新监控点名称 box->setCurrentText(outData->strMName.isEmpty() ? "不设置" : outData->strMName); box->m_currentText = box->currentText(); } else { QStyledItemDelegate::setEditorData(editor,index); } } void ChannelSettingDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { if(index.column() == 1) { CustomComboBox* box = static_cast(editor); QModelIndex rowIndex = model->index(index.row(), 0); if(!model->data(rowIndex, Qt::UserRole).canConvert()) return; stMeterInfo* outData = model->data(rowIndex, Qt::UserRole).value(); if(box->currentIndex() == 0) { outData->strMName.clear(); } else { outData->strMName = box->currentText(); } QVariant var; var.setValue(outData); model->setData(rowIndex, var, Qt::UserRole); } else { QStyledItemDelegate::setModelData(editor,model,index); } } void ChannelSettingDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } void ChannelSettingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.column() == 3) { QStyledItemDelegate::paint(painter, option, index); } else { //绘制文本 PaintText(painter, option, index); } } bool ChannelSettingDelegate::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); if (keyEvent->key() == Qt::Key_Tab) { return true; } } return QObject::eventFilter(obj, event); }