mainwindow.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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(this);
  14. ui->content_layout->addWidget(status_frame);
  15. rusification_frame = new RusificationWidget(this);
  16. ui->content_layout->addWidget(rusification_frame);
  17. hideAllContentWidgets();
  18. status_frame->show();
  19. setupWindowBackgroundAndMask(1);
  20. setupMenuHoverWidget();
  21. show();
  22. }
  23. void MainWindow::mousePressEvent(QMouseEvent *event)
  24. {
  25. if (event->button() == Qt::LeftButton) {
  26. dragPosition = event->globalPos() - frameGeometry().topLeft();
  27. event->accept();
  28. }
  29. }
  30. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  31. {
  32. if (event->buttons() & Qt::LeftButton) {
  33. move(event->globalPos() - dragPosition);
  34. event->accept();
  35. }
  36. }
  37. void MainWindow::resizeEvent(QResizeEvent * /* event */)
  38. {
  39. QPixmap maskPix(":/assets/bg1.png");
  40. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
  41. setMask(maskPix.mask());
  42. }
  43. MainWindow::~MainWindow()
  44. {
  45. delete ui;
  46. }
  47. void MainWindow::on_menuentry_1_clicked()
  48. {
  49. hideAllContentWidgets();
  50. status_frame->show();
  51. }
  52. void MainWindow::on_menuentry_2_clicked()
  53. {
  54. hideAllContentWidgets();
  55. }
  56. void MainWindow::on_menuentry_3_clicked()
  57. {
  58. hideAllContentWidgets();
  59. rusification_frame->show();
  60. }
  61. void MainWindow::on_menuentry_4_clicked()
  62. {
  63. hideAllContentWidgets();
  64. }
  65. void MainWindow::on_menuentry_5_clicked()
  66. {
  67. hideAllContentWidgets();
  68. }
  69. void MainWindow::onHoverMenuentry()
  70. {
  71. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  72. }
  73. void MainWindow::setupWindowBackgroundAndMask(int bg_id)
  74. {
  75. QPixmap maskPix(":/assets/bg" + QString::number(bg_id) + ".png");
  76. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  77. setMask(maskPix.mask());
  78. ui->centralWidget->setStyleSheet("border-image: url(:/assets/bg" + QString::number(bg_id) + ".png)");
  79. }
  80. void MainWindow::setupMenuHoverWidget()
  81. {
  82. menuHoverWidget = new QWidget(ui->menu_widget);
  83. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  84. menuHoverWidget->resize(0, 0);
  85. connect(ui->menuentry_1, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  86. connect(ui->menuentry_2, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  87. connect(ui->menuentry_3, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  88. connect(ui->menuentry_4, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  89. connect(ui->menuentry_5, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  90. MenuEntry::setActiveLabel(ui->menuentry_1);
  91. menu_hover_checker_timer.setInterval(500);
  92. connect(&menu_hover_checker_timer, &QTimer::timeout, this, &MainWindow::checkMenuIsHovered);
  93. menu_hover_checker_timer.start();
  94. }
  95. void MainWindow::moveMenuHoverWidget(MenuEntry *target)
  96. {
  97. if (menuHoverWidget->size() == QSize(0, 0)) {
  98. menuHoverWidget->resize(target->size() + QSize(10, 0));
  99. menuHoverWidget->move(target->pos() + QPoint(-5, 0));
  100. } else {
  101. if (menuHoverWidgetAnimation == nullptr)
  102. menuHoverWidgetAnimation = new QPropertyAnimation(menuHoverWidget, "geometry");
  103. else
  104. menuHoverWidgetAnimation->stop();
  105. menuHoverWidgetAnimation->setDuration(200);
  106. menuHoverWidgetAnimation->setStartValue(QRect(menuHoverWidget->pos(), menuHoverWidget->size()));
  107. menuHoverWidgetAnimation->setEndValue(QRect(target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
  108. menuHoverWidgetAnimation->start();
  109. }
  110. ui->menuentry_1->raise();
  111. ui->menuentry_2->raise();
  112. ui->menuentry_3->raise();
  113. ui->menuentry_4->raise();
  114. ui->menuentry_5->raise();
  115. }
  116. void MainWindow::checkMenuIsHovered()
  117. {
  118. QPoint pos = QCursor::pos();
  119. QWidget *hovered = qApp->widgetAt(pos);
  120. if (!hovered || hovered->objectName().size() < 4 ||
  121. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  122. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  123. MenuEntry::setHoverLabel(nullptr);
  124. }
  125. }
  126. void MainWindow::hideAllContentWidgets()
  127. {
  128. status_frame->hide();
  129. rusification_frame->hide();
  130. }
  131. void MainWindow::on_closeButton_clicked()
  132. {
  133. qApp->quit();
  134. }
  135. void MainWindow::on_minimizeButton_clicked()
  136. {
  137. setWindowState(Qt::WindowMinimized);
  138. }