statuswidget.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "statuswidget.h"
  2. #include "ui_statuswidget.h"
  3. #include "widgets/mainwindow.h"
  4. #include "models/patchdownloader.h"
  5. #include "models/lotrodatmanager.h"
  6. #include "constants.h"
  7. #include <QDesktopServices>
  8. #include <QUrl>
  9. #include <QDebug>
  10. #include <QMessageBox>
  11. StatusWidget::StatusWidget(QSettings* settings, PatchDownloader* patch_downloader, LotroDatManager* lotro_dat_manager, QWidget *parent) :
  12. QWidget(parent), app_settings(settings), patch_updater(patch_downloader), lotro_manager(lotro_dat_manager),
  13. ui(new Ui::StatusWidget)
  14. {
  15. ui->setupUi(this);
  16. last_progress_update_time.start();
  17. connect(patch_updater, &PatchDownloader::checkForUpdatesStarted, this, &StatusWidget::onPatchDownloaderStarted, Qt::QueuedConnection);
  18. connect(patch_updater, &PatchDownloader::progressChanged, this, &StatusWidget::onPatchDownloaderProgressChanged, Qt::QueuedConnection);
  19. connect(patch_updater, &PatchDownloader::checkForUpdatesFinished, this, &StatusWidget::onPatchDownloaderFinished, Qt::QueuedConnection);
  20. connect(patch_updater, &PatchDownloader::changePatchStatus, this, &StatusWidget::updatePatchStatus);
  21. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showCompletedTooltip, this, &StatusWidget::setToolTipToWeeklyCodeComplete);
  22. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showHelpTooltip, this, &StatusWidget::setToolTipToWeeklyCodeHelp);
  23. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showNoTooltip, this, &StatusWidget::resetToolTip);
  24. connect(ui->news_list, &NewsListWidget::showHelpToolTip, this, &StatusWidget::setToolTipToNewsHelp);
  25. connect(ui->news_list, &NewsListWidget::showNoToolTip, this, &StatusWidget::resetToolTip);
  26. tooltip_effects[ui->news_tooltip->objectName()] = new QGraphicsOpacityEffect(ui->news_tooltip);
  27. tooltip_effects[ui->weekly_code_tooltip_1->objectName()] = new QGraphicsOpacityEffect(ui->weekly_code_tooltip_1);
  28. tooltip_effects[ui->weekly_code_tooltip_2->objectName()] = new QGraphicsOpacityEffect(ui->weekly_code_tooltip_2);
  29. tooltip_effects[ui->patches_status->objectName()] = new QGraphicsOpacityEffect(ui->patches_status);
  30. tooltip_animations[ui->news_tooltip->objectName()] = new QPropertyAnimation(tooltip_effects[ui->news_tooltip->objectName()], "opacity");
  31. tooltip_animations[ui->weekly_code_tooltip_1->objectName()] = new QPropertyAnimation(tooltip_effects[ui->weekly_code_tooltip_1->objectName()], "opacity");
  32. tooltip_animations[ui->weekly_code_tooltip_2->objectName()] = new QPropertyAnimation(tooltip_effects[ui->weekly_code_tooltip_2->objectName()], "opacity");
  33. tooltip_animations[ui->patches_status->objectName()] = new QPropertyAnimation(tooltip_effects[ui->patches_status->objectName()], "opacity");
  34. ui->news_tooltip->setGraphicsEffect(tooltip_effects[ui->news_tooltip->objectName()]);
  35. ui->weekly_code_tooltip_1->setGraphicsEffect(tooltip_effects[ui->weekly_code_tooltip_1->objectName()]);
  36. ui->weekly_code_tooltip_2->setGraphicsEffect(tooltip_effects[ui->weekly_code_tooltip_2->objectName()]);
  37. ui->patches_status->setGraphicsEffect(tooltip_effects[ui->patches_status->objectName()]);
  38. foreach (QPropertyAnimation* animation, tooltip_animations) {
  39. animation->setDuration(250);
  40. animation->setStartValue(0);
  41. animation->setEndValue(1);
  42. animation->setDirection(QAbstractAnimation::Forward);
  43. }
  44. foreach (QGraphicsOpacityEffect* effect, tooltip_effects) {
  45. effect->setOpacity(0);
  46. }
  47. active_tooltip = nullptr;
  48. resetToolTip();
  49. }
  50. StatusWidget::~StatusWidget()
  51. {
  52. delete ui;
  53. }
  54. void StatusWidget::updateFontsSizes()
  55. {
  56. QFont pt10_font = QFont(ui->news_label->font());
  57. pt10_font.setPixelSize(pixels_in_10_pt);
  58. QFont pt9_font = QFont(ui->progress_label->font());
  59. pt9_font.setPixelSize(pixels_in_9_pt);
  60. QFont pt11_font = QFont(ui->game_button->font());
  61. pt11_font.setPixelSize(pixels_in_11_pt);
  62. QFont pt8_font = QFont(ui->images_label->font());
  63. pt8_font.setPixelSize(pixels_in_8_pt);
  64. ui->progress_label->setFont(crimson_10pt);
  65. ui->game_button->setFont(trajan_11pt);
  66. ui->news_label->setFont(trajan_10pt);
  67. ui->news_tooltip->setFont(garamond_10pt);
  68. ui->images_label->setFont(crimson_11pt);
  69. ui->images_status->setFont(crimson_11pt);
  70. ui->sounds_label->setFont(crimson_11pt);
  71. ui->sounds_status->setFont(crimson_11pt);
  72. ui->texts_label->setFont(crimson_11pt);
  73. ui->texts_status->setFont(crimson_11pt);
  74. ui->videos_label->setFont(crimson_11pt);
  75. ui->videos_status->setFont(crimson_11pt);
  76. ui->weekly_code_tooltip_1->setFont(garamond_10pt);
  77. ui->weekly_code_tooltip_2->setFont(garamond_10pt);
  78. }
  79. void StatusWidget::resizeEvent(QResizeEvent *event)
  80. {
  81. double coefficient = window_width / default_window_width;
  82. ui->game_button->move(QPoint(820, 460) * coefficient);
  83. ui->game_button->resize(QSize(150, 60) * coefficient);
  84. ui->progressBar->move(QPoint(320, 480) * coefficient);
  85. ui->progressBar->resize(QSize(470, 40) * coefficient);
  86. ui->progress_label->move(QPoint(330, 390) * coefficient);
  87. ui->progress_label->resize(QSize(380, 90) * coefficient);
  88. ui->news_label->move(QPoint(45, 33)* coefficient);
  89. ui->news_label->resize(QSize(180, 21) * coefficient);
  90. ui->news_scroll_area->move(QPoint(40, 75) * coefficient);
  91. ui->news_scroll_area->resize(QSize(240, 440) * coefficient);
  92. ui->server_status_widget->move(QPoint(820, 90) * coefficient);
  93. ui->server_status_widget->resize(QSize(155, 320) * coefficient);
  94. ui->weekly_code_widget->move(QPoint(810, 13) * coefficient);
  95. ui->weekly_code_widget->resize(QSize(173, 57) * coefficient);
  96. ui->galadriel_widget->move(QPoint(320, 20) * coefficient);
  97. ui->galadriel_widget->resize(QSize(531, 461) * coefficient);
  98. ui->news_tooltip->move(QPoint(38, 13) * coefficient);
  99. ui->news_tooltip->resize(QSize(365, 114) * coefficient);
  100. ui->weekly_code_tooltip_1->move(QPoint(38, 13) * coefficient);
  101. ui->weekly_code_tooltip_1->resize(QSize(365, 114) * coefficient);
  102. ui->weekly_code_tooltip_2->move(QPoint(38, 13) * coefficient);
  103. ui->weekly_code_tooltip_2->resize(QSize(365, 114) * coefficient);
  104. ui->patches_status->move(QPoint(38, 13) * coefficient);
  105. ui->patches_status->resize(QSize(365, 114) * coefficient);
  106. updateFontsSizes();
  107. }
  108. void StatusWidget::onPatchDownloaderStarted()
  109. {
  110. qDebug() << "Status widget received DownloadStarted signal!";
  111. ui->game_button->setDisabled(true);
  112. ui->progress_label->setText("Проверка обновлений...");
  113. ui->progressBar->setValue(0);
  114. }
  115. void StatusWidget::onPatchDownloaderFinished()
  116. {
  117. ui->progress_label->setText("Все патчи загружены!");
  118. ui->progressBar->setValue(100);
  119. ui->game_button->setDisabled(false);
  120. }
  121. void StatusWidget::onPatchDownloaderProgressChanged(QList<PatchDownloadData> patches_data)
  122. {
  123. if (last_progress_update_time.elapsed() < 200) {
  124. return;
  125. } else {
  126. last_progress_update_time.restart();
  127. }
  128. foreach (PatchDownloadData data, patches_data) {
  129. if (data.patch_name == "general") {
  130. ui->progress_label->setText("Скачивание переводов " + data.download_speed_formatted + ".\nЗагружено "
  131. + Downloader::getSizeFormatted(data.bytesDownloaded) + " из " + Downloader::getSizeFormatted(data.bytesTotal)
  132. + " (" + QString::number(double(data.bytesDownloaded) * 100.0 / double(data.bytesTotal), 'f', 1) + "%) "
  133. + "\nОставшееся время: " + data.elapsed_time_formatted);
  134. ui->progressBar->setValue(data.bytesDownloaded * 100 / data.bytesTotal);
  135. } else {
  136. updatePatchStatus(data.patch_name, "Скачивание " + QString::number(double(data.bytesDownloaded) * 100.0 / double(data.bytesTotal), 'f', 1) + "% (" + data.download_speed_formatted + ")");
  137. }
  138. }
  139. }
  140. void StatusWidget::updatePatchStatus(QString patch_name, QString status)
  141. {
  142. QString status_label_name = patch_name + "s_status";
  143. QLabel* status_label = findChild<QLabel*>(status_label_name);
  144. if (!status_label)
  145. return;
  146. status_label->setText(status);
  147. }
  148. void StatusWidget::on_game_button_clicked()
  149. {
  150. MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
  151. window->showChooseVersionDialog();
  152. }
  153. void StatusWidget::setToolTipToWeeklyCodeHelp()
  154. {
  155. fadeBetweenToolTips(ui->weekly_code_tooltip_1);
  156. }
  157. void StatusWidget::setToolTipToWeeklyCodeComplete()
  158. {
  159. fadeBetweenToolTips(ui->weekly_code_tooltip_2);
  160. }
  161. void StatusWidget::setToolTipToNewsHelp()
  162. {
  163. fadeBetweenToolTips(ui->news_tooltip);
  164. }
  165. void StatusWidget::resetToolTip()
  166. {
  167. fadeBetweenToolTips(ui->patches_status);
  168. }
  169. void StatusWidget::fadeBetweenToolTips(QWidget* next_tooltip)
  170. {
  171. if (active_tooltip) {
  172. QPropertyAnimation* active_tooltip_anim = tooltip_animations[active_tooltip->objectName()];
  173. active_tooltip_anim->setDirection(QAbstractAnimation::Backward);
  174. if (active_tooltip_anim->state() == QAbstractAnimation::Stopped)
  175. active_tooltip_anim->start();
  176. }
  177. QPropertyAnimation* next_active_tooltip_anim = tooltip_animations[next_tooltip->objectName()];
  178. next_active_tooltip_anim->setDirection(QAbstractAnimation::Forward);
  179. if (next_active_tooltip_anim->state() == QAbstractAnimation::Stopped)
  180. next_active_tooltip_anim->start();
  181. active_tooltip = next_tooltip;
  182. }