weeklycodewidget.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "widgets/weeklycodewidget.h"
  2. #include "ui_weeklycodewidget.h"
  3. #include "weeklycodewidget.h"
  4. #include <QApplication>
  5. #include <QtConcurrent/QtConcurrent>
  6. #include <QPainter>
  7. #include <QPaintEvent>
  8. #include <QClipboard>
  9. WeeklyCodeWidget::WeeklyCodeWidget(QWidget *parent) :
  10. QWidget(parent), ui(new Ui::WeeklyCodeWidget)
  11. {
  12. setAttribute(Qt::WA_Hover);
  13. setMouseTracking(true);
  14. ui->setupUi(this);
  15. ui->code->setText("загрузка...");
  16. code_data = "";
  17. code_downloader.targetBytearray = &code_data;
  18. code_downloader.setUrl(QUrl("http://translate.lotros.ru/coupon.txt"));
  19. connect(&code_update_timer, &QTimer::timeout, &code_downloader, &Downloader::start);
  20. connect(&code_downloader, &Downloader::downloadFinished, this, &WeeklyCodeWidget::updateCode, Qt::QueuedConnection);
  21. emit code_downloader.start();
  22. code_update_timer.setInterval(1000 * 60); // 1 minute;
  23. code_update_timer.start();
  24. }
  25. WeeklyCodeWidget::~WeeklyCodeWidget()
  26. {
  27. delete ui;
  28. }
  29. void WeeklyCodeWidget::enterEvent(QEvent * event)
  30. {
  31. QWidget::enterEvent(event);
  32. event->ignore();
  33. if (QApplication::clipboard()->text() == ui->code->text())
  34. emit showCompletedTooltip();
  35. else
  36. emit showHelpTooltip();
  37. }
  38. void WeeklyCodeWidget::mousePressEvent(QMouseEvent *ev)
  39. {
  40. ui->code->setStyleSheet("color: rgb(255, 150, 0);");
  41. ev->ignore();
  42. }
  43. void WeeklyCodeWidget::mouseReleaseEvent(QMouseEvent *ev)
  44. {
  45. ui->code->setStyleSheet("color: rgb(255, 180, 0);");
  46. if (ui->code->text() != "загрузка...") {
  47. QApplication::clipboard()->setText(ui->code->text());
  48. emit showCompletedTooltip();
  49. }
  50. ev->ignore();
  51. }
  52. void WeeklyCodeWidget::updateCode()
  53. {
  54. qDebug() << "New code: " << code_data;
  55. if (code_data.isEmpty())
  56. return;
  57. ui->code->setText(code_data);
  58. repaint();
  59. code_data = "";
  60. }
  61. void WeeklyCodeWidget::leaveEvent(QEvent * event)
  62. {
  63. // qDebug() << Q_FUNC_INFO << this->objectName();
  64. emit showNoTooltip();
  65. }