weeklycodewidget.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "weeklycodewidget.h"
  2. #include <QApplication>
  3. #include <QtConcurrent/QtConcurrent>
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. #include <QClipboard>
  7. WeeklyCodeWidget::WeeklyCodeWidget(QWidget *parent) : QLabel(parent)
  8. {
  9. setAttribute(Qt::WA_Hover);
  10. setMouseTracking(true);
  11. current_bg = QPixmap(":/buttons/lotr_circle.png").scaled(130, 130);
  12. changeImageSrc(":/buttons/lotr_circle.png");
  13. setText("ЗАГРУЗКА");
  14. code_data = "";
  15. code_downloader.targetBytearray = &code_data;
  16. code_downloader.setUrl(QUrl("http://translate.lotros.ru/coupon.txt"));
  17. connect(&code_update_timer, &QTimer::timeout, &code_downloader, &Downloader::start);
  18. connect(&code_downloader, &Downloader::downloadFinished, this, &WeeklyCodeWidget::updateCode, Qt::QueuedConnection);
  19. emit code_downloader.start();
  20. code_update_timer.setInterval(1000 * 60); // 1 minute;
  21. code_update_timer.start();
  22. }
  23. void WeeklyCodeWidget::changeImageSrc(const QString &src)
  24. {
  25. if (!qApp)
  26. return;
  27. img_src = src;
  28. QPixmap *cur_bg = new QPixmap(current_bg.scaled(130, 130));
  29. QPixmap *new_bg= new QPixmap(QPixmap(src).scaled(130, 130));
  30. QtConcurrent::run([cur_bg, new_bg, src, this](){
  31. const int iterations_num = 50;
  32. const int iteration_sleep = 40;
  33. for (int i = 0; i < iterations_num && qApp; i++) {
  34. if (img_src != src)
  35. break;
  36. QPainter painter;
  37. painter.begin(cur_bg);
  38. painter.setOpacity(double(10 + i) / double(iterations_num));
  39. painter.setCompositionMode(QPainter::CompositionMode_Source);
  40. painter.drawPixmap(0,0, *new_bg);
  41. painter.end();
  42. bg_lock.lock();
  43. current_bg = *cur_bg;
  44. bg_lock.unlock();
  45. QMetaObject::invokeMethod(this, "repaint", Qt::QueuedConnection);
  46. QThread::msleep(iteration_sleep);
  47. }
  48. delete cur_bg;
  49. delete new_bg;
  50. });
  51. }
  52. void WeeklyCodeWidget::enterEvent(QEvent * event)
  53. {
  54. changeImageSrc(":/buttons/lotr_circle_hover.png");
  55. QWidget::enterEvent(event);
  56. }
  57. void WeeklyCodeWidget::paintEvent(QPaintEvent *event)
  58. {
  59. QPainter painter;
  60. painter.begin(this);
  61. painter.drawPixmap(0,0, current_bg);
  62. painter.end();
  63. QLabel::paintEvent(event);
  64. }
  65. void WeeklyCodeWidget::mousePressEvent(QMouseEvent *ev)
  66. {
  67. setStyleSheet("color: rgb(255, 150, 0);");
  68. }
  69. void WeeklyCodeWidget::mouseReleaseEvent(QMouseEvent *ev)
  70. {
  71. setStyleSheet("color: rgb(255, 180, 0);");
  72. QApplication::clipboard()->setText(text());
  73. }
  74. void WeeklyCodeWidget::updateCode()
  75. {
  76. qDebug() << "New code: " << code_data;
  77. setText(code_data);
  78. repaint();
  79. code_data = "";
  80. }
  81. void WeeklyCodeWidget::leaveEvent(QEvent * event)
  82. {
  83. qDebug() << Q_FUNC_INFO << this->objectName();
  84. changeImageSrc(":/buttons/lotr_circle.png");
  85. QWidget::leaveEvent(event);
  86. }