ColumnGroupItemDelegate.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "ColumnGroupItemDelegate.h"
  10. // Qt
  11. #include <QHeaderView>
  12. #include <QPainter>
  13. #include <QTreeView>
  14. ColumnGroupItemDelegate::ColumnGroupItemDelegate(QObject* parent)
  15. : QStyledItemDelegate(parent)
  16. {
  17. }
  18. QSize ColumnGroupItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
  19. {
  20. // group title indexes have no own width, their text is drawn over all columns
  21. if (index.model()->hasChildren(index) && index.column() == 0)
  22. {
  23. return QSize(32, QStyledItemDelegate::sizeHint(option, index).height());
  24. }
  25. return QStyledItemDelegate::sizeHint(option, index);
  26. }
  27. void ColumnGroupItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
  28. {
  29. if (index.model()->hasChildren(index))
  30. {
  31. painter->setClipping(false);
  32. painter->setPen(option.palette.text().color());
  33. if (option.state & QStyle::State_Selected)
  34. {
  35. painter->setPen(option.palette.highlightedText().color());
  36. }
  37. QRect textRect = option.rect;
  38. textRect.setRight(qobject_cast<QWidget*>(parent())->width());
  39. if (option.state & QStyle::State_Selected && index.column() == 0)
  40. {
  41. painter->fillRect(textRect, option.palette.highlight());
  42. }
  43. // there's just one text - somewhere in the model row, show it!
  44. QString content;
  45. const int columnCount = index.model()->columnCount(index.parent());
  46. for (int column = 0; column < columnCount; ++column)
  47. {
  48. content += index.sibling(index.row(), column).data().toString();
  49. }
  50. int alignment = index.data(Qt::TextAlignmentRole).toInt();
  51. if (index.column() == 0)
  52. {
  53. painter->drawText(textRect, (alignment == 0 ? Qt::AlignLeft | Qt::AlignVCenter : alignment) | Qt::ElideRight, content);
  54. }
  55. if (!index.parent().isValid() && index.row() > 0)
  56. {
  57. QTreeView* tv = qobject_cast<QTreeView*>(option.styleObject);
  58. if (tv)
  59. {
  60. // draw a line in the same color as the table header for separation between groups
  61. QStyleOptionHeader header;
  62. header.rect = QRect(QPoint(1, textRect.top()), textRect.topRight() - QPoint(1,0));
  63. tv->style()->drawControl(QStyle::CE_HeaderSection, &header, painter, tv->header());
  64. }
  65. }
  66. }
  67. else
  68. {
  69. QStyledItemDelegate::paint(painter, option, index);
  70. }
  71. }