statuswidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. process_completed_tooltip_hide_timer_.stop();
  35. connect(&process_completed_tooltip_hide_timer_, &QTimer::timeout, this, [this](){unsetToolTipMessage(E_PROCESS); process_completed_tooltip_hide_timer_.stop();});
  36. }
  37. StatusWidget::~StatusWidget()
  38. {
  39. delete ui;
  40. }
  41. void StatusWidget::updateFontsSizes()
  42. {
  43. ui->game_button->setFont(trajan_11pt);
  44. ui->news_label->setFont(trajan_10pt);
  45. }
  46. void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState state)
  47. {
  48. tooltip_messages_[state] = message;
  49. tooltip_state_ = ToolTipState(int(tooltip_state_) | int(state));
  50. QString message_id = message;
  51. if (state == E_PROCESS) {
  52. message_id = "E_PROCESS";
  53. }
  54. if (!tooltip_widgets_.contains(message_id)) {
  55. createTooltipMessageWidget(message_id);
  56. }
  57. tooltip_widgets_[message_id]->setText(message);
  58. if (tooltip_state_ < state * 2) {
  59. fadeBetweenToolTips(message_id);
  60. }
  61. }
  62. void StatusWidget::unsetToolTipMessage(StatusWidget::ToolTipState state)
  63. {
  64. tooltip_state_ = ToolTipState(int(tooltip_state_) & (~int(state)));
  65. ToolTipState showing_state = E_RANDOM;
  66. if (tooltip_state_ & E_INFO) {
  67. showing_state = E_INFO;
  68. } else if (tooltip_state_ & E_ERROR) {
  69. showing_state = E_ERROR;
  70. } else if (tooltip_state_ & E_PROCESS) {
  71. showing_state = E_PROCESS;
  72. }
  73. QString message_id = tooltip_messages_[showing_state];
  74. if (showing_state == E_PROCESS) {
  75. message_id = "E_PROCESS";
  76. }
  77. if (!tooltip_widgets_.contains(message_id)) {
  78. createTooltipMessageWidget(message_id);
  79. }
  80. tooltip_widgets_[message_id]->setText(tooltip_messages_[showing_state]);
  81. fadeBetweenToolTips(message_id);
  82. }
  83. void StatusWidget::onErrorStatusChanged(LegacyApplication::ErrorStatus status)
  84. {
  85. if (status == LegacyApplication::E_NO_ERRORS) {
  86. init_error_type_message_ = "";
  87. unsetToolTipMessage(E_ERROR);
  88. ui->news_list->updateNewsWidget();
  89. ui->server_status_widget->updateServerStatus();
  90. ui->weekly_code_widget->updateWeeklyCodeWidget();
  91. return;
  92. }
  93. error_timeout_message_ = "Повторная попытка инициализации через 10 секунд";
  94. if (status & LegacyApplication::E_WRONG_GAME_FOLDER) {
  95. init_error_type_message_ = "Ошибка инициализации: некорректная папка с игрой!\n";
  96. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  97. return;
  98. }
  99. if (status & LegacyApplication::E_DAT_FILES_MISSING) {
  100. init_error_type_message_ = "Ошибка инициализации: отсутствуют файлы данных игры!\n";
  101. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  102. return;
  103. }
  104. if (status & LegacyApplication::E_WRONG_FILE_PERMISSIONS) {
  105. init_error_type_message_ = "Ошибка инициализации: недостаточно прав для изменения файлов данных!\n";
  106. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  107. return;
  108. }
  109. if (status & LegacyApplication::E_CANNOT_ACCESS_DAT_FILES) {
  110. init_error_type_message_ = "Ошибка инициализации: нет доступа к файлам данных!\n";
  111. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  112. return;
  113. }
  114. if (status & LegacyApplication::E_DAT_FILE_INCORRECT) {
  115. init_error_type_message_ = "Ошибка инициализации: формат файла данных устарел или некорректен!\n";
  116. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  117. return;
  118. }
  119. if (status & LegacyApplication::E_NO_SERVER_CONNECTION) {
  120. init_error_type_message_ = "Ошибка инициализации: нет связи с сервером Наследия!\n";
  121. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  122. return;
  123. }
  124. }
  125. void StatusWidget::onSecondsToNextTryToInitChanged(size_t seconds_elapsed)
  126. {
  127. if (!init_error_type_message_.isEmpty()) {
  128. error_timeout_message_ = "Повторная попытка инициализации через " + QString::number(seconds_elapsed) + " секунд";
  129. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  130. }
  131. }
  132. void StatusWidget::resizeEvent(QResizeEvent *)
  133. {
  134. double coefficient = window_width / default_window_width;
  135. // ui->game_button->move(QPoint(820, 460) * coefficient);
  136. // ui->game_button->resize(QSize(150, 60) * coefficient);
  137. // ui->progressBar->move(QPoint(320, 480) * coefficient);
  138. // ui->progressBar->resize(QSize(470, 40) * coefficient);
  139. // ui->progress_label->move(QPoint(330, 390) * coefficient);
  140. // ui->progress_label->resize(QSize(380, 90) * coefficient);
  141. // ui->news_label->move(QPoint(45, 33)* coefficient);
  142. // ui->news_label->resize(QSize(180, 21) * coefficient);
  143. // ui->news_scroll_area->move(QPoint(40, 75) * coefficient);
  144. // ui->news_scroll_area->resize(QSize(240, 440) * coefficient);
  145. // ui->server_status_widget->move(QPoint(820, 90) * coefficient);
  146. // ui->server_status_widget->resize(QSize(155, 320) * coefficient);
  147. // ui->weekly_code_widget->move(QPoint(810, 13) * coefficient);
  148. // ui->weekly_code_widget->resize(QSize(173, 57) * coefficient);
  149. // ui->galadriel_widget->move(QPoint(320, 20) * coefficient);
  150. // ui->galadriel_widget->resize(QSize(531, 461) * coefficient);
  151. // ui->news_tooltip->move(QPoint(38, 13) * coefficient);
  152. // ui->news_tooltip->resize(QSize(365, 114) * coefficient);
  153. // ui->weekly_code_tooltip_1->move(QPoint(38, 13) * coefficient);
  154. // ui->weekly_code_tooltip_1->resize(QSize(365, 114) * coefficient);
  155. // ui->weekly_code_tooltip_2->move(QPoint(38, 13) * coefficient);
  156. // ui->weekly_code_tooltip_2->resize(QSize(365, 114) * coefficient);
  157. // ui->server_status_tooltip->move(QPoint(38, 13) * coefficient);
  158. // ui->server_status_tooltip->resize(QSize(365, 114) * coefficient);
  159. // ui->patches_status->move(QPoint(38, 13) * coefficient);
  160. // ui->patches_status->resize(QSize(385, 114) * coefficient);
  161. updateFontsSizes();
  162. }
  163. void StatusWidget::generateRandomTooltipMessage()
  164. {
  165. quint32 number = QRandomGenerator::system()->generate();
  166. setToolTipMessage("СЛУЧАЙНОЕ СООБЩЕНИЕ №" + QString::number(number), E_RANDOM);
  167. }
  168. void StatusWidget::fadeBetweenToolTips(QString tooltip_id)
  169. {
  170. if (tooltip_id == current_tooltip_message_) {
  171. return;
  172. }
  173. QPropertyAnimation* showing_anim = tooltip_animations_[tooltip_id];
  174. showing_anim->setDirection(QAbstractAnimation::Forward);
  175. if (showing_anim->state() == QAbstractAnimation::Stopped) {
  176. showing_anim->start();
  177. }
  178. if (tooltip_widgets_.contains(current_tooltip_message_)) {
  179. QString msg = current_tooltip_message_;
  180. connect(tooltip_animations_[current_tooltip_message_], &QPropertyAnimation::finished, this, [&, msg](){
  181. if (tooltip_effects_.contains(msg) && tooltip_effects_[msg]->opacity() == 0) {
  182. tooltip_widgets_.take(msg)->deleteLater();
  183. tooltip_effects_.take(msg)->deleteLater();
  184. tooltip_animations_.take(msg)->deleteLater();
  185. }
  186. });
  187. QPropertyAnimation* hiding_anim = tooltip_animations_[current_tooltip_message_];
  188. hiding_anim->setDirection(QAbstractAnimation::Backward);
  189. if (hiding_anim->state() == QAbstractAnimation::Stopped) {
  190. hiding_anim->start();
  191. }
  192. }
  193. current_tooltip_message_ = tooltip_id;
  194. }
  195. void StatusWidget::onPatchTotalOperationsStarted()
  196. {
  197. all_patch_operations_finished_ = false;
  198. ui->game_button->setEnabled(false);
  199. ui->check_for_updates_button->setEnabled(false);
  200. setToolTipMessage("Запуск процесса проверки и установки русификации...", E_PROCESS);
  201. }
  202. void StatusWidget::onPatchTotalOperationsFinished()
  203. {
  204. all_patch_operations_finished_ = true;
  205. ui->game_button->setEnabled(true);
  206. ui->check_for_updates_button->setEnabled(true);
  207. setToolTipMessage("Все процессы установки русификации завершены. Можно играть. Mae govannen. Еще какой-то текст...", E_PROCESS);
  208. process_completed_tooltip_hide_timer_.start();
  209. }
  210. void StatusWidget::on_game_button_clicked()
  211. {
  212. MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
  213. window->showChooseVersionDialog();
  214. }
  215. void StatusWidget::updatePatchProgressStatus(Patch::OperationProgress progress)
  216. {
  217. if (last_statusbar_update_time_.elapsed() > 200) {
  218. QString text = "Выполнение операций...";
  219. if (progress.download_total_bytes != 0) {
  220. text += "\nЗагрузка данных: " + QString::number(progress.getDownloadPercent(), 'f', 1) + "% ("
  221. + Downloader::getSizeFormatted(progress.download_finished_bytes) + "/"
  222. + Downloader::getSizeFormatted(progress.download_total_bytes) + ", "
  223. + Downloader::getSpeedFormatted(progress.download_speed) + ")\n"
  224. + "До конца загрузки: " + Downloader::getElapsedTimeFormatted(progress.download_elapsed_time);
  225. }
  226. if (progress.install_total_parts != 0) {
  227. text += "\nПрименение патчей: " + QString::number(progress.getInstallPercent()) + "% "
  228. + "(часть " + QString::number(progress.install_finished_parts + 1) + " из " + QString::number(progress.install_total_parts);
  229. }
  230. setToolTipMessage(text, E_PROCESS);
  231. last_statusbar_update_time_.restart();
  232. }
  233. }
  234. void StatusWidget::createTooltipMessageWidget(QString tooltip_id)
  235. {
  236. tooltip_widgets_[tooltip_id] = new QLabel(ui->galadriel_widget);
  237. tooltip_widgets_[tooltip_id]->setGeometry(ui->galadriel_tooltip_example->geometry());
  238. tooltip_widgets_[tooltip_id]->setStyleSheet(ui->galadriel_tooltip_example->styleSheet());
  239. tooltip_widgets_[tooltip_id]->setFont(ui->galadriel_tooltip_example->font());
  240. tooltip_widgets_[tooltip_id]->setWordWrap(true);
  241. tooltip_widgets_[tooltip_id]->setAlignment(Qt::AlignCenter);
  242. tooltip_widgets_[tooltip_id]->show();
  243. tooltip_effects_[tooltip_id] = new QGraphicsOpacityEffect(tooltip_widgets_[tooltip_id]);
  244. tooltip_effects_[tooltip_id]->setOpacity(0);
  245. tooltip_animations_[tooltip_id] = new QPropertyAnimation(tooltip_effects_[tooltip_id], "opacity");
  246. tooltip_animations_[tooltip_id]->setDuration(300);
  247. tooltip_animations_[tooltip_id]->setStartValue(0);
  248. tooltip_animations_[tooltip_id]->setEndValue(1);
  249. tooltip_animations_[tooltip_id]->setDirection(QAbstractAnimation::Forward);
  250. tooltip_widgets_[tooltip_id]->setGraphicsEffect(tooltip_effects_[tooltip_id]);
  251. }
  252. void StatusWidget::on_check_for_updates_button_clicked()
  253. {
  254. QMetaObject::invokeMethod(legacy_patches_, &PatchList::update, Qt::QueuedConnection);
  255. }