#include "MainWindowForm.h" #include "ui_MainWindowForm.h" #include <QRandomGenerator> MainWindowForm::MainWindowForm(QWidget *parent) : FramelessWindow(parent), ui(new Ui::MainWindowForm) { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); initForm(); initMenuWidgets(); initFunctionButtons(); // 每秒更新 connect(TimeCounterUtil::getInstance().clockTimer, &QTimer::timeout, this, &MainWindowForm::onUpdateTimestampHandler); TimeCounterUtil::getInstance().clockTimer->start(1000); /** 临时调试代码 **/ emit ui->btnInfomation->clicked(); } MainWindowForm::~MainWindowForm() { qDebug() << "~MainWindowForm"; delete ui; } void MainWindowForm::setLoginUsername(QString loginUsername) { ui->labelUserName->setText(loginUsername); } void MainWindowForm::initForm() { setWindowFlags(Qt::FramelessWindowHint); // 设置全屏 setWindowState(Qt::WindowState::WindowMaximized); this->setTitleBar(ui->wdgtTop); // 设置窗口位置和大小 move(0, 0); QRect screenRect = QApplication::screens().at(0)->availableGeometry(); qint16 screenWidth = screenRect.width(); qint16 screenHeight = screenRect.height(); resize(screenWidth, screenHeight); // 加载样式表 QString qssStyle = QssFileUtils::loadQssFileContent(":/qss/mainForm.css"); if (qssStyle.isEmpty() == false) { ui->wdgtTop->setStyleSheet(qssStyle); // 仅本窗口生效 } // 设置标题 ui->labelTitle->setText(SettingConfig::getInstance().CLIENT_TITLE); setWindowTitle(SettingConfig::getInstance().CLIENT_TITLE); // 设置最小尺寸(宽度, 高度) lxc QScreen *screen = QGuiApplication::primaryScreen(); QRect screenSize = screen->availableGeometry(); // 可用区域(排除任务栏) // 设置最小尺寸为屏幕宽度的 80%,高度 600 int minWidth = screenSize.width() * 0.8; this->setMinimumSize(minWidth, 600); } void MainWindowForm::initMenuWidgets() { wdgtInfoMain = new InfoMainForm(this); // wdgtInfo = new TF_InfoManag(this); // wdgtCheckMethod = new CheckMethodManage(this); // wdgtCheck = new CheckWindow(this); // wdgtData = new TF_TextDataManag(this); // wdgtCert = new TF_CertificateApproval(this); // wdgtUserMan = new TF_UserManag(this); // wdgtTestDev = new TF_TextDevice(this); // wdgtStandDev = new tf_StandardDevice(this); // wdgtStdDevForm = new StandardDeviceForm(this); // wdgtEqptDevForm = new TestDeviceForm(this); // wdgtFileResForm = new FileResourcesForm(this); // wdgtCalibrationForm = new CalibrationForm(this); // 绑定事件 // connect(wdgtCalibrationForm, &CalibrationForm::enableMenuButtons, this, &MainWindowForm::onMenuButtonsEabled); // connect(wdgtCheck, &CheckWindow::BarsetEnabled, this, &MainWindowForm::onMenuButtonsEabled); // 添加到stackWidget中 ui->wdgtContent->insertWidget(2, wdgtInfoMain); // ui->wdgtContent->insertWidget(0, wdgtCalibrationForm); // ui->wdgtContent->insertWidget(1, wdgtStdDevForm); // ui->wdgtContent->insertWidget(1, wdgtStandDev); // ui->wdgtContent->insertWidget(2, wdgtTestDev); // ui->wdgtContent->insertWidget(2, wdgtEqptDevForm); // ui->wdgtContent->insertWidget(3, wdgtCheckMethod); // ui->wdgtContent->insertWidget(4, wdgtCheck); // ui->wdgtContent->insertWidget(5, wdgtData); // ui->wdgtContent->insertWidget(6, wdgtCert); // ui->wdgtContent->insertWidget(7, wdgtFileResForm); // ui->wdgtContent->insertWidget(8, wdgtUserMan); } void MainWindowForm::initFunctionButtons() { // 查找所有 QPushButton 类型的子控件 QButtonGroup * funcButtGroup = new QButtonGroup(); // buttonGroup用于checked状态互斥 QList<QToolButton *> buttList = ui->wdgtFunc->findChildren<QToolButton *>(); for (int i = 0; i < buttList.size(); i++) { QToolButton * funcButt = buttList.at(i); funcButt->setProperty("index", i); funcButt->setCheckable(true); funcButtGroup->addButton(funcButt); connect(funcButt, &QToolButton::clicked, this, &MainWindowForm::onFunctionButtonsAction); } connect(this, SIGNAL(titleDblClick()), this, SLOT(titleDblClick())); } void MainWindowForm::on_btnMinWin_clicked() { setWindowState(Qt::WindowMinimized | windowState()); } void MainWindowForm::on_btnMaxWin_clicked() { QScreen *screen = QGuiApplication::primaryScreen(); QRect screenSize = screen->availableGeometry(); // 可用区域(排除任务栏) if (this->isMaximized()) { resize(screenSize.width() * 0.8, screenSize.height() * 0.8); move(QRandomGenerator::global()->bounded(10, 100), QRandomGenerator::global()->bounded(10, 50)); this->showNormal(); } else { resize(screenSize.width(), screenSize.height()); move(0, 0); this->showMaximized(); } } void MainWindowForm::on_btnCloseWin_clicked() { // 创建一个消息框 int ret = CustomMessageBox::confirm(this, "退出", "确定要退出登录吗?"); if (ret == 1) { LogUtil::PrintLog("INFO", QString("[%1]退出登录").arg(ProMemory::getInstance().getLoginUser().value("name").toString())); emit logout(); emit close(); } } void MainWindowForm::onFunctionButtonsAction() { QToolButton *btn = qobject_cast<QToolButton*>(sender()); if(btn) { btn->setChecked(true); ui->wdgtContent->setCurrentIndex(btn->property("index").toInt()); } } void MainWindowForm::onUpdateTimestampHandler() { QDateTime now = QDateTime::currentDateTime(); ui->labelTime->setText(now.toString("yyyy-MM-dd HH:mm:ss")); } void MainWindowForm::titleDblClick() { on_btnMaxWin_clicked(); } void MainWindowForm::onMenuButtonsEabled(bool enable) { QList<QToolButton *> menuButtonList = ui->wdgtFunc->findChildren<QToolButton *>(); for (QToolButton * btn : menuButtonList) { btn->setEnabled(enable); } }