statuswidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "ui_statuswidget.h"
  2. #include "statuswidget.h"
  3. #include "models/patchlist.h"
  4. #include "widgets/mainwindow.h"
  5. #include "constants.h"
  6. #include <QDesktopServices>
  7. #include <QUrl>
  8. #include <QDebug>
  9. #include <QMessageBox>
  10. #include <QRandomGenerator>
  11. StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
  12. : QWidget(parent)
  13. , ui(new Ui::StatusWidget)
  14. , legacy_patches_(legacy_patches)
  15. {
  16. ui->setupUi(this);
  17. last_statusbar_update_time_.start();
  18. ui->galadriel_tooltip_example->hide();
  19. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showCompletedTooltip, this, [this](){setToolTipMessage("ЕЖЕНЕДЕЛЬНЫЙ КОД СКОПИРОВАН В БУФЕР ОБМЕНА", E_INFO);});
  20. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showHelpTooltip, this, [this](){setToolTipMessage("КЛИКНИТЕ ПО ЕЖЕНЕДЕЛЬНОМУ КОДУ, ЧТОБЫ СКОПИРОВАТЬ ЕГО", E_INFO);});
  21. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
  22. connect(ui->news_list, &NewsListWidget::showHelpToolTip, this, [this](){setToolTipMessage("НАЖМИТЕ НА ЗАГОЛОВОК НОВОСТИ, ЧТОБЫ ОТКРЫТЬ ЕЕ В БРАУЗЕРЕ", E_INFO);});
  23. connect(ui->news_list, &NewsListWidget::showNoToolTip, this, [this](){unsetToolTipMessage(E_INFO);});
  24. connect(ui->server_status_widget, &ServerStatusWidget::showServersTooltip, this, [this](QString message){setToolTipMessage(message, E_INFO);});
  25. connect(ui->server_status_widget, &ServerStatusWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
  26. connect(legacy_patches_, &PatchList::patchOperationsStarted, this, &StatusWidget::onPatchTotalOperationsStarted);
  27. connect(legacy_patches_, &PatchList::patchOperationsFinished, this, &StatusWidget::onPatchTotalOperationsFinished);
  28. connect(legacy_patches_, &PatchList::progressChanged, this, &StatusWidget::updatePatchProgressStatus);
  29. generateRandomTooltipMessage();
  30. random_tooltip_generator_timer_.setInterval(5 * 1000);
  31. connect(&random_tooltip_generator_timer_, &QTimer::timeout, this, &StatusWidget::generateRandomTooltipMessage);
  32. random_tooltip_generator_timer_.start();
  33. process_completed_tooltip_hide_timer_.setInterval(10 * 1000); // 10 seconds message "Installation completed" will be shown
  34. connect(&process_completed_tooltip_hide_timer_, &QTimer::timeout, this, [this](){unsetToolTipMessage(E_PROCESS);});
  35. }
  36. StatusWidget::~StatusWidget()
  37. {
  38. delete ui;
  39. }
  40. void StatusWidget::updateFontsSizes()
  41. {
  42. ui->game_button->setFont(trajan_11pt);
  43. ui->news_label->setFont(trajan_10pt);
  44. }
  45. void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState state)
  46. {
  47. tooltip_messages_[state] = message;
  48. tooltip_state_ = ToolTipState(int(tooltip_state_) | int(state));
  49. QString message_id = message;
  50. if (state == E_PROCESS) {
  51. message_id = "E_PROCESS";
  52. }
  53. if (!tooltip_widgets_.contains(message_id)) {
  54. createTooltipMessageWidget(message_id);
  55. }
  56. tooltip_widgets_[message_id]->setText(message);
  57. if (tooltip_state_ < state * 2) {
  58. fadeBetweenToolTips(message_id);
  59. }
  60. }
  61. void StatusWidget::unsetToolTipMessage(StatusWidget::ToolTipState state)
  62. {
  63. tooltip_state_ = ToolTipState(int(tooltip_state_) & (~int(state)));
  64. ToolTipState showing_state = E_RANDOM;
  65. if (tooltip_state_ & E_INFO) {
  66. showing_state = E_INFO;
  67. } else if (tooltip_state_ & E_ERROR) {
  68. showing_state = E_ERROR;
  69. } else if (tooltip_state_ & E_PROCESS) {
  70. showing_state = E_PROCESS;
  71. }
  72. QString message_id = tooltip_messages_[showing_state];
  73. if (showing_state == E_PROCESS) {
  74. message_id = "E_PROCESS";
  75. }
  76. if (!tooltip_widgets_.contains(message_id)) {
  77. createTooltipMessageWidget(message_id);
  78. }
  79. tooltip_widgets_[message_id]->setText(tooltip_messages_[showing_state]);
  80. fadeBetweenToolTips(message_id);
  81. }
  82. void StatusWidget::resizeEvent(QResizeEvent *)
  83. {
  84. double coefficient = window_width / default_window_width;
  85. // ui->game_button->move(QPoint(820, 460) * coefficient);
  86. // ui->game_button->resize(QSize(150, 60) * coefficient);
  87. // ui->progressBar->move(QPoint(320, 480) * coefficient);
  88. // ui->progressBar->resize(QSize(470, 40) * coefficient);
  89. // ui->progress_label->move(QPoint(330, 390) * coefficient);
  90. // ui->progress_label->resize(QSize(380, 90) * coefficient);
  91. // ui->news_label->move(QPoint(45, 33)* coefficient);
  92. // ui->news_label->resize(QSize(180, 21) * coefficient);
  93. // ui->news_scroll_area->move(QPoint(40, 75) * coefficient);
  94. // ui->news_scroll_area->resize(QSize(240, 440) * coefficient);
  95. // ui->server_status_widget->move(QPoint(820, 90) * coefficient);
  96. // ui->server_status_widget->resize(QSize(155, 320) * coefficient);
  97. // ui->weekly_code_widget->move(QPoint(810, 13) * coefficient);
  98. // ui->weekly_code_widget->resize(QSize(173, 57) * coefficient);
  99. // ui->galadriel_widget->move(QPoint(320, 20) * coefficient);
  100. // ui->galadriel_widget->resize(QSize(531, 461) * coefficient);
  101. // ui->news_tooltip->move(QPoint(38, 13) * coefficient);
  102. // ui->news_tooltip->resize(QSize(365, 114) * coefficient);
  103. // ui->weekly_code_tooltip_1->move(QPoint(38, 13) * coefficient);
  104. // ui->weekly_code_tooltip_1->resize(QSize(365, 114) * coefficient);
  105. // ui->weekly_code_tooltip_2->move(QPoint(38, 13) * coefficient);
  106. // ui->weekly_code_tooltip_2->resize(QSize(365, 114) * coefficient);
  107. // ui->server_status_tooltip->move(QPoint(38, 13) * coefficient);
  108. // ui->server_status_tooltip->resize(QSize(365, 114) * coefficient);
  109. // ui->patches_status->move(QPoint(38, 13) * coefficient);
  110. // ui->patches_status->resize(QSize(385, 114) * coefficient);
  111. updateFontsSizes();
  112. }
  113. void StatusWidget::generateRandomTooltipMessage()
  114. {
  115. quint32 number = QRandomGenerator::system()->generate();
  116. setToolTipMessage("СЛУЧАЙНОЕ СООБЩЕНИЕ №" + QString::number(number), E_RANDOM);
  117. }
  118. void StatusWidget::fadeBetweenToolTips(QString tooltip_id)
  119. {
  120. if (tooltip_id == current_tooltip_message_) {
  121. return;
  122. }
  123. QPropertyAnimation* showing_anim = tooltip_animations_[tooltip_id];
  124. showing_anim->setDirection(QAbstractAnimation::Forward);
  125. if (showing_anim->state() == QAbstractAnimation::Stopped) {
  126. showing_anim->start();
  127. }
  128. if (tooltip_widgets_.contains(current_tooltip_message_)) {
  129. QString msg = current_tooltip_message_;
  130. connect(tooltip_animations_[current_tooltip_message_], &QPropertyAnimation::finished, this, [&, msg](){
  131. if (tooltip_effects_.contains(msg) && tooltip_effects_[msg]->opacity() == 0) {
  132. tooltip_widgets_.take(msg)->deleteLater();
  133. tooltip_effects_.take(msg)->deleteLater();
  134. tooltip_animations_.take(msg)->deleteLater();
  135. }
  136. });
  137. QPropertyAnimation* hiding_anim = tooltip_animations_[current_tooltip_message_];
  138. hiding_anim->setDirection(QAbstractAnimation::Backward);
  139. if (hiding_anim->state() == QAbstractAnimation::Stopped) {
  140. hiding_anim->start();
  141. }
  142. }
  143. current_tooltip_message_ = tooltip_id;
  144. }
  145. void StatusWidget::onPatchTotalOperationsStarted()
  146. {
  147. all_patch_operations_finished_ = false;
  148. ui->game_button->setEnabled(false);
  149. ui->check_for_updates_button->setEnabled(false);
  150. setToolTipMessage("Запуск процесса проверки и установки русификации...", E_PROCESS);
  151. }
  152. void StatusWidget::onPatchTotalOperationsFinished()
  153. {
  154. all_patch_operations_finished_ = true;
  155. ui->game_button->setEnabled(true);
  156. ui->check_for_updates_button->setEnabled(true);
  157. setToolTipMessage("Все процессы установки русификации завершены. Можно играть. Mae govannen. Еще какой-то текст...", E_PROCESS);
  158. process_completed_tooltip_hide_timer_.start();
  159. }
  160. void StatusWidget::on_game_button_clicked()
  161. {
  162. MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
  163. window->showChooseVersionDialog();
  164. }
  165. void StatusWidget::updatePatchProgressStatus(Patch::OperationProgress progress)
  166. {
  167. if (last_statusbar_update_time_.elapsed() > 400) {
  168. QString text = "Выполнение операций...";
  169. if (progress.download_total_bytes != 0) {
  170. text += "\nЗагрузка данных: " + QString::number(progress.getDownloadPercent(), 'f', 1) + "% ("
  171. + Downloader::getSizeFormatted(progress.download_finished_bytes) + "/"
  172. + Downloader::getSizeFormatted(progress.download_total_bytes) + ", "
  173. + Downloader::getSpeedFormatted(progress.download_speed) + ")\n"
  174. + "До конца загрузки: " + Downloader::getElapsedTimeFormatted(progress.download_elapsed_time);
  175. }
  176. if (progress.install_total_parts != 0) {
  177. text += "\nПрименение патчей: " + QString::number(progress.getInstallPercent()) + "% "
  178. + "(часть " + QString::number(progress.install_finished_parts + 1) + " из " + QString::number(progress.install_total_parts);
  179. }
  180. setToolTipMessage(text, E_PROCESS);
  181. last_statusbar_update_time_.restart();
  182. }
  183. }
  184. void StatusWidget::createTooltipMessageWidget(QString tooltip_id)
  185. {
  186. tooltip_widgets_[tooltip_id] = new QLabel(ui->galadriel_widget);
  187. tooltip_widgets_[tooltip_id]->setGeometry(ui->galadriel_tooltip_example->geometry());
  188. tooltip_widgets_[tooltip_id]->setStyleSheet(ui->galadriel_tooltip_example->styleSheet());
  189. tooltip_widgets_[tooltip_id]->setFont(ui->galadriel_tooltip_example->font());
  190. tooltip_widgets_[tooltip_id]->setWordWrap(true);
  191. tooltip_widgets_[tooltip_id]->setAlignment(Qt::AlignCenter);
  192. tooltip_widgets_[tooltip_id]->show();
  193. tooltip_effects_[tooltip_id] = new QGraphicsOpacityEffect(tooltip_widgets_[tooltip_id]);
  194. tooltip_effects_[tooltip_id]->setOpacity(0);
  195. tooltip_animations_[tooltip_id] = new QPropertyAnimation(tooltip_effects_[tooltip_id], "opacity");
  196. tooltip_animations_[tooltip_id]->setDuration(300);
  197. tooltip_animations_[tooltip_id]->setStartValue(0);
  198. tooltip_animations_[tooltip_id]->setEndValue(1);
  199. tooltip_animations_[tooltip_id]->setDirection(QAbstractAnimation::Forward);
  200. tooltip_widgets_[tooltip_id]->setGraphicsEffect(tooltip_effects_[tooltip_id]);
  201. }
  202. void StatusWidget::on_check_for_updates_button_clicked()
  203. {
  204. QMetaObject::invokeMethod(legacy_patches_, &PatchList::update, Qt::QueuedConnection);
  205. }