Files
Radio/module/radio/Global.cpp
T
2026-07-29 18:08:57 +08:00

158 lines
5.1 KiB
C++

#include "Global.h"
#include "BackEnd/MainWidget.h"
#include "Widget/base/export.h"
#include "menu/BaseWidgetMenu.h"
#include <QScreen>
QVBoxLayout* create_vbox_layout(const std::vector<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* create_hbox_layout(const std::vector<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 get_max_rect(const QRect& rect, double WHRate){
double w = rect.width(), h = rect.height();
QRect ret;
if(w/h > WHRate) {
double should_w = h * WHRate;
double x = (w - should_w)/2;
ret = QRect((int)x, 0, (int)should_w, (int)h);
} else {
double should_h = w / WHRate;
double y = (h - should_h)/2;
ret = QRect(0, (int)y, (int)w, (int)should_h);
}
return ret;
}
int calculate_optimal_font_size(const std::vector<QString>& text_list, const QSize& space_size, const QFont& font) {
int font_size = 1, textWidth, textHeight;
do {
textWidth = -1;
textHeight = 0;
QFontMetrics fontMetrics(QFont(font.family(), font_size));
for(const QString& text : text_list){
textWidth = std::max(textWidth, fontMetrics.horizontalAdvance(text));
textHeight += fontMetrics.height();
}
font_size++;
} while (textWidth < space_size.width() && textHeight < space_size.height());
return font_size;
}
int calculate_optimal_font_size(const QString& text, const QSize& space_size, const QFont& font) {
int font_size = 1, textWidth, textHeight;
do {
QFontMetrics fontMetrics(QFont(font.family(), font_size));
textWidth = fontMetrics.horizontalAdvance(text);
textHeight = fontMetrics.height();
font_size++;
} while (textWidth < space_size.width() && textHeight < space_size.height());
return font_size;
}
Global::Global() {
}
int Global::adapt_height(int full_screen_height) {
auto m = Main_Widget::instance();
return (int)(double(m->height() * full_screen_height)/double(QGuiApplication::primaryScreen()->size().height()));
}
int Global::adapt_width(int full_screen_width) {
auto m = Main_Widget::instance();
return (int)(double(m->width() * full_screen_width)/double(QGuiApplication::primaryScreen()->size().width()));
}
QPushButton *create_button(const QString& text, int font_size, const std::function<void(void)>& func) {
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(font_size);
button->setFont(font);
if(func) QObject::connect(button, &QPushButton::clicked, func);
return button;
}
QPushButton *create_click_button(const QString &text, int font_size, const std::function<void(void)> &func,
renderive::Roll_Object<QPushButton> *rollObject) {
auto button = new QPushButton;
init_click_button(button, text, font_size, func, rollObject);
return button;
}
void init_click_button(QPushButton* button, const QString &text, int font_size, const std::function<void(void)> &func,
renderive::Roll_Object<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(font_size);
button->setFont(font);
if(func) QObject::connect(button, &QPushButton::clicked, [button, rollObject, func](){
rollObject->set_cur(button);
func();
});
}
QPushButton* create_normal(const QString& text, int font_size, const std::function<void(void)>& func) {
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(font_size);
button->setFont(font);
if(func) QObject::connect(button, &QPushButton::clicked, func);
return button;
}
// int cell_width = width() / 15;
// int cell_height = height() / 32;
// // 绘制水平线条
// for (int row = 0; row < 33; ++row) {
// int y = row * cell_height;
// painter.drawLine(QPoint(0, y), QPoint(width(), y));
// }
// // 绘制垂直线条
// for (int col = 0; col < 17; ++col) {
// int x = col * cell_width;
// painter.drawLine(QPoint(x, 0), QPoint(x, height()));
// }