#include "QModelMangerUI.h" #include #include #include #include #include "QPLCIndexUI.h" #include "WfModel.h" #pragma execution_character_set("utf-8") QModelMangerUI::QModelMangerUI(QWidget *parent) : QWidget(parent) { ui.setupUi(this); connect(ui.wf_model_select_button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); connect(ui.wf_model_add_button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); connect(ui.wf_model_mod_button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); connect(ui.wf_model_delete_button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); QRegExp regx("[a-zA-Z0-9]+$"); QSharedPointer validator = QSharedPointer(new QRegExpValidator(regx)); ui.wf_model_input_edit->setValidator(validator.data()); connect(ui.wf_chkbox_read_mode_from_plc, SIGNAL(stateChanged(int)), this, SLOT(onCheckBoxChange(int))); } QModelMangerUI::~QModelMangerUI() { } Q_SLOT void QModelMangerUI::onButtonClicked() { QString strObj = sender()->objectName(); if ("wf_model_select_button" == strObj) { if (!m_pCtrl->IOnlineMode()) { IStation *pStation = getStation(); if (pStation) { QString strModel = pStation->currentSelectModel(); if (!strModel.isEmpty()) { m_pCtrl->ISelModel(currentTab(), strModel); ui.mLblModelState->setText(tr("current %1 OK!").arg(strModel)); m_pCtrl->ISetModifyModel(true); } } } } else if ("wf_model_add_button" == strObj) { QString strModel = ui.wf_model_input_edit->text(); if (!strModel.isEmpty() && !strModel.contains(_WF_UNIQUE_SPLIT) && !strModel.contains("##") && strModel.size() >= 3 && strModel.size() <= 20 && !strModel.contains(" ")) { int nIndex = currentTab(); bool bFlag = m_pCtrl->IAddModel(nIndex, strModel); if (bFlag) { ui.mLblModelState->setText(tr("add %1 successful!").arg(strModel)); ui.wf_model_input_edit->setText(""); } else { ui.mLblModelState->setText(tr("add %1 failed!").arg(strModel)); } } else { ui.mLblModelState->setText(QObject::tr("模型号中不能包括中文字符、空格,字符个数在3与20之间!")); } QTimer::singleShot(5000, [&]() { ui.mLblModelState->setText(""); }); } else if ("wf_model_mod_button" == strObj) { } else if ("wf_model_delete_button" == strObj) { IStation *pStation = getStation(); if (pStation) { QString strModel = pStation->currentRunningModel(); if (!strModel.isEmpty()) { QString strName = m_pCtrl->IGetCurrentRuningModel(currentTab()); if (strModel == strName) { QMessageBox msgWarning(QMessageBox::Warning, QObject::tr("错误警告"), strModel + QObject::tr(" 正在使用,不能被删除"), QMessageBox::Yes); msgWarning.setWindowIcon(QIcon(":/leaper/app.png")); msgWarning.setButtonText(QMessageBox::Yes, QObject::tr("确认")); msgWarning.exec(); return ; } QMessageBox msgBox(QMessageBox::Warning, QObject::tr("型号删除"), QObject::tr("删除") + strModel + "?", QMessageBox::Yes | QMessageBox::No); msgBox.setWindowIcon(QIcon(":/leaper/app.png")); if (QMessageBox::Yes == msgBox.exec()) { bool b = m_pCtrl->IDeleteModel(currentTab(), strModel); if (!b) { ui.mLblModelState->setText(tr("delete %1 failed!").arg(strModel)); } else { ui.mLblModelState->setText(tr("delete %1 successful!").arg(strModel)); } } } } } } int QModelMangerUI::lastNum(QString str) { return str.right(1).toInt(); } Q_SLOT bool QModelMangerUI::OnCellDoubleClicked(const QModelIndex &index) { QString strListName = sender()->objectName(); if (1 != index.column()) { return false; } int stationID = lastNum(strListName); IStation *pStation = getStation(stationID); if (pStation) { QString strModel = pStation->model(index.row()); QString strName = QObject::tr("模型号:") + strModel; WfModel *pModel = pStation->wfModel(strModel); if (!pModel) { return false; } QPLCIndexUI dlg(this); dlg.setModelName(strName); dlg.setModelIndex(pModel->nIndex); if (dlg.exec() == QDialog::Accepted) { int nIndex = dlg.getModelIndex(); pModel->nIndex = nIndex; } } return true; } Q_SLOT void QModelMangerUI::onCheckBoxChange(int state) { if (state > 0) { ui.wf_model_select_button->setDisabled(true); if (m_pCtrl) m_pCtrl->ISetOnlineModel(true); } else { ui.wf_model_select_button->setDisabled(false); if (m_pCtrl) m_pCtrl->ISetOnlineModel(false); } } IStation * QModelMangerUI::getStation(int nIndex) { if (nIndex == -1) { QWidget *pWidget = ui.tabWidgetStation->currentWidget(); if (pWidget) { nIndex = lastNum(pWidget->objectName()); } } return m_pCtrl->IGetStationById(nIndex); } void QModelMangerUI::showEvent(QShowEvent *event) { if (m_pCtrl) { if (m_pCtrl->IOnlineMode() == true) { ui.wf_chkbox_read_mode_from_plc->setChecked(true); } else { ui.wf_chkbox_read_mode_from_plc->setChecked(false); } } } void QModelMangerUI::closeEvent(QCloseEvent *event) { } void QModelMangerUI::onInitModelList(IWfCtrl *pCtrl) { m_pCtrl = pCtrl; if (m_pCtrl) { ui.tabWidgetStation->clear(); QStringList lst = m_pCtrl->IGetStationKeys(); for (int i = 0; i < lst.size(); i++) { QTableView *pW = new QTableView(ui.tabWidgetStation); IStation *pStation = m_pCtrl->IGetStationByKey(lst.at(i)); QString strName = "wf_cam_listwidget_" + QString::number(pStation->stationId()); pW->setObjectName(strName); int nIndex = pStation->stationId()-1; ui.tabWidgetStation->insertTab(nIndex, (QWidget*)pW, pStation->stationShowName()); pStation->setWidget(strName, pW); connect(pW, SIGNAL(doubleClicked(const QModelIndex &)),this, SLOT(OnCellDoubleClicked(const QModelIndex &))); } } } int QModelMangerUI::currentTab() { QWidget *pWidget = ui.tabWidgetStation->currentWidget(); if (pWidget) { return lastNum(pWidget->objectName()); } return -1; }