weeklycodewidget.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "constants.h"
  10. #include "models/settings.h"
  11. WeeklyCodeWidget::WeeklyCodeWidget(QWidget *parent) :
  12. QWidget(parent), ui(new Ui::WeeklyCodeWidget)
  13. {
  14. setAttribute(Qt::WA_Hover);
  15. setMouseTracking(true);
  16. ui->setupUi(this);
  17. ui->code->setText("загрузка...");
  18. code_data = "";
  19. code_downloader.targetBytearray = &code_data;
  20. code_downloader.setUrl(Settings::getValue("Network/weekly_code_url").toUrl());
  21. connect(&code_update_timer, &QTimer::timeout, &code_downloader, &Downloader::start);
  22. connect(&code_downloader, &Downloader::downloadFinished, this, &WeeklyCodeWidget::updateCode, Qt::QueuedConnection);
  23. code_downloader.start();
  24. code_update_timer.setInterval(1000 * 60); // 1 minute;
  25. code_update_timer.start();
  26. }
  27. WeeklyCodeWidget::~WeeklyCodeWidget()
  28. {
  29. delete ui;
  30. }
  31. void WeeklyCodeWidget::updateFontsSizes()
  32. {
  33. ui->title->setFont(trajan_9pt);
  34. ui->code->setFont(trajan_10pt);
  35. }
  36. void WeeklyCodeWidget::resizeEvent(QResizeEvent *event)
  37. {
  38. updateFontsSizes();
  39. }
  40. void WeeklyCodeWidget::enterEvent(QEvent * event)
  41. {
  42. QWidget::enterEvent(event);
  43. event->ignore();
  44. if (QApplication::clipboard()->text() == ui->code->text())
  45. emit showCompletedTooltip();
  46. else
  47. emit showHelpTooltip();
  48. }
  49. void WeeklyCodeWidget::mousePressEvent(QMouseEvent *ev)
  50. {
  51. ui->code->setStyleSheet("color: rgb(255, 150, 0);");
  52. ev->ignore();
  53. }
  54. void WeeklyCodeWidget::mouseReleaseEvent(QMouseEvent *ev)
  55. {
  56. ui->code->setStyleSheet("color: rgb(255, 180, 0);");
  57. if (ui->code->text() != "загрузка...") {
  58. QApplication::clipboard()->setText(ui->code->text());
  59. emit showCompletedTooltip();
  60. }
  61. ev->ignore();
  62. }
  63. void WeeklyCodeWidget::updateWeeklyCodeWidget()
  64. {
  65. code_downloader.start();
  66. }
  67. void WeeklyCodeWidget::updateCode()
  68. {
  69. qDebug() << "New code: " << code_data;
  70. if (code_data.isEmpty())
  71. return;
  72. ui->code->setText(code_data);
  73. repaint();
  74. code_data = "";
  75. }
  76. void WeeklyCodeWidget::leaveEvent(QEvent * event)
  77. {
  78. // qDebug() << Q_FUNC_INFO << this->objectName();
  79. emit showNoTooltip();
  80. }