GoToButtonDelegate.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "GoToButtonDelegate.h"
  9. #include <qpainter.h>
  10. #include <qevent.h>
  11. namespace AssetProcessor
  12. {
  13. GoToButtonDelegate::GoToButtonDelegate(QObject* parent)
  14. : QStyledItemDelegate(parent)
  15. , m_icon(":/AssetProcessor_goto.svg")
  16. , m_hoverIcon(":/AssetProcessor_goto_hover.svg")
  17. {
  18. }
  19. void GoToButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
  20. {
  21. QStyledItemDelegate::paint(painter, option, index);
  22. if (index.data().canConvert<GoToButtonData>())
  23. {
  24. constexpr int MarginPX = 3;
  25. auto marginRect = option.rect.marginsRemoved(QMargins(MarginPX, MarginPX, MarginPX, MarginPX));
  26. if (option.state & QStyle::State_MouseOver)
  27. {
  28. m_hoverIcon.paint(painter, marginRect);
  29. }
  30. else
  31. {
  32. m_icon.paint(painter, marginRect);
  33. }
  34. }
  35. }
  36. bool GoToButtonDelegate::editorEvent(
  37. QEvent* event, QAbstractItemModel* /*model*/, const QStyleOptionViewItem& /*option*/, const QModelIndex& index)
  38. {
  39. if (index.data().canConvert<GoToButtonData>())
  40. {
  41. if (event->type() == QEvent::MouseButtonPress)
  42. {
  43. Q_EMIT Clicked(index.data().value<GoToButtonData>());
  44. }
  45. }
  46. return false;
  47. }
  48. }