MultiLevelHeaderView.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef MULTILEVELHEADERVIEW_H
  2. #define MULTILEVELHEADERVIEW_H
  3. #include <QHeaderView>
  4. #include <QModelIndex>
  5. enum MyItemDataRole
  6. {
  7. COLUMN_SPAN_ROLE = Qt::UserRole + 1,
  8. ROW_SPAN_ROLE,
  9. };
  10. class MultiLevelHeaderView : public QHeaderView
  11. {
  12. Q_OBJECT
  13. public:
  14. MultiLevelHeaderView(Qt::Orientation orientation, int rows, int columns, QWidget *parent = 0);
  15. virtual ~MultiLevelHeaderView();
  16. void setRowHeight(int row, int rowHeight);
  17. void setColumnWidth(int col, int colWidth);
  18. void setCellData(int row, int column, int role, const QVariant &value);
  19. // the below methods is just shortcut for setCellData
  20. void setCellSpan(int row, int column, int rowSpanCount, int columnSpanCount);
  21. void setCellBackgroundColor(int row, int column, const QColor &);
  22. void setCellForegroundColor(int row, int column, const QColor &);
  23. void setCellText(int row, int column, const QString &text);
  24. QModelIndex columnSpanIndex(const QModelIndex &currentIndex) const;
  25. QModelIndex rowSpanIndex(const QModelIndex &currentIndex) const;
  26. protected:
  27. // override
  28. virtual void mousePressEvent(QMouseEvent *event) override;
  29. virtual QModelIndex indexAt(const QPoint &) const override;
  30. virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
  31. virtual QSize sectionSizeFromContents(int logicalIndex) const override;
  32. // inherent features
  33. int columnSpanSize(int row, int from, int spanCount) const;
  34. int rowSpanSize(int column, int from, int spanCount) const;
  35. bool getRootCell(int row, int column, int &rootCellRow, int &rootCellColumn) const;
  36. // (row, column) must be root cell of merged cells
  37. QRect getCellRect(int row, int column) const;
  38. int getSectionRange(QModelIndex &index, int *beginSection, int *endSection) const;
  39. protected slots:
  40. void onSectionResized(int logicalIdx, int oldSize, int newSize);
  41. signals:
  42. void sectionPressed(int from, int to);
  43. };
  44. #endif // MULTILEVELHEADERVIEW_H