AdjustableHeaderWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <AdjustableHeaderWidget.h>
  9. #include <AzCore/Debug/Trace.h>
  10. #include <QHeaderView>
  11. #include <QTimer>
  12. namespace O3DE::ProjectManager
  13. {
  14. AdjustableHeaderWidget::AdjustableHeaderWidget( const QStringList& headerLabels,
  15. const QVector<int>& defaultHeaderWidths, int minHeaderWidth,
  16. const QVector<QHeaderView::ResizeMode>& resizeModes, QWidget* parent)
  17. : QTableWidget(parent)
  18. {
  19. setObjectName("adjustableHeaderWidget");
  20. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
  21. setFixedHeight(s_headerWidgetHeight);
  22. m_header = horizontalHeader();
  23. m_header->setDefaultAlignment(Qt::AlignLeft);
  24. setColumnCount(headerLabels.count());
  25. setHorizontalHeaderLabels(headerLabels);
  26. AZ_Assert(defaultHeaderWidths.count() == columnCount(), "Default header widths does not match number of columns");
  27. AZ_Assert(resizeModes.count() == columnCount(), "Resize modesdoes not match number of columns");
  28. for (int column = 0; column < columnCount(); ++column)
  29. {
  30. m_header->resizeSection(column, defaultHeaderWidths[column]);
  31. m_header->setSectionResizeMode(column, resizeModes[column]);
  32. }
  33. m_header->setMinimumSectionSize(minHeaderWidth);
  34. m_header->setCascadingSectionResizes(true);
  35. connect(m_header, &QHeaderView::sectionResized, this, &AdjustableHeaderWidget::OnSectionResized);
  36. }
  37. void AdjustableHeaderWidget::OnSectionResized(int logicalIndex, int oldSize, int newSize)
  38. {
  39. const int headerCount = columnCount();
  40. const int headerWidth = m_header->width();
  41. const int totalSectionWidth = m_header->length();
  42. if (totalSectionWidth > headerWidth && newSize > oldSize)
  43. {
  44. int xPos = 0;
  45. int requiredWidth = 0;
  46. for (int i = 0; i < headerCount; i++)
  47. {
  48. if (i < logicalIndex)
  49. {
  50. xPos += m_header->sectionSize(i);
  51. }
  52. else if (i == logicalIndex)
  53. {
  54. xPos += newSize;
  55. }
  56. else if (i > logicalIndex)
  57. {
  58. if (m_header->sectionResizeMode(i) == QHeaderView::ResizeMode::Fixed)
  59. {
  60. requiredWidth += m_header->sectionSize(i);
  61. }
  62. else
  63. {
  64. requiredWidth += m_header->minimumSectionSize();
  65. }
  66. }
  67. }
  68. if (xPos + requiredWidth > headerWidth)
  69. {
  70. m_header->resizeSection(logicalIndex, oldSize);
  71. }
  72. }
  73. // wait till all columns resized
  74. QTimer::singleShot(0, [&]()
  75. {
  76. // only re-paint when the header and section widths have settled
  77. const int headerWidth = m_header->width();
  78. const int totalSectionWidth = m_header->length();
  79. if (totalSectionWidth == headerWidth)
  80. {
  81. emit sectionsResized();
  82. }
  83. });
  84. }
  85. QPair<int, int> AdjustableHeaderWidget::CalcColumnXBounds(int headerIndex) const
  86. {
  87. // Total the widths of all headers before this one in first and including it in second
  88. QPair<int, int> bounds(0, 0);
  89. for (int curIndex = 0; curIndex <= headerIndex; ++curIndex)
  90. {
  91. if (curIndex == headerIndex)
  92. {
  93. bounds.first = bounds.second;
  94. }
  95. bounds.second += m_header->sectionSize(curIndex);
  96. }
  97. return bounds;
  98. }
  99. } // namespace O3DE::ProjectManager