EditorPreferencesTreeWidgetItemDelegate.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "EditorPreferencesTreeWidgetItemDelegate.h"
  10. #include <QIcon>
  11. #include <QPainter>
  12. #include <QStyledItemDelegate>
  13. static const int IconX = 26;
  14. static const int TextX = 27;
  15. EditorPreferencesTreeWidgetItemDelegate::EditorPreferencesTreeWidgetItemDelegate(QWidget* parent)
  16. : QStyledItemDelegate(parent)
  17. {
  18. m_mouseOverBrush = QBrush(QColor(0x40, 0x40, 0x40));
  19. m_selectedBrush = QBrush(QColor(0x47, 0x47, 0x47));
  20. }
  21. void EditorPreferencesTreeWidgetItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
  22. {
  23. painter->save();
  24. painter->setPen(Qt::NoPen);
  25. const auto textHeight = option.fontMetrics.height();
  26. int vCenter = option.rect.bottom() - option.rect.height() / 2;
  27. int iconSize = option.decorationSize.width();
  28. if ((option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver))
  29. {
  30. if ((option.state & QStyle::State_Selected))
  31. {
  32. painter->setBrush(m_selectedBrush);
  33. }
  34. else if ((option.state & QStyle::State_MouseOver))
  35. {
  36. painter->setBrush(m_mouseOverBrush);
  37. }
  38. const QRect backGroundRect(option.rect.left(), option.rect.top(), option.rect.width(), option.rect.height());
  39. painter->drawRect(backGroundRect);
  40. }
  41. const auto& iconVariant = index.data(Qt::DecorationRole);
  42. if (!iconVariant.isNull())
  43. {
  44. const auto& icon = iconVariant.value<QIcon>();
  45. QRect r(IconX, vCenter - iconSize/2, iconSize, iconSize);
  46. r.setX(-r.width());
  47. icon.paint(painter, r);
  48. }
  49. const QString& text = index.data(Qt::DisplayRole).toString();
  50. painter->setPen(Qt::white);
  51. const auto textRect = QRect{ TextX, vCenter - textHeight/2, option.rect.width() - TextX, textHeight };
  52. painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
  53. painter->restore();
  54. }
  55. QSize EditorPreferencesTreeWidgetItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
  56. {
  57. return QStyledItemDelegate::sizeHint(option, index);
  58. }
  59. #include <moc_EditorPreferencesTreeWidgetItemDelegate.cpp>