groupscreendelegate.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "groupscreendelegate.h"
  2. #include "Common/External/Core/PaintHelper/painthelper.h"
  3. #include "Common/DataBase/entities.h"
  4. #include <QDebug>
  5. GroupScreenDelegate::GroupScreenDelegate(QObject *parent)
  6. : ButtonDelegate(parent)
  7. , m_nTargetRow(-1)
  8. , m_bIsUpOn(false)
  9. , m_nRadius(8)
  10. {
  11. }
  12. void GroupScreenDelegate::SetDragTarget(int nTargetRow, bool bIsUpOn)
  13. {
  14. m_nTargetRow = nTargetRow;
  15. m_bIsUpOn = bIsUpOn;
  16. }
  17. QString GroupScreenDelegate::GetCheckboxImage(Qt::CheckState state) const
  18. {
  19. if(state == Qt::Unchecked)
  20. {
  21. return ":/checkbox_unchecked.png";
  22. }
  23. else if(state == Qt::Checked)
  24. {
  25. return ":/checkbox_checked.png";
  26. }
  27. return "";
  28. }
  29. void GroupScreenDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  30. {
  31. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  32. QString text = index.data().toString();
  33. painterEx->setFont(GetFont(option, index));
  34. QRect rectText = GetFontRect(option, index);
  35. if(index.column() == 0)
  36. {
  37. rectText = QRect(QPoint(rectText.left() + 12 + 16 + 16, rectText.top()), rectText.size());
  38. }
  39. painterEx->DrawText(rectText, text, GetTextColor(option, index), Qt::AlignLeft|Qt::AlignVCenter);
  40. QRect rect = option.rect;
  41. // 绘制复选框
  42. if(painterEx && index.column() == 0)
  43. {
  44. if(!index.model()->data(index, Qt::UserRole).canConvert<stGroupScreen>())
  45. {
  46. return;
  47. }
  48. stGroupScreen data = index.model()->data(index, Qt::UserRole).value<stGroupScreen>();
  49. painterEx->save();
  50. QRect rectTarget(rect.left() + 12, rect.top() + (rect.height() - 18) / 2, 18, 18);
  51. painterEx->DrawPixmap(rectTarget, GetCheckboxImage(data.checkState));
  52. painterEx->restore();
  53. }
  54. }
  55. void GroupScreenDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  56. {
  57. ButtonDelegate::paint(painter,option,index);
  58. if(m_nTargetRow == -1) return;
  59. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  60. // 绘制第一列单元格圆圈和线
  61. if(index.row() == m_nTargetRow && index.column() == 0)
  62. {
  63. int y = m_bIsUpOn ? option.rect.top() : option.rect.bottom();
  64. painterEx->DrawCircle(QPoint(m_nRadius + 1, y), m_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2));
  65. painterEx->setPen(QPen(QColor(86,135,228), 2));
  66. painterEx->drawLine(option.rect.left() + m_nRadius * 2 + 1, y, option.rect.right(), y);
  67. }
  68. // 如果指示器绘制在当前行的下方, 因为头部圆圈会跨到下一行, 所以在绘制下一行的时候, 又绘制一次
  69. if(!m_bIsUpOn && (index.row() == m_nTargetRow + 1) && index.column() == 0)
  70. {
  71. int y = option.rect.top() - 1;
  72. painterEx->DrawCircle(QPoint(m_nRadius + 1, y), m_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2));
  73. painterEx->setPen(QPen(QColor(86,135,228), 2));
  74. painterEx->drawLine(option.rect.left() + m_nRadius * 2 + 1, y, option.rect.right(), y);
  75. }
  76. // 绘制后面单元格的线
  77. if(index.row() == m_nTargetRow && index.column() != 0)
  78. {
  79. painterEx->DrawBorder(option.rect, QPen(QColor(86,135,228), 2), m_bIsUpOn ? PainterEx::RectBorderTop : PainterEx::RectBorderBottom);
  80. }
  81. }