statuswidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "ui_statuswidget.h"
  2. #include "statuswidget.h"
  3. #include "models/patchdownloader.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(QWidget *parent)
  12. : QWidget(parent)
  13. , ui(new Ui::StatusWidget)
  14. {
  15. ui->setupUi(this);
  16. last_statusbar_update_time_.start();
  17. ui->galadriel_tooltip_example->hide();
  18. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showCompletedTooltip, this, [this](){setToolTipMessage("ЕЖЕНЕДЕЛЬНЫЙ КОД СКОПИРОВАН В БУФЕР ОБМЕНА", E_INFO);});
  19. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showHelpTooltip, this, [this](){setToolTipMessage("КЛИКНИТЕ ПО ЕЖЕНЕДЕЛЬНОМУ КОДУ, ЧТОБЫ СКОПИРОВАТЬ ЕГО", E_INFO);});
  20. connect(ui->weekly_code_widget, &WeeklyCodeWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
  21. connect(ui->news_list, &NewsListWidget::showHelpToolTip, this, [this](){setToolTipMessage("НАЖМИТЕ НА ЗАГОЛОВОК НОВОСТИ, ЧТОБЫ ОТКРЫТЬ ЕЕ В БРАУЗЕРЕ", E_INFO);});
  22. connect(ui->news_list, &NewsListWidget::showNoToolTip, this, [this](){unsetToolTipMessage(E_INFO);});
  23. connect(ui->server_status_widget, &ServerStatusWidget::showServersTooltip, this, [this](QString message){setToolTipMessage(message, E_INFO);});
  24. connect(ui->server_status_widget, &ServerStatusWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
  25. connect(&PatchDownloader::instance(), &PatchDownloader::started, this, &StatusWidget::onPatchDownloaderStarted);
  26. connect(&PatchDownloader::instance(), &PatchDownloader::progressChanged, this, &StatusWidget::onPatchDownloaderProgressChanged);
  27. connect(&PatchDownloader::instance(), &PatchDownloader::finished, this, &StatusWidget::onPatchDownloaderFinished);
  28. connect(&PatchInstaller::instance(), &PatchInstaller::started, this, &StatusWidget::onPatchInstallerStarted);
  29. connect(&PatchInstaller::instance(), &PatchInstaller::progressChanged, this, &StatusWidget::onPatchInstallerProgressChanged);
  30. connect(&PatchInstaller::instance(), &PatchInstaller::finished, this, &StatusWidget::onPatchInstallerFinished);
  31. connect(&PatchInstaller::instance(), &PatchInstaller::videosDownloadProgressChanged, this, &StatusWidget::onPatchInstallerVideoProgressChanged);
  32. generateRandomTooltipMessage();
  33. random_tooltip_generator_timer_.setInterval(5 * 1000);
  34. connect(&random_tooltip_generator_timer_, &QTimer::timeout, this, &StatusWidget::generateRandomTooltipMessage);
  35. random_tooltip_generator_timer_.start();
  36. process_completed_tooltip_hide_timer_.setInterval(10 * 1000); // 10 seconds message "Installation completed" will be shown
  37. process_completed_tooltip_hide_timer_.stop();
  38. connect(&process_completed_tooltip_hide_timer_, &QTimer::timeout, this, [this](){unsetToolTipMessage(E_PROCESS); process_completed_tooltip_hide_timer_.stop();});
  39. }
  40. StatusWidget::~StatusWidget()
  41. {
  42. delete ui;
  43. }
  44. void StatusWidget::updateFontsSizes()
  45. {
  46. ui->game_button->setFont(trajan_11pt);
  47. ui->news_label->setFont(trajan_10pt);
  48. }
  49. void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState state)
  50. {
  51. tooltip_messages_[state] = message;
  52. tooltip_state_ = ToolTipState(int(tooltip_state_) | int(state));
  53. QString message_id = message;
  54. if (state == E_PROCESS) {
  55. message_id = "E_PROCESS";
  56. }
  57. if (!tooltip_widgets_.contains(message_id)) {
  58. createTooltipMessageWidget(message_id);
  59. }
  60. tooltip_widgets_[message_id]->setText(message);
  61. if (tooltip_state_ < state * 2) {
  62. fadeBetweenToolTips(message_id);
  63. }
  64. }
  65. void StatusWidget::unsetToolTipMessage(StatusWidget::ToolTipState state)
  66. {
  67. tooltip_state_ = ToolTipState(int(tooltip_state_) & (~int(state)));
  68. ToolTipState showing_state = E_RANDOM;
  69. if (tooltip_state_ & E_INFO) {
  70. showing_state = E_INFO;
  71. } else if (tooltip_state_ & E_ERROR) {
  72. showing_state = E_ERROR;
  73. } else if (tooltip_state_ & E_PROCESS) {
  74. showing_state = E_PROCESS;
  75. }
  76. QString message_id = tooltip_messages_[showing_state];
  77. if (showing_state == E_PROCESS) {
  78. message_id = "E_PROCESS";
  79. }
  80. if (!tooltip_widgets_.contains(message_id)) {
  81. createTooltipMessageWidget(message_id);
  82. }
  83. tooltip_widgets_[message_id]->setText(tooltip_messages_[showing_state]);
  84. fadeBetweenToolTips(message_id);
  85. }
  86. void StatusWidget::onErrorStatusChanged(AppErrorStatus status)
  87. {
  88. if (status == E_NO_ERRORS) {
  89. init_error_type_message_ = "";
  90. unsetToolTipMessage(E_ERROR);
  91. ui->news_list->updateNewsWidget();
  92. ui->server_status_widget->updateServerStatus();
  93. ui->weekly_code_widget->updateWeeklyCodeWidget();
  94. return;
  95. }
  96. error_timeout_message_ = "Повторная попытка инициализации через 10 секунд";
  97. if (status & E_WRONG_GAME_FOLDER) {
  98. init_error_type_message_ = "Ошибка инициализации: некорректная папка с игрой!\n";
  99. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  100. return;
  101. }
  102. if (status & E_DAT_FILES_MISSING) {
  103. init_error_type_message_ = "Ошибка инициализации: отсутствуют файлы данных игры!\n";
  104. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  105. return;
  106. }
  107. if (status & E_WRONG_FILE_PERMISSIONS) {
  108. init_error_type_message_ = "Ошибка инициализации: недостаточно прав для изменения файлов данных!\n";
  109. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  110. return;
  111. }
  112. if (status & E_CANNOT_ACCESS_DAT_FILES) {
  113. init_error_type_message_ = "Ошибка инициализации: нет доступа к файлам данных!\n";
  114. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  115. return;
  116. }
  117. if (status & E_DAT_FILE_INCORRECT) {
  118. init_error_type_message_ = "Ошибка инициализации: формат файла данных устарел или некорректен!\n";
  119. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  120. return;
  121. }
  122. if (status & E_NO_SERVER_CONNECTION) {
  123. init_error_type_message_ = "Ошибка инициализации: нет связи с сервером Наследия!\n";
  124. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  125. return;
  126. }
  127. }
  128. void StatusWidget::onSecondsToNextTryToInitChanged(size_t seconds_elapsed)
  129. {
  130. if (!init_error_type_message_.isEmpty()) {
  131. error_timeout_message_ = "Повторная попытка инициализации через " + QString::number(seconds_elapsed) + " секунд";
  132. setToolTipMessage(init_error_type_message_ + error_timeout_message_, E_ERROR);
  133. }
  134. }
  135. void StatusWidget::resizeEvent(QResizeEvent *)
  136. {
  137. double coefficient = window_width / default_window_width;
  138. // ui->game_button->move(QPoint(820, 460) * coefficient);
  139. // ui->game_button->resize(QSize(150, 60) * coefficient);
  140. // ui->progressBar->move(QPoint(320, 480) * coefficient);
  141. // ui->progressBar->resize(QSize(470, 40) * coefficient);
  142. // ui->progress_label->move(QPoint(330, 390) * coefficient);
  143. // ui->progress_label->resize(QSize(380, 90) * coefficient);
  144. // ui->news_label->move(QPoint(45, 33)* coefficient);
  145. // ui->news_label->resize(QSize(180, 21) * coefficient);
  146. // ui->news_scroll_area->move(QPoint(40, 75) * coefficient);
  147. // ui->news_scroll_area->resize(QSize(240, 440) * coefficient);
  148. // ui->server_status_widget->move(QPoint(820, 90) * coefficient);
  149. // ui->server_status_widget->resize(QSize(155, 320) * coefficient);
  150. // ui->weekly_code_widget->move(QPoint(810, 13) * coefficient);
  151. // ui->weekly_code_widget->resize(QSize(173, 57) * coefficient);
  152. // ui->galadriel_widget->move(QPoint(320, 20) * coefficient);
  153. // ui->galadriel_widget->resize(QSize(531, 461) * coefficient);
  154. // ui->news_tooltip->move(QPoint(38, 13) * coefficient);
  155. // ui->news_tooltip->resize(QSize(365, 114) * coefficient);
  156. // ui->weekly_code_tooltip_1->move(QPoint(38, 13) * coefficient);
  157. // ui->weekly_code_tooltip_1->resize(QSize(365, 114) * coefficient);
  158. // ui->weekly_code_tooltip_2->move(QPoint(38, 13) * coefficient);
  159. // ui->weekly_code_tooltip_2->resize(QSize(365, 114) * coefficient);
  160. // ui->server_status_tooltip->move(QPoint(38, 13) * coefficient);
  161. // ui->server_status_tooltip->resize(QSize(365, 114) * coefficient);
  162. // ui->patches_status->move(QPoint(38, 13) * coefficient);
  163. // ui->patches_status->resize(QSize(385, 114) * coefficient);
  164. updateFontsSizes();
  165. }
  166. void StatusWidget::generateRandomTooltipMessage()
  167. {
  168. quint32 number = QRandomGenerator::system()->generate();
  169. setToolTipMessage("СЛУЧАЙНОЕ СООБЩЕНИЕ №" + QString::number(number), E_RANDOM);
  170. }
  171. void StatusWidget::fadeBetweenToolTips(QString tooltip_id)
  172. {
  173. if (tooltip_id == current_tooltip_message_) {
  174. return;
  175. }
  176. QPropertyAnimation* showing_anim = tooltip_animations_[tooltip_id];
  177. showing_anim->setDirection(QAbstractAnimation::Forward);
  178. if (showing_anim->state() == QAbstractAnimation::Stopped) {
  179. showing_anim->start();
  180. }
  181. if (tooltip_widgets_.contains(current_tooltip_message_)) {
  182. QString msg = current_tooltip_message_;
  183. connect(tooltip_animations_[current_tooltip_message_], &QPropertyAnimation::finished, this, [&, msg](){
  184. if (tooltip_effects_.contains(msg) && tooltip_effects_[msg]->opacity() == 0) {
  185. tooltip_widgets_.take(msg)->deleteLater();
  186. tooltip_effects_.take(msg)->deleteLater();
  187. tooltip_animations_.take(msg)->deleteLater();
  188. }
  189. });
  190. QPropertyAnimation* hiding_anim = tooltip_animations_[current_tooltip_message_];
  191. hiding_anim->setDirection(QAbstractAnimation::Backward);
  192. if (hiding_anim->state() == QAbstractAnimation::Stopped) {
  193. hiding_anim->start();
  194. }
  195. }
  196. current_tooltip_message_ = tooltip_id;
  197. }
  198. void StatusWidget::onPatchDownloaderStarted() {
  199. all_patch_operations_finished_ = false;
  200. ui->game_button->setEnabled(false);
  201. ui->check_for_updates_button->setEnabled(false);
  202. setToolTipMessage("Проверка обновлений русификации...", E_PROCESS);
  203. }
  204. void StatusWidget::onPatchDownloaderFinished() {
  205. all_patch_operations_finished_ = true;
  206. ui->game_button->setEnabled(true);
  207. ui->check_for_updates_button->setEnabled(true);
  208. setToolTipMessage("Скачивание данных русификации завершено!", E_PROCESS);
  209. process_completed_tooltip_hide_timer_.start();
  210. }
  211. void StatusWidget::onPatchDownloaderProgressChanged(Downloader::Status status) {
  212. if (last_statusbar_update_time_.elapsed() > 450) {
  213. double download_percent = double(status.downloaded_bytes) * 100.0 / double(status.total_bytes);
  214. QString text = "Загрузка данных: " + QString::number(download_percent, 'f', 1) + "% ("
  215. + Downloader::getSizeFormatted(status.downloaded_bytes) + "/"
  216. + Downloader::getSizeFormatted(status.total_bytes) + ")\n"
  217. + "До конца загрузки: " + Downloader::getElapsedTimeFormatted(status.elapsed_time);
  218. setToolTipMessage(text, E_PROCESS);
  219. last_statusbar_update_time_.restart();
  220. }
  221. }
  222. void StatusWidget::onPatchInstallerStarted() {
  223. all_patch_operations_finished_ = false;
  224. ui->game_button->setEnabled(false);
  225. ui->check_for_updates_button->setEnabled(false);
  226. setToolTipMessage("Подготовка русификатора...", E_PROCESS);
  227. }
  228. void StatusWidget::onPatchInstallerFinished() {
  229. all_patch_operations_finished_ = true;
  230. ui->game_button->setEnabled(true);
  231. ui->check_for_updates_button->setEnabled(true);
  232. setToolTipMessage("Все операции выполнены!", E_PROCESS);
  233. process_completed_tooltip_hide_timer_.start();
  234. }
  235. void StatusWidget::onPatchInstallerProgressChanged(PatchInstaller::Status status) {
  236. if (last_statusbar_update_time_.elapsed() > 450 || status.finished_parts == 0) {
  237. QString text = "Установка русификации: "
  238. + QString::number(double(status.finished_parts) * 100.0 / double(status.total_parts), 'f', 1)
  239. + "%\n(" + QString::number(status.finished_parts)
  240. + " фрагментов из " + QString::number(status.total_parts) + ")";
  241. setToolTipMessage(text, E_PROCESS);
  242. last_statusbar_update_time_.restart();
  243. }
  244. }
  245. void StatusWidget::onPatchInstallerVideoProgressChanged(int finished_videos, int total_videos, Downloader::Status status) {
  246. if (last_statusbar_update_time_.elapsed() > 450) {
  247. double download_percent = double(status.downloaded_bytes) * 100.0 / double(status.total_bytes);
  248. QString text = "Загрузка и установка видеороликов ("
  249. + QString::number(finished_videos + 1) + " из " + QString::number(total_videos)
  250. + ")\n"
  251. + "Загружено: " + QString::number(download_percent, 'f', 1) + "% ("
  252. + Downloader::getSizeFormatted(status.downloaded_bytes) + "/"
  253. + Downloader::getSizeFormatted(status.total_bytes) + ")";
  254. setToolTipMessage(text, E_PROCESS);
  255. last_statusbar_update_time_.restart();
  256. }
  257. }
  258. void StatusWidget::on_game_button_clicked()
  259. {
  260. MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
  261. window->showChooseVersionDialog();
  262. }
  263. void StatusWidget::createTooltipMessageWidget(QString tooltip_id)
  264. {
  265. tooltip_widgets_[tooltip_id] = new QLabel(ui->galadriel_widget);
  266. tooltip_widgets_[tooltip_id]->setGeometry(ui->galadriel_tooltip_example->geometry());
  267. tooltip_widgets_[tooltip_id]->setStyleSheet(ui->galadriel_tooltip_example->styleSheet());
  268. tooltip_widgets_[tooltip_id]->setFont(ui->galadriel_tooltip_example->font());
  269. tooltip_widgets_[tooltip_id]->setWordWrap(true);
  270. tooltip_widgets_[tooltip_id]->setAlignment(Qt::AlignCenter);
  271. tooltip_widgets_[tooltip_id]->show();
  272. tooltip_effects_[tooltip_id] = new QGraphicsOpacityEffect(tooltip_widgets_[tooltip_id]);
  273. tooltip_effects_[tooltip_id]->setOpacity(0);
  274. tooltip_animations_[tooltip_id] = new QPropertyAnimation(tooltip_effects_[tooltip_id], "opacity");
  275. tooltip_animations_[tooltip_id]->setDuration(300);
  276. tooltip_animations_[tooltip_id]->setStartValue(0);
  277. tooltip_animations_[tooltip_id]->setEndValue(1);
  278. tooltip_animations_[tooltip_id]->setDirection(QAbstractAnimation::Forward);
  279. tooltip_widgets_[tooltip_id]->setGraphicsEffect(tooltip_effects_[tooltip_id]);
  280. }
  281. void StatusWidget::on_check_for_updates_button_clicked()
  282. {
  283. QMetaObject::invokeMethod(&PatchDownloader::instance(), &PatchDownloader::startPatchDownloaderChain, Qt::QueuedConnection);
  284. }