Files
Radio/module/radio/Global.cpp
T
2026-06-16 13:38:08 +08:00

158 lines
5.0 KiB
C++

#include "Global.h"
#include "BackEnd/MainWidget.h"
#include "YSGraphic_Core/base/export.h"
#include "menu/BaseWidgetMenu.h"
#include <QScreen>
QVBoxLayout* createVBoxLayout(const QVector<QWidget*>& children, QWidget *parent){
auto ret = new QVBoxLayout(parent);
ret->setMargin(0);
ret->setSpacing(0);
// ret->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
for(auto child : children){
ret->addWidget(child, 1);
}
return ret;
}
QHBoxLayout* createHBoxLayout(const QVector<QWidget*>& children, QWidget *parent){
auto ret = new QHBoxLayout(parent);
ret->setMargin(0);
ret->setSpacing(0);
// ret->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
for(auto child : children){
ret->addWidget(child, 1);
}
return ret;
}
QRect getMaxRect(const QRect& rect, double WHRate){
double w = rect.width(), h = rect.height();
QRect ret;
if(w/h > WHRate) {
double shouldW = h * WHRate;
double x = (w - shouldW)/2;
ret = QRect((int)x, 0, (int)shouldW, (int)h);
} else {
double shouldH = w / WHRate;
double y = (h - shouldH)/2;
ret = QRect(0, (int)y, (int)w, (int)shouldH);
}
return ret;
}
int calculateOptimalFontSize(const QVector<QString>& textList, const QSize& spaceSize, const QFont& font) {
int fontSize = 1, textWidth, textHeight;
do {
textWidth = -1;
textHeight = 0;
QFontMetrics fontMetrics(QFont(font.family(), fontSize));
for(const QString& text : textList){
textWidth = std::max(textWidth, fontMetrics.horizontalAdvance(text));
textHeight += fontMetrics.height();
}
fontSize++;
} while (textWidth < spaceSize.width() && textHeight < spaceSize.height());
return fontSize;
}
int calculateOptimalFontSize(const QString& text, const QSize& spaceSize, const QFont& font) {
int fontSize = 1, textWidth, textHeight;
do {
QFontMetrics fontMetrics(QFont(font.family(), fontSize));
textWidth = fontMetrics.horizontalAdvance(text);
textHeight = fontMetrics.height();
fontSize++;
} while (textWidth < spaceSize.width() && textHeight < spaceSize.height());
return fontSize;
}
Global::Global() {
}
int Global::adaptHeight(int fullScreenHeight) {
auto m = MainWidget::instance();
return (int)(double(m->height() * fullScreenHeight)/double(QGuiApplication::primaryScreen()->size().height()));
}
int Global::adaptWidth(int fullScreenWidth) {
auto m = MainWidget::instance();
return (int)(double(m->width() * fullScreenWidth)/double(QGuiApplication::primaryScreen()->size().width()));
}
QPushButton *createButton(const QString& text, int fontSize, const std::function<void(void)>& mFunc) {
auto button = new QPushButton(text);
button->setDisabled(true);
button->setFocusPolicy(Qt::NoFocus);
button->setFlat(true);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
button->setCheckable(true);
button->setChecked(false);
QFont font;
font.setPixelSize(fontSize);
button->setFont(font);
if(mFunc) QObject::connect(button, &QPushButton::clicked, mFunc);
return button;
}
QPushButton *createClickButton(const QString &text, int fontSize, const std::function<void(void)> &mFunc,
YSG::RollObject<QPushButton> *rollObject) {
auto button = new QPushButton;
initClickButton(button, text, fontSize, mFunc, rollObject);
return button;
}
void initClickButton(QPushButton* button, const QString &text, int fontSize, const std::function<void(void)> &mFunc,
YSG::RollObject<QPushButton> *rollObject){
button->setText(text);
button->setFocusPolicy(Qt::NoFocus);
button->setFlat(true);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
button->setCheckable(true);
button->setChecked(false);
QFont font;
font.setPixelSize(fontSize);
button->setFont(font);
if(mFunc) QObject::connect(button, &QPushButton::clicked, [button, rollObject, mFunc](){
rollObject->setCur(button);
mFunc();
});
}
QPushButton* createNormal(const QString& text, int fontSize, const std::function<void(void)>& mFunc) {
auto button = new QPushButton(text);
button->setDisabled(true);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
button->setFocusPolicy(Qt::NoFocus);
//button->setDefault(true);
QFont font;
font.setPixelSize(fontSize);
button->setFont(font);
if(mFunc) QObject::connect(button, &QPushButton::clicked, mFunc);
return button;
}
// int cellWidth = width() / 15;
// int cellHeight = height() / 32;
// // 绘制水平线条
// for (int row = 0; row < 33; ++row) {
// int y = row * cellHeight;
// painter.drawLine(QPoint(0, y), QPoint(width(), y));
// }
// // 绘制垂直线条
// for (int col = 0; col < 17; ++col) {
// int x = col * cellWidth;
// painter.drawLine(QPoint(x, 0), QPoint(x, height()));
// }