mainwindow.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QBitmap>
  4. #include <QMouseEvent>
  5. #include <QMessageBox>
  6. #include <QDesktopWidget>
  7. MainWindow::MainWindow(QWidget *parent) :
  8. QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint),
  9. ui(new Ui::MainWindow), menuHoverWidget(nullptr), menuHoverWidgetAnimation(nullptr)
  10. {
  11. ui->setupUi(this);
  12. setupWindowBackgroundAndMask(1);
  13. setupMenuHoverWidget();
  14. }
  15. void MainWindow::mousePressEvent(QMouseEvent *event)
  16. {
  17. if (event->button() == Qt::LeftButton) {
  18. dragPosition = event->globalPos() - frameGeometry().topLeft();
  19. event->accept();
  20. }
  21. }
  22. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  23. {
  24. if (event->buttons() & Qt::LeftButton) {
  25. move(event->globalPos() - dragPosition);
  26. event->accept();
  27. }
  28. }
  29. void MainWindow::resizeEvent(QResizeEvent * /* event */)
  30. {
  31. QPixmap maskPix(":/assets/bg1.png");
  32. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
  33. setMask(maskPix.mask());
  34. }
  35. MainWindow::~MainWindow()
  36. {
  37. delete ui;
  38. }
  39. void MainWindow::on_menuentry_1_clicked()
  40. {
  41. QMessageBox msgBox;
  42. msgBox.setIcon(QMessageBox::Information);
  43. msgBox.setText("Нажали на \"Статус\"!");
  44. msgBox.exec();
  45. }
  46. void MainWindow::on_menuentry_2_clicked()
  47. {
  48. QMessageBox msgBox;
  49. msgBox.setIcon(QMessageBox::Information);
  50. msgBox.setText("Нажали на \"Настройки\"!");
  51. msgBox.exec();
  52. }
  53. void MainWindow::on_menuentry_3_clicked()
  54. {
  55. QMessageBox msgBox;
  56. msgBox.setIcon(QMessageBox::Information);
  57. msgBox.setText("Нажали на \"Русификация\"!");
  58. msgBox.exec();
  59. }
  60. void MainWindow::on_menuentry_4_clicked()
  61. {
  62. QMessageBox msgBox;
  63. msgBox.setIcon(QMessageBox::Information);
  64. msgBox.setText("Нажали на \"Новости\"!");
  65. msgBox.exec();
  66. }
  67. void MainWindow::on_menuentry_5_clicked()
  68. {
  69. QMessageBox msgBox;
  70. msgBox.setIcon(QMessageBox::Information);
  71. msgBox.setText("Нажали на \"Помощь\"!");
  72. msgBox.exec();
  73. }
  74. void MainWindow::on_menuentry_6_clicked()
  75. {
  76. QMessageBox msgBox;
  77. msgBox.setIcon(QMessageBox::Information);
  78. msgBox.setText("Нажали на \"О нас\"!");
  79. msgBox.exec();
  80. }
  81. void MainWindow::onHoverMenuentry()
  82. {
  83. moveMenuHoverWidget(MenuEntry::getHoverLabel());
  84. }
  85. void MainWindow::setupWindowBackgroundAndMask(int bg_id)
  86. {
  87. QPixmap maskPix(":/assets/bg" + QString::number(bg_id) + ".png");
  88. maskPix = maskPix.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  89. setMask(maskPix.mask());
  90. ui->centralWidget->setStyleSheet("border-image: url(:/assets/bg" + QString::number(bg_id) + ".png)");
  91. }
  92. void MainWindow::setupMenuHoverWidget()
  93. {
  94. menuHoverWidget = new QWidget(ui->menu_widget);
  95. menuHoverWidget->setStyleSheet("background-color: rgba(55, 37, 31, 250);");
  96. menuHoverWidget->resize(0, 0);
  97. connect(ui->menuentry_1, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  98. connect(ui->menuentry_2, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  99. connect(ui->menuentry_3, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  100. connect(ui->menuentry_4, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  101. connect(ui->menuentry_5, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  102. connect(ui->menuentry_6, &MenuEntry::hover_label_changed, this, &MainWindow::onHoverMenuentry);
  103. MenuEntry::setActiveLabel(ui->menuentry_1);
  104. menu_hover_checker_timer.setInterval(700);
  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->raise();
  124. ui->menuentry_2->raise();
  125. ui->menuentry_3->raise();
  126. ui->menuentry_4->raise();
  127. ui->menuentry_5->raise();
  128. ui->menuentry_6->raise();
  129. }
  130. void MainWindow::checkMenuIsHovered()
  131. {
  132. QPoint pos = QCursor::pos();
  133. QWidget *hovered = qApp->widgetAt(pos);
  134. if (!hovered || hovered->objectName().size() < 4 ||
  135. (hovered->objectName().left(9) != "menuentry" && hovered->objectName() != "menu_widget")) {
  136. moveMenuHoverWidget(MenuEntry::getActiveLabel());
  137. MenuEntry::setHoverLabel(nullptr);
  138. }
  139. }
  140. void MainWindow::on_closeButton_clicked()
  141. {
  142. qApp->quit();
  143. }
  144. void MainWindow::on_minimizeButton_clicked()
  145. {
  146. setWindowState(Qt::WindowMinimized);
  147. }