ColumnGroupHeaderView.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "ColumnGroupHeaderView.h"
  10. // Qt
  11. #include <QPainter>
  12. // Editor
  13. #include "Util/ColumnGroupProxyModel.h"
  14. ColumnGroupHeaderView::ColumnGroupHeaderView(QWidget* parent)
  15. : QHeaderView(Qt::Horizontal, parent)
  16. , m_groupModel(nullptr)
  17. , m_showGroups(false)
  18. {
  19. setSectionsMovable(true);
  20. setStretchLastSection(true);
  21. setSortIndicatorShown(false);
  22. }
  23. void ColumnGroupHeaderView::setModel(QAbstractItemModel* model)
  24. {
  25. QHeaderView::setModel(model);
  26. m_groupModel = qobject_cast<ColumnGroupProxyModel*>(model);
  27. if (m_groupModel)
  28. {
  29. connect(m_groupModel, SIGNAL(SortChanged()), this, SLOT(update()));
  30. }
  31. }
  32. QSize ColumnGroupHeaderView::sizeHint() const
  33. {
  34. QSize s = QHeaderView::sizeHint();
  35. if (m_showGroups)
  36. {
  37. s.setHeight(s.height() + GroupViewHeight());
  38. }
  39. return s;
  40. }
  41. bool ColumnGroupHeaderView::IsGroupsShown() const
  42. {
  43. return m_showGroups;
  44. }
  45. bool ColumnGroupHeaderView::event(QEvent* event)
  46. {
  47. switch (event->type())
  48. {
  49. case QEvent::Paint:
  50. {
  51. if (!m_showGroups || !m_groupModel)
  52. {
  53. return true;
  54. }
  55. QPainter painter(this);
  56. painter.fillRect(rect(), QColor(145, 145, 145));
  57. int xOffset = 10;
  58. int yOffset = 10;
  59. auto groups = m_groupModel->Groups();
  60. m_groups.clear();
  61. foreach(int column, groups)
  62. {
  63. const int width = sectionSize(column);
  64. const QRect r(xOffset, yOffset, width == 0 ? defaultSectionSize() : width, QHeaderView::sizeHint().height());
  65. xOffset += r.width() + 10;
  66. yOffset += 10;
  67. if (column != groups.last())
  68. {
  69. painter.setPen(Qt::black);
  70. painter.drawLine(r.bottomRight() + QPoint(-3, 0), r.bottomRight() + QPoint(-3, 3));
  71. painter.drawLine(r.bottomRight() + QPoint(-3, 3), r.bottomRight() + QPoint(10, 3));
  72. }
  73. m_groups.push_back({ r, column });
  74. paintSection(&painter, r, column);
  75. }
  76. break;
  77. }
  78. case QEvent::MouseButtonRelease:
  79. {
  80. auto mouseEvent = static_cast<QMouseEvent*>(event);
  81. if (m_showGroups && m_groupModel)
  82. {
  83. foreach(const Group &group, m_groups)
  84. {
  85. if (group.rect.contains(mouseEvent->pos()))
  86. {
  87. auto sortOrder = m_groupModel->SortOrder(group.col);
  88. m_groupModel->sort(group.col, (sortOrder == Qt::AscendingOrder) ?
  89. Qt::DescendingOrder : Qt::AscendingOrder);
  90. }
  91. }
  92. }
  93. break;
  94. }
  95. default:
  96. return QHeaderView::event(event);
  97. break;
  98. }
  99. return true;
  100. }
  101. void ColumnGroupHeaderView::ShowGroups(bool showGroups)
  102. {
  103. m_showGroups = showGroups;
  104. Q_EMIT geometriesChanged();
  105. }
  106. void ColumnGroupHeaderView::updateGeometries()
  107. {
  108. setViewportMargins(0, m_showGroups ? GroupViewHeight() : 0, 0, 0);
  109. QHeaderView::updateGeometries();
  110. }
  111. int ColumnGroupHeaderView::GroupViewHeight() const
  112. {
  113. if (!m_groupModel)
  114. {
  115. return 0;
  116. }
  117. int groupCount = m_groupModel->Groups().size();
  118. return QHeaderView::sizeHint().height() + qMax(0, groupCount - 1) * 10 + 20;
  119. }
  120. #include <Util/moc_ColumnGroupHeaderView.cpp>