mainwindow.cpp 8.3 KB

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