mainwindow.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "legacyapp.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. #include <QFontDatabase>
  14. MainWindow::MainWindow(LegacyApp *app, QWidget *parent) :
  15. QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint), app(app),
  16. ui(new Ui::MainWindow), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  17. {
  18. }
  19. void MainWindow::Init() {
  20. ui->setupUi(this);
  21. QResource::registerResource(QApplication::applicationDirPath() + "/res/data009.gtr");
  22. QFontDatabase::addApplicationFont(":/assets/fonts/trajan.ttf");
  23. QFontDatabase::addApplicationFont(":/assets/fonts/viking.ttf");
  24. status_frame = new StatusWidget(app, this);
  25. ui->content_layout->addWidget(status_frame);
  26. rusification_frame = new RusificationWidget(app, this);
  27. ui->content_layout->addWidget(rusification_frame);
  28. settings_frame = new SettingsWidget(app, this);
  29. ui->content_layout->addWidget(settings_frame);
  30. news_frame = new NewsWidget(app, this);
  31. ui->content_layout->addWidget(news_frame);
  32. help_frame = new HelpWidget(app, this);
  33. ui->content_layout->addWidget(help_frame);
  34. hideAllContentWidgets();
  35. status_frame->show();
  36. changeFontSizeRecursive(100, this);
  37. updateGeometry();
  38. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
  39. background = new QPixmap(":/assets/backgrounds/bg" + QString::number(qrand() % MAX_PIXMAP_ID + 1) + ".png");
  40. setupWindowBackgroundAndMask();
  41. setupMenuHoverWidget();
  42. background_update_timer.setInterval(30 * 1000);
  43. connect(&background_update_timer, &QTimer::timeout, this, &MainWindow::randomChangeBackground);
  44. background_update_timer.start();
  45. ui->centralWidget->setStyleSheet("");
  46. show();
  47. }
  48. void MainWindow::mousePressEvent(QMouseEvent *event)
  49. {
  50. if (event->button() == Qt::LeftButton) {
  51. dragPosition = event->globalPos() - frameGeometry().topLeft();
  52. event->accept();
  53. }
  54. }
  55. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  56. {
  57. if (event->buttons() & Qt::LeftButton) {
  58. move(event->globalPos() - dragPosition);
  59. event->accept();
  60. }
  61. }
  62. void MainWindow::resizeEvent(QResizeEvent * event)
  63. {
  64. int width = event->size().width();
  65. int height = event->size().height();
  66. ui->menu_widget->move(width * 675 / 2000, height * 80 / 1296);
  67. ui->menu_widget->resize(width * 1260 / 2000, height * 90 / 1296);
  68. ui->content_area->move(width * 25 / 2000, height * 250 / 1296);
  69. ui->content_area->resize(width * 1950 / 2000, height * 1000 / 1296);
  70. setupWindowBackgroundAndMask();
  71. }
  72. void MainWindow::randomChangeBackground()
  73. {
  74. if (!qApp)
  75. return;
  76. qDebug() << "Starting background update";
  77. if (fade_animation_timer.isActive()) {
  78. qDebug() << "MainWindow::startBackgroundUpdate() - cannot start, because update is still active";
  79. return;
  80. }
  81. next_pixmap_opacity = 0;
  82. int next_pixmap_id = qrand() % MAX_PIXMAP_ID + 1;
  83. if (!next_pixmap)
  84. next_pixmap = new QPixmap();
  85. next_pixmap->load(":/assets/backgrounds/bg" + QString::number(next_pixmap_id) + ".png");
  86. qDebug() << "Next pixmap id" << next_pixmap_id << "!";
  87. if (next_pixmap->isNull()) {
  88. qDebug() << "Incorrect pixmap id " << next_pixmap_id << "!";
  89. return;
  90. }
  91. QtConcurrent::run([this](){
  92. qDebug() << "Starting background update";
  93. while (next_pixmap_opacity < 1) {
  94. if (!qApp)
  95. return;
  96. QPainter painter;
  97. painter.begin(background);
  98. painter.setOpacity(next_pixmap_opacity);
  99. painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
  100. painter.drawPixmap(0,0, *next_pixmap);
  101. painter.end();
  102. setupWindowBackgroundAndMask();
  103. next_pixmap_opacity += 0.005;
  104. QThread::msleep(50);
  105. }
  106. qDebug() << "Background update finished";
  107. });
  108. }
  109. MainWindow::~MainWindow()
  110. {
  111. delete ui;
  112. }
  113. void MainWindow::on_menuentry_1_common_clicked()
  114. {
  115. hideAllContentWidgets();
  116. status_frame->show();
  117. }
  118. void MainWindow::on_menuentry_2_common_clicked()
  119. {
  120. hideAllContentWidgets();
  121. settings_frame->show();
  122. }
  123. void MainWindow::on_menuentry_3_common_clicked()
  124. {
  125. hideAllContentWidgets();
  126. rusification_frame->show();
  127. }
  128. void MainWindow::on_menuentry_4_common_clicked()
  129. {
  130. hideAllContentWidgets();
  131. news_frame->show();
  132. }
  133. void MainWindow::on_menuentry_5_common_clicked()
  134. {
  135. hideAllContentWidgets();
  136. help_frame->show();
  137. }
  138. void MainWindow::onHoverMenuentry()
  139. {
  140. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  141. }
  142. void MainWindow::setupWindowBackgroundAndMask()
  143. {
  144. QPixmap maskPix = background->scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  145. setMask(maskPix.mask());
  146. QPalette palette;
  147. palette.setBrush(QPalette::Window, maskPix);
  148. setPalette(palette);
  149. }
  150. void MainWindow::setupMenuHoverWidget()
  151. {
  152. menuHoverWidget = new QWidget(ui->menu_widget);
  153. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  154. menuHoverWidget->resize(0, 0);
  155. connect(ui->menuentry_1_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  156. connect(ui->menuentry_2_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  157. connect(ui->menuentry_3_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  158. connect(ui->menuentry_4_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  159. connect(ui->menuentry_5_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  160. MenuEntry::setActiveLabel(ui->menuentry_1_common);
  161. menu_hover_checker_timer.setInterval(500);
  162. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  163. menu_hover_checker_timer.start();
  164. }
  165. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  166. {
  167. if (menuHoverWidget->size() == QSize(0, 0)) {
  168. menuHoverWidget->resize(target->size() + QSize(10, 0));
  169. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  170. } else {
  171. if (menuHoverWidgetAnimation == nullptr)
  172. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  173. else
  174. menuHoverWidgetAnimation->stop();
  175. menuHoverWidgetAnimation->setDuration(200);
  176. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  177. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  178. menuHoverWidgetAnimation->start();
  179. }
  180. ui->menuentry_1_common->raise();
  181. ui->menuentry_2_common->raise();
  182. ui->menuentry_3_common->raise();
  183. ui->menuentry_4_common->raise();
  184. ui->menuentry_5_common->raise();
  185. }
  186. void MainWindow::checkMenuIsHovered()
  187. {
  188. QPoint pos = QCursor::pos();
  189. QWidget *hovered = qApp->widgetAt(pos);
  190. if (!hovered || hovered->objectName().size() < 4 ||
  191. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  192. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  193. MenuEntry::setHoverLabel(nullptr);
  194. }
  195. }
  196. void MainWindow::hideAllContentWidgets()
  197. {
  198. status_frame->hide();
  199. rusification_frame->hide();
  200. settings_frame->hide();
  201. news_frame->hide();
  202. help_frame->hide();
  203. }
  204. void MainWindow::changeFontSizeRecursive(size_t percent, QWidget *widget)
  205. {
  206. if (!widget)
  207. return;
  208. QFont widget_font = widget->font();
  209. QString widget_name = widget->objectName();
  210. if (widget_name.contains("_common"))
  211. widget_font.setPixelSize(common_font_size * percent / 100);
  212. if (widget_name.contains("_title"))
  213. widget_font.setPixelSize(title_font_size * percent / 100);
  214. if (widget_name.contains("_supertitle"))
  215. widget_font.setPixelSize(supertitle_font_size * percent / 100);
  216. if (widget_name.contains("_bigbutton"))
  217. widget_font.setPixelSize(bigbutton_font_size * percent / 100);
  218. widget->setFont(widget_font);
  219. for (QObject* child : widget->children())
  220. if (child->isWidgetType()) {
  221. QWidget* w = qobject_cast<QWidget *>(child);
  222. changeFontSizeRecursive(percent, w);
  223. w->resize(w->sizeHint());
  224. }
  225. }
  226. void MainWindow::on_closeButton_clicked()
  227. {
  228. hide();
  229. qApp->quit();
  230. }
  231. void MainWindow::on_minimizeButton_clicked()
  232. {
  233. setWindowState(Qt::WindowMinimized);
  234. }