OriginalWnd.cpp 13 KB

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