mainwindow.cpp 8.2 KB

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