channelsettingdelegate.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "channelsettingdelegate.h"
  2. #include "Common/External/Core/PaintHelper/painthelper.h"
  3. #include "Common/External/Core/UpdateSkinStyle/lhstylemanager.h"
  4. #include <QApplication>
  5. #include <QTimer>
  6. #include <QCompleter>
  7. #include <QGraphicsDropShadowEffect>
  8. ChannelSettingDelegate::ChannelSettingDelegate(QObject *parent)
  9. : QStyledItemDelegate(parent)
  10. {
  11. }
  12. QColor ChannelSettingDelegate::GetTextColor(const QStyleOptionViewItem &option, const QModelIndex &index) const
  13. {
  14. QModelIndex rowIndex = index.model()->index(index.row(), 0);
  15. if(!index.model()->data(rowIndex, Qt::UserRole).canConvert<stMeterInfo*>())
  16. {
  17. QColor c = option.state.testFlag(QStyle::State_Selected)?QColor(255, 255, 255):QColor(213, 213, 213);
  18. return c;
  19. }
  20. QColor colorText;
  21. if(LHStyleManager::Instance()->GetCurSkinStyle() == eLightStyle)
  22. {
  23. colorText = QColor(58, 63, 99);
  24. }
  25. else if(LHStyleManager::Instance()->GetCurSkinStyle() == eDeepStyle)
  26. {
  27. colorText = QColor(210, 210, 210);
  28. }
  29. return colorText;
  30. }
  31. void ChannelSettingDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  32. {
  33. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  34. QString text = index.data().toString();
  35. //获取字体
  36. FontEx font(qApp->font().family(), 14, false);
  37. font.setBold(option.state.testFlag(QStyle::State_None));
  38. painterEx->setFont(font);
  39. QRect rectText = option.rect.adjusted(0, 0, 0, 0);
  40. if(index.column() == 0)
  41. {
  42. rectText = QRect(QPoint(rectText.left() + 12, rectText.top()), rectText.size());
  43. }
  44. painterEx->DrawText(rectText, text, GetTextColor(option, index), Qt::AlignLeft|Qt::AlignVCenter);
  45. }
  46. QWidget* ChannelSettingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  47. {
  48. if(index.column() == 1)
  49. {
  50. CustomComboBox *tableComBox = new CustomComboBox(parent);
  51. tableComBox->installEventFilter(parent);
  52. tableComBox->addItem("不设置");
  53. for(const stMonitorPin &chn : qAsConst(m_listMonitorPin))
  54. {
  55. QVariant variant;
  56. variant.setValue(chn);
  57. tableComBox->addItem(chn.strMName, variant);
  58. }
  59. tableComBox->setCurrentIndex(0);
  60. return tableComBox;
  61. }
  62. return QStyledItemDelegate::createEditor(parent,option,index);
  63. }
  64. void ChannelSettingDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  65. {
  66. if(index.column() == 1)
  67. {
  68. QModelIndex rowIndex = index.model()->index(index.row(), 0);
  69. if(!index.model()->data(rowIndex, Qt::UserRole).canConvert<stMeterInfo*>()) return;
  70. stMeterInfo* outData = index.model()->data(rowIndex, Qt::UserRole).value<stMeterInfo*>();
  71. CustomComboBox* box = static_cast<CustomComboBox*>(editor);
  72. //通过监控点id获取最新监控点名称
  73. box->setCurrentText(outData->strMName.isEmpty() ? "不设置" : outData->strMName);
  74. box->m_currentText = box->currentText();
  75. }
  76. else
  77. {
  78. QStyledItemDelegate::setEditorData(editor,index);
  79. }
  80. }
  81. void ChannelSettingDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  82. {
  83. if(index.column() == 1)
  84. {
  85. CustomComboBox* box = static_cast<CustomComboBox*>(editor);
  86. QModelIndex rowIndex = model->index(index.row(), 0);
  87. if(!model->data(rowIndex, Qt::UserRole).canConvert<stMeterInfo*>()) return;
  88. stMeterInfo* outData = model->data(rowIndex, Qt::UserRole).value<stMeterInfo*>();
  89. if(box->currentIndex() == 0)
  90. {
  91. outData->strMName.clear();
  92. }
  93. else
  94. {
  95. outData->strMName = box->currentText();
  96. }
  97. QVariant var;
  98. var.setValue(outData);
  99. model->setData(rowIndex, var, Qt::UserRole);
  100. }
  101. else
  102. {
  103. QStyledItemDelegate::setModelData(editor,model,index);
  104. }
  105. }
  106. void ChannelSettingDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  107. {
  108. editor->setGeometry(option.rect);
  109. }
  110. void ChannelSettingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  111. {
  112. if(index.column() == 3)
  113. {
  114. QStyledItemDelegate::paint(painter, option, index);
  115. }
  116. else
  117. {
  118. //绘制文本
  119. PaintText(painter, option, index);
  120. }
  121. }
  122. bool ChannelSettingDelegate::eventFilter(QObject *obj, QEvent *event)
  123. {
  124. if (event->type() == QEvent::KeyPress)
  125. {
  126. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
  127. if (keyEvent->key() == Qt::Key_Tab)
  128. {
  129. return true;
  130. }
  131. }
  132. return QObject::eventFilter(obj, event);
  133. }