basedelegate.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "basedelegate.h"
  2. #include <QPainter>
  3. #include <Common/External/Core/PaintHelper/painthelper.h>
  4. #include <QApplication>
  5. BaseDelegate::BaseDelegate(QObject *parent, bool bottomBorderVisible, Qt::Alignment flags)
  6. : BaseItemDelegate(parent)
  7. , m_bBottomBorderVisible(bottomBorderVisible)
  8. , m_alignmentText(flags)
  9. , m_BaseHHeader(nullptr)
  10. {
  11. SetSelectedColor(QColor(67, 142, 255, 51));
  12. }
  13. void BaseDelegate::SetBottomBorderVisible(bool value)
  14. {
  15. m_bBottomBorderVisible = value;
  16. }
  17. void BaseDelegate::SetTextAlignment(Qt::Alignment flags)
  18. {
  19. m_alignmentText = flags;
  20. }
  21. QColor BaseDelegate::GetBaseBGColor(const QStyleOptionViewItem &, const QModelIndex &) const
  22. {
  23. return QColor(70, 70, 73);
  24. }
  25. QColor BaseDelegate::GetSpecifiedBGColor(const QStyleOptionViewItem &, const QModelIndex &, SpecifiedBGRole) const
  26. {
  27. return QColor();
  28. }
  29. QFont BaseDelegate::GetFont(const QStyleOptionViewItem &option, const QModelIndex &) const
  30. {
  31. FontEx font(qApp->font().family(), 14, false);
  32. font.setBold(option.state.testFlag(QStyle::State_None));
  33. return font;
  34. // if(index.column() == 0) return FontEx("黑体", 14, false);
  35. // if(index.column() == 1) return FontEx("黑体", 16, false);
  36. // if(index.column() == 2) return FontEx("微软雅黑", 14, false);
  37. // if(index.column() == 3) return FontEx("微软雅黑", 16, false);
  38. // if(index.column() == 4) return FontEx("阿里巴巴普惠体", 14, false);
  39. // if(index.column() == 5) return FontEx("阿里巴巴普惠体", 16, false);
  40. //return FontEx("微软雅黑", 14, false);
  41. }
  42. QColor BaseDelegate::GetTextColor(const QStyleOptionViewItem &option, const QModelIndex &) const
  43. {
  44. QColor c = option.state.testFlag(QStyle::State_Selected)?QColor(210, 210, 210):QColor(210, 210, 210);
  45. return c;
  46. }
  47. QRect BaseDelegate::GetFontRect(const QStyleOptionViewItem &option, const QModelIndex &) const
  48. {
  49. return option.rect.adjusted(0, 0, 0, 0);
  50. }
  51. void BaseDelegate::PaintBase(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  52. {
  53. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  54. QColor baseBGColor = GetBaseBGColor(option, index);
  55. if(baseBGColor.isValid())
  56. {
  57. painterEx->SetBrushOnly(baseBGColor);
  58. painterEx->drawRect(option.rect);
  59. }
  60. //单元格的底部线
  61. // if(m_bBottomBorderVisible) painterEx->DrawBorder(option.rect, QColor(255, 255, 255, 38), PainterEx::RectBorderBottom);
  62. }
  63. void BaseDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  64. {
  65. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  66. QString text = QFontMetrics(GetFont(option, index)).elidedText(index.data().toString(), Qt::ElideRight, GetFontRect(option, index).width());
  67. painterEx->setFont(GetFont(option, index));
  68. QRect rectText = GetFontRect(option, index);
  69. if(index.column() == 0)
  70. {
  71. rectText = QRect(QPoint(rectText.left() + 12, rectText.top()), rectText.size());
  72. }
  73. painterEx->DrawText(rectText, text, GetTextColor(option, index), m_alignmentText);
  74. }