PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/profile-widget/divepixmapitem.cpp

https://github.com/dirkhh/subsurface
C++ | 107 lines | 88 code | 16 blank | 3 comment | 2 complexity | 308520f5460e7e93f719e75000d97455 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "profile-widget/divepixmapitem.h"
  3. #include "profile-widget/animationfunctions.h"
  4. #include "core/pref.h"
  5. #include "core/qthelper.h"
  6. #include "core/settings/qPrefDisplay.h"
  7. #include "core/subsurface-qt/divelistnotifier.h"
  8. #include <QDesktopServices>
  9. #include <QPen>
  10. #include <QUrl>
  11. #include <QGraphicsSceneMouseEvent>
  12. DivePixmapItem::DivePixmapItem(QGraphicsItem *parent) : QGraphicsPixmapItem(parent)
  13. {
  14. }
  15. CloseButtonItem::CloseButtonItem(QGraphicsItem *parent): DivePixmapItem(parent)
  16. {
  17. static QPixmap p = QPixmap(":list-remove-icon");
  18. setPixmap(p);
  19. setFlag(ItemIgnoresTransformations);
  20. }
  21. void CloseButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *)
  22. {
  23. emit clicked();
  24. }
  25. DivePictureItem::DivePictureItem(QGraphicsItem *parent): DivePixmapItem(parent),
  26. canvas(new QGraphicsRectItem(this)),
  27. shadow(new QGraphicsRectItem(this)),
  28. button(new CloseButtonItem(this)),
  29. baseZValue(0.0)
  30. {
  31. setFlag(ItemIgnoresTransformations);
  32. setAcceptHoverEvents(true);
  33. setScale(0.2);
  34. connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &DivePictureItem::settingsChanged);
  35. connect(button, &CloseButtonItem::clicked, [this] () { emit removePicture(fileUrl); });
  36. canvas->setPen(Qt::NoPen);
  37. canvas->setBrush(QColor(Qt::white));
  38. canvas->setFlag(ItemStacksBehindParent);
  39. canvas->setZValue(-1);
  40. shadow->setPos(5,5);
  41. shadow->setPen(Qt::NoPen);
  42. shadow->setBrush(QColor(Qt::lightGray));
  43. shadow->setFlag(ItemStacksBehindParent);
  44. shadow->setZValue(-2);
  45. button->setScale(0.2);
  46. button->setZValue(7);
  47. button->hide();
  48. }
  49. // The base z-value is used for correct paint-order of the thumbnails. On hoverEnter the z-value is raised
  50. // so that the thumbnail is drawn on top of all other thumbnails and on hoverExit it is restored to the base value.
  51. void DivePictureItem::setBaseZValue(double z)
  52. {
  53. baseZValue = z;
  54. setZValue(z);
  55. }
  56. void DivePictureItem::settingsChanged()
  57. {
  58. setVisible(prefs.show_pictures_in_profile);
  59. }
  60. void DivePictureItem::setPixmap(const QPixmap &pix)
  61. {
  62. DivePixmapItem::setPixmap(pix);
  63. QRectF r = boundingRect();
  64. canvas->setRect(0 - 10, 0 -10, r.width() + 20, r.height() + 20);
  65. shadow->setRect(canvas->rect());
  66. button->setPos(boundingRect().width() - button->boundingRect().width() * 0.2,
  67. boundingRect().height() - button->boundingRect().height() * 0.2);
  68. }
  69. void DivePictureItem::hoverEnterEvent(QGraphicsSceneHoverEvent*)
  70. {
  71. Animations::scaleTo(this, qPrefDisplay::animation_speed(), 1.0);
  72. setZValue(baseZValue + 5.0);
  73. button->setOpacity(0);
  74. button->show();
  75. Animations::show(button, qPrefDisplay::animation_speed());
  76. }
  77. void DivePictureItem::setFileUrl(const QString &s)
  78. {
  79. fileUrl = s;
  80. }
  81. void DivePictureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent*)
  82. {
  83. Animations::scaleTo(this, qPrefDisplay::animation_speed(), 0.2);
  84. setZValue(baseZValue);
  85. Animations::hide(button, qPrefDisplay::animation_speed());
  86. }
  87. void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  88. {
  89. if (event->button() == Qt::LeftButton)
  90. QDesktopServices::openUrl(QUrl::fromLocalFile(localFilePath(fileUrl)));
  91. }