123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include "channelsettingdelegate.h"
- #include "Common/External/Core/PaintHelper/painthelper.h"
- #include "Common/External/Core/UpdateSkinStyle/lhstylemanager.h"
- #include <QApplication>
- #include <QTimer>
- #include <QCompleter>
- #include <QGraphicsDropShadowEffect>
- 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<stMeterInfo*>())
- {
- 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<PainterEx*>(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<stMeterInfo*>()) return;
- stMeterInfo* outData = index.model()->data(rowIndex, Qt::UserRole).value<stMeterInfo*>();
- CustomComboBox* box = static_cast<CustomComboBox*>(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<CustomComboBox*>(editor);
- QModelIndex rowIndex = model->index(index.row(), 0);
- if(!model->data(rowIndex, Qt::UserRole).canConvert<stMeterInfo*>()) return;
- stMeterInfo* outData = model->data(rowIndex, Qt::UserRole).value<stMeterInfo*>();
- 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<QKeyEvent*>(event);
- if (keyEvent->key() == Qt::Key_Tab)
- {
- return true;
- }
- }
- return QObject::eventFilter(obj, event);
- }
|