2021-12-08 01:34:27 +00:00
|
|
|
|
#pragma execution_character_set("utf-8")
|
|
|
|
|
|
|
|
|
|
#include "panelwidget.h"
|
|
|
|
|
#include "qscrollarea.h"
|
|
|
|
|
#include "qframe.h"
|
|
|
|
|
#include "qboxlayout.h"
|
|
|
|
|
#include "qdebug.h"
|
|
|
|
|
|
|
|
|
|
PanelWidget::PanelWidget(QWidget *parent) : QWidget(parent)
|
|
|
|
|
{
|
|
|
|
|
scrollArea = new QScrollArea(this);
|
|
|
|
|
scrollArea->setObjectName("scrollAreaMain");
|
|
|
|
|
scrollArea->setWidgetResizable(true);
|
|
|
|
|
|
|
|
|
|
scrollAreaContents = new QWidget();
|
|
|
|
|
scrollAreaContents->setGeometry(QRect(0, 0, 100, 100));
|
|
|
|
|
|
|
|
|
|
verticalLayout = new QVBoxLayout(scrollAreaContents);
|
|
|
|
|
verticalLayout->setSpacing(0);
|
|
|
|
|
verticalLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
frame = new QFrame(scrollAreaContents);
|
|
|
|
|
frame->setObjectName("frameMain");
|
|
|
|
|
|
|
|
|
|
gridLayout = new QGridLayout(frame);
|
|
|
|
|
gridLayout->setSpacing(0);
|
|
|
|
|
gridLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
verticalLayout->addWidget(frame);
|
|
|
|
|
scrollArea->setWidget(scrollAreaContents);
|
|
|
|
|
frame->setStyleSheet("QFrame#frameMain{border-width:0px;}");
|
|
|
|
|
|
2023-01-30 03:41:18 +00:00
|
|
|
|
hSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
|
|
|
vSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
margin = 0;
|
|
|
|
|
space = 0;
|
|
|
|
|
autoWidth = false;
|
|
|
|
|
autoHeight = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::resizeEvent(QResizeEvent *)
|
|
|
|
|
{
|
|
|
|
|
scrollArea->resize(this->size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize PanelWidget::sizeHint() const
|
|
|
|
|
{
|
|
|
|
|
return QSize(300, 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize PanelWidget::minimumSizeHint() const
|
|
|
|
|
{
|
|
|
|
|
return QSize(20, 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::setMargin(int left, int top, int right, int bottom)
|
|
|
|
|
{
|
|
|
|
|
gridLayout->setContentsMargins(left, top, right, bottom);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
int PanelWidget::getMargin() const
|
|
|
|
|
{
|
|
|
|
|
return this->margin;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setMargin(int margin)
|
|
|
|
|
{
|
|
|
|
|
if (this->margin != margin) {
|
|
|
|
|
setMargin(margin, margin, margin, margin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
int PanelWidget::getSpace() const
|
|
|
|
|
{
|
|
|
|
|
return this->space;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setSpace(int space)
|
|
|
|
|
{
|
|
|
|
|
if (this->space != space) {
|
|
|
|
|
gridLayout->setSpacing(space);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
bool PanelWidget::getAutoWidth() const
|
|
|
|
|
{
|
|
|
|
|
return this->autoWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setAutoWidth(bool autoWidth)
|
|
|
|
|
{
|
|
|
|
|
if (this->autoWidth != autoWidth) {
|
|
|
|
|
this->autoWidth = autoWidth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
bool PanelWidget::getAutoHeight() const
|
|
|
|
|
{
|
|
|
|
|
return this->autoHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setAutoHeight(bool autoHeight)
|
|
|
|
|
{
|
|
|
|
|
if (this->autoHeight != autoHeight) {
|
|
|
|
|
this->autoHeight = autoHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
int PanelWidget::getColumnCount() const
|
|
|
|
|
{
|
|
|
|
|
return this->columnCount;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setColumnCount(int columnCount)
|
|
|
|
|
{
|
|
|
|
|
if (this->columnCount != columnCount) {
|
|
|
|
|
this->columnCount = columnCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
QList<QWidget *> PanelWidget::getWidgets() const
|
|
|
|
|
{
|
|
|
|
|
return this->widgets;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
void PanelWidget::setWidgets(QList<QWidget *> widgets)
|
|
|
|
|
{
|
|
|
|
|
this->widgets = widgets;
|
|
|
|
|
this->loadWidgets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::loadWidgets()
|
|
|
|
|
{
|
|
|
|
|
int row = 0;
|
|
|
|
|
int column = 0;
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
2023-01-30 03:41:18 +00:00
|
|
|
|
//先把之前的所有移除并不可见
|
2021-12-08 01:34:27 +00:00
|
|
|
|
foreach (QWidget *widget, widgets) {
|
|
|
|
|
gridLayout->removeWidget(widget);
|
|
|
|
|
widget->setVisible(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 03:41:18 +00:00
|
|
|
|
//移除所有弹簧
|
|
|
|
|
gridLayout->removeItem(hSpacer);
|
|
|
|
|
gridLayout->removeItem(vSpacer);
|
|
|
|
|
|
2021-12-08 01:34:27 +00:00
|
|
|
|
//重新添加到布局中并可见
|
|
|
|
|
foreach (QWidget *widget, widgets) {
|
|
|
|
|
gridLayout->addWidget(widget, row, column);
|
|
|
|
|
widget->setVisible(true);
|
|
|
|
|
|
|
|
|
|
column++;
|
|
|
|
|
index++;
|
|
|
|
|
if (index % columnCount == 0) {
|
|
|
|
|
row++;
|
|
|
|
|
column = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row++;
|
|
|
|
|
|
|
|
|
|
//设置右边弹簧
|
|
|
|
|
if (!autoWidth) {
|
|
|
|
|
gridLayout->addItem(hSpacer, 0, gridLayout->columnCount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置底边弹簧
|
|
|
|
|
if (!autoHeight) {
|
|
|
|
|
gridLayout->addItem(vSpacer, row, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::insertWidget(int index, QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
this->widgets.insert(index, widget);
|
|
|
|
|
this->loadWidgets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::removeWidget(QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
this->widgets.removeOne(widget);
|
|
|
|
|
this->loadWidgets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PanelWidget::clearWidgets()
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(this->widgets);
|
|
|
|
|
this->widgets.clear();
|
|
|
|
|
}
|