TemplateButtonWidget.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <TemplateButtonWidget.h>
  9. #include <ProjectManagerDefs.h>
  10. #include <QHBoxLayout>
  11. #include <QVBoxLayout>
  12. #include <QLabel>
  13. #include <QPixmap>
  14. #include <QAbstractButton>
  15. #include <QProgressBar>
  16. #include <QSpacerItem>
  17. #include <QStyle>
  18. #include <QVariant>
  19. namespace O3DE::ProjectManager
  20. {
  21. TemplateButton::TemplateButton(const QString& imagePath, const QString& labelText, QWidget* parent)
  22. : QPushButton(parent)
  23. {
  24. setAutoExclusive(true);
  25. setObjectName("templateButton");
  26. QVBoxLayout* vLayout = new QVBoxLayout();
  27. vLayout->setSpacing(0);
  28. vLayout->setContentsMargins(0, 0, 0, 0);
  29. setLayout(vLayout);
  30. QLabel* image = new QLabel(this);
  31. image->setObjectName("templateImage");
  32. image->setPixmap(QPixmap(imagePath).scaled(
  33. QSize(ProjectTemplateImageWidth, ProjectTemplateImageWidth), Qt::KeepAspectRatio, Qt::SmoothTransformation));
  34. vLayout->addWidget(image);
  35. QLabel* label = new QLabel(labelText, this);
  36. label->setObjectName("templateLabel");
  37. label->setWordWrap(true);
  38. vLayout->addWidget(label);
  39. // Create an overlay for remote templates
  40. QGridLayout* overlayLayout = new QGridLayout();
  41. overlayLayout->setAlignment(Qt::AlignTop);
  42. overlayLayout->setSpacing(0);
  43. overlayLayout->setContentsMargins(0, 0, 0, 0);
  44. // Dark overlay to make text clearer
  45. m_darkenOverlay = new QLabel(this);
  46. m_darkenOverlay->setObjectName("labelButtonOverlay");
  47. m_darkenOverlay->setFixedSize(ProjectTemplateImageWidth, ProjectTemplateImageWidth);
  48. m_darkenOverlay->setVisible(false);
  49. overlayLayout->addWidget(m_darkenOverlay,0,0);
  50. QVBoxLayout* contentsLayout = new QVBoxLayout();
  51. contentsLayout->setSpacing(0);
  52. contentsLayout->setContentsMargins(0, 0, 0, 0);
  53. contentsLayout->setAlignment(Qt::AlignTop);
  54. overlayLayout->addLayout(contentsLayout, 0, 0);
  55. // Top status icons
  56. QHBoxLayout* statusIconsLayout = new QHBoxLayout();
  57. statusIconsLayout->setSpacing(0);
  58. statusIconsLayout->setContentsMargins(0, 0, 0, 0);
  59. statusIconsLayout->addStretch();
  60. m_cloudIcon = new QLabel(this);
  61. m_cloudIcon->setObjectName("projectCloudIconOverlay");
  62. m_cloudIcon->setPixmap(QIcon(":/Download.svg").pixmap(24, 24));
  63. m_cloudIcon->setVisible(false);
  64. statusIconsLayout->addWidget(m_cloudIcon);
  65. statusIconsLayout->addSpacing(5);
  66. image->setLayout(overlayLayout);
  67. QWidget* statusIconArea = new QWidget();
  68. statusIconArea->setFixedSize(ProjectTemplateImageWidth, 24);
  69. statusIconArea->setLayout(statusIconsLayout);
  70. contentsLayout->addWidget(statusIconArea);
  71. QWidget* templateCenter = new QWidget();
  72. templateCenter->setFixedSize(ProjectTemplateImageWidth, 35);
  73. // Center area for download information
  74. QVBoxLayout* centerBlock = new QVBoxLayout();
  75. templateCenter->setLayout(centerBlock);
  76. QHBoxLayout* downloadProgressTextBlock = new QHBoxLayout();
  77. m_progressMessageLabel = new QLabel(tr("0%"), this);
  78. m_progressMessageLabel->setAlignment(Qt::AlignRight);
  79. m_progressMessageLabel->setVisible(false);
  80. downloadProgressTextBlock->addWidget(m_progressMessageLabel);
  81. centerBlock->addLayout(downloadProgressTextBlock);
  82. QHBoxLayout* downloadProgressBlock = new QHBoxLayout();
  83. m_progessBar = new QProgressBar(this);
  84. m_progessBar->setValue(100);
  85. m_progessBar->setVisible(false);
  86. downloadProgressBlock->addSpacing(10);
  87. downloadProgressBlock->addWidget(m_progessBar);
  88. downloadProgressBlock->addSpacing(10);
  89. centerBlock->addLayout(downloadProgressBlock);
  90. contentsLayout->addWidget(templateCenter);
  91. QSpacerItem* spacer =
  92. new QSpacerItem(ProjectTemplateImageWidth, ProjectTemplateImageWidth - 35 - 24, QSizePolicy::Fixed, QSizePolicy::Fixed);
  93. contentsLayout->addSpacerItem(spacer);
  94. connect(this, &QAbstractButton::toggled, this, &TemplateButton::onToggled);
  95. }
  96. void TemplateButton::SetIsRemote(bool isRemote)
  97. {
  98. if (!isRemote)
  99. {
  100. ShowDownloadProgress(false);
  101. }
  102. m_cloudIcon->setVisible(isRemote);
  103. }
  104. void TemplateButton::ShowDownloadProgress(bool showProgress)
  105. {
  106. m_progessBar->setVisible(showProgress);
  107. m_progressMessageLabel->setVisible(showProgress);
  108. m_darkenOverlay->setVisible(showProgress);
  109. }
  110. void TemplateButton::SetProgressPercentage(float percentage)
  111. {
  112. m_progessBar->setValue(static_cast<int>(percentage));
  113. m_progressMessageLabel->setText(QString("%1%").arg(static_cast<int>(percentage)));
  114. }
  115. void TemplateButton::onToggled()
  116. {
  117. setProperty("Checked", isChecked());
  118. // we must unpolish/polish every child after changing a property
  119. // or else they won't use the correct stylesheet selector
  120. for (auto child : findChildren<QWidget*>())
  121. {
  122. child->style()->unpolish(child);
  123. child->style()->polish(child);
  124. }
  125. style()->unpolish(this);
  126. style()->polish(this);
  127. }
  128. } // namespace O3DE::ProjectManager