GemRequirementDelegate.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <GemCatalog/GemRequirementDelegate.h>
  9. #include <GemCatalog/GemModel.h>
  10. #include <QPainter>
  11. #include <QMouseEvent>
  12. #include <QDesktopServices>
  13. namespace O3DE::ProjectManager
  14. {
  15. GemRequirementDelegate::GemRequirementDelegate(QAbstractItemModel* model, QObject* parent)
  16. : GemItemDelegate(model, nullptr, parent)
  17. {
  18. }
  19. void GemRequirementDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& modelIndex) const
  20. {
  21. if (!modelIndex.isValid())
  22. {
  23. return;
  24. }
  25. QStyleOptionViewItem options(option);
  26. initStyleOption(&options, modelIndex);
  27. painter->save();
  28. painter->setRenderHint(QPainter::Antialiasing);
  29. QRect fullRect, itemRect, contentRect;
  30. CalcRects(options, fullRect, itemRect, contentRect);
  31. QFont standardFont(options.font);
  32. standardFont.setPixelSize(static_cast<int>(s_fontSize));
  33. QFontMetrics standardFontMetrics(standardFont);
  34. painter->setClipping(true);
  35. painter->setClipRect(fullRect);
  36. painter->setFont(options.font);
  37. // Draw background
  38. painter->fillRect(fullRect, m_backgroundColor);
  39. // Draw item background
  40. const QColor itemBackgroundColor = m_itemBackgroundColor;
  41. painter->fillRect(itemRect, itemBackgroundColor);
  42. // Gem name
  43. QString gemName = GemModel::GetDisplayName(modelIndex);
  44. QFont gemNameFont(options.font);
  45. const int firstColumnMaxTextWidth = s_defaultSummaryStartX - 30;
  46. gemName = QFontMetrics(gemNameFont).elidedText(gemName, Qt::TextElideMode::ElideRight, firstColumnMaxTextWidth);
  47. gemNameFont.setPixelSize(static_cast<int>(s_gemNameFontSize));
  48. gemNameFont.setBold(true);
  49. QRect gemNameRect = GetTextRect(gemNameFont, gemName, s_gemNameFontSize);
  50. gemNameRect.moveTo(contentRect.left(), contentRect.center().y() - static_cast<int>(s_gemNameFontSize));
  51. painter->setFont(gemNameFont);
  52. painter->setPen(m_textColor);
  53. painter->drawText(gemNameRect, Qt::TextSingleLine, gemName);
  54. // Gem requirement
  55. const QRect requirementRect = CalcRequirementRect(contentRect);
  56. const QString& requirement = GemModel::GetGemInfo(modelIndex).m_requirement;
  57. DrawText(requirement, painter, requirementRect, standardFont);
  58. painter->restore();
  59. }
  60. QRect GemRequirementDelegate::CalcRequirementRect(const QRect& contentRect) const
  61. {
  62. const QSize requirementSize = QSize(contentRect.width() - s_defaultSummaryStartX - s_itemMargins.right(), contentRect.height());
  63. return QRect(QPoint(contentRect.left() + s_defaultSummaryStartX, contentRect.top()), requirementSize);
  64. }
  65. bool GemRequirementDelegate::editorEvent(
  66. [[maybe_unused]] QEvent* event,
  67. [[maybe_unused]] QAbstractItemModel* model,
  68. [[maybe_unused]] const QStyleOptionViewItem& option,
  69. [[maybe_unused]] const QModelIndex& modelIndex)
  70. {
  71. if (!modelIndex.isValid())
  72. {
  73. return false;
  74. }
  75. if (event->type() == QEvent::MouseButtonPress)
  76. {
  77. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  78. QRect fullRect, itemRect, contentRect;
  79. CalcRects(option, fullRect, itemRect, contentRect);
  80. const QRect requirementsRect = CalcRequirementRect(contentRect);
  81. if (requirementsRect.contains(mouseEvent->pos()))
  82. {
  83. const QString& requirementHtml = GemModel::GetGemInfo(modelIndex).m_requirement;
  84. QString anchor = anchorAt(requirementHtml, mouseEvent->pos(), requirementsRect);
  85. if (!anchor.isEmpty())
  86. {
  87. QDesktopServices::openUrl(QUrl(anchor));
  88. return true;
  89. }
  90. }
  91. }
  92. return QStyledItemDelegate::editorEvent(event, model, option, modelIndex);
  93. }
  94. } // namespace O3DE::ProjectManager