Added AccountWindow to display account tables linked to devices and TransactionWindow for viewing account details. Enhanced DeviceDAO and AccountDAO to support additional filtering and data retrieval functionalities.
114 lines
3.5 KiB
C++
114 lines
3.5 KiB
C++
#include "MainWindow.h"
|
||
#include "AccountWindow.h"
|
||
|
||
#include <QLabel>
|
||
#include <QScrollArea>
|
||
#include <QMenuBar>
|
||
#include <QStackedWidget>
|
||
|
||
#include "db/DeviceInfo.h"
|
||
#include "DeviceWidget.h"
|
||
#include "dao/DeviceDAO.h"
|
||
#include "widget/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(devicePage);
|
||
// stackedWidget->setCurrentWidget(devicePage); FIXME
|
||
|
||
// FIXME переделать на ленивое создание
|
||
accountPage = new AccountWindow(this);
|
||
stackedWidget->addWidget(accountPage);
|
||
stackedWidget->setCurrentWidget(accountPage);
|
||
|
||
resize(800, 600);
|
||
|
||
// Работа с меню
|
||
QMenu *menu = menuBar()->addMenu("Меню");
|
||
auto *devicesPageAction = new QAction("Активные устройства", this);
|
||
auto *accountPageAction = new QAction("Аккаунты", this);
|
||
menu->addAction(devicesPageAction);
|
||
menu->addAction(accountPageAction);
|
||
|
||
connect(devicesPageAction, &QAction::triggered, this, [=]() {
|
||
if (!devicePage) {
|
||
createDevicePage();
|
||
}
|
||
stackedWidget->setCurrentWidget(devicePage);
|
||
});
|
||
|
||
connect(accountPageAction, &QAction::triggered, this, [=]() {
|
||
stackedWidget->setCurrentWidget(accountPage);
|
||
deleteDevicePage(); // освобождаем ресурсы
|
||
});
|
||
}
|
||
|
||
|
||
void MainWindow::createDevicePage() {
|
||
if (devicePage) {
|
||
return;
|
||
}
|
||
|
||
auto *scrollArea = new QScrollArea(this);
|
||
auto *contentWidget = new QWidget;
|
||
flowLayout = new FlowLayout(contentWidget);
|
||
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);
|
||
|
||
devicePage = scrollArea;
|
||
stackedWidget->addWidget(devicePage);
|
||
|
||
timer->start(1000);
|
||
}
|
||
|
||
void MainWindow::deleteDevicePage() {
|
||
if (!devicePage) {
|
||
return;
|
||
}
|
||
|
||
timer->stop();
|
||
|
||
stackedWidget->removeWidget(devicePage);
|
||
devicePage->deleteLater();
|
||
devicePage = 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));
|
||
}
|
||
}
|