mainwindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "models/patchdownloader.h"
  4. #include "models/lotrodatmanager.h"
  5. #include "widgets/chooseversiondialog.h"
  6. #include <QBitmap>
  7. #include <QPainter>
  8. #include <QPixmap>
  9. #include <QDebug>
  10. #include <QThread>
  11. #include <QMouseEvent>
  12. #include <QMessageBox>
  13. #include <QDesktopWidget>
  14. #include <QtConcurrent/QtConcurrent>
  15. #include <QGraphicsBlurEffect>
  16. MainWindow::MainWindow(QWidget *parent) :
  17. QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint),
  18. ui(new Ui::MainWindow), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  19. {
  20. setObjectName("ApplicationMainWindow");
  21. ui->setupUi(this);
  22. qDebug() << "Initialising Settings module";
  23. QSettings *settings = new QSettings(qApp->applicationDirPath() + "/legacy_v2.ini", QSettings::IniFormat);
  24. qDebug() << "Creating patch downloader instance & thread";
  25. lotro_functions_thread = new QThread();
  26. patch_updater = new PatchDownloader(settings);
  27. lotro_manager = new LotroDatManager(settings, patch_updater);
  28. QObject::connect(lotro_functions_thread, &QThread::finished, patch_updater, &QObject::deleteLater, Qt::QueuedConnection);
  29. QObject::connect(lotro_functions_thread, &QThread::finished, lotro_manager, &QObject::deleteLater, Qt::QueuedConnection);
  30. patch_updater->moveToThread(lotro_functions_thread);
  31. lotro_manager->moveToThread(lotro_functions_thread);
  32. lotro_functions_thread->start();
  33. qDebug() << "Initialising lotro manager...";
  34. QMetaObject::invokeMethod(lotro_manager, "InitialiseManager", Qt::QueuedConnection);
  35. qDebug() << "Starting check for patch updates...";
  36. QMetaObject::invokeMethod(patch_updater, "checkForUpdates", Qt::QueuedConnection);
  37. qDebug() << "Starting installation for patch updates...";
  38. QMetaObject::invokeMethod(lotro_manager, "installUpdates", Qt::QueuedConnection);
  39. status_widget = new StatusWidget(settings, patch_updater, lotro_manager, this);
  40. rusification_widget = new RusificationWidget(settings, patch_updater, lotro_manager, this);
  41. settings_widget = new SettingsWidget(settings, patch_updater, lotro_manager, this);
  42. help_widget = new HelpWidget(settings, patch_updater, lotro_manager, this);
  43. choose_locale_dialog = new ChooseVersionDialog(settings, patch_updater, lotro_manager, this);
  44. choose_locale_dialog->resize(size());
  45. choose_locale_dialog->hide();
  46. connect(choose_locale_dialog, &ChooseVersionDialog::cancelled, this, &MainWindow::hideChooseVersionDialog);
  47. ui->content_layout->addWidget(status_widget);
  48. ui->content_layout->addWidget(rusification_widget);
  49. ui->content_layout->addWidget(settings_widget);
  50. ui->content_layout->addWidget(help_widget);
  51. ui->centralWidget->setStyleSheet("");
  52. hideAllContentWidgets();
  53. status_widget->show();
  54. qDebug() << "Making background";
  55. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
  56. current_bg_id = qrand() % MAX_PIXMAP_ID + 1;
  57. QPixmap background(":/backgrounds/bg" + QString::number(current_bg_id) + ".png");
  58. setupWindowBackgroundAndMask(background);
  59. setupMenuHoverWidget();
  60. updateGeometry();
  61. repaint();
  62. qDebug() << "Initialising background updates";
  63. background_update_timer.setInterval(30 * 1000);
  64. connect(&background_update_timer, &QTimer::timeout, this, &MainWindow::randomChangeBackground);
  65. background_update_timer.start();
  66. randomChangeBackground();
  67. qDebug() << "Initialising main window connections";
  68. makeConnections();
  69. qDebug() << "Installing event filters to clickable objects";
  70. setEventFilterRecursive(this);
  71. qDebug() << "Adopting elements and fonts to window size";
  72. resize(size());
  73. qDebug() << "Finishing main frame initialisation";
  74. show();
  75. }
  76. void MainWindow::mousePressEvent(QMouseEvent *event)
  77. {
  78. if (event->button() == Qt::LeftButton) {
  79. dragPosition = event->globalPos() - frameGeometry().topLeft();
  80. event->accept();
  81. }
  82. }
  83. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  84. {
  85. if (event->buttons() & Qt::LeftButton) {
  86. move(event->globalPos() - dragPosition);
  87. // choose_locale_dialog->move(this->rect().center() - choose_locale_dialog->rect().center());
  88. event->accept();
  89. }
  90. }
  91. void MainWindow::resizeEvent(QResizeEvent * event)
  92. {
  93. int width = event->size().width();
  94. int height = event->size().height();
  95. ui->menu_widget->move(width * 320 / 1000, height * 34 / 648);
  96. ui->menu_widget->resize(width * 650 / 1000, height * 53 / 648);
  97. ui->content_area->move(0, height * 110 / 648);
  98. ui->content_area->resize(width * 1000 / 1000, height * 520 / 648);
  99. setupWindowBackgroundAndMask(current_bg);
  100. choose_locale_dialog->resize(size());
  101. changeFontSizeRecursive(100, this);
  102. }
  103. void MainWindow::randomChangeBackground()
  104. {
  105. if (!qApp)
  106. return;
  107. qDebug() << "Starting background update";
  108. int next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  109. while (next_pixmap_id == current_bg_id) {
  110. next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  111. }
  112. qDebug() << "Next background id = " << next_pixmap_id;
  113. QPixmap *cur_bg = new QPixmap(current_bg);
  114. QPixmap *new_bg= new QPixmap(":/backgrounds/bg" + QString::number(next_pixmap_id) + ".png");
  115. current_bg_id = next_pixmap_id;
  116. QtConcurrent::run([cur_bg, new_bg, this](){
  117. const int iterations_num = 100;
  118. const int iteration_sleep = 17;
  119. const int starting_opacity_percent = 0;
  120. for (int i = 0; i < iterations_num - starting_opacity_percent && qApp; i++) {
  121. QPixmap composited_bg(*cur_bg);
  122. QPainter painter;
  123. painter.begin(&composited_bg);
  124. painter.setOpacity(double(starting_opacity_percent + i) / double(iterations_num));
  125. painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
  126. painter.drawPixmap(0,0, *new_bg);
  127. painter.end();
  128. QMetaObject::invokeMethod(this, "setupWindowBackgroundAndMask", Qt::QueuedConnection, Q_ARG(QPixmap, composited_bg));
  129. QThread::msleep(iteration_sleep);
  130. }
  131. delete cur_bg;
  132. delete new_bg;
  133. qDebug() << "Background update finished";
  134. });
  135. }
  136. MainWindow::~MainWindow()
  137. {
  138. delete ui;
  139. }
  140. void MainWindow::on_menuentry_1_clicked()
  141. {
  142. hideAllContentWidgets();
  143. status_widget->show();
  144. }
  145. void MainWindow::on_menuentry_2_clicked()
  146. {
  147. hideAllContentWidgets();
  148. settings_widget->show();
  149. }
  150. void MainWindow::on_menuentry_3_clicked()
  151. {
  152. hideAllContentWidgets();
  153. rusification_widget->show();
  154. }
  155. void MainWindow::on_menuentry_4_clicked()
  156. {
  157. hideAllContentWidgets();
  158. help_widget->show();
  159. }
  160. void MainWindow::onHoverMenuentry()
  161. {
  162. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  163. }
  164. void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
  165. {
  166. if (!qApp)
  167. return;
  168. current_bg = background;
  169. QPixmap scaled_bg = current_bg.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  170. current_mask = scaled_bg.mask();
  171. setMask(current_mask);
  172. QPalette palette;
  173. palette.setBrush(QPalette::Window, scaled_bg);
  174. setPalette(palette);
  175. }
  176. void MainWindow::makeConnections()
  177. {
  178. }
  179. void MainWindow::setupMenuHoverWidget()
  180. {
  181. menuHoverWidget = new QWidget(ui->menu_widget);
  182. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  183. menuHoverWidget->resize(0, 0);
  184. connect(ui->menuentry_1, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  185. connect(ui->menuentry_2, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  186. connect(ui->menuentry_3, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  187. connect(ui->menuentry_4, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  188. MenuEntry::setActiveLabel(ui->menuentry_1);
  189. menu_hover_checker_timer.setInterval(500);
  190. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  191. menu_hover_checker_timer.start();
  192. }
  193. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  194. {
  195. if (menuHoverWidget->size() == QSize(0, 0)) {
  196. menuHoverWidget->resize(target->size() + QSize(10, 0));
  197. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  198. } else {
  199. if (menuHoverWidgetAnimation == nullptr)
  200. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  201. else
  202. menuHoverWidgetAnimation->stop();
  203. menuHoverWidgetAnimation->setDuration(200);
  204. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  205. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  206. menuHoverWidgetAnimation->start();
  207. }
  208. ui->menuentry_1->raise();
  209. ui->menuentry_2->raise();
  210. ui->menuentry_3->raise();
  211. ui->menuentry_4->raise();
  212. }
  213. void MainWindow::checkMenuIsHovered()
  214. {
  215. QPoint pos = QCursor::pos();
  216. QWidget *hovered = qApp->widgetAt(pos);
  217. if (!hovered || hovered->objectName().size() < 4 ||
  218. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  219. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  220. MenuEntry::setHoverLabel(nullptr);
  221. }
  222. }
  223. void MainWindow::hideAllContentWidgets()
  224. {
  225. status_widget->hide();
  226. rusification_widget->hide();
  227. settings_widget->hide();
  228. help_widget->hide();
  229. }
  230. void MainWindow::changeFontSizeRecursive(size_t percent, QWidget *widget)
  231. {
  232. if (!widget)
  233. return;
  234. QFont widget_font = widget->font();
  235. QString widget_name = widget->objectName();
  236. if (widget_name.contains("menuentry"))
  237. widget_font.setPixelSize(menuentry_font_size * percent / 100);
  238. if (widget_name.contains("_small"))
  239. widget_font.setPixelSize(small_font_size * percent / 100);
  240. if (widget_name.contains("_common"))
  241. widget_font.setPixelSize(common_font_size * percent / 100);
  242. if (widget_name.contains("_title"))
  243. widget_font.setPixelSize(title_font_size * percent / 100);
  244. if (widget_name.contains("_supertitle"))
  245. widget_font.setPixelSize(supertitle_font_size * percent / 100);
  246. if (widget_name.contains("_bigbutton"))
  247. widget_font.setPixelSize(bigbutton_font_size * percent / 100);
  248. widget->setFont(widget_font);
  249. for (QObject* child : widget->children())
  250. if (child->isWidgetType()) {
  251. QWidget* w = qobject_cast<QWidget *>(child);
  252. changeFontSizeRecursive(percent, w);
  253. }
  254. }
  255. void MainWindow::showChooseVersionDialog()
  256. {
  257. QGraphicsBlurEffect *effect = new QGraphicsBlurEffect();
  258. effect->setBlurRadius(10);
  259. effect->setBlurHints(QGraphicsBlurEffect::QualityHint);
  260. ui->content_area->setGraphicsEffect(effect);
  261. choose_locale_dialog->show();
  262. }
  263. void MainWindow::hideChooseVersionDialog()
  264. {
  265. ui->content_area->setGraphicsEffect(nullptr);
  266. choose_locale_dialog->hide();
  267. }
  268. void MainWindow::on_closeButton_clicked()
  269. {
  270. hide();
  271. qApp->quit();
  272. }
  273. void MainWindow::on_minimizeButton_clicked()
  274. {
  275. setWindowState(Qt::WindowMinimized);
  276. }
  277. void MainWindow::setEventFilterRecursive(QObject *widget)
  278. {
  279. if (!widget)
  280. return;
  281. QStringList classes_to_set = {
  282. "QPushButton",
  283. "QCheckBox",
  284. "QComboBox"
  285. };
  286. if (classes_to_set.contains(widget->metaObject()->className())) {
  287. widget->installEventFilter(this);
  288. }
  289. foreach (QObject* child, widget->children()) {
  290. setEventFilterRecursive(child);
  291. }
  292. }
  293. bool MainWindow::eventFilter(QObject *watched, QEvent *event)
  294. {
  295. if (event->type() == QEvent::MouseButtonPress) {
  296. mousePressEvent((QMouseEvent*)(event));
  297. }
  298. if (event->type() == QEvent::MouseMove) {
  299. mouseMoveEvent((QMouseEvent*)(event));
  300. }
  301. return false;
  302. }