mainwindow.cpp 11 KB

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