#include "OriginalWnd.h" #include "Login.h" #include "ui_OriginalWnd.h" #include "OriginalWnd/ChartsAndCamerasWnd.h" #include "OriginalWnd/MainAndSecondaryCamerasWnd.h" #include "OriginalWnd/SingleCameraOperationWnd.h" #include "OriginalWnd/DbTreeViewManager.h" #include "Src/Sql/SqlOperation.h" #include #include #include #include #include #include #include #include #include OriginalWnd::OriginalWnd(QWidget* parent, CameraBind* pCameraBind) : QMainWindow(parent) , ui(new Ui::OriginalWnd) { ui->setupUi(this); m_pCameraBind = pCameraBind; m_pCameraDistribute = CameraDistribute::GetInstance(); StatusBar(); RegFun(); if (gen_if.getLanguageValue() == 0)//TODO: 这里有问题吧.... ==-1 也加载? { } else { // setChineseMode(this); } InitForm(); } OriginalWnd::~OriginalWnd() { Del(); delete ui; } void OriginalWnd::CameraBindInit(CameraBind* pCameraBind) { m_pCameraBind= pCameraBind; } void OriginalWnd::RegFun() { qRegisterMetaType("CAxis::AXIS_TYPE"); qRegisterMetaType("ns_module::ST_BUTTON_FUN"); } QWidget* OriginalWnd::getWidgetLeft() const { return ui->widget; } void OriginalWnd::SwitchToConfig(const QString& name, int configId, int menuArrayIndex, bool isMenuArray /*= true*/) { int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取 m_pdbTreeViewManager->initializeTree(name, userPrivilege); m_pdbTreeViewManager->currentMenuId = configId; if (isMenuArray) { if (m_pdbTreeViewManager->menuArray[menuArrayIndex].isthird == true) { m_pdbTreeViewManager->loadpage(configId); } } } void OriginalWnd::switchToHomeConfig() { SwitchToConfig("Home", 1, 0, false); } void OriginalWnd::switchToProductionConfig() { SwitchToConfig("ProductionAssist", 2, 0); } void OriginalWnd::switchToDiagnosisConfig() { SwitchToConfig("Diagnosis", 2, 0); } void OriginalWnd::switchToDispositionConfig() { SwitchToConfig("Calibration", 6, 4); } void OriginalWnd::switchToModuleConfig() { SwitchToConfig("ModuleConfig", 4, 3); } void OriginalWnd::switchToProgramConfig() { SwitchToConfig("Programme", 5, 1); } void OriginalWnd::switchToSystemConfig() { QString name = "SystemInfor"; int configId = 7; // System_config.json 对应的 ConfigId int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取 m_pdbTreeViewManager->initializeTree(name, userPrivilege); } void OriginalWnd::InitForm() { InitTreeViewManager(); m_nTimerID = startTimer(100); m_pLogStateSidebar = new LogStateSidebar(this); m_pLogStateSidebar->move(-m_pLogStateSidebar->width(), 0); m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height()); //给stopbutton加样式 QString stopButton_styleSheet = "QToolButton:hover { background: #cc0000; }" "QToolButton:pressed { background: #990000; }"; ui->stopButton->setStyleSheet(stopButton_styleSheet); ui->stopButton->setIcon(QIcon(":/images/light/stop.png")); ui->startButton->setIcon(QIcon(":/images/light/start.png")); ui->label_userlogo->setPixmap(QPixmap(":/images/TopStatusBar/user.png").scaled(20, 20, Qt::KeepAspectRatio)); ui->label_username->setText(tr("SBT_QQ", "SBT_QQ")); ui->label_username->setStyleSheet("QLabel { color : White; }" "QLabel:hover { color : red; }"); CreateSwitchMainPage(); ui->label_userlogo->installEventFilter(this); ui->label_username->installEventFilter(this); // 这个地方是否也要保存切换? SwitchToConfig("Home", 1, 0, false); } void OriginalWnd::InitTreeViewManager() { m_pdbTreeViewManager.reset(new DbTreeViewManager(ui->menuFunctionsSwitchWidget, this)); connect(m_pdbTreeViewManager.get(), &DbTreeViewManager::RunFunSignals, this, &OriginalWnd::JRunFunSlots); // 把TreeViewManager放到页面上 QVBoxLayout* layout = new QVBoxLayout(ui->menuFunctionsSwitchWidget); layout->addWidget(m_pdbTreeViewManager.get()); ui->menuFunctionsSwitchWidget->setLayout(layout); } void OriginalWnd::UpdateTime() { QDateTime currentDateTime = QDateTime::currentDateTime(); QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss"); ui->label_time->setText(timeStr); //// 用户也要放到注册表? //QSettings settings("YourCompany_2", "YourAppName_2"); //QString userName = settings.value("userName", "???").toString(); //ui->label_username->setText(userName); } void OriginalWnd::Del() { killTimer(m_nTimerID); } void OriginalWnd::RunOrStopSwitch() { // 目前样式切换需要调整 if (m_isDisabled) { // 当按钮处于禁用状态时,执行停止操作并启用按钮 QString styleSheet = "QToolButton { background: #4AB832; color: #FFFFFF; }" "QToolButton:hover { background: #00FF00; }"; ui->startButton->setDisabled(m_isDisabled); ui->stopButton->setDisabled(!m_isDisabled); ui->startButton->setStyleSheet(styleSheet); m_pCameraBind->JRunAutoBond(); } else { // 当按钮处于启用状态时,执行开始操作并禁用按钮 QString styleSheet = "QToolButton:disabled { color: #808080; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F8FBFF, stop: 1 #E7ECF3); }"; ui->startButton->setDisabled(m_isDisabled); ui->stopButton->setDisabled(!m_isDisabled); ui->stopButton->setStyleSheet(styleSheet); m_pCameraBind->JRunStop(); } m_isDisabled = !m_isDisabled; // 切换状态 } bool OriginalWnd::eventFilter(QObject* obj, QEvent* event) { if (obj == this->ui->label_userlogo || obj == this->ui->label_username) { //判断事件类型是否为鼠标事件 if (event->type() == QEvent::MouseButtonPress) { //转换为鼠标事件 QMouseEvent* mouseenevt = static_cast(event); //判断鼠标左键点击 if (mouseenevt->button() == Qt::LeftButton) { if (QStackedWidget* parent = qobject_cast(this->parentWidget())) { Login* loginWnd = new Login(this); parent->addWidget(loginWnd); parent->setCurrentIndex(parent->indexOf(loginWnd)); } } } } return QWidget::eventFilter(obj, event); } void OriginalWnd::timerEvent(QTimerEvent* event) { if (m_nTimerID == event->timerId()) { UpdateTime(); } } void OriginalWnd::resizeEvent(QResizeEvent* event) { if (m_pLogStateSidebar) { m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height()); } QMainWindow::resizeEvent(event); } void OriginalWnd::on_ChartButton_clicked() { CreateSwitchMainPage(EN_WND_SWITCH::ChartsAnd); } void OriginalWnd::on_DoubleImageButton_clicked() { CreateSwitchMainPage(EN_WND_SWITCH::MainAndSecondary); } void OriginalWnd::on_SingleImageButton_clicked() { CreateSwitchMainPage(EN_WND_SWITCH::SingleCameraOperation); } void OriginalWnd::on_LogStatBut_clicked() { if (m_pLogStateSidebar->isExpanded()) { m_pLogStateSidebar->HideSidebar(); } else { m_pLogStateSidebar->ShowSidebar(); } } void OriginalWnd::on_startButton_clicked() { RunOrStopSwitch(); } void OriginalWnd::on_stopButton_clicked() { RunOrStopSwitch(); } void OriginalWnd::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut) { if (m_pCameraBind.isNull()) { RegFun(); } m_pCameraBind->SetImageWnd(pWnd); pWnd->InitMainCameraBind(m_pCameraBind); ui->switchShowPageUI->addWidget(pWnd); ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd)); // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;"); tooBut->setChecked(true); m_pJOriginalMainWnd = pWnd; } void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/) { QSettings settings("OrganizationName__", "ApplicationName__"); if (nlastIndex <= 0) { nlastIndex = settings.value("lastIndex", 1).toInt(); } else { settings.setValue("lastIndex", nlastIndex); } // 切换窗口 switch (nlastIndex) { case EN_WND_SWITCH::ChartsAnd: { ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd; SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton); } break; case EN_WND_SWITCH::MainAndSecondary: { MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd; SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton); } break; case EN_WND_SWITCH::SingleCameraOperation: { SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd; SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton); } break; default: break; } } void OriginalWnd::onHandleLanguageSignal(const int& data) { if (data == 0) { ui->retranslateUi(this); } else { //setChineseMode(this); } } void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra) { m_pCameraBind->JRunButton(pra); } void OriginalWnd::StatusBar() { enum DEVICE_STATUS currentStatus = DEVICE_STATUS::IDLE; updateCalibrationButtonColor(currentStatus); enum CONNECT_STATUS currentStatus2 = OriginalWnd::CONNECT_STATUS::NO_CONNECT; updateMachineLinkColor(currentStatus2); enum HOST_STATUS currentStatus3 = OriginalWnd::HOST_STATUS::DEVICE_OK; updateHostStatusColor(currentStatus3); } void OriginalWnd::retranslateUi() { ui->retranslateUi(this); } void OriginalWnd::updateCalibrationButtonColor(enum DEVICE_STATUS status) { QString styleSheet; QString buttonText; switch (status) { case DEVICE_STATUS::IDLE: // 停机待机 styleSheet = "QPushButton { background-color: green; color: white; }"; buttonText = "IDLE"; break; case DEVICE_STATUS::RUNING: // 生产 styleSheet = "QPushButton { background-color: green; color: white; }"; buttonText = "RUNING"; break; case DEVICE_STATUS::CALIB: // 校准 styleSheet = "QPushButton { background-color: yellow; color: black; }"; buttonText = "CALIB"; break; case DEVICE_STATUS::WARN: // 报警 styleSheet = "QPushButton { background-color: yellow; color: black; }"; buttonText = "WARN"; break; case DEVICE_STATUS::FAULT: // 故障 styleSheet = "QPushButton { background-color: red; color: white; }"; buttonText = "FAULT"; break; case DEVICE_STATUS::DIAGNOSE: // 诊断 styleSheet = "QPushButton { background-color: red; color: white; }"; buttonText = "DIAGNOSE"; break; default: // 未知状态 styleSheet = "QPushButton { background-color: gray; color: white; }"; buttonText = "未知状态"; break; } // 设置按钮样式和文本 ui->CalibrationButton->setStyleSheet(styleSheet); ui->CalibrationButton->setText(buttonText); } void OriginalWnd::updateMachineLinkColor(enum CONNECT_STATUS status) { QString styleSheet; QString buttonText; switch (status) { case CONNECT_STATUS::NO_CONNECT: // 未连接 styleSheet = "QPushButton { background-color: gray; color: white; }"; break; case CONNECT_STATUS::CONNECT_ING: // 连接中 styleSheet = "QPushButton { background-color: yellow; color: black; }"; break; case CONNECT_STATUS::MACHINE_TIMEOUT: // 机台超时 styleSheet = "QPushButton { background-color: orange; color: black; }"; break; case CONNECT_STATUS::HOST_TIMEOUT: // 主机超时 styleSheet = "QPushButton { background-color: blue; color: white; }"; break; case CONNECT_STATUS::CONNECT_OK: // 连接成功 styleSheet = "QPushButton { background-color: green; color: white; }"; break; default: // 未知状态 styleSheet = "QPushButton { background-color: gray; color: white; }"; break; } // 设置按钮样式 ui->MachineLink->setStyleSheet(styleSheet); } void OriginalWnd::updateHostStatusColor(enum HOST_STATUS status) { QString styleSheet; QString buttonText; switch (status) { case HOST_STATUS::HOST_OUT: // 主机离线 styleSheet = "QPushButton { background-color: yellow; color: black; }"; break; case HOST_STATUS::DEVICE_OUT: // 设备离线 styleSheet = "QPushButton { background-color: orange; color: black; }"; break; case HOST_STATUS::DEVICE_ING: // 设备试图连线 styleSheet = "QPushButton { background-color: magenta; color: white; }"; break; case HOST_STATUS::DEVICE_OK: // 设备在线 styleSheet = "QPushButton { background-color: blue; color: white; }"; break; case HOST_STATUS::HOST_CON_DEVICE: // 主机控制设备 styleSheet = "QPushButton { background-color: green; color: white; }"; break; default: // 未知状态 styleSheet = "QPushButton { background-color: gray; color: white; }"; break; } // 设置按钮样式和文本 ui->HostStatus->setStyleSheet(styleSheet); } void OriginalWnd::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); } }