ThumbnailImageItem.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // Qt
  9. #include <QPainter>
  10. #include <QWidget>
  11. // GraphModel
  12. #include <GraphModel/Integration/ThumbnailImageItem.h>
  13. namespace GraphModelIntegration
  14. {
  15. static const QSize IMAGE_MARGIN = QSize(10, 10);
  16. ThumbnailImageItem::ThumbnailImageItem(const QPixmap& image, QGraphicsItem* parent)
  17. : ThumbnailItem(parent)
  18. , m_pixmap(image)
  19. {
  20. }
  21. void ThumbnailImageItem::UpdateImage(const QPixmap& image)
  22. {
  23. m_pixmap = image;
  24. // Schedule a new paint request since we changed the pixmap
  25. update();
  26. }
  27. QSizeF ThumbnailImageItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
  28. {
  29. switch (which)
  30. {
  31. case Qt::MinimumSize:
  32. case Qt::PreferredSize:
  33. return m_pixmap.size() + IMAGE_MARGIN;
  34. case Qt::MaximumSize:
  35. return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
  36. default:
  37. break;
  38. }
  39. return constraint;
  40. }
  41. void ThumbnailImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  42. {
  43. (void)option;
  44. (void)widget;
  45. // Draw the pixmap centered in our given frame
  46. QRectF frame(QPointF(0, 0), geometry().size());
  47. QPointF topLeft = frame.center() - (QPointF(m_pixmap.width(), m_pixmap.height()) / 2);
  48. painter->drawPixmap(topLeft, m_pixmap);
  49. }
  50. }