mainwindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "constants.h"
  4. #include "models/patchdownloader.h"
  5. #include "models/lotrodatmanager.h"
  6. #include "widgets/chooseversiondialog.h"
  7. #include "widgets/dialogwindow.h"
  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), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  28. {
  29. constructFonts();
  30. ui->setupUi(this);
  31. currently_active_entry = ui->menuentry_1;
  32. currently_hover_entry = nullptr;
  33. qDebug() << "Creating patch downloader instance & thread";
  34. lotro_functions_thread = new QThread();
  35. patch_updater = new PatchDownloader();
  36. lotro_manager = new LotroDatManager();
  37. // connect(patch_updater, &PatchDownloader::checkForUpdatesStarted, this, &MainWindow::onPatchDownloaderStarted, Qt::BlockingQueuedConnection);
  38. // connect(patch_updater, &PatchDownloader::checkForUpdatesFinished, this, &MainWindow::onPatchDownloaderFinished, Qt::BlockingQueuedConnection);
  39. // connect(lotro_manager, &LotroDatManager::processStarted, this, &MainWindow::onLotroManagerStarted, Qt::BlockingQueuedConnection);
  40. // connect(lotro_manager, &LotroDatManager::processFinished, this, &MainWindow::onLotroManagerFinished, Qt::BlockingQueuedConnection);
  41. // connect(lotro_manager, &LotroDatManager::caughtError, this, &MainWindow::onLotroManagerErrorOccured, Qt::BlockingQueuedConnection);
  42. connect(lotro_functions_thread, &QThread::finished, patch_updater, &QObject::deleteLater, Qt::QueuedConnection);
  43. connect(lotro_functions_thread, &QThread::finished, lotro_manager, &QObject::deleteLater, Qt::QueuedConnection);
  44. patch_updater->moveToThread(lotro_functions_thread);
  45. lotro_manager->moveToThread(lotro_functions_thread);
  46. lotro_functions_thread->start();
  47. qDebug() << "Initialising lotro manager...";
  48. QMetaObject::invokeMethod(lotro_manager, "InitialiseManager", Qt::QueuedConnection);
  49. status_widget = new StatusWidget(patch_updater, lotro_manager, this);
  50. settings_widget = new SettingsWidget(patch_updater, lotro_manager, this);
  51. help_widget = new HelpWidget(patch_updater, lotro_manager, this);
  52. about_widget = new AboutWidget(patch_updater, lotro_manager, this);
  53. choose_locale_dialog = new ChooseVersionDialog(patch_updater, lotro_manager, this);
  54. choose_locale_dialog->resize(size());
  55. choose_locale_dialog->hide();
  56. connect(choose_locale_dialog, &ChooseVersionDialog::cancelled, this, &MainWindow::hideChooseVersionDialog);
  57. dialog_window = new DialogWindow(this);
  58. dialog_window->resize(size());
  59. ui->content_layout->addWidget(status_widget);
  60. ui->content_layout->addWidget(settings_widget);
  61. ui->content_layout->addWidget(help_widget);
  62. ui->content_layout->addWidget(about_widget);
  63. hideAllContentWidgets();
  64. status_widget->show();
  65. qDebug() << "Making background";
  66. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
  67. current_bg_id = qrand() % MAX_PIXMAP_ID + 1;
  68. QPixmap background(":/backgrounds/bg" + QString::number(current_bg_id) + ".png");
  69. setupWindowBackgroundAndMask(background);
  70. setupMenuHoverWidget();
  71. qDebug() << "Initialising background updates";
  72. background_update_timer.setInterval(30 * 1000);
  73. connect(&background_update_timer, &QTimer::timeout, this, &MainWindow::randomChangeBackground);
  74. background_update_timer.start();
  75. qDebug() << "Initialising main window connections";
  76. makeConnections();
  77. qDebug() << "Installing event filters to clickable objects";
  78. setEventFilterRecursive(this);
  79. qDebug() << "Finishing main frame initialisation";
  80. show();
  81. QMetaObject::invokeMethod(patch_updater, "checkForUpdates");
  82. }
  83. void MainWindow::mousePressEvent(QMouseEvent *event)
  84. {
  85. if (event->button() == Qt::LeftButton) {
  86. dragPosition = event->globalPos() - frameGeometry().topLeft();
  87. event->accept();
  88. }
  89. }
  90. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  91. {
  92. if (event->buttons() & Qt::LeftButton) {
  93. move(event->globalPos() - dragPosition);
  94. event->accept();
  95. }
  96. }
  97. void MainWindow::resizeEvent(QResizeEvent * event)
  98. {
  99. if (event->size().width() == -1 || event->size().height() == -1)
  100. return;
  101. int width = event->size().width();
  102. int height = event->size().height();
  103. window_width = width;
  104. window_height = height;
  105. // window_width * dpi / (default_window_width * default_dpi)
  106. updateFonts(window_width / default_window_width);
  107. ui->menu_widget->move(width * 320 / default_window_width, height * 34 / default_window_height);
  108. ui->menu_widget->resize(width * 650 / default_window_width, height * 53 / default_window_height);
  109. ui->content_area->move(0, height * 110 / default_window_height);
  110. ui->content_area->resize(width * 1000 / default_window_width, height * 538 / default_window_height);
  111. setupWindowBackgroundAndMask(current_bg);
  112. choose_locale_dialog->resize(event->size());
  113. ui->closeButton->setMinimumSize(width * 20 / default_window_width, height * 20 / default_window_height);
  114. ui->minimizeButton->setMinimumSize(width * 20 / default_window_width, height * 20 / default_window_height);
  115. choose_locale_dialog->move({0, 0});
  116. choose_locale_dialog->resize(event->size());
  117. dialog_window->move({0, 0});
  118. dialog_window->resize(size());
  119. updateFontSizes();
  120. }
  121. void MainWindow::randomChangeBackground()
  122. {
  123. if (!qApp)
  124. return;
  125. qDebug() << "Starting background update";
  126. int next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  127. while (next_pixmap_id == current_bg_id) {
  128. next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  129. }
  130. qDebug() << "Next background id = " << next_pixmap_id;
  131. QPixmap *cur_bg = new QPixmap(current_bg);
  132. QPixmap *new_bg= new QPixmap(":/backgrounds/bg" + QString::number(next_pixmap_id) + ".png");
  133. current_bg_id = next_pixmap_id;
  134. QtConcurrent::run([cur_bg, new_bg, this](){
  135. const int iterations_num = 50;
  136. const int iteration_sleep = 40;
  137. const int starting_opacity_percent = 0;
  138. for (int i = 0; i < iterations_num - starting_opacity_percent && qApp; i++) {
  139. QPixmap composited_bg(*cur_bg);
  140. QPainter painter;
  141. painter.begin(&composited_bg);
  142. painter.setOpacity(double(starting_opacity_percent + i) / double(iterations_num));
  143. painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
  144. painter.drawPixmap(0,0, *new_bg);
  145. painter.end();
  146. QMetaObject::invokeMethod(this, "setupWindowBackgroundAndMask", Qt::QueuedConnection, Q_ARG(QPixmap, composited_bg));
  147. QThread::msleep(iteration_sleep);
  148. }
  149. delete cur_bg;
  150. delete new_bg;
  151. qDebug() << "Background update finished";
  152. });
  153. }
  154. MainWindow::~MainWindow()
  155. {
  156. delete ui;
  157. }
  158. void MainWindow::on_menuentry_1_clicked()
  159. {
  160. currently_active_entry = ui->menuentry_1;
  161. hideAllContentWidgets();
  162. status_widget->show();
  163. }
  164. void MainWindow::on_menuentry_2_clicked()
  165. {
  166. currently_active_entry = ui->menuentry_2;
  167. hideAllContentWidgets();
  168. settings_widget->show();
  169. }
  170. void MainWindow::on_menuentry_3_clicked()
  171. {
  172. currently_active_entry = ui->menuentry_3;
  173. hideAllContentWidgets();
  174. help_widget->show();
  175. }
  176. void MainWindow::on_menuentry_4_clicked()
  177. {
  178. currently_active_entry = ui->menuentry_4;
  179. hideAllContentWidgets();
  180. about_widget->show();
  181. }
  182. void MainWindow::onHoverMenuentry(MenuEntry *hovered_entry)
  183. {
  184. if (currently_hover_entry != hovered_entry) {
  185. currently_hover_entry = hovered_entry;
  186. moveMenuHoverWidget(hovered_entry);
  187. }
  188. }
  189. void MainWindow::onPatchDownloaderStarted() {
  190. qDebug() << "Patch downloader started!";
  191. }
  192. void MainWindow::onPatchDownloaderFinished() {
  193. qDebug() << "Patch downloader finished!";
  194. // if (lotro_manager->Initialised()) {
  195. // QMetaObject::invokeMethod(lotro_manager, "InstallUpdates", Qt::QueuedConnection);
  196. // }
  197. return;
  198. }
  199. void MainWindow::onLotroManagerStarted(QString operation, QVector<QVariant> data) {
  200. qDebug() << "LotroManager: Started operation " << operation << " with parameters " << data;
  201. return;
  202. }
  203. void MainWindow::onLotroManagerFinished(QString operation, QVector<QVariant> data) {
  204. qDebug() << "LotroManager: Finished operation " << operation << " with parameters " << data;
  205. // if (operation == "")
  206. return;
  207. }
  208. void MainWindow::onLotroManagerErrorOccured(QString, QVector<QVariant>) {
  209. }
  210. void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
  211. {
  212. if (!qApp)
  213. return;
  214. current_bg = background;
  215. QPixmap scaled_bg = current_bg.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  216. current_mask = scaled_bg.mask();
  217. setMask(current_mask);
  218. QPalette palette;
  219. palette.setBrush(QPalette::Window, scaled_bg);
  220. setPalette(palette);
  221. }
  222. void MainWindow::makeConnections()
  223. {
  224. }
  225. void MainWindow::setupMenuHoverWidget()
  226. {
  227. menuHoverWidget = new QWidget(ui->menu_widget);
  228. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 230);");
  229. menuHoverWidget->resize(0, 0);
  230. connect(ui->menuentry_1, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  231. connect(ui->menuentry_2, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  232. connect(ui->menuentry_3, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  233. connect(ui->menuentry_4, &MenuEntry::entryIsHovered, this, &MainWindow::onHoverMenuentry);
  234. currently_active_entry = ui->menuentry_1;
  235. menu_hover_checker_timer.setInterval(500);
  236. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  237. menu_hover_checker_timer.start();
  238. }
  239. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  240. {
  241. if (menuHoverWidget->size() == QSize(0, 0)) {
  242. menuHoverWidget->resize(target->size() + QSize(10, 0));
  243. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  244. } else {
  245. if (menuHoverWidgetAnimation == nullptr)
  246. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  247. else
  248. menuHoverWidgetAnimation->stop();
  249. menuHoverWidgetAnimation->setDuration(200);
  250. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  251. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  252. menuHoverWidgetAnimation->start();
  253. }
  254. ui->menuentry_1->raise();
  255. ui->menuentry_2->raise();
  256. ui->menuentry_3->raise();
  257. ui->menuentry_4->raise();
  258. }
  259. void MainWindow::checkMenuIsHovered()
  260. {
  261. QPoint pos = QCursor::pos();
  262. QWidget *hovered = qApp->widgetAt(pos);
  263. if (!hovered || hovered->objectName().size() < 4 ||
  264. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  265. moveMenuHoverWidget(currently_active_entry);
  266. currently_hover_entry = nullptr;
  267. }
  268. }
  269. void MainWindow::hideAllContentWidgets()
  270. {
  271. status_widget->hide();
  272. settings_widget->hide();
  273. help_widget->hide();
  274. about_widget->hide();
  275. }
  276. void MainWindow::showChooseVersionDialog()
  277. {
  278. // QGraphicsBlurEffect *effect = new QGraphicsBlurEffect();
  279. // effect->setBlurRadius(10);
  280. // effect->setBlurHints(QGraphicsBlurEffect::QualityHint);
  281. // ui->content_area->setGraphicsEffect(effect);
  282. choose_locale_dialog->show();
  283. }
  284. void MainWindow::hideChooseVersionDialog()
  285. {
  286. // ui->content_area->setGraphicsEffect(nullptr);
  287. choose_locale_dialog->hide();
  288. }
  289. void MainWindow::updateFontSizes()
  290. {
  291. ui->menuentry_1->setFont(trajan_10pt);
  292. ui->menuentry_2->setFont(trajan_10pt);
  293. ui->menuentry_3->setFont(trajan_10pt);
  294. ui->menuentry_4->setFont(trajan_10pt);
  295. }
  296. void MainWindow::on_closeButton_clicked()
  297. {
  298. hide();
  299. qApp->quit();
  300. }
  301. void MainWindow::on_minimizeButton_clicked()
  302. {
  303. setWindowState(Qt::WindowMinimized);
  304. }
  305. void MainWindow::setEventFilterRecursive(QObject *widget)
  306. {
  307. if (!widget)
  308. return;
  309. QStringList classes_to_set = {
  310. "QPushButton",
  311. "QCheckBox",
  312. "QComboBox"
  313. };
  314. if (classes_to_set.contains(widget->metaObject()->className())) {
  315. widget->installEventFilter(this);
  316. }
  317. foreach (QObject* child, widget->children()) {
  318. setEventFilterRecursive(child);
  319. }
  320. }
  321. bool MainWindow::eventFilter(QObject *watched, QEvent *event)
  322. {
  323. if (event->type() == QEvent::MouseButtonPress) {
  324. mousePressEvent((QMouseEvent*)(event));
  325. }
  326. if (event->type() == QEvent::MouseMove) {
  327. mouseMoveEvent((QMouseEvent*)(event));
  328. }
  329. return false;
  330. }