mainwindow.cpp 8.1 KB

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