Replaced QVBoxLayout with QGridLayout for flexible positioning in `DeviceWidget`. Added overlays, labels, and interactive buttons to improve UI and functionality. Introduced new image assets for icons and removed unused files to optimize resources.
132 lines
4.7 KiB
C++
132 lines
4.7 KiB
C++
#include "DeviceWidget.h"
|
||
#include <QVBoxLayout>
|
||
#include <QLabel>
|
||
#include <QPixmap>
|
||
#include <QImage>
|
||
#include <QPushButton>
|
||
#include <QCoreApplication>
|
||
|
||
DeviceWidget::DeviceWidget(const DeviceInfo &device, QWidget *parent): QWidget(parent) {
|
||
auto *layout = new QGridLayout(this);
|
||
layout->setContentsMargins(2, 2, 2, 2);
|
||
layout->setSpacing(0);
|
||
this->setStyleSheet("background-color: lightgray;padding: 2px;");
|
||
|
||
QImage img;
|
||
img.loadFromData(device.image, "JPG");
|
||
|
||
auto *imageLabel = new QLabel(this);
|
||
imageLabel->setPixmap(QPixmap::fromImage(img).scaled(300, 450, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||
imageLabel->setAlignment(Qt::AlignCenter);
|
||
layout->addWidget(imageLabel, 0, 0);
|
||
|
||
auto *statusLabel = new QLabel(device.name, this);
|
||
statusLabel->setWordWrap(true);
|
||
statusLabel->setMaximumWidth(300);
|
||
statusLabel->setStyleSheet(
|
||
"background-color: rgba(0, 0, 0, 0.5);"
|
||
"color: white;"
|
||
);
|
||
// статус — в ту же ячейку, но поверх картинки и по углу
|
||
layout->addWidget(statusLabel, 0, 0, Qt::AlignTop | Qt::AlignLeft);
|
||
|
||
auto *batteryLabel = new QLabel(QString("%1%").arg(device.battery), this);
|
||
batteryLabel->setWordWrap(true);
|
||
batteryLabel->setMaximumWidth(300);
|
||
batteryLabel->setStyleSheet(
|
||
"background-color: rgba(0, 0, 0, 0.5);"
|
||
"color: white;"
|
||
);
|
||
// статус — в ту же ячейку, но поверх картинки и по углу
|
||
layout->addWidget(batteryLabel, 0, 0, Qt::AlignTop | Qt::AlignRight);
|
||
|
||
|
||
auto *overlay = new QWidget(this);
|
||
overlay->setAttribute(Qt::WA_TransparentForMouseEvents, false);
|
||
overlay->setStyleSheet("background: transparent;"); // сам фон прозрачный
|
||
// вертикальный лэйаут списка внутри overlay
|
||
auto *vbox = new QVBoxLayout(overlay);
|
||
vbox->setContentsMargins(2, 0, 0, 2);
|
||
vbox->setSpacing(2);
|
||
|
||
// пример трёх строк (можете генерировать их динамически)
|
||
for (int i = 0; i < 3; ++i) {
|
||
auto *rowWidget = new QWidget(overlay);
|
||
rowWidget->setContentsMargins(0, 0, 0, 0);
|
||
rowWidget->setStyleSheet(
|
||
"background-color: rgba(0, 0, 0, 0.6);" // полупрозрачный фон
|
||
"border-radius: 4px;" // скруглённые углы, если нужно
|
||
);
|
||
|
||
// вешаем на контейнер ваш QHBoxLayout
|
||
auto *hbox = new QHBoxLayout(rowWidget);
|
||
hbox->setContentsMargins(2, 1, 1, 1);
|
||
hbox->setSpacing(0);
|
||
|
||
auto *icon = new QLabel(overlay);
|
||
QPixmap pixmap(QCoreApplication::applicationDirPath() + "/images/alfa_icon.png");
|
||
icon->setPixmap(pixmap);
|
||
icon->setPixmap(pixmap.scaled(18, 18, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||
icon->setStyleSheet(
|
||
"background-color: transparent;"
|
||
"margin: 0px;"
|
||
);
|
||
|
||
|
||
auto *label = new QLabel(QString("Банк %1").arg(i + 1), overlay);
|
||
label->setStyleSheet(
|
||
"background-color: transparent;"
|
||
"margin: 0px;"
|
||
"font-size: 10px;"
|
||
"color: white;"
|
||
);
|
||
|
||
auto *circle = new QLabel(overlay);
|
||
circle->setFixedSize(12, 12);
|
||
circle->setStyleSheet(
|
||
"margin: 0px;"
|
||
"background-color: red; "
|
||
"border-radius: 6px;"
|
||
);
|
||
|
||
// 3) две кнопки
|
||
auto *btn1 = new QPushButton(overlay);
|
||
QIcon btnIcon(QCoreApplication::applicationDirPath() + "/images/turn_on.png");
|
||
btn1->setIcon(btnIcon);
|
||
btn1->setIconSize(QSize(16, 16));
|
||
btn1->setFlat(true);
|
||
btn1->setStyleSheet(
|
||
"margin: 0px;"
|
||
"background-color: transparent;"
|
||
"border: none;"
|
||
);
|
||
|
||
auto *btn2 = new QPushButton(overlay);
|
||
QIcon btn2Icon(QCoreApplication::applicationDirPath() + "/images/cogwheel.png");
|
||
btn2->setIcon(btn2Icon);
|
||
btn2->setIconSize(QSize(16, 16));
|
||
btn2->setFlat(true);
|
||
btn2->setStyleSheet(
|
||
"margin: 0px;"
|
||
"background-color: transparent;"
|
||
"border: none;"
|
||
);
|
||
|
||
|
||
// собираем
|
||
hbox->addWidget(icon);
|
||
hbox->addWidget(label);
|
||
hbox->addWidget(circle);
|
||
hbox->addWidget(btn1);
|
||
hbox->addWidget(btn2);
|
||
|
||
hbox->addStretch();
|
||
vbox->addWidget(rowWidget);
|
||
}
|
||
vbox->addStretch();
|
||
|
||
// кладём overlay в ту же ячейку, что и картинку,
|
||
// но выравниваем его по левому-нижнему углу
|
||
layout->addWidget(overlay, 0, 0, Qt::AlignLeft | Qt::AlignBottom);
|
||
}
|