mainwindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "models/lotromanager.h"
  4. #include <QBitmap>
  5. #include <QPainter>
  6. #include <QPixmap>
  7. #include <QDebug>
  8. #include <QThread>
  9. #include <QMouseEvent>
  10. #include <QMessageBox>
  11. #include <QDesktopWidget>
  12. #include <QtConcurrent/QtConcurrent>
  13. MainWindow::MainWindow(QWidget *parent) :
  14. QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint),
  15. ui(new Ui::MainWindow), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  16. {
  17. ui->setupUi(this);
  18. qDebug() << "Initialising Settings module";
  19. QSettings *settings = new QSettings(qApp->applicationDirPath() + "/legacy_v2.ini", QSettings::IniFormat);
  20. qDebug() << "Creating lotro manager instance & thread";
  21. lotro_mgr_thread = new QThread();
  22. lotro_manager = new LotroManager(settings);
  23. QObject::connect(lotro_mgr_thread, &QThread::finished, lotro_manager, &QObject::deleteLater, Qt::QueuedConnection);
  24. lotro_manager->moveToThread(lotro_mgr_thread);
  25. lotro_mgr_thread->start();
  26. qDebug() << "Creating widgets";
  27. qDebug() << "1";
  28. browse_widget = new BrowseWidget(lotro_manager, settings, this);
  29. qDebug() << "2";
  30. import_widget = new ImportWidget(lotro_manager, settings, this);
  31. qDebug() << "3";
  32. export_widget = new ExportWidget(lotro_manager, settings, this);
  33. qDebug() << "4";
  34. manage_widget = new ManageWidget(lotro_manager, settings, this);
  35. connect(manage_widget, &ManageWidget::disableMenuItems, this, &MainWindow::disableMenuItems);
  36. connect(manage_widget, &ManageWidget::enableMenuItems, this, &MainWindow::enableMenuItems);
  37. qDebug() << "5";
  38. lotro_progress_widget = new LotroProgressWidget(lotro_manager, settings, this);
  39. qDebug() << "Done. Adding to layouts";
  40. ui->content_layout->addWidget(export_widget);
  41. ui->content_layout->addWidget(import_widget);
  42. ui->content_layout->addWidget(manage_widget);
  43. ui->content_layout->addWidget(browse_widget);
  44. ui->content_layout->addWidget(lotro_progress_widget);
  45. lotro_progress_widget->hide();
  46. ui->centralWidget->setStyleSheet("");
  47. hideAllContentWidgets();
  48. manage_widget->show();
  49. qDebug() << "Making fonts and making background";
  50. changeFontSizeRecursive(100, this);
  51. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
  52. current_bg_id = qrand() % MAX_PIXMAP_ID + 1;
  53. QPixmap background(":/backgrounds/bg" + QString::number(current_bg_id) + ".png");
  54. setupWindowBackgroundAndMask(background);
  55. setupMenuHoverWidget();
  56. updateGeometry();
  57. repaint();
  58. qDebug() << "Initialising background updates";
  59. background_update_timer.setInterval(30 * 1000);
  60. connect(&background_update_timer, &QTimer::timeout, this, &MainWindow::randomChangeBackground);
  61. background_update_timer.start();
  62. qDebug() << "Initialising main window connections";
  63. makeConnections();
  64. qDebug() << "Finishing main frame initialisation";
  65. show();
  66. qDebug() << "Starting dat file initialisation";
  67. QMetaObject::invokeMethod(lotro_manager, "initialiseDatFile", Qt::QueuedConnection,
  68. Q_ARG(QString, settings->value("advanced/dat_path").toString()));
  69. }
  70. void MainWindow::mousePressEvent(QMouseEvent *event)
  71. {
  72. if (event->button() == Qt::LeftButton) {
  73. dragPosition = event->globalPos() - frameGeometry().topLeft();
  74. event->accept();
  75. }
  76. }
  77. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  78. {
  79. if (event->buttons() & Qt::LeftButton) {
  80. move(event->globalPos() - dragPosition);
  81. // app->process_window->move(event->globalPos() - dragPosition +
  82. // QPoint((width() - app->process_window->width()) / 2, (height() - app->process_window->height()) / 2)
  83. // );
  84. event->accept();
  85. }
  86. }
  87. void MainWindow::resizeEvent(QResizeEvent * event)
  88. {
  89. int width = event->size().width();
  90. int height = event->size().height();
  91. ui->menu_widget->move(width * 675 / 2000, height * 80 / 1296);
  92. ui->menu_widget->resize(width * 1260 / 2000, height * 90 / 1296);
  93. ui->content_area->move(width * 25 / 2000, height * 250 / 1296);
  94. ui->content_area->resize(width * 1950 / 2000, height * 1000 / 1296);
  95. setupWindowBackgroundAndMask(current_bg);
  96. }
  97. void MainWindow::enableMenuItems()
  98. {
  99. ui->menuentry_1_common->setEnabled(true);
  100. ui->menuentry_2_common->setEnabled(true);
  101. ui->menuentry_3_common->setEnabled(true);
  102. ui->menuentry_4_common->setEnabled(true);
  103. }
  104. void MainWindow::disableMenuItems()
  105. {
  106. ui->menuentry_1_common->setEnabled(false);
  107. ui->menuentry_2_common->setEnabled(false);
  108. ui->menuentry_3_common->setEnabled(false);
  109. ui->menuentry_4_common->setEnabled(false);
  110. }
  111. void MainWindow::randomChangeBackground()
  112. {
  113. if (!qApp)
  114. return;
  115. qDebug() << "Starting background update";
  116. int next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  117. while (next_pixmap_id == current_bg_id) {
  118. next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  119. }
  120. qDebug() << "Next background id = " << next_pixmap_id;
  121. QPixmap *cur_bg = new QPixmap(current_bg.scaled(size()));
  122. QPixmap *new_bg= new QPixmap(QPixmap(":/backgrounds/bg" + QString::number(next_pixmap_id) + ".png").scaled(size()));
  123. current_bg_id = next_pixmap_id;
  124. QtConcurrent::run([cur_bg, new_bg, this](){
  125. const int iterations_num = 150;
  126. const int iteration_sleep = 75;
  127. for (int i = 0; i < iterations_num && qApp; i++) {
  128. QPainter painter;
  129. painter.begin(cur_bg);
  130. painter.setOpacity(double(10 + i) / double(iterations_num));
  131. painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
  132. painter.drawPixmap(0,0, *new_bg);
  133. painter.end();
  134. QMetaObject::invokeMethod(this, "setupWindowBackgroundAndMask", Qt::QueuedConnection, Q_ARG(QPixmap, *cur_bg));
  135. QThread::msleep(iteration_sleep);
  136. }
  137. delete cur_bg;
  138. delete new_bg;
  139. qDebug() << "Background update finished";
  140. });
  141. }
  142. MainWindow::~MainWindow()
  143. {
  144. delete ui;
  145. }
  146. void MainWindow::on_menuentry_1_common_clicked()
  147. {
  148. hideAllContentWidgets();
  149. manage_widget->show();
  150. }
  151. void MainWindow::on_menuentry_2_common_clicked()
  152. {
  153. hideAllContentWidgets();
  154. export_widget->show();
  155. }
  156. void MainWindow::on_menuentry_3_common_clicked()
  157. {
  158. hideAllContentWidgets();
  159. import_widget->show();
  160. }
  161. void MainWindow::on_menuentry_4_common_clicked()
  162. {
  163. hideAllContentWidgets();
  164. browse_widget->show();
  165. }
  166. void MainWindow::onHoverMenuentry()
  167. {
  168. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  169. }
  170. void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
  171. {
  172. if (!qApp)
  173. return;
  174. current_bg = background.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  175. current_mask = current_bg.mask();
  176. setMask(current_mask);
  177. QPalette palette;
  178. palette.setBrush(QPalette::Window, current_bg);
  179. setPalette(palette);
  180. }
  181. void MainWindow::onLotroManagerStarted(QString, QVector<QVariant>)
  182. {
  183. hideAllContentWidgets();
  184. lotro_progress_widget->updateUI();
  185. lotro_progress_widget->show();
  186. disableMenuItems();
  187. }
  188. void MainWindow::onLotroManagerFinished(QString, QVector<QVariant>)
  189. {
  190. hideAllContentWidgets();
  191. lotro_progress_widget->hide();
  192. emit MenuEntry::getActiveLabel()->clicked();
  193. if (lotro_manager->initialised())
  194. enableMenuItems();
  195. }
  196. void MainWindow::onLotroManagerErrorOccured(QString, QVector<QVariant>) {
  197. QMessageBox::warning(nullptr, "Ошибка ядра Наследия!", "Случилась ошибка! Всё очень плохо! (надо добавить информацию об ошибке)");
  198. }
  199. void MainWindow::makeConnections()
  200. {
  201. connect(lotro_manager, &LotroManager::processStarted, this, &MainWindow::onLotroManagerStarted, Qt::QueuedConnection);
  202. connect(lotro_manager, &LotroManager::processFinished, this, &MainWindow::onLotroManagerFinished, Qt::QueuedConnection);
  203. connect(lotro_manager, &LotroManager::caughtError, this, &MainWindow::onLotroManagerErrorOccured, Qt::QueuedConnection);
  204. }
  205. void MainWindow::setupMenuHoverWidget()
  206. {
  207. menuHoverWidget = new QWidget(ui->menu_widget);
  208. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  209. menuHoverWidget->resize(0, 0);
  210. connect(ui->menuentry_1_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  211. connect(ui->menuentry_2_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  212. connect(ui->menuentry_3_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  213. connect(ui->menuentry_4_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  214. MenuEntry::setActiveLabel(ui->menuentry_1_common);
  215. menu_hover_checker_timer.setInterval(500);
  216. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  217. menu_hover_checker_timer.start();
  218. }
  219. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  220. {
  221. if (menuHoverWidget->size() == QSize(0, 0)) {
  222. menuHoverWidget->resize(target->size() + QSize(10, 0));
  223. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  224. } else {
  225. if (menuHoverWidgetAnimation == nullptr)
  226. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  227. else
  228. menuHoverWidgetAnimation->stop();
  229. menuHoverWidgetAnimation->setDuration(200);
  230. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  231. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  232. menuHoverWidgetAnimation->start();
  233. }
  234. ui->menuentry_1_common->raise();
  235. ui->menuentry_2_common->raise();
  236. ui->menuentry_3_common->raise();
  237. ui->menuentry_4_common->raise();
  238. }
  239. void MainWindow::checkMenuIsHovered()
  240. {
  241. QPoint pos = QCursor::pos();
  242. QWidget *hovered = qApp->widgetAt(pos);
  243. if (!hovered || hovered->objectName().size() < 4 ||
  244. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  245. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  246. MenuEntry::setHoverLabel(nullptr);
  247. }
  248. }
  249. void MainWindow::hideAllContentWidgets()
  250. {
  251. export_widget->hide();
  252. import_widget->hide();
  253. manage_widget->hide();
  254. browse_widget->hide();
  255. }
  256. void MainWindow::changeFontSizeRecursive(size_t percent, QWidget *widget)
  257. {
  258. if (!widget)
  259. return;
  260. QFont widget_font = widget->font();
  261. QString widget_name = widget->objectName();
  262. if (widget_name.contains("_common"))
  263. widget_font.setPixelSize(common_font_size * percent / 100);
  264. if (widget_name.contains("_title"))
  265. widget_font.setPixelSize(title_font_size * percent / 100);
  266. if (widget_name.contains("_supertitle"))
  267. widget_font.setPixelSize(supertitle_font_size * percent / 100);
  268. if (widget_name.contains("_bigbutton"))
  269. widget_font.setPixelSize(bigbutton_font_size * percent / 100);
  270. widget->setFont(widget_font);
  271. for (QObject* child : widget->children())
  272. if (child->isWidgetType()) {
  273. QWidget* w = qobject_cast<QWidget *>(child);
  274. changeFontSizeRecursive(percent, w);
  275. w->resize(w->sizeHint());
  276. }
  277. }
  278. void MainWindow::on_closeButton_clicked()
  279. {
  280. hide();
  281. qApp->quit();
  282. }
  283. void MainWindow::on_minimizeButton_clicked()
  284. {
  285. setWindowState(Qt::WindowMinimized);
  286. }