scene_item_image.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "editor/scene/scene_item_image.h"
  8. namespace Editor {
  9. ItemImage::ItemImage(
  10. const QPixmap &&pixmap,
  11. ItemBase::Data data)
  12. : ItemBase(std::move(data))
  13. , _pixmap(std::move(pixmap)) {
  14. setAspectRatio(_pixmap.isNull()
  15. ? 1.0
  16. : (_pixmap.height() / float64(_pixmap.width())));
  17. }
  18. void ItemImage::paint(
  19. QPainter *p,
  20. const QStyleOptionGraphicsItem *option,
  21. QWidget *w) {
  22. const auto rect = contentRect();
  23. const auto pixmapSize = QSizeF(_pixmap.size() / style::DevicePixelRatio())
  24. .scaled(rect.size(), Qt::KeepAspectRatio);
  25. const auto resultRect = QRectF(rect.topLeft(), pixmapSize).translated(
  26. (rect.width() - pixmapSize.width()) / 2.,
  27. (rect.height() - pixmapSize.height()) / 2.);
  28. p->drawPixmap(resultRect.toRect(), _pixmap);
  29. ItemBase::paint(p, option, w);
  30. }
  31. void ItemImage::performFlip() {
  32. _pixmap = _pixmap.transformed(QTransform().scale(-1, 1));
  33. update();
  34. }
  35. std::shared_ptr<ItemBase> ItemImage::duplicate(ItemBase::Data data) const {
  36. auto pixmap = _pixmap;
  37. return std::make_shared<ItemImage>(std::move(pixmap), std::move(data));
  38. }
  39. } // namespace Editor