mainwindow.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QBitmap>
  4. #include <QMouseEvent>
  5. #include <QMessageBox>
  6. #include <QDesktopWidget>
  7. MainWindow::MainWindow(LegacyApp *app, QWidget *parent) :
  8. QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint), app(app),
  9. ui(new Ui::MainWindow), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  10. {}
  11. void MainWindow::Init() {
  12. ui->setupUi(this);
  13. status_frame = new StatusWidget(app, this);
  14. ui->content_layout->addWidget(status_frame);
  15. rusification_frame = new RusificationWidget(app, this);
  16. ui->content_layout->addWidget(rusification_frame);
  17. settings_frame = new SettingsWidget(app, this);
  18. ui->content_layout->addWidget(settings_frame);
  19. news_frame = new NewsWidget(app, this);
  20. ui->content_layout->addWidget(news_frame);
  21. help_frame = new HelpWidget(app, this);
  22. ui->content_layout->addWidget(help_frame);
  23. hideAllContentWidgets();
  24. status_frame->show();
  25. setupWindowBackgroundAndMask(1);
  26. setupMenuHoverWidget();
  27. changeFontSizeRecursive(100, this);
  28. updateGeometry();
  29. show();
  30. }
  31. void MainWindow::mousePressEvent(QMouseEvent *event)
  32. {
  33. if (event->button() == Qt::LeftButton) {
  34. dragPosition = event->globalPos() - frameGeometry().topLeft();
  35. event->accept();
  36. }
  37. }
  38. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  39. {
  40. if (event->buttons() & Qt::LeftButton) {
  41. move(event->globalPos() - dragPosition);
  42. event->accept();
  43. }
  44. }
  45. void MainWindow::resizeEvent(QResizeEvent * /* event */)
  46. {
  47. QPixmap maskPix(":/assets/bg1.png");
  48. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
  49. setMask(maskPix.mask());
  50. }
  51. MainWindow::~MainWindow()
  52. {
  53. delete ui;
  54. }
  55. void MainWindow::on_menuentry_1_common_clicked()
  56. {
  57. hideAllContentWidgets();
  58. status_frame->show();
  59. }
  60. void MainWindow::on_menuentry_2_common_clicked()
  61. {
  62. hideAllContentWidgets();
  63. settings_frame->show();
  64. }
  65. void MainWindow::on_menuentry_3_common_clicked()
  66. {
  67. hideAllContentWidgets();
  68. rusification_frame->show();
  69. }
  70. void MainWindow::on_menuentry_4_common_clicked()
  71. {
  72. hideAllContentWidgets();
  73. news_frame->show();
  74. }
  75. void MainWindow::on_menuentry_5_common_clicked()
  76. {
  77. hideAllContentWidgets();
  78. help_frame->show();
  79. }
  80. void MainWindow::onHoverMenuentry()
  81. {
  82. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  83. }
  84. void MainWindow::setupWindowBackgroundAndMask(int bg_id)
  85. {
  86. QPixmap maskPix(":/assets/bg" + QString::number(bg_id) + ".png");
  87. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
  88. setMask(maskPix.mask());
  89. QPalette palette;
  90. palette.setBrush(QPalette::Window, maskPix);
  91. setPalette(palette);
  92. }
  93. void MainWindow::setupMenuHoverWidget()
  94. {
  95. menuHoverWidget = new QWidget(ui->menu_widget);
  96. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  97. menuHoverWidget->resize(0, 0);
  98. connect(ui->menuentry_1_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  99. connect(ui->menuentry_2_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  100. connect(ui->menuentry_3_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  101. connect(ui->menuentry_4_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  102. connect(ui->menuentry_5_common, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  103. MenuEntry::setActiveLabel(ui->menuentry_1_common);
  104. menu_hover_checker_timer.setInterval(500);
  105. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  106. menu_hover_checker_timer.start();
  107. }
  108. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  109. {
  110. if (menuHoverWidget->size() == QSize(0, 0)) {
  111. menuHoverWidget->resize(target->size() + QSize(10, 0));
  112. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  113. } else {
  114. if (menuHoverWidgetAnimation == nullptr)
  115. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  116. else
  117. menuHoverWidgetAnimation->stop();
  118. menuHoverWidgetAnimation->setDuration(200);
  119. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  120. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  121. menuHoverWidgetAnimation->start();
  122. }
  123. ui->menuentry_1_common->raise();
  124. ui->menuentry_2_common->raise();
  125. ui->menuentry_3_common->raise();
  126. ui->menuentry_4_common->raise();
  127. ui->menuentry_5_common->raise();
  128. }
  129. void MainWindow::checkMenuIsHovered()
  130. {
  131. QPoint pos = QCursor::pos();
  132. QWidget *hovered = qApp->widgetAt(pos);
  133. if (!hovered || hovered->objectName().size() < 4 ||
  134. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  135. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  136. MenuEntry::setHoverLabel(nullptr);
  137. }
  138. }
  139. void MainWindow::hideAllContentWidgets()
  140. {
  141. status_frame->hide();
  142. rusification_frame->hide();
  143. settings_frame->hide();
  144. news_frame->hide();
  145. help_frame->hide();
  146. }
  147. void MainWindow::changeFontSizeRecursive(size_t percent, QWidget *widget)
  148. {
  149. if (!widget)
  150. return;
  151. QFont widget_font = widget->font();
  152. QString widget_name = widget->objectName();
  153. if (widget_name.contains("_common"))
  154. widget_font.setPixelSize(common_font_size * percent / 100);
  155. if (widget_name.contains("_title"))
  156. widget_font.setPixelSize(title_font_size * percent / 100);
  157. if (widget_name.contains("_supertitle"))
  158. widget_font.setPixelSize(supertitle_font_size * percent / 100);
  159. if (widget_name.contains("_bigbutton"))
  160. widget_font.setPixelSize(bigbutton_font_size * percent / 100);
  161. widget->setFont(widget_font);
  162. for (QObject* child : widget->children())
  163. if (child->isWidgetType()) {
  164. QWidget* w = qobject_cast<QWidget *>(child);
  165. changeFontSizeRecursive(percent, w);
  166. w->resize(w->sizeHint());
  167. }
  168. }
  169. void MainWindow::on_closeButton_clicked()
  170. {
  171. qApp->quit();
  172. }
  173. void MainWindow::on_minimizeButton_clicked()
  174. {
  175. setWindowState(Qt::WindowMinimized);
  176. }