OriginalWnd.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. }
  36. OriginalWnd::~OriginalWnd()
  37. {
  38. Del();
  39. delete ui;
  40. }
  41. void OriginalWnd::CameraBindInit(CameraBind* pCameraBind)
  42. {
  43. m_pCameraBind = pCameraBind;
  44. }
  45. void OriginalWnd::RegFun()
  46. {
  47. qRegisterMetaType<CAxis::AXIS_TYPE>("CAxis::AXIS_TYPE");
  48. qRegisterMetaType<ns_module::ST_BUTTON_FUN>("ns_module::ST_BUTTON_FUN");
  49. }
  50. QWidget* OriginalWnd::getWidgetLeft() const
  51. {
  52. return ui->widget;
  53. }
  54. void OriginalWnd::SwitchToConfig(const QString& name, int configId, int menuArrayIndex, bool isMenuArray /*= true*/)
  55. {
  56. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  57. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  58. m_pdbTreeViewManager->currentMenuId = configId;
  59. if (isMenuArray)
  60. {
  61. if (m_pdbTreeViewManager->menuArray[menuArrayIndex].isthird == true) {
  62. m_pdbTreeViewManager->loadpage(configId);
  63. }
  64. }
  65. }
  66. void OriginalWnd::switchMsgConfig()
  67. {
  68. SwitchToConfig("Msg", 1, 0, false);
  69. }
  70. void OriginalWnd::switchToHomeConfig()
  71. {
  72. SwitchToConfig("Home", 1, 0, false);
  73. }
  74. void OriginalWnd::switchToProductionConfig()
  75. {
  76. SwitchToConfig("ProductionAssist", 2, 0);
  77. }
  78. void OriginalWnd::switchToDiagnosisConfig()
  79. {
  80. SwitchToConfig("Diagnosis", 2, 0);
  81. }
  82. void OriginalWnd::switchToDispositionConfig()
  83. {
  84. SwitchToConfig("Calibration", 6, 4);
  85. }
  86. void OriginalWnd::switchToModuleConfig()
  87. {
  88. SwitchToConfig("ModuleConfig", 4, 3);
  89. }
  90. void OriginalWnd::switchToProgramConfig()
  91. {
  92. SwitchToConfig("Programme", 5, 1);
  93. }
  94. void OriginalWnd::switchToSystemConfig()
  95. {
  96. QString name = "SystemInfor";
  97. int configId = 7; // System_config.json 对应的 ConfigId
  98. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  99. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  100. }
  101. void OriginalWnd::InitForm()
  102. {
  103. InitTreeViewManager();
  104. m_nTimerID = startTimer(100);
  105. m_pLogStateSidebar = new LogStateSidebar(this);
  106. m_pLogStateSidebar->move(-m_pLogStateSidebar->width(), 0);
  107. m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height());
  108. //给stopbutton加样式
  109. QString stopButton_styleSheet = "QToolButton:hover { background: #cc0000; }"
  110. "QToolButton:pressed { background: #990000; }";
  111. ui->stopButton->setStyleSheet(stopButton_styleSheet);
  112. ui->stopButton->setIcon(QIcon(":/images/light/stop.png"));
  113. ui->startButton->setIcon(QIcon(":/images/light/start.png"));
  114. ui->label_userlogo->setPixmap(QPixmap(":/images/TopStatusBar/user.png").scaled(20, 20, Qt::KeepAspectRatio));
  115. ui->label_username->setText(tr("SBT_QQ", "SBT_QQ"));
  116. ui->label_username->setStyleSheet("QLabel { color : White; }"
  117. "QLabel:hover { color : red; }");
  118. CreateSwitchMainPage();
  119. ui->label_userlogo->installEventFilter(this);
  120. ui->label_username->installEventFilter(this);
  121. // 这个地方是否也要保存切换?
  122. SwitchToConfig("Home", 1, 0, false);
  123. }
  124. void OriginalWnd::InitTreeViewManager()
  125. {
  126. //TODO: 不行的话直接m_pCameraBind 传递当前指针,这样不会存在释放问题
  127. //m_pCameraBind
  128. m_pdbTreeViewManager.reset(new DbTreeViewManager(ui->menuFunctionsSwitchWidget, this));
  129. connect(m_pdbTreeViewManager.get(), &DbTreeViewManager::RunFunSignals, this, &OriginalWnd::JRunFunSlots);
  130. // 把TreeViewManager放到页面上
  131. QVBoxLayout* layout = new QVBoxLayout(ui->menuFunctionsSwitchWidget);
  132. layout->addWidget(m_pdbTreeViewManager.get());
  133. ui->menuFunctionsSwitchWidget->setLayout(layout);
  134. }
  135. void OriginalWnd::UpdateTime()
  136. {
  137. QDateTime currentDateTime = QDateTime::currentDateTime();
  138. QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
  139. ui->label_time->setText(timeStr);
  140. //// 用户也要放到注册表?
  141. //QSettings settings("YourCompany_2", "YourAppName_2");
  142. //QString userName = settings.value("userName", "???").toString();
  143. //ui->label_username->setText(userName);
  144. }
  145. void OriginalWnd::Del()
  146. {
  147. killTimer(m_nTimerID);
  148. }
  149. void OriginalWnd::RunOrStopSwitch()
  150. {
  151. // 目前样式切换需要调整
  152. if (m_isDisabled)
  153. {
  154. // 当按钮处于禁用状态时,执行停止操作并启用按钮
  155. QString styleSheet = "QToolButton { background: #4AB832; color: #FFFFFF; }"
  156. "QToolButton:hover { background: #00FF00; }";
  157. ui->startButton->setDisabled(m_isDisabled);
  158. ui->stopButton->setDisabled(!m_isDisabled);
  159. ui->startButton->setStyleSheet(styleSheet);
  160. m_pCameraBind->JRunAutoBond();
  161. }
  162. else
  163. {
  164. // 当按钮处于启用状态时,执行开始操作并禁用按钮
  165. QString styleSheet = "QToolButton:disabled { color: #808080; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F8FBFF, stop: 1 #E7ECF3); }";
  166. ui->startButton->setDisabled(m_isDisabled);
  167. ui->stopButton->setDisabled(!m_isDisabled);
  168. ui->stopButton->setStyleSheet(styleSheet);
  169. m_pCameraBind->JRunStop();
  170. }
  171. m_isDisabled = !m_isDisabled; // 切换状态
  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();
  238. }
  239. void OriginalWnd::on_stopButton_clicked()
  240. {
  241. RunOrStopSwitch();
  242. }
  243. void OriginalWnd::DisableAllFeatures(bool isDisable)
  244. {
  245. emit sendDisableFeaturesSig(isDisable);
  246. ui->startButton->setDisabled(isDisable);
  247. ui->ChartButton->setDisabled(isDisable);
  248. ui->DoubleImageButton->setDisabled(isDisable);
  249. ui->SingleImageButton->setDisabled(isDisable);
  250. // 树菜单
  251. ui->menuFunctionsSwitchWidget->setDisabled(isDisable);
  252. //TODO:这里有歧义
  253. ui->widget->setDisabled(isDisable);
  254. }
  255. void OriginalWnd::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut)
  256. {
  257. if (m_pCameraBind.isNull())
  258. {
  259. RegFun();
  260. }
  261. m_pCameraBind->SetImageWnd(pWnd);
  262. pWnd->InitMainCameraBind(m_pCameraBind);
  263. ui->switchShowPageUI->addWidget(pWnd);
  264. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd));
  265. // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
  266. tooBut->setChecked(true);
  267. m_pJOriginalMainWnd = pWnd;
  268. }
  269. void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/)
  270. {
  271. QSettings settings("OrganizationName__", "ApplicationName__");
  272. if (nlastIndex <= 0)
  273. {
  274. nlastIndex = settings.value("lastIndex", 1).toInt();
  275. }
  276. else
  277. {
  278. settings.setValue("lastIndex", nlastIndex);
  279. }
  280. // 切换窗口
  281. switch (nlastIndex)
  282. {
  283. case EN_WND_SWITCH::ChartsAnd:
  284. {
  285. ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd;
  286. SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton);
  287. }
  288. break;
  289. case EN_WND_SWITCH::MainAndSecondary:
  290. {
  291. MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
  292. SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton);
  293. }
  294. break;
  295. case EN_WND_SWITCH::SingleCameraOperation:
  296. {
  297. SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd;
  298. SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton);
  299. }
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. void OriginalWnd::onHandleLanguageSignal(const int& data)
  306. {
  307. if (data == 0)
  308. {
  309. ui->retranslateUi(this);
  310. }
  311. else
  312. {
  313. //setChineseMode(this);
  314. }
  315. }
  316. void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra)
  317. {
  318. m_pCameraBind->JRunButton(pra);
  319. }
  320. void OriginalWnd::StatusBar()
  321. {
  322. enum DEVICE_STATUS currentStatus = DEVICE_STATUS::DIAGNOSE;
  323. updateCalibrationButtonColor(currentStatus);
  324. enum CONNECT_STATUS currentStatus2 = OriginalWnd::CONNECT_STATUS::CONNECT_OK;
  325. updateMachineLinkColor(currentStatus2);
  326. enum HOST_STATUS currentStatus3 = OriginalWnd::HOST_STATUS::HOST_CON_DEVICE;
  327. updateHostStatusColor(currentStatus3);
  328. }
  329. void OriginalWnd::retranslateUi()
  330. {
  331. ui->retranslateUi(this);
  332. }
  333. void OriginalWnd::updateCalibrationButtonColor(enum DEVICE_STATUS status)
  334. {
  335. QString styleSheet;
  336. QString buttonText;
  337. switch (status) {
  338. case DEVICE_STATUS::IDLE: // 停机待机
  339. styleSheet = "QPushButton { background-color: green; color: white; }";
  340. buttonText = "IDLE";
  341. break;
  342. case DEVICE_STATUS::RUNING: // 生产
  343. styleSheet = "QPushButton { background-color: green; color: white; }";
  344. buttonText = "RUNING";
  345. break;
  346. case DEVICE_STATUS::CALIB: // 校准
  347. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  348. buttonText = "CALIB";
  349. break;
  350. case DEVICE_STATUS::WARN: // 报警
  351. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  352. buttonText = "WARN";
  353. break;
  354. case DEVICE_STATUS::FAULT: // 故障
  355. styleSheet = "QPushButton { background-color: red; color: white; }";
  356. buttonText = "FAULT";
  357. break;
  358. case DEVICE_STATUS::DIAGNOSE: // 诊断
  359. styleSheet = "QPushButton { background-color: red; color: white; }";
  360. buttonText = "DIAGNOSE";
  361. break;
  362. default: // 未知状态
  363. styleSheet = "QPushButton { background-color: gray; color: white; }";
  364. buttonText = "未知状态";
  365. break;
  366. }
  367. // 设置按钮样式和文本
  368. ui->CalibrationButton->setStyleSheet(styleSheet);
  369. ui->CalibrationButton->setText(buttonText);
  370. }
  371. void OriginalWnd::updateMachineLinkColor(enum CONNECT_STATUS status)
  372. {
  373. QString styleSheet;
  374. QString buttonText;
  375. switch (status) {
  376. case CONNECT_STATUS::NO_CONNECT: // 未连接
  377. styleSheet = "QPushButton { background-color: gray; color: white; }";
  378. break;
  379. case CONNECT_STATUS::CONNECT_ING: // 连接中
  380. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  381. break;
  382. case CONNECT_STATUS::MACHINE_TIMEOUT: // 机台超时
  383. styleSheet = "QPushButton { background-color: orange; color: black; }";
  384. break;
  385. case CONNECT_STATUS::HOST_TIMEOUT: // 主机超时
  386. styleSheet = "QPushButton { background-color: blue; color: white; }";
  387. break;
  388. case CONNECT_STATUS::CONNECT_OK: // 连接成功
  389. styleSheet = "QPushButton { background-color: green; color: white; }";
  390. break;
  391. default: // 未知状态
  392. styleSheet = "QPushButton { background-color: gray; color: white; }";
  393. break;
  394. }
  395. // 设置按钮样式
  396. ui->MachineLink->setStyleSheet(styleSheet);
  397. }
  398. void OriginalWnd::updateHostStatusColor(enum HOST_STATUS status)
  399. {
  400. QString styleSheet;
  401. QString buttonText;
  402. switch (status) {
  403. case HOST_STATUS::HOST_OUT: // 主机离线
  404. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  405. break;
  406. case HOST_STATUS::DEVICE_OUT: // 设备离线
  407. styleSheet = "QPushButton { background-color: orange; color: black; }";
  408. break;
  409. case HOST_STATUS::DEVICE_ING: // 设备试图连线
  410. styleSheet = "QPushButton { background-color: magenta; color: white; }";
  411. break;
  412. case HOST_STATUS::DEVICE_OK: // 设备在线
  413. styleSheet = "QPushButton { background-color: blue; color: white; }";
  414. break;
  415. case HOST_STATUS::HOST_CON_DEVICE: // 主机控制设备
  416. styleSheet = "QPushButton { background-color: green; color: white; }";
  417. break;
  418. default: // 未知状态
  419. styleSheet = "QPushButton { background-color: gray; color: white; }";
  420. break;
  421. }
  422. // 设置按钮样式和文本
  423. ui->HostStatus->setStyleSheet(styleSheet);
  424. }
  425. void OriginalWnd::changeEvent(QEvent* event)
  426. {
  427. if (event->type() == QEvent::LanguageChange) {
  428. ui->retranslateUi(this);
  429. }
  430. }