FemaLocalSoftware/mainwindow.cpp

203 lines
6.7 KiB
C++
Raw Normal View History

2024-11-24 09:48:52 +00:00
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "httpclient.h"
2024-11-28 08:02:59 +00:00
#include "sensorlayout.h"
2024-11-28 18:34:00 +00:00
#include "sensorwidget.h"
2024-11-24 09:48:52 +00:00
2024-11-28 18:34:00 +00:00
#include <QComboBox>
#include <QGridLayout>
#include <QResizeEvent>
2024-11-28 08:02:59 +00:00
#include <QScrollArea>
#include <QDebug>
#include <QLayout>
#include <QWidget>
#include <QScrollArea>
#include <QStyle>
#include <QRect>
class FlowLayout : public QLayout {
public:
explicit FlowLayout(QWidget *parent = nullptr, int margin = 0, int hSpacing = -1, int vSpacing = -1)
: QLayout(parent), m_hSpacing(hSpacing), m_vSpacing(vSpacing) {
setContentsMargins(margin, margin, margin, margin);
}
~FlowLayout() override {
QLayoutItem *item;
while ((item = takeAt(0))) {
delete item;
}
}
void addItem(QLayoutItem *item) override {
m_items.append(item);
}
int horizontalSpacing() const {
return (m_hSpacing >= 0) ? m_hSpacing : smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
int verticalSpacing() const {
return (m_vSpacing >= 0) ? m_vSpacing : smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
Qt::Orientations expandingDirections() const override {
return Qt::Orientation(0);
}
bool hasHeightForWidth() const override {
return true;
}
int heightForWidth(int width) const override {
int height = doLayout(QRect(0, 0, width, 0), true);
return height;
}
int count() const override {
return m_items.size();
}
QLayoutItem *itemAt(int index) const override {
return m_items.value(index);
}
QLayoutItem *takeAt(int index) override {
return (index >= 0 && index < m_items.size()) ? m_items.takeAt(index) : nullptr;
}
void setGeometry(const QRect &rect) override {
QLayout::setGeometry(rect);
doLayout(rect, false);
}
QSize sizeHint() const override {
return minimumSize();
}
QSize minimumSize() const override {
QSize size;
for (const QLayoutItem *item : qAsConst(m_items)) {
size = size.expandedTo(item->minimumSize());
}
const QMargins margins = contentsMargins();
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
return size;
}
private:
int doLayout(const QRect &rect, bool testOnly) const {
int x = rect.x();
int y = rect.y();
int lineHeight = 0;
for (QLayoutItem *item : qAsConst(m_items)) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
int spaceY = verticalSpacing();
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > rect.right() && lineHeight > 0) {
x = rect.x();
y += lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly) {
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
}
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
return y + lineHeight - rect.y();
}
int smartSpacing(QStyle::PixelMetric pm) const {
QObject *parent = this->parent();
if (!parent) {
return -1;
} else if (parent->isWidgetType()) {
return static_cast<QWidget *>(parent)->style()->pixelMetric(pm, nullptr, static_cast<QWidget *>(parent));
} else {
return static_cast<QLayout *>(parent)->spacing();
}
}
QList<QLayoutItem *> m_items;
int m_hSpacing;
int m_vSpacing;
};
2024-11-24 09:48:52 +00:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
2024-11-28 08:02:59 +00:00
2024-11-28 18:34:00 +00:00
QScrollArea *scrollArea = new QScrollArea(this);
2024-11-28 08:02:59 +00:00
scrollArea->setStyleSheet("background: transparent; border: 0;");
2024-11-28 18:34:00 +00:00
scrollArea->setWidgetResizable(true); // Контейнер будет растягиваться по области просмотра
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // Прокрутка по вертикали по мере необходимости
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Прокрутка по горизонтали по мере необходимости
2024-11-28 08:02:59 +00:00
// Создаем контейнер виджетов и устанавливаем кастомный FlowLayout
QWidget *widgetContainer = new QWidget(scrollArea); // Контейнер для виджетов
FlowLayout *layout = new FlowLayout(widgetContainer, 10, 10, 10); // Используем FlowLayout
2024-11-28 08:02:59 +00:00
// Добавляем виджеты в контейнер
int num = 0;
for (int row = 0; row < 10; ++row) {
for (int col = 0; col < 10; ++col) {
2024-11-28 08:02:59 +00:00
const QString a = QString::number(row);
const QString b = QString::number(col);
const QString c = QString::number(num);
num++;
// Создаем экземпляр SensorWidget
2024-11-28 18:34:00 +00:00
SensorWidget *sensor = new SensorWidget(nullptr, a, b, c);
// Добавляем SensorWidget в FlowLayout
layout->addWidget(sensor);
2024-11-28 08:02:59 +00:00
}
}
// Устанавливаем FlowLayout в качестве основного layout для контейнера
widgetContainer->setLayout(layout);
// Устанавливаем контейнер виджетов как виджет для QScrollArea
scrollArea->setWidget(widgetContainer);
// Добавляем QScrollArea в основной layout (предполагаем, что это ui->SensorsTabLayout)
ui->SensorsTabLayout->addWidget(scrollArea);
qDebug() << "ScrollArea Size:" << scrollArea->size();
//qDebug() << "Widget Count:" << sensorLayout->count();
/* HttpClient httpClient;
2024-11-28 18:34:00 +00:00
for (int i = 0; i < 10; ++i) {
QJsonObject getResult = httpClient.get("http://raspberrypi.lan:8080/data/getDevices");
qDebug() << "GET Response:" << getResult;
}*/
2024-11-24 09:48:52 +00:00
}
MainWindow::~MainWindow()
{
delete ui;
}
2024-11-28 18:34:00 +00:00
void MainWindow::ResizeEvent(QResizeEvent *event) {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupTabs() {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupSensorTab(QWidget *tab, QVBoxLayout *mainLayout) {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupIncidentTab(QWidget *tab, QVBoxLayout *mainLayout) {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupStatisticsTab(QWidget *tab, QVBoxLayout *mainLayout) {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupJournalTab(QWidget *tab, QVBoxLayout *mainLayout) {}
2024-11-28 18:34:00 +00:00
void MainWindow::SetupSettingsTab(QWidget *tab, QVBoxLayout *mainLayout) {}