OriginalWnd.cpp 14 KB

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