1
0
forked from BRT/arc
arc/views/MainWindow.cpp
2025-07-27 16:11:43 +05:00

133 lines
4.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MainWindow.h"
#include "bank/AccountWindow.h"
#include <QLabel>
#include <QScrollArea>
#include <QMenuBar>
#include <QStackedWidget>
#include "TutorialWindow.h"
#include "db/DeviceInfo.h"
#include "device/DeviceWidget.h"
#include "dao/DeviceDAO.h"
#include "widget/common/FlowLayout.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
// Инициализируем таймер обновления
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::loadDevices);
stackedWidget = new QStackedWidget(this);
setCentralWidget(stackedWidget);
createDevicePage();
stackedWidget->addWidget(m_devicesWindow);
stackedWidget->setCurrentWidget(m_devicesWindow);
// FIXME переделать на ленивое создание
// m_accountsWindow = new AccountWindow(this);
// stackedWidget->addWidget(m_accountsWindow);
// stackedWidget->setCurrentWidget(accountPage);
resize(800, 600);
// Работа с меню
QMenu *menu = menuBar()->addMenu("Меню");
auto *devicesPageAction = new QAction("Активные устройства", this);
// auto *accountPageAction = new QAction("Аккаунты", this);
auto *tutorialPageAction = new QAction("Инструкция", this);
menu->addAction(devicesPageAction);
// menu->addAction(accountPageAction);
menu->addAction(tutorialPageAction);
connect(devicesPageAction, &QAction::triggered, this, [=]() {
if (!m_devicesWindow) {
createDevicePage();
}
stackedWidget->setCurrentWidget(m_devicesWindow);
});
// connect(accountPageAction, &QAction::triggered, this, [=]() {
// stackedWidget->setCurrentWidget(m_accountsWindow);
// deleteDevicePage(); // освобождаем ресурсы
// });
connect(tutorialPageAction, &QAction::triggered, this, [=]() {
if (!m_tutorialWindow) {
m_tutorialWindow = new TutorialWindow(this);
}
m_tutorialWindow->show();
m_tutorialWindow->raise();
m_tutorialWindow->activateWindow();
});
// m_tutorialWindow = new TutorialWindow(this);
// m_tutorialWindow->show();
// QTimer::singleShot(0, m_tutorialWindow, [w = m_tutorialWindow]() {
// w->raise();
// w->activateWindow();
// });
}
void MainWindow::createDevicePage() {
if (m_devicesWindow) {
return;
}
auto *scrollArea = new QScrollArea(this);
auto *contentWidget = new QWidget;
flowLayout = new FlowLayout(contentWidget, 0, 0, 0);
// flowLayout->setContentsMargins(0, 0, 0, 0);
// flowLayout->setSpacing(0);
QVector<DeviceInfo> devices = DeviceDAO::getAllDevices(true);
for (const auto &device: devices) {
auto *deviceWidget = new DeviceWidget(device);
// deviceWidget->setFixedSize(220, 400);
flowLayout->addWidget(deviceWidget);
}
contentWidget->setLayout(flowLayout);
scrollArea->setWidget(contentWidget);
scrollArea->setWidgetResizable(true);
m_devicesWindow = scrollArea;
stackedWidget->addWidget(m_devicesWindow);
timer->start(1000);
}
void MainWindow::deleteDevicePage() {
if (!m_devicesWindow) {
return;
}
timer->stop();
stackedWidget->removeWidget(m_devicesWindow);
m_devicesWindow->deleteLater();
m_devicesWindow = nullptr;
flowLayout = nullptr;
}
/**
* Однако, в нем вы вызываете delete для элементов, что изменяет память.
* Для того, чтобы не нарушать константность, этот метод должен быть неконстантным (без const в сигнатуре).
*/
void MainWindow::loadDevices() {
const auto devices = DeviceDAO::getAllDevices(true);
// Очистить старые виджеты
QLayoutItem *item;
while ((item = flowLayout->takeAt(0)) != nullptr) {
if (item->widget()) {
delete item->widget(); // Удаляем виджет
}
delete item; // Удаляем сам элемент из layout
}
// Добавляем новые виджеты для каждого устройства
for (const auto &device: devices) {
flowLayout->addWidget(new DeviceWidget(device));
}
}