OriginalWnd.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.reset(pCameraBind);
  24. m_pCameraDistribute = CameraDistribute::GetInstance();
  25. RegFun();
  26. if (gen_if.getLanguageValue() == 0)
  27. {
  28. }
  29. else
  30. {
  31. setChineseMode(this);
  32. }
  33. // 确保 ui->widget_2 已经初始化
  34. if (!ui->widget_2)
  35. {
  36. qWarning() << "ui->widget_2 未初始化";
  37. }
  38. // 初始化 DbTreeViewManager,传递两个参数
  39. m_pdbTreeViewManager.reset(new DbTreeViewManager(this, ui->widget_2, this));
  40. connect(m_pdbTreeViewManager.get(), &DbTreeViewManager::RunFunSignals, this, &OriginalWnd::JRunFunSlots);
  41. // 将 DbTreeViewManager 添加到 UI
  42. // 假设 ui->widget_2 是你原来放 TreeViewManager 的地方
  43. QVBoxLayout* layout = new QVBoxLayout(ui->widget_2);
  44. layout->addWidget(m_pdbTreeViewManager.get());
  45. ui->widget_2->setLayout(layout);
  46. // 初始化界面
  47. InitForm();
  48. SwitchToConfig("Home", 1, 0, false);
  49. }
  50. OriginalWnd::~OriginalWnd()
  51. {
  52. Del();
  53. delete ui;
  54. }
  55. void OriginalWnd::CameraBindInit(CameraBind* pCameraBind)
  56. {
  57. m_pCameraBind.reset(pCameraBind);
  58. }
  59. void OriginalWnd::RegFun()
  60. {
  61. qRegisterMetaType<CAxis::AXIS_TYPE>("CAxis::AXIS_TYPE");
  62. qRegisterMetaType<ns_module::ST_BUTTON_FUN>("ns_module::ST_BUTTON_FUN");
  63. }
  64. QWidget* OriginalWnd::getWidgetLeft() const
  65. {
  66. return ui->widget;
  67. }
  68. void OriginalWnd::SwitchToConfig(const QString& name, int configId, int menuArrayIndex, bool isMenuArray /*= true*/)
  69. {
  70. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  71. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  72. m_pdbTreeViewManager->currentMenuId = configId;
  73. if (isMenuArray)
  74. {
  75. if (m_pdbTreeViewManager->menuArray[menuArrayIndex].isthird == true) {
  76. m_pdbTreeViewManager->loadpage(configId);
  77. }
  78. }
  79. }
  80. void OriginalWnd::switchToHomeConfig()
  81. {
  82. SwitchToConfig("Home", 1, 0, false);
  83. }
  84. void OriginalWnd::switchToProductionConfig()
  85. {
  86. SwitchToConfig("ProductionAssist", 2, 0);
  87. }
  88. void OriginalWnd::switchToDiagnosisConfig()
  89. {
  90. SwitchToConfig("Diagnosis", 2, 0);
  91. }
  92. void OriginalWnd::switchToDispositionConfig()
  93. {
  94. SwitchToConfig("Calibration", 6, 4);
  95. }
  96. void OriginalWnd::switchToModuleConfig()
  97. {
  98. SwitchToConfig("ModuleConfig", 4, 3);
  99. }
  100. void OriginalWnd::switchToProgramConfig()
  101. {
  102. SwitchToConfig("Programme", 5, 1);
  103. }
  104. void OriginalWnd::setChineseMode(QMainWindow* OriginalWnd)
  105. {
  106. OriginalWnd->setWindowTitle(QCoreApplication::translate("OriginalWnd", "主界面", nullptr));
  107. ui->stopButton->setText(QCoreApplication::translate("OriginalWnd", "停止", nullptr));
  108. ui->startButton->setText(QCoreApplication::translate("OriginalWnd", "启动", nullptr));
  109. ui->ChartButton->setText(QString());
  110. ui->DoubleImageButton->setText(QString());
  111. ui->SingleImageButton->setText(QString());
  112. // ui->pushButton_18->setText(QCoreApplication::translate("OriginalWnd", "PushButton", nullptr));
  113. ui->label_name->setText(QCoreApplication::translate("OriginalWnd", "姓名001", nullptr));
  114. ui->RegistrationNumber->setText(QCoreApplication::translate("OriginalWnd", "注册号码 001", nullptr));
  115. ui->CalibrationButton->setText(QCoreApplication::translate("OriginalWnd", "校准", nullptr));
  116. ui->ProductIotNumber->setText(QCoreApplication::translate("OriginalWnd", "xxx生产编号", nullptr));
  117. ui->pushButton_2->setText(QString());
  118. ui->pushButton_3->setText(QString());
  119. ui->label_time->setText(QString());
  120. ui->label_username->setText(QString());
  121. ui->label_userlogo->setText(QString());
  122. }
  123. void OriginalWnd::switchToSystemConfig()
  124. {
  125. QString name = "SystemInfor";
  126. int configId = 7; // System_config.json 对应的 ConfigId
  127. int userPrivilege = 0x1; // 初始设为 1,后续通过登录获取
  128. m_pdbTreeViewManager->initializeTree(name, userPrivilege);
  129. }
  130. void OriginalWnd::InitForm()
  131. {
  132. m_nTimerID = startTimer(100);
  133. //给stopbutton加样式
  134. QString stopButton_styleSheet = "QToolButton:hover { background: #cc0000; }"
  135. "QToolButton:pressed { background: #990000; }";
  136. ui->stopButton->setStyleSheet(stopButton_styleSheet);
  137. ui->stopButton->setIcon(QIcon(":/images/light/stop.png"));
  138. ui->startButton->setIcon(QIcon(":/images/light/start.png"));
  139. ui->label_userlogo->setPixmap(QPixmap(":/images/TopStatusBar/user.png").scaled(20, 20, Qt::KeepAspectRatio));
  140. ui->label_username->setText(tr("SBT_QQ", "SBT_QQ"));
  141. ui->label_username->setStyleSheet("QLabel { color : White; }"
  142. "QLabel:hover { color : red; }");
  143. CreateSwitchMainPage();
  144. ui->label_userlogo->installEventFilter(this);
  145. ui->label_username->installEventFilter(this);
  146. }
  147. void OriginalWnd::UpdateTime()
  148. {
  149. QDateTime currentDateTime = QDateTime::currentDateTime();
  150. QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
  151. ui->label_time->setText(timeStr);
  152. //// 用户也要放到注册表?
  153. //QSettings settings("YourCompany_2", "YourAppName_2");
  154. //QString userName = settings.value("userName", "???").toString();
  155. //ui->label_username->setText(userName);
  156. }
  157. void OriginalWnd::Del()
  158. {
  159. killTimer(m_nTimerID);
  160. }
  161. void OriginalWnd::RunOrStopSwitch()
  162. {
  163. // 目前样式切换需要调整
  164. if (m_isDisabled)
  165. {
  166. // 当按钮处于禁用状态时,执行停止操作并启用按钮
  167. QString styleSheet = "QToolButton { background: #4AB832; color: #FFFFFF; }"
  168. "QToolButton:hover { background: #00FF00; }";
  169. ui->startButton->setDisabled(m_isDisabled);
  170. ui->stopButton->setDisabled(!m_isDisabled);
  171. ui->startButton->setStyleSheet(styleSheet);
  172. m_pCameraBind->JRunAutoBond();
  173. }
  174. else
  175. {
  176. // 当按钮处于启用状态时,执行开始操作并禁用按钮
  177. QString styleSheet = "QToolButton:disabled { color: #808080; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F8FBFF, stop: 1 #E7ECF3); }";
  178. ui->startButton->setDisabled(m_isDisabled);
  179. ui->stopButton->setDisabled(!m_isDisabled);
  180. ui->stopButton->setStyleSheet(styleSheet);
  181. m_pCameraBind->JRunStop();
  182. }
  183. m_isDisabled = !m_isDisabled; // 切换状态
  184. }
  185. bool OriginalWnd::eventFilter(QObject* obj, QEvent* event)
  186. {
  187. if (obj == this->ui->label_userlogo || obj == this->ui->label_username)
  188. {
  189. //判断事件类型是否为鼠标事件
  190. if (event->type() == QEvent::MouseButtonPress)
  191. {
  192. //转换为鼠标事件
  193. QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
  194. //判断鼠标左键点击
  195. if (mouseenevt->button() == Qt::LeftButton)
  196. {
  197. if (QStackedWidget* parent = qobject_cast<QStackedWidget*>(this->parentWidget()))
  198. {
  199. Login* loginWnd = new Login(this);
  200. parent->addWidget(loginWnd);
  201. parent->setCurrentIndex(parent->indexOf(loginWnd));
  202. }
  203. }
  204. }
  205. }
  206. return QWidget::eventFilter(obj, event);
  207. }
  208. void OriginalWnd::timerEvent(QTimerEvent* event)
  209. {
  210. if (m_nTimerID == event->timerId())
  211. {
  212. UpdateTime();
  213. }
  214. }
  215. void OriginalWnd::on_ChartButton_clicked()
  216. {
  217. CreateSwitchMainPage(EN_WND_SWITCH::ChartsAnd);
  218. }
  219. void OriginalWnd::on_DoubleImageButton_clicked()
  220. {
  221. CreateSwitchMainPage(EN_WND_SWITCH::MainAndSecondary);
  222. }
  223. void OriginalWnd::on_SingleImageButton_clicked()
  224. {
  225. CreateSwitchMainPage(EN_WND_SWITCH::SingleCameraOperation);
  226. }
  227. void OriginalWnd::on_startButton_clicked()
  228. {
  229. RunOrStopSwitch();
  230. }
  231. void OriginalWnd::on_stopButton_clicked()
  232. {
  233. RunOrStopSwitch();
  234. }
  235. void OriginalWnd::SwitchMainPage(JOriginalMainWnd* pWnd, QToolButton* tooBut)
  236. {
  237. if (m_pCameraBind == nullptr)
  238. {
  239. RegFun();
  240. }
  241. m_pCameraBind->SetImageWnd(pWnd);
  242. pWnd->InitMainCameraBind(m_pCameraBind.get());
  243. ui->switchShowPageUI->addWidget(pWnd);
  244. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(pWnd));
  245. // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
  246. tooBut->setChecked(true);
  247. m_pJOriginalMainWnd = pWnd;
  248. }
  249. void OriginalWnd::CreateSwitchMainPage(int nlastIndex /*= -1*/)
  250. {
  251. QSettings settings("OrganizationName__", "ApplicationName__");
  252. if (nlastIndex <= 0)
  253. {
  254. nlastIndex = settings.value("lastIndex", 1).toInt();
  255. }
  256. else
  257. {
  258. settings.setValue("lastIndex", nlastIndex);
  259. }
  260. // 切换窗口
  261. switch (nlastIndex)
  262. {
  263. case EN_WND_SWITCH::ChartsAnd:
  264. {
  265. ChartsAndCamerasWnd* chartsAndCamerasWnd = new ChartsAndCamerasWnd;
  266. SwitchMainPage(chartsAndCamerasWnd, ui->ChartButton);
  267. }
  268. break;
  269. case EN_WND_SWITCH::MainAndSecondary:
  270. {
  271. MainAndSecondaryCamerasWnd* mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
  272. SwitchMainPage(mainAndSecondaryCamerasWnd, ui->DoubleImageButton);
  273. }
  274. break;
  275. case EN_WND_SWITCH::SingleCameraOperation:
  276. {
  277. SingleCameraOperationWnd* singleCameraOperationWnd = new SingleCameraOperationWnd;
  278. SwitchMainPage(singleCameraOperationWnd, ui->SingleImageButton);
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. void OriginalWnd::onHandleLanguageSignal(const int& data)
  286. {
  287. if (data == 0)
  288. {
  289. ui->retranslateUi(this);
  290. }
  291. else
  292. {
  293. setChineseMode(this);
  294. }
  295. }
  296. void OriginalWnd::JRunFunSlots(ns_module::ST_BUTTON_FUN pra)
  297. {
  298. m_pCameraBind->JRunButton(pra);
  299. }