GemItemDelegate.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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/GemItemDelegate.h>
  9. #include <GemCatalog/GemModel.h>
  10. #include <GemCatalog/GemSortFilterProxyModel.h>
  11. #include <AdjustableHeaderWidget.h>
  12. #include <ProjectManagerDefs.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <QEvent>
  15. #include <QAbstractItemView>
  16. #include <QPainter>
  17. #include <QMouseEvent>
  18. #include <QHelpEvent>
  19. #include <QToolTip>
  20. #include <QHoverEvent>
  21. #include <QTextDocument>
  22. #include <QAbstractTextDocumentLayout>
  23. #include <QDesktopServices>
  24. #include <QMovie>
  25. #include <QHeaderView>
  26. #include <QDir>
  27. namespace O3DE::ProjectManager
  28. {
  29. GemItemDelegate::GemItemDelegate(QAbstractItemModel* model, AdjustableHeaderWidget* header, bool readOnly, QObject* parent)
  30. : QStyledItemDelegate(parent)
  31. , m_model(model)
  32. , m_headerWidget(header)
  33. , m_readOnly(readOnly)
  34. {
  35. AddPlatformIcon(GemInfo::Android, ":/Android.svg");
  36. AddPlatformIcon(GemInfo::iOS, ":/iOS.svg");
  37. AddPlatformIcon(GemInfo::Linux, ":/Linux.svg");
  38. AddPlatformIcon(GemInfo::macOS, ":/macOS.svg");
  39. AddPlatformIcon(GemInfo::Windows, ":/Windows.svg");
  40. SetStatusIcon(m_notDownloadedPixmap, ":/Download.svg");
  41. SetStatusIcon(m_unknownStatusPixmap, ":/X.svg");
  42. SetStatusIcon(m_downloadSuccessfulPixmap, ":/checkmark.svg");
  43. SetStatusIcon(m_downloadFailedPixmap, ":/Warning.svg");
  44. SetStatusIcon(m_downloadedPixmap, ":/Downloaded.svg");
  45. m_updatePixmap = QIcon(":/Update.svg").pixmap(s_statusIconSize, s_statusIconSize);
  46. m_downloadingMovie = new QMovie(":/in_progress.gif");
  47. }
  48. void GemItemDelegate::AddPlatformIcon(GemInfo::Platform platform, const QString& iconPath)
  49. {
  50. QPixmap pixmap(iconPath);
  51. qreal aspectRatio = static_cast<qreal>(pixmap.width()) / pixmap.height();
  52. m_platformIcons.insert(platform, QIcon(iconPath).pixmap(static_cast<int>(static_cast<qreal>(s_platformIconSize) * aspectRatio), s_platformIconSize));
  53. }
  54. void GemItemDelegate::SetStatusIcon(QPixmap& m_iconPixmap, const QString& iconPath)
  55. {
  56. QPixmap pixmap(iconPath);
  57. const float aspectRatio = static_cast<float>(pixmap.width()) / pixmap.height();
  58. int xScaler = m_readOnly ? s_statusIconSizeLarge : s_statusIconSize;
  59. int yScaler = xScaler;
  60. if (aspectRatio > 1.0f)
  61. {
  62. yScaler = static_cast<int>(1.0f / aspectRatio * xScaler);
  63. }
  64. else if (aspectRatio < 1.0f)
  65. {
  66. xScaler = static_cast<int>(aspectRatio * yScaler);
  67. }
  68. m_iconPixmap = QIcon(iconPath).pixmap(xScaler, yScaler);
  69. }
  70. void GemItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& modelIndex) const
  71. {
  72. if (!modelIndex.isValid())
  73. {
  74. return;
  75. }
  76. const GemInfo& gemInfo = GemModel::GetGemInfo(modelIndex);
  77. QStyleOptionViewItem options(option);
  78. initStyleOption(&options, modelIndex);
  79. painter->setRenderHint(QPainter::Antialiasing);
  80. QRect fullRect, itemRect, contentRect;
  81. CalcRects(options, fullRect, itemRect, contentRect);
  82. QFont standardFont(options.font);
  83. standardFont.setPixelSize(static_cast<int>(s_fontSize));
  84. QFontMetrics standardFontMetrics(standardFont);
  85. painter->save();
  86. painter->setClipping(true);
  87. painter->setClipRect(fullRect);
  88. painter->setFont(options.font);
  89. // Draw background
  90. painter->fillRect(fullRect, m_backgroundColor);
  91. // Draw item background
  92. const QColor itemBackgroundColor = options.state & QStyle::State_MouseOver ? m_itemBackgroundColor.lighter(120) : m_itemBackgroundColor;
  93. painter->fillRect(itemRect, itemBackgroundColor);
  94. // Draw border
  95. if (options.state & QStyle::State_Selected)
  96. {
  97. painter->save();
  98. QPen borderPen(m_borderColor);
  99. borderPen.setWidth(s_borderWidth);
  100. painter->setPen(borderPen);
  101. painter->drawRect(itemRect);
  102. painter->restore();
  103. }
  104. // Gem preview
  105. QString previewPath = QDir(gemInfo.m_path).filePath(ProjectPreviewImagePath);
  106. QPixmap gemPreviewImage(previewPath);
  107. QRect gemPreviewRect(
  108. contentRect.left() + AdjustableHeaderWidget::s_headerTextIndent,
  109. contentRect.center().y() - GemPreviewImageHeight / 2,
  110. GemPreviewImageWidth, GemPreviewImageHeight);
  111. painter->drawPixmap(gemPreviewRect, gemPreviewImage);
  112. // Gem (dispay) name
  113. QFont gemNameFont(options.font);
  114. QPair<int, int> nameXBounds = CalcColumnXBounds(HeaderOrder::Name);
  115. const int nameStartX = nameXBounds.first;
  116. const int nameColumnTextStartX = s_itemMargins.left() + nameStartX + AdjustableHeaderWidget::s_headerTextIndent;
  117. const int nameColumnMaxTextWidth = nameXBounds.second - nameStartX - AdjustableHeaderWidget::s_headerTextIndent;
  118. gemNameFont.setPixelSize(static_cast<int>(s_gemNameFontSize));
  119. gemNameFont.setBold(true);
  120. QString gemName = QFontMetrics(gemNameFont).elidedText(GemModel::GetDisplayName(modelIndex), Qt::TextElideMode::ElideRight, nameColumnMaxTextWidth);
  121. QRect gemNameRect = GetTextRect(gemNameFont, gemName, s_gemNameFontSize);
  122. gemNameRect.moveTo(nameColumnTextStartX, contentRect.top());
  123. painter->setFont(gemNameFont);
  124. painter->setPen(m_textColor);
  125. gemNameRect = painter->boundingRect(gemNameRect, Qt::TextSingleLine, gemName);
  126. painter->drawText(gemNameRect, Qt::TextSingleLine, gemName);
  127. // Gem creator
  128. QString gemCreator = gemInfo.m_origin;
  129. gemCreator = standardFontMetrics.elidedText(gemCreator, Qt::TextElideMode::ElideRight, nameColumnMaxTextWidth);
  130. QRect gemCreatorRect = GetTextRect(standardFont, gemCreator, s_fontSize);
  131. gemCreatorRect.moveTo(nameColumnTextStartX, contentRect.top() + gemNameRect.height());
  132. painter->setFont(standardFont);
  133. gemCreatorRect = painter->boundingRect(gemCreatorRect, Qt::TextSingleLine, gemCreator);
  134. painter->drawText(gemCreatorRect, Qt::TextSingleLine, gemCreator);
  135. // Gem summary
  136. const bool hasTags = !gemInfo.m_features.isEmpty();
  137. const QRect summaryRect = CalcSummaryRect(contentRect, hasTags);
  138. DrawText(gemInfo.m_summary, painter, summaryRect, standardFont);
  139. // Gem Version
  140. if (!gemInfo.m_version.isEmpty() && !gemInfo.m_version.contains("unknown", Qt::CaseInsensitive))
  141. {
  142. QPair<int, int> versionXBounds = CalcColumnXBounds(HeaderOrder::Version);
  143. QRect gemVersionRect{ versionXBounds.first, contentRect.top(), versionXBounds.second - versionXBounds.first, contentRect.height() };
  144. painter->setFont(standardFont);
  145. gemVersionRect = painter->boundingRect(gemVersionRect, Qt::TextWordWrap | Qt::AlignRight | Qt::AlignVCenter, gemInfo.m_version);
  146. painter->drawText(gemVersionRect, Qt::TextWordWrap | Qt::AlignRight | Qt::AlignVCenter, gemInfo.m_version);
  147. GemSortFilterProxyModel* proxyModel = reinterpret_cast<GemSortFilterProxyModel*>(m_model);
  148. bool showCompatibleUpdatesOnly = proxyModel ? proxyModel->GetCompatibleFilterFlag() : true;
  149. if (GemModel::HasUpdates(modelIndex, showCompatibleUpdatesOnly))
  150. {
  151. painter->drawPixmap(gemVersionRect.left() - s_statusButtonSpacing - m_updatePixmap.width(),
  152. contentRect.center().y() - m_updatePixmap.height() / 2,
  153. m_updatePixmap);
  154. }
  155. }
  156. QRect buttonRect = CalcButtonRect(contentRect);
  157. DrawDownloadStatusIcon(painter, contentRect, buttonRect, modelIndex);
  158. if (!m_readOnly)
  159. {
  160. DrawButton(painter, buttonRect, modelIndex);
  161. }
  162. DrawPlatformText(painter, contentRect, standardFont, modelIndex);
  163. DrawFeatureTags(painter, contentRect, gemInfo.m_features, standardFont, summaryRect);
  164. painter->restore();
  165. }
  166. QRect GemItemDelegate::CalcSummaryRect(const QRect& contentRect, bool hasTags) const
  167. {
  168. const int featureTagAreaHeight = 40;
  169. const int summaryHeight = contentRect.height() - (hasTags * featureTagAreaHeight);
  170. const auto [summaryStartX, summaryEndX] = CalcColumnXBounds(HeaderOrder::Summary);
  171. const QSize summarySize =
  172. QSize(summaryEndX - summaryStartX - AdjustableHeaderWidget::s_headerTextIndent - s_extraSummarySpacing,
  173. summaryHeight);
  174. return QRect(
  175. QPoint(s_itemMargins.left() + summaryStartX + AdjustableHeaderWidget::s_headerTextIndent, contentRect.top()), summarySize);
  176. }
  177. QSize GemItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& modelIndex) const
  178. {
  179. QStyleOptionViewItem options(option);
  180. initStyleOption(&options, modelIndex);
  181. int marginsHorizontal = s_itemMargins.left() + s_itemMargins.right() + s_contentMargins.left() + s_contentMargins.right();
  182. return QSize(marginsHorizontal + s_buttonWidth + s_defaultSummaryStartX, s_height);
  183. }
  184. bool GemItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& modelIndex)
  185. {
  186. if (!modelIndex.isValid())
  187. {
  188. return false;
  189. }
  190. if (event->type() == QEvent::KeyPress)
  191. {
  192. auto keyEvent = static_cast<const QKeyEvent*>(event);
  193. if (keyEvent->key() == Qt::Key_Space && !m_readOnly)
  194. {
  195. const bool isAdded = GemModel::IsAdded(modelIndex);
  196. GemModel::SetIsAdded(*model, modelIndex, !isAdded);
  197. return true;
  198. }
  199. }
  200. else if (event->type() == QEvent::MouseButtonPress)
  201. {
  202. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  203. QRect fullRect, itemRect, contentRect;
  204. CalcRects(option, fullRect, itemRect, contentRect);
  205. if (!m_readOnly)
  206. {
  207. const QRect buttonRect = CalcButtonRect(contentRect);
  208. if (buttonRect.contains(mouseEvent->pos()))
  209. {
  210. const bool isAdded = GemModel::IsAdded(modelIndex);
  211. GemModel::SetIsAdded(*model, modelIndex, !isAdded);
  212. return true;
  213. }
  214. }
  215. // we must manually handle html links because we aren't using QLabels
  216. const GemInfo& gemInfo = GemModel::GetGemInfo(modelIndex);
  217. const bool hasTags = !gemInfo.m_features.isEmpty();
  218. const QRect summaryRect = CalcSummaryRect(contentRect, hasTags);
  219. if (summaryRect.contains(mouseEvent->pos()))
  220. {
  221. QString anchor = anchorAt(gemInfo.m_summary, mouseEvent->pos(), summaryRect);
  222. if (!anchor.isEmpty())
  223. {
  224. QDesktopServices::openUrl(QUrl(anchor));
  225. return true;
  226. }
  227. }
  228. }
  229. return QStyledItemDelegate::editorEvent(event, model, option, modelIndex);
  230. }
  231. QString GetGemNameList(const QVector<QPersistentModelIndex> modelIndices)
  232. {
  233. QString gemNameList;
  234. for (int i = 0; i < modelIndices.size(); ++i)
  235. {
  236. if (!gemNameList.isEmpty())
  237. {
  238. if (i == modelIndices.size() - 1)
  239. {
  240. gemNameList.append(" and ");
  241. }
  242. else
  243. {
  244. gemNameList.append(", ");
  245. }
  246. }
  247. gemNameList.append(GemModel::GetDisplayName(modelIndices[i]));
  248. }
  249. return gemNameList;
  250. }
  251. bool GemItemDelegate::helpEvent(QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, const QModelIndex& index)
  252. {
  253. if (event->type() == QEvent::ToolTip)
  254. {
  255. QRect fullRect, itemRect, contentRect;
  256. CalcRects(option, fullRect, itemRect, contentRect);
  257. const QRect buttonRect = CalcButtonRect(contentRect);
  258. if (buttonRect.contains(event->pos()))
  259. {
  260. if (!QToolTip::isVisible())
  261. {
  262. if(GemModel::IsAddedDependency(index) && !GemModel::IsAdded(index))
  263. {
  264. const GemModel* gemModel = GemModel::GetSourceModel(index.model());
  265. AZ_Assert(gemModel, "Failed to obtain GemModel");
  266. // we only want to display the gems that must be de-selected to automatically
  267. // disable this dependency, so don't include any that haven't been selected (added)
  268. constexpr bool addedOnly = true;
  269. auto dependents = gemModel->GatherDependentGems(index, addedOnly);
  270. QString nameList = GetGemNameList(dependents);
  271. if (!nameList.isEmpty())
  272. {
  273. QToolTip::showText(event->globalPos(), tr("This gem is a dependency of %1.\nTo disable this gem, first disable %1.").arg(nameList));
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. else if (QToolTip::isVisible())
  280. {
  281. QToolTip::hideText();
  282. event->ignore();
  283. return true;
  284. }
  285. }
  286. return QStyledItemDelegate::helpEvent(event, view, option, index);
  287. }
  288. void GemItemDelegate::CalcRects(const QStyleOptionViewItem& option, QRect& outFullRect, QRect& outItemRect, QRect& outContentRect) const
  289. {
  290. outFullRect = QRect(option.rect);
  291. outItemRect = QRect(outFullRect.adjusted(s_itemMargins.left(), s_itemMargins.top(), -s_itemMargins.right(), -s_itemMargins.bottom()));
  292. outContentRect = QRect(outItemRect.adjusted(s_contentMargins.left(), s_contentMargins.top(), -s_contentMargins.right(), -s_contentMargins.bottom()));
  293. }
  294. QRect GemItemDelegate::GetTextRect(QFont& font, const QString& text, qreal fontSize) const
  295. {
  296. font.setPixelSize(static_cast<int>(fontSize));
  297. return QFontMetrics(font).boundingRect(text);
  298. }
  299. QPair<int, int> GemItemDelegate::CalcColumnXBounds(HeaderOrder header) const
  300. {
  301. if (m_headerWidget)
  302. {
  303. return m_headerWidget->CalcColumnXBounds(static_cast<int>(header));
  304. }
  305. else
  306. {
  307. return QPair<int, int>(0, 0);
  308. }
  309. }
  310. QRect GemItemDelegate::CalcButtonRect(const QRect& contentRect) const
  311. {
  312. const QPoint topLeft = QPoint(
  313. s_itemMargins.left() + CalcColumnXBounds(HeaderOrder::Status).first + AdjustableHeaderWidget::s_headerTextIndent + s_statusIconSize +
  314. s_statusButtonSpacing,
  315. contentRect.center().y() - s_buttonHeight / 2);
  316. const QSize size = QSize(s_buttonWidth, s_buttonHeight);
  317. return QRect(topLeft, size);
  318. }
  319. void GemItemDelegate::DrawPlatformIcons(QPainter* painter, const QRect& contentRect, const QModelIndex& modelIndex) const
  320. {
  321. const GemInfo::Platforms platforms = GemModel::GetGemInfo(modelIndex).m_platforms;
  322. int startX = s_itemMargins.left() + CalcColumnXBounds(HeaderOrder::Name).first + AdjustableHeaderWidget::s_headerTextIndent;
  323. // Iterate and draw the platforms in the order they are defined in the enum.
  324. for (int i = 0; i < GemInfo::NumPlatforms; ++i)
  325. {
  326. // Check if the platform is supported by the given gem.
  327. const GemInfo::Platform platform = static_cast<GemInfo::Platform>(1 << i);
  328. if (platforms & platform)
  329. {
  330. // Get the icon for the platform and draw it.
  331. const auto iterator = m_platformIcons.find(platform);
  332. if (iterator != m_platformIcons.end())
  333. {
  334. const QPixmap& pixmap = iterator.value();
  335. painter->drawPixmap(contentRect.left() + startX, contentRect.bottom() - s_platformIconSize, pixmap);
  336. qreal aspectRatio = static_cast<qreal>(pixmap.width()) / pixmap.height();
  337. startX += static_cast<int>(s_platformIconSize * aspectRatio + s_platformIconSize / 2.5);
  338. }
  339. }
  340. }
  341. }
  342. void GemItemDelegate::DrawPlatformText(QPainter* painter, const QRect& contentRect, const QFont& standardFont, const QModelIndex& modelIndex) const
  343. {
  344. const GemInfo::Platforms platforms = GemModel::GetGemInfo(modelIndex).m_platforms;
  345. auto xbounds = CalcColumnXBounds(HeaderOrder::Name);
  346. const int startX = s_platformTextleftMarginCorrection + xbounds.first;
  347. QFont platformFont(standardFont);
  348. platformFont.setPixelSize(s_featureTagFontSize);
  349. platformFont.setBold(false);
  350. painter->setFont(platformFont);
  351. QStringList platformList;
  352. //If no platforms are specified, there is nothing to draw
  353. if(platforms == 0)
  354. {
  355. return;
  356. }
  357. //UX prefers that we show platforms in reverse alphabetical order
  358. for(int i = GemInfo::NumPlatforms-1; i >= 0; i--)
  359. {
  360. const GemInfo::Platform platform = static_cast<GemInfo::Platform>(1 << i);
  361. if (platforms & platform)
  362. {
  363. platformList.append(GemInfo::GetPlatformString(platform));
  364. }
  365. }
  366. //figure out the ideal rect size for the platform text space constraints
  367. const QRect platformRect = QRect(contentRect.left() + startX, contentRect.bottom() - s_platformTextHeightAdjustment,
  368. xbounds.second -xbounds.first - s_platformTextWrapAroundMargin,
  369. (s_featureTagFontSize + s_platformTextLineBottomMargin) * s_platformTextWrapAroundLineMaxCount);
  370. DrawText(platformList.join(", "), painter, platformRect, platformFont);
  371. }
  372. void GemItemDelegate::DrawFeatureTags(
  373. QPainter* painter,
  374. const QRect& contentRect,
  375. const QStringList& featureTags,
  376. const QFont& standardFont,
  377. const QRect& summaryRect) const
  378. {
  379. QFont gemFeatureTagFont(standardFont);
  380. gemFeatureTagFont.setPixelSize(s_featureTagFontSize);
  381. gemFeatureTagFont.setBold(false);
  382. painter->setFont(gemFeatureTagFont);
  383. int x = CalcColumnXBounds(HeaderOrder::Summary).first + AdjustableHeaderWidget::s_headerTextIndent;
  384. for (const QString& featureTag : featureTags)
  385. {
  386. QRect featureTagRect = GetTextRect(gemFeatureTagFont, featureTag, s_featureTagFontSize);
  387. featureTagRect.moveTo(s_itemMargins.left() + x + s_featureTagBorderMarginX,
  388. contentRect.top() + 47);
  389. featureTagRect = painter->boundingRect(featureTagRect, Qt::TextSingleLine, featureTag);
  390. QRect backgroundRect = featureTagRect;
  391. backgroundRect = backgroundRect.adjusted(/*left=*/-s_featureTagBorderMarginX,
  392. /*top=*/-s_featureTagBorderMarginY,
  393. /*right=*/s_featureTagBorderMarginX,
  394. /*bottom=*/s_featureTagBorderMarginY);
  395. // Skip drawing all following feature tags as there is no more space available.
  396. if (backgroundRect.right() > summaryRect.right())
  397. {
  398. break;
  399. }
  400. // Draw border.
  401. painter->setPen(m_textColor);
  402. painter->setBrush(Qt::NoBrush);
  403. painter->drawRect(backgroundRect);
  404. // Draw text within the border.
  405. painter->setPen(m_textColor);
  406. painter->drawText(featureTagRect, Qt::TextSingleLine, featureTag);
  407. x += backgroundRect.width() + s_featureTagSpacing;
  408. }
  409. }
  410. AZStd::unique_ptr<QTextDocument> GetTextDocument(const QString& text, int width)
  411. {
  412. // using unique_ptr as a workaround for QTextDocument having a private copy constructor
  413. auto doc = AZStd::make_unique<QTextDocument>();
  414. QTextOption textOption(doc->defaultTextOption());
  415. textOption.setWrapMode(QTextOption::WordWrap);
  416. doc->setDefaultTextOption(textOption);
  417. doc->setHtml(text);
  418. doc->setTextWidth(width);
  419. return doc;
  420. }
  421. void GemItemDelegate::DrawText(const QString& text, QPainter* painter, const QRect& rect, const QFont& standardFont) const
  422. {
  423. painter->save();
  424. if (text.contains('<'))
  425. {
  426. painter->translate(rect.topLeft());
  427. // use QTextDocument because drawText does not support rich text or html
  428. QAbstractTextDocumentLayout::PaintContext paintContext;
  429. paintContext.clip = QRect(0, 0, rect.width(), rect.height());
  430. paintContext.palette.setColor(QPalette::Text, painter->pen().color());
  431. AZStd::unique_ptr<QTextDocument> textDocument = GetTextDocument(text, rect.width());
  432. textDocument->documentLayout()->draw(painter, paintContext);
  433. }
  434. else
  435. {
  436. painter->setFont(standardFont);
  437. painter->setPen(m_textColor);
  438. painter->drawText(rect, Qt::AlignLeft | Qt::TextWordWrap, text);
  439. }
  440. painter->restore();
  441. }
  442. void GemItemDelegate::DrawButton(QPainter* painter, const QRect& buttonRect, const QModelIndex& modelIndex) const
  443. {
  444. painter->save();
  445. QPoint circleCenter;
  446. if (GemModel::IsAdded(modelIndex))
  447. {
  448. painter->setBrush(m_buttonEnabledColor);
  449. painter->setPen(m_buttonEnabledColor);
  450. circleCenter = buttonRect.center() + QPoint(buttonRect.width() / 2 - s_buttonBorderRadius + 1, 1);
  451. }
  452. else if (GemModel::IsAddedDependency(modelIndex))
  453. {
  454. painter->setBrush(m_buttonImplicitlyEnabledColor);
  455. painter->setPen(m_buttonImplicitlyEnabledColor);
  456. circleCenter = buttonRect.center() + QPoint(buttonRect.width() / 2 - s_buttonBorderRadius + 1, 1);
  457. }
  458. else
  459. {
  460. circleCenter = buttonRect.center() + QPoint(-buttonRect.width() / 2 + s_buttonBorderRadius + 1, 1);
  461. }
  462. // Rounded rect
  463. painter->drawRoundedRect(buttonRect, s_buttonBorderRadius, s_buttonBorderRadius);
  464. // Circle
  465. painter->setBrush(m_textColor);
  466. painter->drawEllipse(circleCenter, s_buttonCircleRadius, s_buttonCircleRadius);
  467. painter->restore();
  468. }
  469. QString GemItemDelegate::anchorAt(const QString& html, const QPoint& position, const QRect& rect)
  470. {
  471. if (!html.isEmpty())
  472. {
  473. AZStd::unique_ptr<QTextDocument> doc = GetTextDocument(html, rect.width());
  474. QAbstractTextDocumentLayout* layout = doc->documentLayout();
  475. if (layout)
  476. {
  477. return layout->anchorAt(position - rect.topLeft());
  478. }
  479. }
  480. return QString();
  481. }
  482. void GemItemDelegate::DrawDownloadStatusIcon(QPainter* painter, const QRect& contentRect, const QRect& buttonRect, const QModelIndex& modelIndex) const
  483. {
  484. if(GemModel::GetGemInfo(modelIndex).m_gemOrigin != GemInfo::Remote)
  485. {
  486. return;
  487. }
  488. const GemInfo::DownloadStatus downloadStatus = GemModel::GetDownloadStatus(modelIndex);
  489. QPixmap currentFrame;
  490. const QPixmap* statusPixmap;
  491. if (downloadStatus == GemInfo::DownloadStatus::Downloaded)
  492. {
  493. statusPixmap = &m_downloadedPixmap;
  494. }
  495. else if (downloadStatus == GemInfo::DownloadStatus::Downloading)
  496. {
  497. if (m_downloadingMovie->state() != QMovie::Running)
  498. {
  499. m_downloadingMovie->start();
  500. emit MovieStartedPlaying(m_downloadingMovie);
  501. }
  502. currentFrame = m_downloadingMovie->currentPixmap();
  503. currentFrame = currentFrame.scaled(s_statusIconSize, s_statusIconSize);
  504. statusPixmap = &currentFrame;
  505. }
  506. else if (downloadStatus == GemInfo::DownloadStatus::DownloadSuccessful)
  507. {
  508. statusPixmap = &m_downloadSuccessfulPixmap;
  509. }
  510. else if (downloadStatus == GemInfo::DownloadStatus::DownloadFailed)
  511. {
  512. statusPixmap = &m_downloadFailedPixmap;
  513. }
  514. else if (downloadStatus == GemInfo::DownloadStatus::NotDownloaded)
  515. {
  516. statusPixmap = &m_notDownloadedPixmap;
  517. }
  518. else
  519. {
  520. statusPixmap = &m_unknownStatusPixmap;
  521. }
  522. QSize statusSize = statusPixmap->size();
  523. if (m_readOnly)
  524. {
  525. // for now, we don't draw the status button in read only state
  526. // so draw the status icon centered
  527. painter->drawPixmap(
  528. buttonRect.center().x() - statusSize.width() / 2,
  529. contentRect.center().y() - statusSize.height() / 2,
  530. *statusPixmap);
  531. }
  532. else
  533. {
  534. painter->drawPixmap(
  535. buttonRect.left() - s_statusButtonSpacing - statusSize.width(),
  536. contentRect.center().y() - statusSize.height() / 2,
  537. *statusPixmap);
  538. }
  539. }
  540. } // namespace O3DE::ProjectManager