xcheckboxheaderview.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef CCHECKBOXHEADERVIEW
  2. #define CCHECKBOXHEADERVIEW
  3. #endif // CCHECKBOXHEADERVIEW
  4. #include <QtGui>
  5. #include <QHeaderView>
  6. #include <QStyleOptionButton>
  7. #include <QStyle>
  8. #include "SNCCore_global.h"
  9. class SNCCORESHARED_EXPORT XCheckBoxHeaderView : public QHeaderView
  10. {
  11. Q_OBJECT
  12. public:
  13. XCheckBoxHeaderView( int checkColumnIndex,
  14. Qt::Orientation orientation,
  15. QWidget * parent = 0) :
  16. QHeaderView(orientation, parent)
  17. {
  18. m_checkColIdx = checkColumnIndex;
  19. }
  20. signals:
  21. void selected();
  22. void unselected();
  23. protected:
  24. void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
  25. {
  26. painter->save();
  27. QHeaderView::paintSection(painter, rect, logicalIndex);
  28. painter->restore();
  29. if (logicalIndex == m_checkColIdx)
  30. {
  31. QStyleOptionButton option;
  32. int width = 10;
  33. for (int i=0; i<logicalIndex; ++i)
  34. width += sectionSize( i );
  35. option.rect = QRect(width, 5, 15, 15);
  36. if (isOn)
  37. option.state = QStyle::State_On;
  38. else
  39. option.state = QStyle::State_Off;
  40. // this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
  41. this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
  42. }
  43. }
  44. void mousePressEvent(QMouseEvent *event)
  45. {
  46. if (visualIndexAt(event->pos().x()) == m_checkColIdx)
  47. {
  48. // setSortIndicatorShown( false );
  49. if (isOn)
  50. {
  51. isOn = false;
  52. emit unselected();
  53. }
  54. else
  55. {
  56. isOn = true;
  57. emit selected();
  58. }
  59. this->updateSection(m_checkColIdx);
  60. }
  61. // else
  62. // setSortIndicatorShown( true );
  63. QHeaderView::mousePressEvent(event);
  64. }
  65. private:
  66. bool isOn;
  67. int m_checkColIdx;
  68. };