weeklycodewidget.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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::updateCode()
  64. {
  65. qDebug() << "New code: " << code_data;
  66. if (code_data.isEmpty())
  67. return;
  68. ui->code->setText(code_data);
  69. repaint();
  70. code_data = "";
  71. }
  72. void WeeklyCodeWidget::leaveEvent(QEvent * event)
  73. {
  74. // qDebug() << Q_FUNC_INFO << this->objectName();
  75. emit showNoTooltip();
  76. }