GradientPreviewWidget.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <UI/GradientPreviewWidget.h>
  9. #include <QIcon>
  10. #include <QPainter>
  11. #include <QToolButton>
  12. #include <QVBoxLayout>
  13. namespace GradientSignal
  14. {
  15. GradientPreviewWidget::GradientPreviewWidget(QWidget* parent, bool enablePopout)
  16. : QWidget(parent)
  17. {
  18. setMinimumSize(256, 256);
  19. setAttribute(Qt::WA_OpaquePaintEvent); // We're responsible for painting everything, don't bother erasing before paint
  20. // For the preview with popout icon, configure an icon in the top-right
  21. // corner of our preview that only appears when the user hovers over
  22. // the preview
  23. if (enablePopout)
  24. {
  25. const int IconMargin = 2;
  26. const int IconSize = 24;
  27. QVBoxLayout* layout = new QVBoxLayout(this);
  28. layout->setContentsMargins(QMargins(IconMargin, IconMargin, IconMargin, IconMargin));
  29. layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  30. QIcon icon;
  31. icon.addPixmap(QPixmap(":/Application/popout-overlay.svg"), QIcon::Normal);
  32. icon.addPixmap(QPixmap(":/Application/popout-overlay-hover.svg"), QIcon::Active);
  33. m_popoutButton = new QToolButton(this);
  34. m_popoutButton->setIcon(icon);
  35. m_popoutButton->setAutoRaise(true);
  36. m_popoutButton->setIconSize(QSize(IconSize, IconSize));
  37. m_popoutButton->setCursor(Qt::PointingHandCursor);
  38. m_popoutButton->hide();
  39. layout->addWidget(m_popoutButton);
  40. QObject::connect(m_popoutButton, &QToolButton::clicked, this, &GradientPreviewWidget::popoutClicked);
  41. }
  42. }
  43. GradientPreviewWidget::~GradientPreviewWidget()
  44. {
  45. }
  46. void GradientPreviewWidget::OnUpdate()
  47. {
  48. update();
  49. }
  50. QSize GradientPreviewWidget::GetPreviewSize() const
  51. {
  52. return size();
  53. }
  54. void GradientPreviewWidget::enterEvent(QEvent* event)
  55. {
  56. QWidget::enterEvent(event);
  57. if (m_popoutButton)
  58. {
  59. m_popoutButton->show();
  60. }
  61. }
  62. void GradientPreviewWidget::leaveEvent(QEvent* event)
  63. {
  64. QWidget::leaveEvent(event);
  65. if (m_popoutButton)
  66. {
  67. m_popoutButton->hide();
  68. }
  69. }
  70. void GradientPreviewWidget::paintEvent([[maybe_unused]] QPaintEvent* paintEvent)
  71. {
  72. QPainter painter(this);
  73. if (!m_previewImage.isNull())
  74. {
  75. painter.drawImage(QPoint(0, 0), m_previewImage);
  76. }
  77. }
  78. void GradientPreviewWidget::resizeEvent(QResizeEvent* resizeEvent)
  79. {
  80. QWidget::resizeEvent(resizeEvent);
  81. QueueUpdate();
  82. }
  83. } //namespace GradientSignal
  84. #include "UI/moc_GradientPreviewWidget.cpp"