statuswidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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::onPatchTotalProgressChanged);
  29. foreach (Patch* patch, legacy_patches_->getPatchList()) {
  30. connect(patch, &Patch::progressChanged, this, &StatusWidget::onPatchProgressChanged);
  31. connect(patch, &Patch::operationStarted, this, &StatusWidget::onPatchOperationStarted);
  32. connect(patch, &Patch::operationFinished, this, &StatusWidget::onPatchOperationFinished);
  33. }
  34. generateRandomTooltipMessage();
  35. random_tooltip_generator_timer_.setInterval(5 * 1000);
  36. connect(&random_tooltip_generator_timer_, &QTimer::timeout, this, &StatusWidget::generateRandomTooltipMessage);
  37. random_tooltip_generator_timer_.start();
  38. }
  39. StatusWidget::~StatusWidget()
  40. {
  41. delete ui;
  42. }
  43. void StatusWidget::updateFontsSizes()
  44. {
  45. ui->game_button->setFont(trajan_11pt);
  46. ui->news_label->setFont(trajan_10pt);
  47. }
  48. void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState state)
  49. {
  50. tooltip_messages_[state] = message;
  51. tooltip_state_ = ToolTipState(int(tooltip_state_) | int(state));
  52. qDebug() << "New tooltip state: " << int(tooltip_state_);
  53. if (tooltip_state_ < state * 2) {
  54. qDebug() << "Fading between tooltips";
  55. fadeBetweenToolTips(message);
  56. }
  57. }
  58. void StatusWidget::unsetToolTipMessage(StatusWidget::ToolTipState state)
  59. {
  60. tooltip_state_ = ToolTipState(int(tooltip_state_) & (~int(state)));
  61. qDebug() << "New tooltip state: " << int(tooltip_state_);
  62. if (tooltip_state_ & E_INFO) {
  63. fadeBetweenToolTips(tooltip_messages_[E_INFO]);
  64. } else if (tooltip_state_ & E_ERROR) {
  65. fadeBetweenToolTips(tooltip_messages_[E_ERROR]);
  66. } else if (tooltip_state_ & E_PROCESS) {
  67. fadeBetweenToolTips(tooltip_messages_[E_PROCESS]);
  68. } else {
  69. fadeBetweenToolTips(tooltip_messages_[E_RANDOM]);
  70. }
  71. }
  72. void StatusWidget::resizeEvent(QResizeEvent *)
  73. {
  74. double coefficient = window_width / default_window_width;
  75. // ui->game_button->move(QPoint(820, 460) * coefficient);
  76. // ui->game_button->resize(QSize(150, 60) * coefficient);
  77. // ui->progressBar->move(QPoint(320, 480) * coefficient);
  78. // ui->progressBar->resize(QSize(470, 40) * coefficient);
  79. // ui->progress_label->move(QPoint(330, 390) * coefficient);
  80. // ui->progress_label->resize(QSize(380, 90) * coefficient);
  81. // ui->news_label->move(QPoint(45, 33)* coefficient);
  82. // ui->news_label->resize(QSize(180, 21) * coefficient);
  83. // ui->news_scroll_area->move(QPoint(40, 75) * coefficient);
  84. // ui->news_scroll_area->resize(QSize(240, 440) * coefficient);
  85. // ui->server_status_widget->move(QPoint(820, 90) * coefficient);
  86. // ui->server_status_widget->resize(QSize(155, 320) * coefficient);
  87. // ui->weekly_code_widget->move(QPoint(810, 13) * coefficient);
  88. // ui->weekly_code_widget->resize(QSize(173, 57) * coefficient);
  89. // ui->galadriel_widget->move(QPoint(320, 20) * coefficient);
  90. // ui->galadriel_widget->resize(QSize(531, 461) * coefficient);
  91. // ui->news_tooltip->move(QPoint(38, 13) * coefficient);
  92. // ui->news_tooltip->resize(QSize(365, 114) * coefficient);
  93. // ui->weekly_code_tooltip_1->move(QPoint(38, 13) * coefficient);
  94. // ui->weekly_code_tooltip_1->resize(QSize(365, 114) * coefficient);
  95. // ui->weekly_code_tooltip_2->move(QPoint(38, 13) * coefficient);
  96. // ui->weekly_code_tooltip_2->resize(QSize(365, 114) * coefficient);
  97. // ui->server_status_tooltip->move(QPoint(38, 13) * coefficient);
  98. // ui->server_status_tooltip->resize(QSize(365, 114) * coefficient);
  99. // ui->patches_status->move(QPoint(38, 13) * coefficient);
  100. // ui->patches_status->resize(QSize(385, 114) * coefficient);
  101. updateFontsSizes();
  102. }
  103. void StatusWidget::generateRandomTooltipMessage()
  104. {
  105. quint32 number = QRandomGenerator::system()->generate();
  106. setToolTipMessage("СЛУЧАЙНОЕ СООБЩЕНИЕ №" + QString::number(number), E_RANDOM);
  107. }
  108. void StatusWidget::fadeBetweenToolTips(QString next_tooltip_message)
  109. {
  110. if (next_tooltip_message == current_tooltip_message_) {
  111. return;
  112. }
  113. if (!tooltip_widgets_.contains(next_tooltip_message)) {
  114. createTooltipMessageWidget(next_tooltip_message);
  115. }
  116. QPropertyAnimation* showing_anim = tooltip_animations_[next_tooltip_message];
  117. showing_anim->setDirection(QAbstractAnimation::Forward);
  118. if (showing_anim->state() == QAbstractAnimation::Stopped)
  119. showing_anim->start();
  120. if (tooltip_widgets_.contains(current_tooltip_message_)) {
  121. QString msg = current_tooltip_message_;
  122. connect(tooltip_animations_[current_tooltip_message_], &QPropertyAnimation::finished, this, [&, msg](){
  123. if (tooltip_effects_.contains(msg) && tooltip_effects_[msg]->opacity() == 0) {
  124. qDebug() << "Deleting " << msg;
  125. tooltip_widgets_.take(msg)->deleteLater();
  126. tooltip_effects_.take(msg)->deleteLater();
  127. tooltip_animations_.take(msg)->deleteLater();
  128. }
  129. });
  130. QPropertyAnimation* hiding_anim = tooltip_animations_[current_tooltip_message_];
  131. hiding_anim->setDirection(QAbstractAnimation::Backward);
  132. if (hiding_anim->state() == QAbstractAnimation::Stopped)
  133. hiding_anim->start();
  134. }
  135. current_tooltip_message_ = next_tooltip_message;
  136. }
  137. void StatusWidget::onPatchTotalOperationsStarted()
  138. {
  139. all_patch_operations_finished_ = false;
  140. ui->game_button->setEnabled(false);
  141. ui->check_for_updates_button->setEnabled(false);
  142. }
  143. void StatusWidget::onPatchTotalOperationsFinished()
  144. {
  145. all_patch_operations_finished_ = true;
  146. ui->game_button->setEnabled(true);
  147. ui->check_for_updates_button->setEnabled(true);
  148. // for (Patch* patch : legacy_patches_->getPatchList()) {
  149. // QString label_name = patch->getPatchName().toLower() + "_status";
  150. // QLabel *label = findChild<QLabel*>(label_name);
  151. // if (!label) {
  152. // return;
  153. // }
  154. // QString install_date;
  155. // if (!(install_date = Settings::getValue("UpdateDates/" + patch->getPatchName()).toString()).isEmpty()) {
  156. // label->setText("Патч версии от " + install_date);
  157. // } else {
  158. // label->setText("Все операции с патчем завершены.");
  159. // }
  160. // }
  161. // ui->progress_label->setText("");
  162. }
  163. void StatusWidget::onPatchOperationStarted(Patch::Operation operation, Patch *patch)
  164. {
  165. // QString label_name = patch->getPatchName().toLower() + "_status";
  166. // QLabel *label = findChild<QLabel*>(label_name);
  167. // patch_operations[patch] = operation;
  168. // if (!label) {
  169. // return;
  170. // }
  171. // switch (operation) {
  172. // case Patch::E_CHECKFORUPDATES:
  173. // label->setText("Проверка наличия обновлений...");
  174. // break;
  175. // case Patch::E_DOWNLOAD:
  176. // label->setText("Подготовка к загрузке данных...");
  177. // break;
  178. // case Patch::E_INSTALL:
  179. // label->setText("Ожидание начала установки...");
  180. // break;
  181. // case Patch::E_ACTIVATE:
  182. // label->setText("Ожидание начала активации...");
  183. // default:
  184. // break;
  185. // }
  186. }
  187. void StatusWidget::onPatchOperationFinished(Patch::Operation operation, Patch *patch)
  188. {
  189. // if (all_patch_operations_finished_) {
  190. // return;
  191. // }
  192. // QString label_name = patch->getPatchName().toLower() + "_status";
  193. // QLabel *label = findChild<QLabel*>(label_name);
  194. // if (!label) {
  195. // return;
  196. // }
  197. // switch (operation) {
  198. // case Patch::E_CHECKFORUPDATES:
  199. // label->setText("Проверка обновлений завершена");
  200. // break;
  201. // case Patch::E_DOWNLOAD:
  202. // label->setText("Загрузка данных завершена");
  203. // break;
  204. // case Patch::E_INSTALL:
  205. // label->setText("Установка патча завершена");
  206. // break;
  207. // case Patch::E_ACTIVATE:
  208. // label->setText("Активация патча завершена");
  209. // default:
  210. // break;
  211. // }
  212. }
  213. void StatusWidget::onPatchTotalProgressChanged(Patch::OperationProgress operation_progress)
  214. {
  215. // updateStatusBar(operation_progress);
  216. }
  217. void StatusWidget::on_game_button_clicked()
  218. {
  219. MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
  220. window->showChooseVersionDialog();
  221. }
  222. void StatusWidget::onPatchProgressChanged(Patch::OperationProgress progress, Patch *patch)
  223. {
  224. // QString label_name = patch->getPatchName().toLower() + "_status";
  225. // QLabel *label = findChild<QLabel*>(label_name);
  226. // if (!label) {
  227. // return;
  228. // }
  229. // switch (patch_operations[patch]) {
  230. // case Patch::E_CHECKFORUPDATES:
  231. // label->setText("Проверка наличия обновлений...");
  232. // break;
  233. // case Patch::E_DOWNLOAD:
  234. // label->setText("Загрузка... " + QString::number(progress.getDownloadPercent(), 'f', 1)
  235. // + "% (" + Downloader::getSizeFormatted(progress.download_finished_bytes)
  236. // + "/" + Downloader::getSizeFormatted(progress.download_total_bytes) + ")");
  237. // break;
  238. // case Patch::E_INSTALL:
  239. // label->setText("Установка... " + QString::number(progress.getInstallPercent(), 'f', 2)
  240. // + "% (" + QString::number(progress.install_finished_parts) + "/" + QString::number(progress.install_total_parts) + ")");
  241. // break;
  242. // case Patch::E_ACTIVATE:
  243. // label->setText("Активация... " + QString::number(progress.getInstallPercent(), 'f', 2)
  244. // + "% (" + QString::number(progress.install_finished_parts) + "/" + QString::number(progress.install_total_parts) + ")");
  245. // default:
  246. // break;
  247. // }
  248. }
  249. void StatusWidget::updateStatusBar(Patch::OperationProgress progress)
  250. {
  251. // if (last_statusbar_update_time_.elapsed() > 500) {
  252. // QString text = "Выполнение операций...";
  253. // if (progress.download_total_bytes != 0) {
  254. // text += "\nЗагрузка данных: " + QString::number(progress.getDownloadPercent(), 'f', 1) + "% ("
  255. // + Downloader::getSizeFormatted(progress.download_finished_bytes) + "/"
  256. // + Downloader::getSizeFormatted(progress.download_total_bytes) + ", "
  257. // + Downloader::getSpeedFormatted(progress.download_speed) + ")\n"
  258. // + "До конца загрузки: " + Downloader::getElapsedTimeFormatted(progress.download_elapsed_time);
  259. // }
  260. // if (progress.install_total_parts != 0) {
  261. // text += "\nПрименение патчей: " + QString::number(progress.getInstallPercent()) + "% "
  262. // + "(часть " + QString::number(progress.install_finished_parts + 1) + " из " + QString::number(progress.install_total_parts);
  263. // }
  264. // ui->progress_label->setText(text);
  265. // last_statusbar_update_time_.restart();
  266. // }
  267. }
  268. void StatusWidget::createTooltipMessageWidget(QString tooltip_message)
  269. {
  270. qDebug() << "Creating:" << tooltip_message;
  271. tooltip_widgets_[tooltip_message] = new QLabel(ui->galadriel_widget);
  272. tooltip_widgets_[tooltip_message]->setText(tooltip_message);
  273. tooltip_widgets_[tooltip_message]->setGeometry(ui->galadriel_tooltip_example->geometry());
  274. tooltip_widgets_[tooltip_message]->setStyleSheet(ui->galadriel_tooltip_example->styleSheet());
  275. tooltip_widgets_[tooltip_message]->setFont(ui->galadriel_tooltip_example->font());
  276. tooltip_widgets_[tooltip_message]->setWordWrap(true);
  277. tooltip_widgets_[tooltip_message]->setAlignment(Qt::AlignCenter);
  278. tooltip_widgets_[tooltip_message]->show();
  279. tooltip_effects_[tooltip_message] = new QGraphicsOpacityEffect(tooltip_widgets_[tooltip_message]);
  280. tooltip_effects_[tooltip_message]->setOpacity(0);
  281. tooltip_animations_[tooltip_message] = new QPropertyAnimation(tooltip_effects_[tooltip_message], "opacity");
  282. tooltip_animations_[tooltip_message]->setDuration(350);
  283. tooltip_animations_[tooltip_message]->setStartValue(0);
  284. tooltip_animations_[tooltip_message]->setEndValue(1);
  285. tooltip_animations_[tooltip_message]->setDirection(QAbstractAnimation::Forward);
  286. tooltip_widgets_[tooltip_message]->setGraphicsEffect(tooltip_effects_[tooltip_message]);
  287. }
  288. void StatusWidget::on_check_for_updates_button_clicked()
  289. {
  290. QMetaObject::invokeMethod(legacy_patches_, &PatchList::update, Qt::QueuedConnection);
  291. }