#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 读 .qss
QFile style_file("./style.qss");
if (style_file.exists()) {
style_file.open(QFile::ReadOnly);
qDebug("Open succeed");
QString styleSheet = QLatin1String(style_file.readAll());
this->setStyleSheet(styleSheet);
style_file.close();
} else {
qDebug("Open failed");
}
QFile file("./ui_style.qss");
file.open(QFile::ReadOnly);
if (file.isOpen()) {
QString styleSheet = QLatin1String(file.readAll());
ui->centralwidget->setStyleSheet(styleSheet);
file.close();
qDebug() << "success";
} else {
qDebug() << "error";
}
this->setWindowFlags(Qt::FramelessWindowHint); //去除标题框
this->setAttribute(Qt::WA_TranslucentBackground); // 设置透明背景
// 获取屏幕分辨率
// 屏幕分辨率 = 像素大小 * 缩放大小(取整)
QScreen *screen = QApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
qreal devicePixelRatio = screen->devicePixelRatio();
int my_screen_width = screenGeometry.size().width() * devicePixelRatio;
int my_screen_height = screenGeometry.size().height() * devicePixelRatio;
// 获取参考值屏幕的分辨率的比值 之后有需要的部分位置调整要加上比值去运算,以更加自然的适配更多的屏幕显示
multiple_width = CONSULT_WIDTH / float(my_screen_width);
multiple_height = CONSULT_HEIGHT / float(my_screen_height);
menu = new class menu_bar(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::menu_bar::menu_bar(class MainWindow *parent)
{
unsigned char i;
this->parent = parent;
last_globalPosition = new QPointF;
trigger_wiget_status = VOID_WIGET;
status = false;
timer_20ms = new QTimer(parent);
this->parent->connect(timer_20ms, &QTimer::timeout, this->parent, &MainWindow::timeover_20ms);
timer_20ms->start(OVERTIME);
main_under_widget = new QWidget(parent);
main_under_widget->setObjectName("under_widget");
conceal = new QPushButton(parent);
windowing = new QPushButton(parent);
close = new QPushButton(parent);
for (i = 0; i < TIGGER_WIDGET_NUM; i++)
tigger_widget[i] = new QWidget(parent);
main_under_widget->lower();
for (i = 0; i < TIGGER_WIDGET_NUM; i += 2)
tigger_widget[i]->raise();
conceal->raise();
windowing->raise();
close->raise();
for (i = 1; i < TIGGER_WIDGET_NUM; i += 2)
tigger_widget[i]->raise();
// 设置名字
conceal->setObjectName("menu_conceal");
windowing->setObjectName("menu_windowing");
close->setObjectName("menu_close");
// 设置大小
conceal->resize(BUTTON_WIDTH, BUTTON_HEIGHT);
windowing->resize(BUTTON_WIDTH, BUTTON_HEIGHT);
close->resize(BUTTON_WIDTH, BUTTON_HEIGHT);
// 绑定槽函数
this->parent->connect(close, &QPushButton::clicked, this->parent, &MainWindow::close_windon);
this->parent->connect(windowing, &QPushButton::clicked, this->parent, &MainWindow::windowing_windon);
this->parent->connect(conceal, &QPushButton::clicked, this->parent, &MainWindow::conceal_windon);
#if TRY
// 调试显示模式
tigger_widget[0]->setStyleSheet(
"QWidget {"
"background-color:rgb(10, 10, 120);"
"}"
);
tigger_widget[1]->setStyleSheet(
"QWidget {"
"background-color:rgb(100, 100, 100);"
"}"
);
tigger_widget[2]->setStyleSheet(
"QWidget {"
"background-color:rgb(150, 150, 150);"
"}"
);
tigger_widget[3]->setStyleSheet(
"QWidget {"
"background-color:rgb(200, 200, 200);"
"}"
);
tigger_widget[4]->setStyleSheet(
"QWidget {"
"background-color:rgb(10, 10, 10);"
"}"
);
tigger_widget[5]->setStyleSheet(
"QWidget {"
"background-color:rgb(100, 100, 100);"
"}"
);
tigger_widget[6]->setStyleSheet(
"QWidget {"
"background-color:rgb(100, 255, 100);"
"}"
);
tigger_widget[7]->setStyleSheet(
"QWidget {"
"background-color:rgb(50, 50, 50);"
"border-radius: 10px;"
"}"
);
#else
// 透明
for (i = 1; i < TIGGER_WIDGET_NUM; i++)
tigger_widget[i]->setWindowOpacity(1);
tigger_widget[0]->setObjectName("top_widget");
#endif
// 设置光标
tigger_widget[1]->setCursor(Qt::SizeBDiagCursor);
tigger_widget[3]->setCursor(Qt::SizeFDiagCursor);
tigger_widget[5]->setCursor(Qt::SizeBDiagCursor);
tigger_widget[7]->setCursor(Qt::SizeFDiagCursor);
tigger_widget[2]->setCursor(Qt::SizeHorCursor);
tigger_widget[4]->setCursor(Qt::SizeVerCursor);
tigger_widget[6]->setCursor(Qt::SizeHorCursor);
update();
}
MainWindow::menu_bar::~menu_bar()
{
delete timer_20ms;
for (unsigned char i = 0; i < TIGGER_WIDGET_NUM; i++)
delete tigger_widget[i];
delete conceal;
delete windowing;
delete close;
delete main_under_widget;
delete last_globalPosition;
}
void MainWindow::menu_bar::update(void)
{
unsigned char i;
main_under_widget->move(0, 0);
main_under_widget->resize(parent->width(), parent->height());
conceal->move(parent->width() - close->width() - windowing->width() - conceal->width(), 0);
windowing->move(parent->width() - close->width() - windowing->width(), 0);
close->move(parent->width() - close->width(), 0);
tigger_widget[1]->move(this->parent->width() - tigger_widget[1]->width(), 0);
tigger_widget[3]->move(this->parent->width() - tigger_widget[3]->width(), this->parent->height() - tigger_widget[3]->height());
tigger_widget[5]->move(0, this->parent->height() - tigger_widget[5]->height());
tigger_widget[7]->move(0, 0);
for (i = 1; i < TIGGER_WIDGET_NUM; i += 2)
tigger_widget[i]->resize(AROUND_SIZE, AROUND_SIZE);
tigger_widget[0]->move(0, 0);
tigger_widget[0]->resize(this->parent->width(), TOP_HEIGHT);
tigger_widget[2]->move(this->parent->width() - tigger_widget[2]->width(), 0);
tigger_widget[2]->resize(AROUND_SIZE, this->parent->height());
tigger_widget[4]->move(0, this->parent->height() - tigger_widget[4]->height());
tigger_widget[4]->resize(this->parent->width(), AROUND_SIZE);
tigger_widget[6]->move(0, 0);
tigger_widget[6]->resize(AROUND_SIZE, this->parent->height());
}
// 鼠标按下事件处理
void MainWindow::menu_bar::mouse_press_handle(QMouseEvent * event)
{
if (event->buttons() == Qt::LeftButton) {
QWidget* widget = parent->childAt(parent->mapFromGlobal(QCursor::pos()));
for (unsigned char i = 0; i < TIGGER_WIDGET_NUM; i++)
if (widget == tigge