raceicon.cpp 2.7 KB

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