Beautifulizing flowlayout done

This commit is contained in:
PetrLaptop 2024-12-10 11:02:54 +03:00
parent 4de8b88fee
commit 037e77f082
8 changed files with 355 additions and 278 deletions

View File

@ -28,8 +28,9 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
${PROJECT_SOURCES}
resources.qrc
sensorwidget.h sensorwidget.cpp
sensorlayout.h sensorlayout.cpp
httpclient.h httpclient.cpp
flowlayout.h flowlayout.cpp
)
# Define target properties for Android with Qt 6 as:

View File

@ -17,6 +17,7 @@
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTabWidget>
@ -37,7 +38,6 @@ public:
QLabel *group;
QLabel *showHidden;
QSpacerItem *horizontalSpacer_4;
QSpacerItem *horizontalSpacer;
QHBoxLayout *selectLayout;
QComboBox *showHiddenSelect;
QComboBox *groupSelect;
@ -48,6 +48,21 @@ public:
QVBoxLayout *sensorsLayout;
QWidget *incidentsTab;
QGridLayout *gridLayout_8;
QScrollArea *scrollArea;
QWidget *scrollAreaWidgetContents;
QVBoxLayout *verticalLayout_2;
QHBoxLayout *horizontalLayout_3;
QLabel *label_2;
QLabel *label;
QSpacerItem *horizontalSpacer_7;
QHBoxLayout *horizontalLayout_2;
QComboBox *comboBox_2;
QComboBox *comboBox;
QSpacerItem *horizontalSpacer_8;
QHBoxLayout *horizontalLayout;
QLineEdit *lineEdit;
QSpacerItem *horizontalSpacer;
QHBoxLayout *asd;
QWidget *journalTab;
QGridLayout *gridLayout_7;
QWidget *statisticsTab;
@ -70,7 +85,7 @@ public:
MainWindow->setMaximumSize(QSize(16777215, 16777215));
MainWindow->setBaseSize(QSize(0, 0));
MainWindow->setStyleSheet(QString::fromUtf8(""));
MainWindow->setTabShape(QTabWidget::Rounded);
MainWindow->setTabShape(QTabWidget::TabShape::Rounded);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName("centralwidget");
centralwidget->setAutoFillBackground(false);
@ -84,7 +99,12 @@ public:
gridLayout_4->setContentsMargins(0, 0, 0, 0);
tabWidget = new QTabWidget(centralwidget);
tabWidget->setObjectName("tabWidget");
tabWidget->setMinimumSize(QSize(0, 0));
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(tabWidget->sizePolicy().hasHeightForWidth());
tabWidget->setSizePolicy(sizePolicy1);
tabWidget->setMinimumSize(QSize(488, 60));
tabWidget->setAutoFillBackground(false);
tabWidget->setStyleSheet(QString::fromUtf8("QTabBar::tab{\n"
" background: #dcd174;\n"
@ -115,11 +135,11 @@ public:
"}"));
sensorsTab = new QWidget();
sensorsTab->setObjectName("sensorsTab");
QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(sensorsTab->sizePolicy().hasHeightForWidth());
sensorsTab->setSizePolicy(sizePolicy1);
QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy2.setHorizontalStretch(0);
sizePolicy2.setVerticalStretch(0);
sizePolicy2.setHeightForWidth(sensorsTab->sizePolicy().hasHeightForWidth());
sensorsTab->setSizePolicy(sizePolicy2);
sensorsTab->setMinimumSize(QSize(0, 0));
sensorsTab->setStyleSheet(QString::fromUtf8("#sensorsTab {\n"
" image: url(:/visual/res/svg/fema-wing.svg);\n"
@ -129,20 +149,20 @@ public:
SensorsTabLayout = new QVBoxLayout(sensorsTab);
SensorsTabLayout->setSpacing(10);
SensorsTabLayout->setObjectName("SensorsTabLayout");
SensorsTabLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
SensorsTabLayout->setSizeConstraint(QLayout::SizeConstraint::SetDefaultConstraint);
SensorsTabLayout->setContentsMargins(0, 25, 0, 0);
nameLayout = new QHBoxLayout();
nameLayout->setSpacing(15);
nameLayout->setObjectName("nameLayout");
nameLayout->setSizeConstraint(QLayout::SetFixedSize);
nameLayout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
nameLayout->setContentsMargins(25, -1, -1, -1);
group = new QLabel(sensorsTab);
group->setObjectName("group");
QSizePolicy sizePolicy2(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy2.setHorizontalStretch(0);
sizePolicy2.setVerticalStretch(0);
sizePolicy2.setHeightForWidth(group->sizePolicy().hasHeightForWidth());
group->setSizePolicy(sizePolicy2);
QSizePolicy sizePolicy3(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy3.setHorizontalStretch(0);
sizePolicy3.setVerticalStretch(0);
sizePolicy3.setHeightForWidth(group->sizePolicy().hasHeightForWidth());
group->setSizePolicy(sizePolicy3);
group->setMinimumSize(QSize(488, 36));
group->setStyleSheet(QString::fromUtf8("width: 488px;\n"
"height: 36px;\n"
@ -159,8 +179,8 @@ public:
showHidden = new QLabel(sensorsTab);
showHidden->setObjectName("showHidden");
sizePolicy2.setHeightForWidth(showHidden->sizePolicy().hasHeightForWidth());
showHidden->setSizePolicy(sizePolicy2);
sizePolicy3.setHeightForWidth(showHidden->sizePolicy().hasHeightForWidth());
showHidden->setSizePolicy(sizePolicy3);
showHidden->setMinimumSize(QSize(275, 36));
showHidden->setStyleSheet(QString::fromUtf8("width: 275px;\n"
"height: 36px;\n"
@ -179,22 +199,18 @@ public:
nameLayout->addItem(horizontalSpacer_4);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
nameLayout->addItem(horizontalSpacer);
SensorsTabLayout->addLayout(nameLayout);
selectLayout = new QHBoxLayout();
selectLayout->setSpacing(15);
selectLayout->setObjectName("selectLayout");
selectLayout->setSizeConstraint(QLayout::SetFixedSize);
selectLayout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
selectLayout->setContentsMargins(25, -1, -1, 10);
showHiddenSelect = new QComboBox(sensorsTab);
showHiddenSelect->setObjectName("showHiddenSelect");
sizePolicy2.setHeightForWidth(showHiddenSelect->sizePolicy().hasHeightForWidth());
showHiddenSelect->setSizePolicy(sizePolicy2);
sizePolicy3.setHeightForWidth(showHiddenSelect->sizePolicy().hasHeightForWidth());
showHiddenSelect->setSizePolicy(sizePolicy3);
showHiddenSelect->setMinimumSize(QSize(488, 60));
showHiddenSelect->setStyleSheet(QString::fromUtf8("QComboBox::drop-down {\n"
" border: 0px;\n"
@ -213,8 +229,8 @@ public:
groupSelect = new QComboBox(sensorsTab);
groupSelect->setObjectName("groupSelect");
sizePolicy2.setHeightForWidth(groupSelect->sizePolicy().hasHeightForWidth());
groupSelect->setSizePolicy(sizePolicy2);
sizePolicy3.setHeightForWidth(groupSelect->sizePolicy().hasHeightForWidth());
groupSelect->setSizePolicy(sizePolicy3);
groupSelect->setMinimumSize(QSize(275, 60));
groupSelect->setStyleSheet(QString::fromUtf8("QComboBox::drop-down {\n"
" border: 0px;\n"
@ -241,12 +257,12 @@ public:
searchLayout = new QHBoxLayout();
searchLayout->setSpacing(0);
searchLayout->setObjectName("searchLayout");
searchLayout->setSizeConstraint(QLayout::SetFixedSize);
searchLayout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
searchLayout->setContentsMargins(25, -1, -1, 10);
search = new QLineEdit(sensorsTab);
search->setObjectName("search");
sizePolicy2.setHeightForWidth(search->sizePolicy().hasHeightForWidth());
search->setSizePolicy(sizePolicy2);
sizePolicy3.setHeightForWidth(search->sizePolicy().hasHeightForWidth());
search->setSizePolicy(sizePolicy3);
search->setMinimumSize(QSize(778, 60));
search->setStyleSheet(QString::fromUtf8("width: 778px;\n"
"height: 60px;\n"
@ -281,8 +297,8 @@ public:
tabWidget->addTab(sensorsTab, QString());
incidentsTab = new QWidget();
incidentsTab->setObjectName("incidentsTab");
sizePolicy1.setHeightForWidth(incidentsTab->sizePolicy().hasHeightForWidth());
incidentsTab->setSizePolicy(sizePolicy1);
sizePolicy2.setHeightForWidth(incidentsTab->sizePolicy().hasHeightForWidth());
incidentsTab->setSizePolicy(sizePolicy2);
incidentsTab->setStyleSheet(QString::fromUtf8("#incidentsTab {\n"
" image: url(:/visual/res/svg/fema-wing.svg);\n"
" background-color: #ffffff; \n"
@ -290,11 +306,156 @@ public:
"}"));
gridLayout_8 = new QGridLayout(incidentsTab);
gridLayout_8->setObjectName("gridLayout_8");
scrollArea = new QScrollArea(incidentsTab);
scrollArea->setObjectName("scrollArea");
scrollArea->setStyleSheet(QString::fromUtf8("background: transparent"));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName("scrollAreaWidgetContents");
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 1608, 770));
verticalLayout_2 = new QVBoxLayout(scrollAreaWidgetContents);
verticalLayout_2->setObjectName("verticalLayout_2");
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setSpacing(15);
horizontalLayout_3->setObjectName("horizontalLayout_3");
horizontalLayout_3->setContentsMargins(25, -1, -1, -1);
label_2 = new QLabel(scrollAreaWidgetContents);
label_2->setObjectName("label_2");
label_2->setMinimumSize(QSize(488, 36));
label_2->setStyleSheet(QString::fromUtf8("width: 488px;\n"
"height: 36px;\n"
"background: transparent;\n"
"font-family: Inter;\n"
"font-size: 30px;\n"
"font-weight: 400;\n"
"line-height: 36.31px;\n"
"text-align: left;\n"
"color: #13385F;\n"
""));
horizontalLayout_3->addWidget(label_2);
label = new QLabel(scrollAreaWidgetContents);
label->setObjectName("label");
label->setMinimumSize(QSize(275, 36));
label->setStyleSheet(QString::fromUtf8("width: 275px;\n"
"height: 36px;\n"
"background: transparent;\n"
"font-family: Inter;\n"
"font-size: 30px;\n"
"font-weight: 400;\n"
"line-height: 36.31px;\n"
"text-align: left;\n"
"color: #13385F;\n"
""));
horizontalLayout_3->addWidget(label);
horizontalSpacer_7 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer_7);
verticalLayout_2->addLayout(horizontalLayout_3);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setSpacing(15);
horizontalLayout_2->setObjectName("horizontalLayout_2");
horizontalLayout_2->setContentsMargins(25, -1, -1, -1);
comboBox_2 = new QComboBox(scrollAreaWidgetContents);
comboBox_2->setObjectName("comboBox_2");
QSizePolicy sizePolicy4(QSizePolicy::Preferred, QSizePolicy::Fixed);
sizePolicy4.setHorizontalStretch(0);
sizePolicy4.setVerticalStretch(0);
sizePolicy4.setHeightForWidth(comboBox_2->sizePolicy().hasHeightForWidth());
comboBox_2->setSizePolicy(sizePolicy4);
comboBox_2->setMinimumSize(QSize(488, 60));
comboBox_2->setStyleSheet(QString::fromUtf8("QComboBox::drop-down {\n"
" border: 0px;\n"
"}\n"
"\n"
"QComboBox::down-arrow {\n"
" image: url(:/visual/res/svg/chevron-down.png);\n"
"}\n"
"\n"
"QComboBox {\n"
" border: 2px solid #DCD174;\n"
" background: transparent;\n"
"}"));
horizontalLayout_2->addWidget(comboBox_2);
comboBox = new QComboBox(scrollAreaWidgetContents);
comboBox->setObjectName("comboBox");
comboBox->setMinimumSize(QSize(275, 60));
comboBox->setStyleSheet(QString::fromUtf8("QComboBox::drop-down {\n"
" border: 0px;\n"
"}\n"
"\n"
"QComboBox::down-arrow {\n"
" image: url(:/visual/res/svg/chevron-down.png);\n"
"}\n"
"\n"
"QComboBox {\n"
" border: 2px solid #DCD174;\n"
" background: transparent;\n"
"}"));
horizontalLayout_2->addWidget(comboBox);
horizontalSpacer_8 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_2->addItem(horizontalSpacer_8);
verticalLayout_2->addLayout(horizontalLayout_2);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName("horizontalLayout");
horizontalLayout->setContentsMargins(25, -1, -1, -1);
lineEdit = new QLineEdit(scrollAreaWidgetContents);
lineEdit->setObjectName("lineEdit");
sizePolicy4.setHeightForWidth(lineEdit->sizePolicy().hasHeightForWidth());
lineEdit->setSizePolicy(sizePolicy4);
lineEdit->setMinimumSize(QSize(778, 60));
lineEdit->setStyleSheet(QString::fromUtf8("width: 778px;\n"
"height: 60px;\n"
"border: 2px solid #DCD174;\n"
"font-family: Inter;\n"
"font-size: 26px;\n"
"font-weight: 400;\n"
"line-height: 31.47px;\n"
"text-align: left;\n"
"background: #00000000;\n"
"color: #a6a6a6;\n"
"\n"
"\n"
"\n"
""));
horizontalLayout->addWidget(lineEdit);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer);
verticalLayout_2->addLayout(horizontalLayout);
asd = new QHBoxLayout();
asd->setObjectName("asd");
verticalLayout_2->addLayout(asd);
scrollArea->setWidget(scrollAreaWidgetContents);
gridLayout_8->addWidget(scrollArea, 0, 0, 1, 1);
tabWidget->addTab(incidentsTab, QString());
journalTab = new QWidget();
journalTab->setObjectName("journalTab");
sizePolicy1.setHeightForWidth(journalTab->sizePolicy().hasHeightForWidth());
journalTab->setSizePolicy(sizePolicy1);
sizePolicy2.setHeightForWidth(journalTab->sizePolicy().hasHeightForWidth());
journalTab->setSizePolicy(sizePolicy2);
journalTab->setStyleSheet(QString::fromUtf8("#journalTab {\n"
" image: url(:/visual/res/svg/fema-wing.svg);\n"
" background-color: #ffffff; \n"
@ -305,8 +466,8 @@ public:
tabWidget->addTab(journalTab, QString());
statisticsTab = new QWidget();
statisticsTab->setObjectName("statisticsTab");
sizePolicy1.setHeightForWidth(statisticsTab->sizePolicy().hasHeightForWidth());
statisticsTab->setSizePolicy(sizePolicy1);
sizePolicy2.setHeightForWidth(statisticsTab->sizePolicy().hasHeightForWidth());
statisticsTab->setSizePolicy(sizePolicy2);
statisticsTab->setStyleSheet(QString::fromUtf8("#statisticsTab {\n"
" image: url(:/visual/res/svg/fema-wing.svg);\n"
" background-color: #ffffff; \n"
@ -317,8 +478,8 @@ public:
tabWidget->addTab(statisticsTab, QString());
settingsTab = new QWidget();
settingsTab->setObjectName("settingsTab");
sizePolicy1.setHeightForWidth(settingsTab->sizePolicy().hasHeightForWidth());
settingsTab->setSizePolicy(sizePolicy1);
sizePolicy2.setHeightForWidth(settingsTab->sizePolicy().hasHeightForWidth());
settingsTab->setSizePolicy(sizePolicy2);
settingsTab->setStyleSheet(QString::fromUtf8("#settingsTab {\n"
" image: url(:/visual/res/svg/fema-wing.svg);\n"
" background-color: #ffffff; \n"
@ -350,6 +511,8 @@ public:
showHidden->setText(QCoreApplication::translate("MainWindow", "\320\237\320\276\320\272\320\260\320\267\320\260\321\202\321\214 \321\201\320\272\321\200\321\213\321\202\321\213\320\265", nullptr));
search->setText(QCoreApplication::translate("MainWindow", "\320\237\320\276\320\270\321\201\320\272...", nullptr));
tabWidget->setTabText(tabWidget->indexOf(sensorsTab), QCoreApplication::translate("MainWindow", "\320\224\320\260\321\202\321\207\320\270\320\272\320\270", nullptr));
label_2->setText(QCoreApplication::translate("MainWindow", "\320\223\321\200\321\203\320\277\320\277\320\260:", nullptr));
label->setText(QCoreApplication::translate("MainWindow", "\320\237\320\276\320\272\320\260\320\267\320\260\321\202\321\214 \321\201\320\272\321\200\321\213\321\202\321\213\320\265", nullptr));
tabWidget->setTabText(tabWidget->indexOf(incidentsTab), QCoreApplication::translate("MainWindow", "\320\230\320\275\321\206\320\270\320\264\320\265\320\275\321\202\321\213", nullptr));
tabWidget->setTabText(tabWidget->indexOf(journalTab), QCoreApplication::translate("MainWindow", "\320\226\321\203\321\200\320\275\320\260\320\273", nullptr));
tabWidget->setTabText(tabWidget->indexOf(statisticsTab), QCoreApplication::translate("MainWindow", "\320\241\321\202\320\260\321\202\320\270\321\201\321\202\320\270\320\272\320\260", nullptr));

108
flowlayout.cpp Normal file
View File

@ -0,0 +1,108 @@
#include "flowlayout.h"
#include <QWidget>
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpacing(hSpacing), m_vSpacing(vSpacing) {
setContentsMargins(margin, margin, margin, margin);
}
FlowLayout::~FlowLayout() {
QLayoutItem *item;
while ((item = takeAt(0))) {
delete item;
}
}
void FlowLayout::addItem(QLayoutItem *item) {
m_items.append(item);
}
int FlowLayout::count() const {
return m_items.size();
}
QLayoutItem *FlowLayout::itemAt(int index) const {
return m_items.value(index);
}
QLayoutItem *FlowLayout::takeAt(int index) {
return (index >= 0 && index < m_items.size()) ? m_items.takeAt(index) : nullptr;
}
Qt::Orientations FlowLayout::expandingDirections() const {
return Qt::Orientation(0);
}
bool FlowLayout::hasHeightForWidth() const {
return true;
}
int FlowLayout::heightForWidth(int width) const {
return doLayout(QRect(0, 0, width, 0), true);
}
void FlowLayout::setGeometry(const QRect &rect) {
QLayout::setGeometry(rect);
doLayout(rect, false);
}
QSize FlowLayout::sizeHint() const {
return minimumSize();
}
QSize FlowLayout::minimumSize() const {
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;
}
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const {
int x = rect.x();
int y = rect.y();
int lineHeight = 0;
for (QLayoutItem *item : qAsConst(m_items)) {
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 FlowLayout::horizontalSpacing() const {
return (m_hSpacing >= 0) ? m_hSpacing : smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
int FlowLayout::verticalSpacing() const {
return (m_vSpacing >= 0) ? m_vSpacing : smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
int FlowLayout::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();
}
}

38
flowlayout.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef FLOWLAYOUT_H
#define FLOWLAYOUT_H
#include <QLayout>
#include <QStyle>
#include <QRect>
#include <QList>
class FlowLayout : public QLayout {
public:
explicit FlowLayout(QWidget *parent = nullptr, int margin = 0, int hSpacing = -1, int vSpacing = -1);
~FlowLayout() override;
void addItem(QLayoutItem *item) override;
int count() const override;
QLayoutItem *itemAt(int index) const override;
QLayoutItem *takeAt(int index) override;
Qt::Orientations expandingDirections() const override;
bool hasHeightForWidth() const override;
int heightForWidth(int width) const override;
void setGeometry(const QRect &rect) override;
QSize sizeHint() const override;
QSize minimumSize() const override;
private:
int doLayout(const QRect &rect, bool testOnly) const;
int horizontalSpacing() const;
int verticalSpacing() const;
int smartSpacing(QStyle::PixelMetric pm) const;
QList<QLayoutItem *> m_items;
int m_hSpacing;
int m_vSpacing;
};
#endif // FLOWLAYOUT_H

View File

@ -1,7 +1,7 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "httpclient.h"
#include "sensorlayout.h"
#include "flowlayout.h"
#include "sensorwidget.h"
#include <QComboBox>
@ -11,126 +11,6 @@
#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;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
@ -139,42 +19,30 @@ MainWindow::MainWindow(QWidget *parent)
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setStyleSheet("background: transparent; border: 0;");
scrollArea->setWidgetResizable(true); // Контейнер будет растягиваться по области просмотра
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // Прокрутка по вертикали по мере необходимости
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Прокрутка по горизонтали по мере необходимости
scrollArea->setWidgetResizable(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Создаем контейнер виджетов и устанавливаем кастомный FlowLayout
QWidget *widgetContainer = new QWidget(scrollArea); // Контейнер для виджетов
FlowLayout *layout = new FlowLayout(widgetContainer, 10, 10, 10); // Используем FlowLayout
QWidget *widgetContainer = new QWidget(scrollArea);
FlowLayout *layout = new FlowLayout(widgetContainer, 10, 10, 10);
// Добавляем виджеты в контейнер
int num = 0;
for (int row = 0; row < 10; ++row) {
for (int col = 0; col < 10; ++col) {
for (int col = 0; col < 1; ++col) {
const QString a = QString::number(row);
const QString b = QString::number(col);
const QString c = QString::number(num);
num++;
// Создаем экземпляр SensorWidget
SensorWidget *sensor = new SensorWidget(nullptr, a, b, c);
// Добавляем SensorWidget в FlowLayout
layout->addWidget(sensor);
}
}
// Устанавливаем 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;
for (int i = 0; i < 10; ++i) {
QJsonObject getResult = httpClient.get("http://raspberrypi.lan:8080/data/getDevices");

View File

@ -1,74 +0,0 @@
#include "sensorlayout.h"
SensorLayout::SensorLayout(QWidget *parent)
: QLayout(parent)
{}
SensorLayout::~SensorLayout()
{
while (!items.isEmpty())
delete items.takeFirst();
}
void SensorLayout::addItem(QLayoutItem *item)
{
items.append(item);
}
QSize SensorLayout::sizeHint() const
{
return QSize(400, 400); // Базовый размер
}
QSize SensorLayout::minimumSize() const
{
return QSize(200, 200); // Минимальный размер
}
int SensorLayout::count() const
{
return items.size();
}
QLayoutItem *SensorLayout::itemAt(int index) const
{
return items.value(index);
}
QLayoutItem *SensorLayout::takeAt(int index)
{
if (index >= 0 && index < items.size())
return items.takeAt(index);
return nullptr;
}
void SensorLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
int x = spacing; // Отступ слева
int y = spacing; // Отступ сверху
int maxWidth = rect.width(); // Максимальная ширина доступного пространства
int rowHeight = 0; // Высота текущей строки
for (QLayoutItem *item : qAsConst(items)) {
QSize itemSize = item->sizeHint();
// Проверяем, помещается ли элемент в текущую строку
if (x + itemSize.width() > maxWidth - spacing) {
// Переход на новую строку
x = spacing; // Сбрасываем горизонтальное положение
y += rowHeight + spacing; // Увеличиваем вертикальное положение
rowHeight = 0; // Сбрасываем высоту строки
}
// Устанавливаем элемент
item->setGeometry(QRect(QPoint(x, y), itemSize));
// Обновляем `x` для следующего элемента
x += itemSize.width() + spacing;
// Высота строки обновляется, если текущий элемент выше
rowHeight = qMax(rowHeight, itemSize.height());
}
}

View File

@ -1,27 +0,0 @@
#ifndef SENSORLAYOUT_H
#define SENSORLAYOUT_H
#include <QLayout>
#include <QRect>
#include <QWidgetItem>
class SensorLayout : public QLayout
{
public:
SensorLayout(QWidget *parent = nullptr);
~SensorLayout();
void addItem(QLayoutItem *item) override;
QSize sizeHint() const override;
QSize minimumSize() const override;
int count() const override;
QLayoutItem *itemAt(int index) const override;
QLayoutItem *takeAt(int index) override;
void setGeometry(const QRect &rect) override;
private:
QList<QLayoutItem *> items;
int spacing = 20;
};
#endif // SENSORLAYOUT_H