uniticon.cpp 2.5 KB

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