mainwindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "constants.h"
  4. #include "models/patchdownloader.h"
  5. #include "models/patchinstaller.h"
  6. #include "models/settings.h"
  7. #include <QWidget>
  8. #include <QBitmap>
  9. #include <QPainter>
  10. #include <QPixmap>
  11. #include <QDebug>
  12. #include <QThread>
  13. #include <QMouseEvent>
  14. #include <QMessageBox>
  15. #include <QDesktopWidget>
  16. #include <QtConcurrent/QtConcurrent>
  17. #include <QGraphicsBlurEffect>
  18. #include <QFont>
  19. #include <QFontDatabase>
  20. #include <QNetworkAccessManager>
  21. #include <QWinTaskbarProgress>
  22. #include <ui_statuswidget.h>
  23. namespace Ui {
  24. class StatusWidget;
  25. }
  26. MainWindow::MainWindow(QWidget *parent)
  27. : QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint)
  28. , ui(new Ui::MainWindow)
  29. {
  30. setAttribute(Qt::WA_DeleteOnClose, true);
  31. constructFonts();
  32. ui->setupUi(this);
  33. current_active_entry_ = ui->menuentry_1;
  34. current_hovered_entry_ = nullptr;
  35. qDebug() << __FUNCTION__ << "Initialising main frame...";
  36. status_widget_ = new StatusWidget(this);
  37. help_widget_ = new HelpWidget(this);
  38. about_widget_ = new AboutWidget(this);
  39. ui->content_layout->addWidget(status_widget_);
  40. ui->content_layout->addWidget(help_widget_);
  41. ui->content_layout->addWidget(about_widget_);
  42. hideAllContentWidgets();
  43. status_widget_->show();
  44. qDebug() << __FUNCTION__ << "Making background";
  45. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
  46. current_bg_id_ = qrand() % MAX_PIXMAP_ID + 1;
  47. QPixmap background(":/backgrounds/bg" + QString::number(current_bg_id_) + ".png");
  48. setupWindowBackgroundAndMask(background);
  49. setupMenuHoverWidget();
  50. qDebug() << __FUNCTION__ << "Initialising background updates";
  51. background_update_timer_.setInterval(30 * 1000);
  52. connect(&background_update_timer_, &QTimer::timeout, this, &MainWindow::randomChangeBackground);
  53. background_update_timer_.start();
  54. qDebug() << __FUNCTION__ << "Initialising main window connections";
  55. makeConnections();
  56. qDebug() << __FUNCTION__ << "Installing event filters to clickable objects";
  57. setEventFilterRecursive(this);
  58. qDebug() << __FUNCTION__ << "Resizing window due to Settings value";
  59. int window_scale_factor = Settings::getValue("General/UI_scale").toInt();;
  60. window_width = default_window_width * window_scale_factor / 100;
  61. window_height = default_window_height * window_scale_factor / 100;
  62. resize(window_width, window_height);
  63. show();
  64. qDebug() << __FUNCTION__ << "Initializing taskbar icon and progress...";
  65. _windows_taskbar_button = new QWinTaskbarButton(this);
  66. _windows_taskbar_button->setWindow(windowHandle());
  67. qDebug() << __FUNCTION__ << "Finished main frame initialisation";
  68. }
  69. MainWindow::~MainWindow()
  70. {
  71. background_update_timer_.stop();
  72. delete ui;
  73. }
  74. NewsListWidget *MainWindow::getNewsWidget()
  75. {
  76. return ui->news_list;
  77. }
  78. QScrollArea *MainWindow::getNewsWidgetOverlay()
  79. {
  80. return ui->news_scroll_area;
  81. }
  82. void MainWindow::updateFontSizes()
  83. {
  84. ui->menuentry_1->setFont(trajan_9pt);
  85. ui->menuentry_2->setFont(trajan_9pt);
  86. ui->menuentry_4->setFont(trajan_9pt);
  87. }
  88. void MainWindow::closeEvent(QCloseEvent *event)
  89. {
  90. if (show_warning_on_close) {
  91. auto result = QMessageBox::question(this, "Подтвердите действие", "Внимание! В настоящий момент выполняются процессы установки/обновления.\n"
  92. "Выход из приложения может привести к ошибкам и повреждению файлов игры.\n\n"
  93. "Вы уверены, что хотите прервать работу Наследия?");
  94. if (result != QMessageBox::Yes) {
  95. event->ignore();
  96. return;
  97. }
  98. }
  99. event->accept();
  100. }
  101. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  102. {
  103. if (event->buttons() & Qt::LeftButton) {
  104. move(event->globalPos() - drag_position_);
  105. event->accept();
  106. }
  107. }
  108. void MainWindow::mousePressEvent(QMouseEvent *event)
  109. {
  110. if (event->button() == Qt::LeftButton) {
  111. drag_position_ = event->globalPos() - frameGeometry().topLeft();
  112. event->accept();
  113. }
  114. }
  115. void MainWindow::resizeEvent(QResizeEvent * event)
  116. {
  117. if (event->size().width() == -1 || event->size().height() == -1)
  118. return;
  119. int width = event->size().width();
  120. int height = event->size().height();
  121. window_width = width;
  122. window_height = height;
  123. // window_width * dpi / (default_window_width * default_dpi)
  124. updateFonts(window_width / default_window_width);
  125. ui->menu_widget->move(width * 320 / default_window_width, height * 34 / default_window_height);
  126. ui->menu_widget->resize(width * 650 / default_window_width, height * 53 / default_window_height);
  127. ui->content_area->move(0, height * 110 / default_window_height);
  128. ui->content_area->resize(width * 1000 / default_window_width, height * 530 / default_window_height);
  129. setupWindowBackgroundAndMask(current_bg_);
  130. ui->closeButton->setMinimumSize(width * 20 / default_window_width, height * 20 / default_window_height);
  131. ui->minimizeButton->setMinimumSize(width * 20 / default_window_width, height * 20 / default_window_height);
  132. updateFontSizes();
  133. }
  134. bool MainWindow::eventFilter(QObject *, QEvent *event)
  135. {
  136. if (event->type() == QEvent::MouseButtonPress) {
  137. mousePressEvent((QMouseEvent*)(event));
  138. }
  139. if (event->type() == QEvent::MouseMove) {
  140. mouseMoveEvent((QMouseEvent*)(event));
  141. }
  142. return false;
  143. }
  144. void MainWindow::randomChangeBackground()
  145. {
  146. if (!qApp)
  147. return;
  148. qDebug() << __FUNCTION__ << "Starting background update";
  149. int next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  150. while (next_pixmap_id == current_bg_id_) {
  151. next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  152. }
  153. qDebug() << __FUNCTION__ << "Next background id = " << next_pixmap_id;
  154. QPixmap *cur_bg = new QPixmap(current_bg_);
  155. QPixmap *new_bg= new QPixmap(":/backgrounds/bg" + QString::number(next_pixmap_id) + ".png");
  156. current_bg_id_ = next_pixmap_id;
  157. QtConcurrent::run([cur_bg, new_bg, this](){
  158. const int iterations_num = 50;
  159. const int iteration_sleep = 40;
  160. const int starting_opacity_percent = 0;
  161. for (int i = 0; i < iterations_num - starting_opacity_percent && qApp; i++) {
  162. QPixmap composited_bg(*cur_bg);
  163. QPainter painter;
  164. painter.begin(&composited_bg);
  165. painter.setOpacity(double(starting_opacity_percent + i) / double(iterations_num));
  166. painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
  167. painter.drawPixmap(0,0, *new_bg);
  168. painter.end();
  169. QMetaObject::invokeMethod(this, "setupWindowBackgroundAndMask", Qt::QueuedConnection, Q_ARG(QPixmap, composited_bg));
  170. QThread::msleep(iteration_sleep);
  171. }
  172. delete cur_bg;
  173. delete new_bg;
  174. qDebug() << __FUNCTION__ << "Background update finished";
  175. });
  176. }
  177. void MainWindow::on_menuentry_1_clicked()
  178. {
  179. current_active_entry_ = ui->menuentry_1;
  180. hideAllContentWidgets();
  181. status_widget_->show();
  182. }
  183. void MainWindow::on_menuentry_2_clicked()
  184. {
  185. current_active_entry_ = ui->menuentry_2;
  186. hideAllContentWidgets();
  187. help_widget_->show();
  188. }
  189. void MainWindow::on_menuentry_4_clicked()
  190. {
  191. current_active_entry_ = ui->menuentry_4;
  192. hideAllContentWidgets();
  193. about_widget_->show();
  194. }
  195. void MainWindow::onHoverMenuentry(MenuEntry *hovered_entry)
  196. {
  197. if (current_hovered_entry_ != hovered_entry) {
  198. current_hovered_entry_ = hovered_entry;
  199. moveMenuHoverWidget(hovered_entry);
  200. }
  201. }
  202. void MainWindow::on_closeButton_clicked()
  203. {
  204. close();
  205. }
  206. void MainWindow::on_minimizeButton_clicked()
  207. {
  208. setWindowState(Qt::WindowMinimized);
  209. }
  210. void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
  211. {
  212. current_bg_ = background;
  213. QPixmap scaled_bg = current_bg_.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  214. current_mask_ = scaled_bg.mask();
  215. setMask(current_mask_);
  216. QPalette palette;
  217. palette.setBrush(QPalette::Window, scaled_bg);
  218. setPalette(palette);
  219. }
  220. void MainWindow::onPatchDownloaderStarted() {
  221. _windows_taskbar_button->progress()->setVisible(true);
  222. }
  223. void MainWindow::onPatchDownloaderFinished() {
  224. _windows_taskbar_button->progress()->setVisible(false);
  225. }
  226. void MainWindow::onPatchDownloaderProgressChanged(Downloader::Status status) {
  227. int download_percent = status.total_bytes > 0 ? status.downloaded_bytes * 100 / status.total_bytes : 100;
  228. _windows_taskbar_button->progress()->setValue(download_percent);
  229. }
  230. void MainWindow::onPatchInstallerStarted() {
  231. _windows_taskbar_button->progress()->setVisible(true);
  232. }
  233. void MainWindow::onPatchInstallerFinished() {
  234. _windows_taskbar_button->progress()->setVisible(false);
  235. }
  236. void MainWindow::onPatchInstallerProgressChanged(PatchInstaller::Status status) {
  237. int install_percent = status.total_parts > 0 ? status.finished_parts * 100 / status.total_parts : 100;
  238. _windows_taskbar_button->progress()->setValue(install_percent);
  239. }
  240. void MainWindow::onPatchInstallerVideoProgressChanged(int finished_videos, int total_videos, Downloader::Status) {
  241. int install_percent = total_videos > 0 ? finished_videos * 100 / total_videos : 100;
  242. _windows_taskbar_button->progress()->setValue(install_percent);
  243. }
  244. void MainWindow::makeConnections()
  245. {
  246. connect(&PatchInstaller::instance(), &PatchInstaller::started, this, [this](){show_warning_on_close = true;});
  247. connect(&PatchInstaller::instance(), &PatchInstaller::finished, this, [this](){show_warning_on_close = false;});
  248. connect(&PatchDownloader::instance(), &PatchDownloader::started, this, &MainWindow::onPatchDownloaderStarted);
  249. connect(&PatchDownloader::instance(), &PatchDownloader::progressChanged, this, &MainWindow::onPatchDownloaderProgressChanged);
  250. connect(&PatchDownloader::instance(), &PatchDownloader::finished, this, &MainWindow::onPatchDownloaderFinished);
  251. connect(&PatchInstaller::instance(), &PatchInstaller::started, this, &MainWindow::onPatchInstallerStarted);
  252. connect(&PatchInstaller::instance(), &PatchInstaller::progressChanged, this, &MainWindow::onPatchInstallerProgressChanged);
  253. connect(&PatchInstaller::instance(), &PatchInstaller::finished, this, &MainWindow::onPatchInstallerFinished);
  254. connect(&PatchInstaller::instance(), &PatchInstaller::videosDownloadProgressChanged, this, &MainWindow::onPatchInstallerVideoProgressChanged);
  255. }
  256. void MainWindow::setupMenuHoverWidget()
  257. {
  258. menu_hover_widget_ = new QWidget(ui->menu_widget);
  259. menu_hover_widget_->setStyleSheet("background-color: rgba(55, 37, 31, 230);");
  260. menu_hover_widget_->resize(0, 0);
  261. connect(ui->menuentry_1, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  262. connect(ui->menuentry_2, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  263. connect(ui->menuentry_4, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  264. current_active_entry_ = ui->menuentry_1;
  265. menu_hover_checker_timer_.setInterval(500);
  266. connect(&menu_hover_checker_timer_, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  267. menu_hover_checker_timer_.start();
  268. }
  269. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  270. {
  271. if (menu_hover_widget_->size() == QSize(0, 0)) {
  272. menu_hover_widget_->resize(target->size() + QSize(10, 0));
  273. menu_hover_widget_->move(target->pos() + QPoint(-5, 0));
  274. } else {
  275. if (menu_hover_widget_animation_ == nullptr)
  276. menu_hover_widget_animation_ = new QPropertyAnimation(menu_hover_widget_, "geometry");
  277. else
  278. menu_hover_widget_animation_->stop();
  279. menu_hover_widget_animation_->setDuration(200);
  280. menu_hover_widget_animation_->setStartValue(QRect(menu_hover_widget_->pos(), menu_hover_widget_->size()));
  281. menu_hover_widget_animation_->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  282. menu_hover_widget_animation_->start();
  283. }
  284. ui->menuentry_1->raise();
  285. ui->menuentry_2->raise();
  286. ui->menuentry_4->raise();
  287. }
  288. void MainWindow::checkMenuIsHovered()
  289. {
  290. QPoint pos = QCursor::pos();
  291. QWidget *hovered = qApp->widgetAt(pos);
  292. if (!hovered || hovered->objectName().size() < 4 ||
  293. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  294. moveMenuHoverWidget(current_active_entry_);
  295. current_hovered_entry_ = nullptr;
  296. }
  297. }
  298. void MainWindow::hideAllContentWidgets()
  299. {
  300. status_widget_->hide();
  301. help_widget_->hide();
  302. about_widget_->hide();
  303. }
  304. void MainWindow::setEventFilterRecursive(QObject *widget)
  305. {
  306. if (!widget) {
  307. return;
  308. }
  309. const QStringList exclude_widgets_list = {"QScrollBar"};
  310. if (exclude_widgets_list.indexOf(widget->metaObject()->className()) == -1) {
  311. widget->installEventFilter(this);
  312. }
  313. foreach (QObject* child, widget->children()) {
  314. setEventFilterRecursive(child);
  315. }
  316. }