mainwindow.cpp 5.2 KB

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