OriginalWnd.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "OriginalWnd.h"
  2. #include "Login.h"
  3. #include "ui_OriginalWnd.h"
  4. #include "OriginalWnd/ChartsAndCamerasWnd.h"
  5. #include "OriginalWnd/MainAndSecondaryCamerasWnd.h"
  6. #include "OriginalWnd/SingleCameraOperationWnd.h"
  7. #include "OriginalWnd/DbTreeViewManager.h"
  8. #include "Src/Sql/SqlOperation.h"
  9. #include <QDebug>
  10. #include <QJsonDocument>
  11. #include <QFile>
  12. #include <QFileDialog>
  13. #include <QDateTime>
  14. #include <QTimer>
  15. #include <QSettings>
  16. #include <QMouseEvent>
  17. #include <QDir>
  18. OriginalWnd::OriginalWnd(QWidget* parent, CameraBind* pCameraBind)
  19. : QMainWindow(parent)
  20. , ui(new Ui::OriginalWnd)
  21. {
  22. ui->setupUi(this);
  23. m_pCameraBind = pCameraBind;
  24. m_pCameraDistribute = CameraDistribute::GetInstance();
  25. StatusBar();
  26. RegFun();
  27. if (gen_if.getLanguageValue() == 0)//TODO: 这里有问题吧.... ==-1 也加载?
  28. {
  29. }
  30. else
  31. {
  32. // setChineseMode(this);
  33. }
  34. InitForm();
  35. //DisableControls(true);
  36. }
  37. OriginalWnd::~OriginalWnd()
  38. {
  39. Del();
  40. delete ui;
  41. }
  42. void OriginalWnd::CameraBindInit(CameraBind* pCameraBind)
  43. {
  44. m_pCameraBind = pCameraBind;
  45. }
  46. void OriginalWnd::RegFun()
  47. {
  48. qRegisterMetaType<CAxis::AXIS_TYPE>("CAxis::AXIS_TYPE");
  49. qRegisterMetaType<ns_module::ST_BUTTON_FUN>("ns_module::ST_BUTTON_FUN");
  50. }
  51. QWidget* OriginalWnd::getWidgetLeft() const
  52. {
  53. return ui->widget;
  54. }
  55. void OriginalWnd::SwitchToConfig(const QString& name, int configId, int menuArrayIndex, bool isMenuArray /*= true*/)
  56. {
  57. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  58. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  59. m_pdbTreeViewManager->currentMenuId = configId;
  60. if (isMenuArray)
  61. {
  62. if (m_pdbTreeViewManager->menuArray[menuArrayIndex].isthird == true) {
  63. m_pdbTreeViewManager->loadpage(configId);
  64. }
  65. }
  66. if (name == "Home")
  67. {
  68. IsOpenToolsWidgetSlots(false);
  69. }
  70. }
  71. void OriginalWnd::switchMsgConfig()
  72. {
  73. SwitchToConfig("Msg", 1, 0, false);
  74. }
  75. void OriginalWnd::switchToHomeConfig()
  76. {
  77. SwitchToConfig("Home", 1, 0, false);
  78. //IsOpenToolsWidgetSlots(false);
  79. }
  80. void OriginalWnd::switchToProductionConfig()
  81. {
  82. SwitchToConfig("ProductionAssist", 2, 0);
  83. }
  84. void OriginalWnd::switchToDiagnosisConfig()
  85. {
  86. SwitchToConfig("Diagnosis", 2, 0);
  87. }
  88. void OriginalWnd::switchToDispositionConfig()
  89. {
  90. SwitchToConfig("Calibration", 6, 4);
  91. }
  92. void OriginalWnd::switchToModuleConfig()
  93. {
  94. SwitchToConfig("ModuleConfig", 4, 3);
  95. }
  96. void OriginalWnd::switchToProgramConfig()
  97. {
  98. SwitchToConfig("Programme", 5, 1);
  99. }
  100. void OriginalWnd::switchToSystemConfig()
  101. {
  102. QString name = "SystemInfor";
  103. int configId = 7; // System_config.json 对应的 ConfigId
  104. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  105. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  106. }
  107. void OriginalWnd::DisableControls(bool isDisable)
  108. {
  109. ui->menuFunctionsSwitchWidget->setDisabled(isDisable);
  110. ui->widget->setDisabled(isDisable);
  111. }
  112. void OriginalWnd::InitForm()
  113. {
  114. InitTreeViewManager();
  115. m_nTimerID = startTimer(100);
  116. m_pLogStateSidebar = new LogStateSidebar(this);
  117. m_pLogStateSidebar->move(-m_pLogStateSidebar->width(), 0);
  118. m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height());
  119. ui->stopButton->setIcon(QIcon(":/images/light/stop.png"));
  120. ui->startButton->setIcon(QIcon(":/images/light/start.png"));
  121. ui->label_userlogo->setPixmap(QPixmap(":/images/TopStatusBar/user.png").scaled(20, 20, Qt::KeepAspectRatio));
  122. ui->label_username->setText(tr("SBT_QQ", "SBT_QQ"));
  123. ui->label_username->setStyleSheet("QLabel { color : White; }"
  124. "QLabel:hover { color : red; }");
  125. CreateSwitchMainPage();
  126. ui->label_userlogo->installEventFilter(this);
  127. ui->label_username->installEventFilter(this);
  128. // 这个地方是否也要保存切换?
  129. SwitchToConfig("Home", 1, 0, false);
  130. }
  131. void OriginalWnd::InitTreeViewManager()
  132. {
  133. //TODO: 不行的话直接m_pCameraBind 传递当前指针,这样不会存在释放问题
  134. //m_pCameraBind
  135. m_pdbTreeViewManager.reset(new DbTreeViewManager(ui->menuFunctionsSwitchWidget, this));
  136. connect(m_pdbTreeViewManager.get(), &DbTreeViewManager::RunFunSignals, this, &OriginalWnd::JRunFunSlots);
  137. connect(m_pdbTreeViewManager.get(), &DbTreeViewManager::IsOpenToolsWidgetSig, this, &OriginalWnd::IsOpenToolsWidgetSlots);
  138. // 把TreeViewManager放到页面上
  139. QVBoxLayout* layout = new QVBoxLayout(ui->menuFunctionsSwitchWidget);
  140. layout->addWidget(m_pdbTreeViewManager.get());
  141. ui->menuFunctionsSwitchWidget->setLayout(layout);
  142. IsOpenToolsWidgetSlots(false);
  143. }
  144. void OriginalWnd::UpdateTime()
  145. {
  146. QDateTime currentDateTime = QDateTime::currentDateTime();
  147. QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
  148. ui->label_time->setText(timeStr);
  149. //// 用户也要放到注册表?
  150. //QSettings settings("YourCompany_2", "YourAppName_2");
  151. //QString userName = settings.value("userName", "???").toString();
  152. //ui->label_username->setText(userName);
  153. }
  154. void OriginalWnd::Del()
  155. {
  156. killTimer(m_nTimerID);
  157. }
  158. void OriginalWnd::RunOrStopSwitch(bool isStart /*= false*/)
  159. {
  160. if (m_pCameraBind)
  161. {
  162. if (isStart)
  163. {
  164. m_pCameraBind->JRunAutoBond();
  165. }
  166. else
  167. {
  168. m_pCameraBind->JRunStop();
  169. }
  170. ui->startButton->setDisabled(isStart);
  171. }
  172. }
  173. bool OriginalWnd::eventFilter(QObject* obj, QEvent* event)
  174. {
  175. if (obj == this->ui->label_userlogo || obj == this->ui->label_username)
  176. {
  177. //判断事件类型是否为鼠标事件
  178. if (event->type() == QEvent::MouseButtonPress)
  179. {
  180. //转换为鼠标事件
  181. QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
  182. //判断鼠标左键点击
  183. if (mouseenevt->button() == Qt::LeftButton)
  184. {
  185. if (QStackedWidget* parent = qobject_cast<QStackedWidget*>(this->parentWidget()))
  186. {
  187. //Login* loginWnd = new Login(this);
  188. QPointer<Login> login = Login::instance();
  189. parent->addWidget(login);
  190. parent->setCurrentIndex(parent->indexOf(login));
  191. }
  192. }
  193. }
  194. }
  195. return QWidget::eventFilter(obj, event);
  196. }
  197. void OriginalWnd::timerEvent(QTimerEvent* event)
  198. {
  199. if (m_nTimerID == event->timerId())
  200. {
  201. UpdateTime();
  202. }
  203. }
  204. void OriginalWnd::resizeEvent(QResizeEvent* event)
  205. {
  206. if (m_pLogStateSidebar)
  207. {
  208. m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height());
  209. }
  210. QMainWindow::resizeEvent(event);
  211. }
  212. void OriginalWnd::on_ChartButton_clicked()
  213. {
  214. CreateSwitchMainPage(EN_WND_SWITCH::ChartsAnd);
  215. }
  216. void OriginalWnd::on_DoubleImageButton_clicked()
  217. {
  218. CreateSwitchMainPage(EN_WND_SWITCH::MainAndSecondary);
  219. }
  220. void OriginalWnd::on_SingleImageButton_clicked()
  221. {
  222. CreateSwitchMainPage(EN_WND_SWITCH::SingleCameraOperation);
  223. }
  224. void OriginalWnd::on_LogStatBut_clicked()
  225. {
  226. if (m_pLogStateSidebar->isExpanded())
  227. {
  228. m_pLogStateSidebar->HideSidebar();
  229. }
  230. else
  231. {
  232. m_pLogStateSidebar->ShowSidebar();
  233. }
  234. }
  235. void OriginalWnd::on_startButton_clicked()
  236. {
  237. RunOrStopSwitch(true);
  238. }
  239. void OriginalWnd::on_stopButton_clicked()
  240. {
  241. RunOrStopSwitch();
  242. }
  243. void OriginalWnd::on_saveBut_clicked()
  244. {
  245. if (m_pdbTreeViewManager != nullptr)
  246. {
  247. m_pdbTreeViewManager->IsSaveData(true);
  248. }
  249. }
  250. void OriginalWnd::DisableAllFeatures(bool isDisable)
  251. {
  252. emit sendDisableFeaturesSig(isDisable);
  253. ui->startButton->setDisabled(isDisable);
  254. ui->ChartButton->setDisabled(isDisable);
  255. ui->DoubleImageButton->setDisabled(isDisable);
  256. ui->SingleImageButton->setDisabled(isDisable);
  257. // 树菜单
  258. ui->menuFunctionsSwitchWidget->setDisabled(isDisable);
  259. //TODO:这里有歧义
  260. ui->widget->setDisabled(isDisable);
  261. }
  262. void OriginalWnd::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut)
  263. {
  264. if (m_pCameraBind.isNull())
  265. {
  266. RegFun();
  267. }
  268. m_pCameraBind->SetImageWnd(pWnd);
  269. pWnd->InitMainCameraBind(m_pCameraBind);
  270. ui->switchShowPageUI->addWidget(pWnd);
  271. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd));
  272. // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
  273. tooBut->setChecked(true);
  274. m_pJOriginalMainWnd = pWnd;
  275. }
  276. void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/)
  277. {
  278. ui->switchShowPageUI->removeWidget(m_pJOriginalMainWnd);
  279. m_pJOriginalMainWnd->deleteLater();
  280. m_pJOriginalMainWnd = nullptr;
  281. QSettings settings("OrganizationName__", "ApplicationName__");
  282. if (nlastIndex <= 0)
  283. {
  284. nlastIndex = settings.value("lastIndex", 1).toInt();
  285. }
  286. else
  287. {
  288. settings.setValue("lastIndex", nlastIndex);
  289. }
  290. // 切换窗口
  291. switch (nlastIndex)
  292. {
  293. case EN_WND_SWITCH::ChartsAnd:
  294. {
  295. ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd;
  296. SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton);
  297. }
  298. break;
  299. case EN_WND_SWITCH::MainAndSecondary:
  300. {
  301. MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
  302. SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton);
  303. }
  304. break;
  305. case EN_WND_SWITCH::SingleCameraOperation:
  306. {
  307. SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd;
  308. SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton);
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. void OriginalWnd::onHandleLanguageSlots(const int& data)
  316. {
  317. if (data == 0)
  318. {
  319. ui->retranslateUi(this);
  320. }
  321. else
  322. {
  323. //setChineseMode(this);
  324. }
  325. }
  326. void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra)
  327. {
  328. m_pCameraBind->JRunButton(pra);
  329. }
  330. void OriginalWnd::IsOpenToolsWidgetSlots(bool isOpen)
  331. {
  332. int widgetHeight = ui->menuFunctionsSwitchWidget->height();
  333. if (isOpen)
  334. {
  335. if (widgetHeight == 1009)
  336. {
  337. widgetHeight -= 58;
  338. }
  339. ui->horizontalWidget->show();
  340. }
  341. else
  342. {
  343. if (widgetHeight != 1009)
  344. {
  345. widgetHeight += 58;
  346. }
  347. ui->horizontalWidget->hide();
  348. }
  349. ui->menuFunctionsSwitchWidget->resize(ui->menuFunctionsSwitchWidget->width(), widgetHeight);
  350. }
  351. void OriginalWnd::StatusBar()
  352. {
  353. enum DEVICE_STATUS currentStatus = DEVICE_STATUS::DIAGNOSE;
  354. updateCalibrationButtonColor(currentStatus);
  355. enum CONNECT_STATUS currentStatus2 = OriginalWnd::CONNECT_STATUS::CONNECT_OK;
  356. updateMachineLinkColor(currentStatus2);
  357. enum HOST_STATUS currentStatus3 = OriginalWnd::HOST_STATUS::HOST_CON_DEVICE;
  358. updateHostStatusColor(currentStatus3);
  359. }
  360. void OriginalWnd::retranslateUi()
  361. {
  362. ui->retranslateUi(this);
  363. }
  364. void OriginalWnd::updateCalibrationButtonColor(enum DEVICE_STATUS status)
  365. {
  366. QString styleSheet;
  367. QString buttonText;
  368. switch (status) {
  369. case DEVICE_STATUS::IDLE: // 停机待机
  370. styleSheet = "QPushButton { background-color: green; color: white; }";
  371. buttonText = "IDLE";
  372. break;
  373. case DEVICE_STATUS::RUNING: // 生产
  374. styleSheet = "QPushButton { background-color: green; color: white; }";
  375. buttonText = "RUNING";
  376. break;
  377. case DEVICE_STATUS::CALIB: // 校准
  378. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  379. buttonText = "CALIB";
  380. break;
  381. case DEVICE_STATUS::WARN: // 报警
  382. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  383. buttonText = "WARN";
  384. break;
  385. case DEVICE_STATUS::FAULT: // 故障
  386. styleSheet = "QPushButton { background-color: red; color: white; }";
  387. buttonText = "FAULT";
  388. break;
  389. case DEVICE_STATUS::DIAGNOSE: // 诊断
  390. styleSheet = "QPushButton { background-color: red; color: white; }";
  391. buttonText = "DIAGNOSE";
  392. break;
  393. default: // 未知状态
  394. styleSheet = "QPushButton { background-color: gray; color: white; }";
  395. buttonText = "未知状态";
  396. break;
  397. }
  398. // 设置按钮样式和文本
  399. ui->CalibrationButton->setStyleSheet(styleSheet);
  400. ui->CalibrationButton->setText(buttonText);
  401. }
  402. void OriginalWnd::updateMachineLinkColor(enum CONNECT_STATUS status)
  403. {
  404. QString styleSheet;
  405. QString buttonText;
  406. switch (status) {
  407. case CONNECT_STATUS::NO_CONNECT: // 未连接
  408. styleSheet = "QPushButton { background-color: gray; color: white; }";
  409. break;
  410. case CONNECT_STATUS::CONNECT_ING: // 连接中
  411. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  412. break;
  413. case CONNECT_STATUS::MACHINE_TIMEOUT: // 机台超时
  414. styleSheet = "QPushButton { background-color: orange; color: black; }";
  415. break;
  416. case CONNECT_STATUS::HOST_TIMEOUT: // 主机超时
  417. styleSheet = "QPushButton { background-color: blue; color: white; }";
  418. break;
  419. case CONNECT_STATUS::CONNECT_OK: // 连接成功
  420. styleSheet = "QPushButton { background-color: green; color: white; }";
  421. break;
  422. default: // 未知状态
  423. styleSheet = "QPushButton { background-color: gray; color: white; }";
  424. break;
  425. }
  426. // 设置按钮样式
  427. ui->MachineLink->setStyleSheet(styleSheet);
  428. }
  429. void OriginalWnd::updateHostStatusColor(enum HOST_STATUS status)
  430. {
  431. QString styleSheet;
  432. QString buttonText;
  433. switch (status) {
  434. case HOST_STATUS::HOST_OUT: // 主机离线
  435. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  436. break;
  437. case HOST_STATUS::DEVICE_OUT: // 设备离线
  438. styleSheet = "QPushButton { background-color: orange; color: black; }";
  439. break;
  440. case HOST_STATUS::DEVICE_ING: // 设备试图连线
  441. styleSheet = "QPushButton { background-color: magenta; color: white; }";
  442. break;
  443. case HOST_STATUS::DEVICE_OK: // 设备在线
  444. styleSheet = "QPushButton { background-color: blue; color: white; }";
  445. break;
  446. case HOST_STATUS::HOST_CON_DEVICE: // 主机控制设备
  447. styleSheet = "QPushButton { background-color: green; color: white; }";
  448. break;
  449. default: // 未知状态
  450. styleSheet = "QPushButton { background-color: gray; color: white; }";
  451. break;
  452. }
  453. // 设置按钮样式和文本
  454. ui->HostStatus->setStyleSheet(styleSheet);
  455. }
  456. void OriginalWnd::changeEvent(QEvent* event)
  457. {
  458. if (event->type() == QEvent::LanguageChange) {
  459. ui->retranslateUi(this);
  460. }
  461. }