mainwindow.cpp 10 KB

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