OriginalWnd.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut)
  238. {
  239. if (m_pCameraBind.isNull())
  240. {
  241. RegFun();
  242. }
  243. m_pCameraBind->SetImageWnd(pWnd);
  244. pWnd->InitMainCameraBind(m_pCameraBind);
  245. ui->switchShowPageUI->addWidget(pWnd);
  246. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd));
  247. // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
  248. tooBut->setChecked(true);
  249. m_pJOriginalMainWnd = pWnd;
  250. }
  251. void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/)
  252. {
  253. QSettings settings("OrganizationName__", "ApplicationName__");
  254. if (nlastIndex <= 0)
  255. {
  256. nlastIndex = settings.value("lastIndex", 1).toInt();
  257. }
  258. else
  259. {
  260. settings.setValue("lastIndex", nlastIndex);
  261. }
  262. // 切换窗口
  263. switch (nlastIndex)
  264. {
  265. case EN_WND_SWITCH::ChartsAnd:
  266. {
  267. ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd;
  268. SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton);
  269. }
  270. break;
  271. case EN_WND_SWITCH::MainAndSecondary:
  272. {
  273. MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
  274. SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton);
  275. }
  276. break;
  277. case EN_WND_SWITCH::SingleCameraOperation:
  278. {
  279. SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd;
  280. SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton);
  281. }
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. void OriginalWnd::onHandleLanguageSignal(const int& data)
  288. {
  289. if (data == 0)
  290. {
  291. ui->retranslateUi(this);
  292. }
  293. else
  294. {
  295. //setChineseMode(this);
  296. }
  297. }
  298. void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra)
  299. {
  300. m_pCameraBind->JRunButton(pra);
  301. }
  302. void OriginalWnd::StatusBar()
  303. {
  304. enum DEVICE_STATUS currentStatus = DEVICE_STATUS::IDLE;
  305. updateCalibrationButtonColor(currentStatus);
  306. enum CONNECT_STATUS currentStatus2 = OriginalWnd::CONNECT_STATUS::NO_CONNECT;
  307. updateMachineLinkColor(currentStatus2);
  308. enum HOST_STATUS currentStatus3 = OriginalWnd::HOST_STATUS::DEVICE_OK;
  309. updateHostStatusColor(currentStatus3);
  310. }
  311. void OriginalWnd::retranslateUi()
  312. {
  313. ui->retranslateUi(this);
  314. }
  315. void OriginalWnd::updateCalibrationButtonColor(enum DEVICE_STATUS status)
  316. {
  317. QString styleSheet;
  318. QString buttonText;
  319. switch (status) {
  320. case DEVICE_STATUS::IDLE: // 停机待机
  321. styleSheet = "QPushButton { background-color: green; color: white; }";
  322. buttonText = "IDLE";
  323. break;
  324. case DEVICE_STATUS::RUNING: // 生产
  325. styleSheet = "QPushButton { background-color: green; color: white; }";
  326. buttonText = "RUNING";
  327. break;
  328. case DEVICE_STATUS::CALIB: // 校准
  329. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  330. buttonText = "CALIB";
  331. break;
  332. case DEVICE_STATUS::WARN: // 报警
  333. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  334. buttonText = "WARN";
  335. break;
  336. case DEVICE_STATUS::FAULT: // 故障
  337. styleSheet = "QPushButton { background-color: red; color: white; }";
  338. buttonText = "FAULT";
  339. break;
  340. case DEVICE_STATUS::DIAGNOSE: // 诊断
  341. styleSheet = "QPushButton { background-color: red; color: white; }";
  342. buttonText = "DIAGNOSE";
  343. break;
  344. default: // 未知状态
  345. styleSheet = "QPushButton { background-color: gray; color: white; }";
  346. buttonText = "未知状态";
  347. break;
  348. }
  349. // 设置按钮样式和文本
  350. ui->CalibrationButton->setStyleSheet(styleSheet);
  351. ui->CalibrationButton->setText(buttonText);
  352. }
  353. void OriginalWnd::updateMachineLinkColor(enum CONNECT_STATUS status)
  354. {
  355. QString styleSheet;
  356. QString buttonText;
  357. switch (status) {
  358. case CONNECT_STATUS::NO_CONNECT: // 未连接
  359. styleSheet = "QPushButton { background-color: gray; color: white; }";
  360. break;
  361. case CONNECT_STATUS::CONNECT_ING: // 连接中
  362. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  363. break;
  364. case CONNECT_STATUS::MACHINE_TIMEOUT: // 机台超时
  365. styleSheet = "QPushButton { background-color: orange; color: black; }";
  366. break;
  367. case CONNECT_STATUS::HOST_TIMEOUT: // 主机超时
  368. styleSheet = "QPushButton { background-color: blue; color: white; }";
  369. break;
  370. case CONNECT_STATUS::CONNECT_OK: // 连接成功
  371. styleSheet = "QPushButton { background-color: green; color: white; }";
  372. break;
  373. default: // 未知状态
  374. styleSheet = "QPushButton { background-color: gray; color: white; }";
  375. break;
  376. }
  377. // 设置按钮样式
  378. ui->MachineLink->setStyleSheet(styleSheet);
  379. }
  380. void OriginalWnd::updateHostStatusColor(enum HOST_STATUS status)
  381. {
  382. QString styleSheet;
  383. QString buttonText;
  384. switch (status) {
  385. case HOST_STATUS::HOST_OUT: // 主机离线
  386. styleSheet = "QPushButton { background-color: yellow; color: black; }";
  387. break;
  388. case HOST_STATUS::DEVICE_OUT: // 设备离线
  389. styleSheet = "QPushButton { background-color: orange; color: black; }";
  390. break;
  391. case HOST_STATUS::DEVICE_ING: // 设备试图连线
  392. styleSheet = "QPushButton { background-color: magenta; color: white; }";
  393. break;
  394. case HOST_STATUS::DEVICE_OK: // 设备在线
  395. styleSheet = "QPushButton { background-color: blue; color: white; }";
  396. break;
  397. case HOST_STATUS::HOST_CON_DEVICE: // 主机控制设备
  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->HostStatus->setStyleSheet(styleSheet);
  406. }
  407. void OriginalWnd::changeEvent(QEvent* event)
  408. {
  409. if (event->type() == QEvent::LanguageChange) {
  410. ui->retranslateUi(this);
  411. }
  412. }