raceicon.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <gui/raceicon.h>.h>
  2. #include <QDebug>
  3. #include <QTime>
  4. RaceIcon::RaceIcon(QWidget* parent, int width, int height)
  5. : QLabel(parent)
  6. {
  7. state_ = 0;
  8. width_ = width;
  9. height_ = height;
  10. setAttribute(Qt::WA_Hover, true);
  11. installEventFilter(this);
  12. default_border_.load(":/assets/common/unit_icon_default.png");
  13. hover_border_.load(":/assets/common/unit_icon_hover.png");
  14. active_border_.load(":/assets/common/unit_icon_active.png");
  15. drawIcon();
  16. }
  17. RaceIcon::~RaceIcon()
  18. {}
  19. void RaceIcon::drawIcon() {
  20. QPixmap combined(width_, height_);
  21. QPainter p(&combined);
  22. if (state_ == 0) {
  23. p.setBrush(Qt::NoBrush);
  24. p.setPen(Qt::black);
  25. p.drawRect(0, 0, width_, height_);
  26. } else {
  27. p.drawImage(QPoint(0, 0), icon_.scaled(width_, height_, Qt::KeepAspectRatio));
  28. }
  29. if (state_ == 0 || state_ == 1)
  30. p.drawImage(QPoint(0, 0), default_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  31. if (state_ == 2)
  32. p.drawImage(QPoint(0, 0), hover_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  33. if (state_ == 3)
  34. p.drawImage(QPoint(0, 0), active_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  35. p.end();
  36. setPixmap(combined);
  37. }
  38. void RaceIcon::setRaceIcon(QString racename) {
  39. icon_.load(":/assets/units/" + racename + "/icon.png");
  40. if (state_ == 0)
  41. state_ = 1;
  42. drawIcon();
  43. }
  44. void RaceIcon::setRaceIcon(QImage icon) {
  45. icon_ = icon;
  46. if (state_ == 0)
  47. state_ = 1;
  48. drawIcon();
  49. }
  50. void RaceIcon::unsetRaceIcon() {
  51. state_ = 0;
  52. drawIcon();
  53. }
  54. void RaceIcon::resize(int w, int h) {
  55. width_ = w;
  56. height_ = h;
  57. drawIcon();
  58. }
  59. void RaceIcon::deactivate() {
  60. if (state_ > 1)
  61. state_ = 1;
  62. drawIcon();
  63. }
  64. void RaceIcon::activate() {
  65. state_ = 3;
  66. drawIcon();
  67. }
  68. void RaceIcon::mousePressEvent(QMouseEvent*)
  69. {
  70. if (state_ == 0)
  71. return;
  72. activate();
  73. QTime current_time = QTime::currentTime();
  74. if (current_time.msecsSinceStartOfDay() - previous_click_time_.msecsSinceStartOfDay() < 200) {
  75. deactivate();
  76. emit doubleclicked();
  77. } else {
  78. previous_click_time_ = current_time;
  79. emit clicked();
  80. }
  81. }
  82. void RaceIcon::enterEvent(QEvent*)
  83. {
  84. if (state_ == 0 || state_ == 3)
  85. return;
  86. state_ = 2;
  87. drawIcon();
  88. emit hovered();
  89. }
  90. void RaceIcon::leaveEvent(QEvent*)
  91. {
  92. if (state_ == 0 || state_ == 3)
  93. return;
  94. state_ = 1;
  95. drawIcon();
  96. emit unhovered();
  97. }