OriginalWnd.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. QPointer<Login> login = Login::instance();
  183. parent->addWidget(login);
  184. parent->setCurrentIndex(parent->indexOf(login));
  185. }
  186. }
  187. }
  188. }
  189. return QWidget::eventFilter(obj, event);
  190. }
  191. void OriginalWnd::timerEvent(QTimerEvent* event)
  192. {
  193. if (m_nTimerID == event->timerId())
  194. {
  195. UpdateTime();
  196. }
  197. }
  198. void OriginalWnd::resizeEvent(QResizeEvent* event)
  199. {
  200. if (m_pLogStateSidebar)
  201. {
  202. m_pLogStateSidebar->resize(m_pLogStateSidebar->width(), height());
  203. }
  204. QMainWindow::resizeEvent(event);
  205. }
  206. void OriginalWnd::on_ChartButton_clicked()
  207. {
  208. CreateSwitchMainPage(EN_WND_SWITCH::ChartsAnd);
  209. }
  210. void OriginalWnd::on_DoubleImageButton_clicked()
  211. {
  212. CreateSwitchMainPage(EN_WND_SWITCH::MainAndSecondary);
  213. }
  214. void OriginalWnd::on_SingleImageButton_clicked()
  215. {
  216. CreateSwitchMainPage(EN_WND_SWITCH::SingleCameraOperation);
  217. }
  218. void OriginalWnd::on_LogStatBut_clicked()
  219. {
  220. if (m_pLogStateSidebar->isExpanded())
  221. {
  222. m_pLogStateSidebar->HideSidebar();
  223. }
  224. else
  225. {
  226. m_pLogStateSidebar->ShowSidebar();
  227. }
  228. }
  229. void OriginalWnd::on_startButton_clicked()
  230. {
  231. RunOrStopSwitch();
  232. }
  233. void OriginalWnd::on_stopButton_clicked()
  234. {
  235. RunOrStopSwitch();
  236. }
  237. void OriginalWnd::DisableAllFeatures(bool isDisable)
  238. {
  239. emit sendDisableFeaturesSig(isDisable);
  240. ui->startButton->setDisabled(isDisable);
  241. ui->ChartButton->setDisabled(isDisable);
  242. ui->DoubleImageButton->setDisabled(isDisable);
  243. ui->SingleImageButton->setDisabled(isDisable);
  244. // 树菜单
  245. ui->menuFunctionsSwitchWidget->setDisabled(isDisable);
  246. //TODO:这里有歧义
  247. ui->widget->setDisabled(isDisable);
  248. }
  249. void OriginalWnd::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut)
  250. {
  251. if (m_pCameraBind.isNull())
  252. {
  253. RegFun();
  254. }
  255. m_pCameraBind->SetImageWnd(pWnd);
  256. pWnd->InitMainCameraBind(m_pCameraBind);
  257. ui->switchShowPageUI->addWidget(pWnd);
  258. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd));
  259. // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
  260. tooBut->setChecked(true);
  261. m_pJOriginalMainWnd = pWnd;
  262. }
  263. void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/)
  264. {
  265. QSettings settings("OrganizationName__", "ApplicationName__");
  266. if (nlastIndex <= 0)
  267. {
  268. nlastIndex = settings.value("lastIndex", 1).toInt();
  269. }
  270. else
  271. {
  272. settings.setValue("lastIndex", nlastIndex);
  273. }
  274. // 切换窗口
  275. switch (nlastIndex)
  276. {
  277. case EN_WND_SWITCH::ChartsAnd:
  278. {
  279. ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd;
  280. SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton);
  281. }
  282. break;
  283. case EN_WND_SWITCH::MainAndSecondary:
  284. {
  285. MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
  286. SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton);
  287. }
  288. break;
  289. case EN_WND_SWITCH::SingleCameraOperation:
  290. {
  291. SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd;
  292. SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton);
  293. }
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. void OriginalWnd::onHandleLanguageSignal(const int& data)
  300. {
  301. if (data == 0)
  302. {
  303. ui->retranslateUi(this);
  304. }
  305. else
  306. {
  307. //setChineseMode(this);
  308. }
  309. }
  310. void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra)
  311. {
  312. m_pCameraBind->JRunButton(pra);
  313. }
  314. void OriginalWnd::StatusBar()
  315. {
  316. enum DEVICE_STATUS currentStatus = DEVICE_STATUS::IDLE;
  317. updateCalibrationButtonColor(currentStatus);
  318. enum CONNECT_STATUS currentStatus2 = OriginalWnd::CONNECT_STATUS::NO_CONNECT;
  319. updateMachineLinkColor(currentStatus2);
  320. enum HOST_STATUS currentStatus3 = OriginalWnd::HOST_STATUS::DEVICE_OK;
  321. updateHostStatusColor(currentStatus3);
  322. }
  323. void OriginalWnd::retranslateUi()
  324. {
  325. ui->retranslateUi(this);
  326. }
  327. void OriginalWnd::updateCalibrationButtonColor(enum DEVICE_STATUS status)
  328. {
  329. QString styleSheet;
  330. QString buttonText;
  331. switch (status) {
  332. case DEVICE_STATUS::IDLE: // 停机待机
  333. styleSheet = "QPushButton { background-color: green; color: white; }";
  334. buttonText = "IDLE";
  335. break;
  336. case DEVICE_STATUS::RUNING: // 生产
  337. styleSheet = "QPushButton { background-color: green; color: white; }";
  338. buttonText = "RUNING";
  339. break;
  340. case DEVICE_STATUS::CALIB: // 校准
  341. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  342. buttonText = "CALIB";
  343. break;
  344. case DEVICE_STATUS::WARN: // 报警
  345. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  346. buttonText = "WARN";
  347. break;
  348. case DEVICE_STATUS::FAULT: // 故障
  349. styleSheet = "QPushButton { background-color: red; color: white; }";
  350. buttonText = "FAULT";
  351. break;
  352. case DEVICE_STATUS::DIAGNOSE: // 诊断
  353. styleSheet = "QPushButton { background-color: red; color: white; }";
  354. buttonText = "DIAGNOSE";
  355. break;
  356. default: // 未知状态
  357. styleSheet = "QPushButton { background-color: gray; color: white; }";
  358. buttonText = "未知状态";
  359. break;
  360. }
  361. // 设置按钮样式和文本
  362. ui->CalibrationButton->setStyleSheet(styleSheet);
  363. ui->CalibrationButton->setText(buttonText);
  364. }
  365. void OriginalWnd::updateMachineLinkColor(enum CONNECT_STATUS status)
  366. {
  367. QString styleSheet;
  368. QString buttonText;
  369. switch (status) {
  370. case CONNECT_STATUS::NO_CONNECT: // 未连接
  371. styleSheet = "QPushButton { background-color: gray; color: white; }";
  372. break;
  373. case CONNECT_STATUS::CONNECT_ING: // 连接中
  374. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  375. break;
  376. case CONNECT_STATUS::MACHINE_TIMEOUT: // 机台超时
  377. styleSheet = "QPushButton { background-color: orange; color: black; }";
  378. break;
  379. case CONNECT_STATUS::HOST_TIMEOUT: // 主机超时
  380. styleSheet = "QPushButton { background-color: blue; color: white; }";
  381. break;
  382. case CONNECT_STATUS::CONNECT_OK: // 连接成功
  383. styleSheet = "QPushButton { background-color: green; color: white; }";
  384. break;
  385. default: // 未知状态
  386. styleSheet = "QPushButton { background-color: gray; color: white; }";
  387. break;
  388. }
  389. // 设置按钮样式
  390. ui->MachineLink->setStyleSheet(styleSheet);
  391. }
  392. void OriginalWnd::updateHostStatusColor(enum HOST_STATUS status)
  393. {
  394. QString styleSheet;
  395. QString buttonText;
  396. switch (status) {
  397. case HOST_STATUS::HOST_OUT: // 主机离线
  398. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  399. break;
  400. case HOST_STATUS::DEVICE_OUT: // 设备离线
  401. styleSheet = "QPushButton { background-color: orange; color: black; }";
  402. break;
  403. case HOST_STATUS::DEVICE_ING: // 设备试图连线
  404. styleSheet = "QPushButton { background-color: magenta; color: white; }";
  405. break;
  406. case HOST_STATUS::DEVICE_OK: // 设备在线
  407. styleSheet = "QPushButton { background-color: blue; color: white; }";
  408. break;
  409. case HOST_STATUS::HOST_CON_DEVICE: // 主机控制设备
  410. styleSheet = "QPushButton { background-color: green; color: white; }";
  411. break;
  412. default: // 未知状态
  413. styleSheet = "QPushButton { background-color: gray; color: white; }";
  414. break;
  415. }
  416. // 设置按钮样式和文本
  417. ui->HostStatus->setStyleSheet(styleSheet);
  418. }
  419. void OriginalWnd::changeEvent(QEvent* event)
  420. {
  421. if (event->type() == QEvent::LanguageChange) {
  422. ui->retranslateUi(this);
  423. }
  424. }