mainwindow.cpp 8.2 KB

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