OriginalWnd.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/treeviewmanager.h"
  8. #include <QDebug>
  9. #include <QJsonDocument>
  10. #include <QFile>
  11. #include <QFileDialog>
  12. #include <QDateTime>
  13. #include <QTimer>
  14. #include <QSettings>
  15. #include <QMouseEvent>
  16. #include <QDir>
  17. OriginalWnd::OriginalWnd(QWidget *parent)
  18. : QMainWindow(parent)
  19. , ui(new Ui::OriginalWnd)
  20. {
  21. ui->setupUi(this);
  22. // 初始化 TreeViewManager
  23. treeViewManager = new TreeViewManager(this, ui->widget_2);
  24. if (!treeViewManager) {
  25. qWarning() << "TreeViewManager 初始化失败";
  26. return;
  27. }
  28. // // 获取应用程序目录
  29. // QString appDir = QCoreApplication::applicationDirPath();
  30. // QDir configDir(appDir);
  31. // // 切换到config目录
  32. // if (!configDir.cd("config")) {
  33. // qDebug() << "无法切换到config目录:" << configDir.absolutePath();
  34. // return ;
  35. // }
  36. // // 外部路径
  37. // QStringList configPaths;
  38. // configPaths << configDir.filePath("Home_config.json")
  39. // << configDir.filePath("Production_config.json")
  40. // << configDir.filePath("Diagnosis_config.json")
  41. // << configDir.filePath("Module_config.json")
  42. // << configDir.filePath("Program_config.json")
  43. // << configDir.filePath("Disposition_config.json")
  44. // << configDir.filePath("System_config.json");
  45. // // 定义需要预加载的配置路径
  46. QStringList configPaths;
  47. configPaths << ":/config/Home_config.json"
  48. << ":/config/Production_config.json"
  49. << ":/config/Diagnosis_config.json"
  50. << ":/config/Module_config.json"
  51. << ":/config/Program_config.json"
  52. << ":/config/Disposition_config.json"
  53. << ":/config/System_config.json";
  54. // 让 TreeViewManager 把文件都读到内存
  55. treeViewManager->preloadAllConfigs(configPaths);
  56. // 一启动就显示 home_config.json
  57. treeViewManager->switchConfig("Home_config.json");
  58. qDebug() << "UI 初始化完成";
  59. initForm();
  60. }
  61. OriginalWnd::~OriginalWnd()
  62. {
  63. delete ui;
  64. }
  65. QWidget* OriginalWnd::getWidgetLeft() const
  66. {
  67. return ui->widget;
  68. }
  69. void OriginalWnd::switchToHomeConfig()
  70. {
  71. if (treeViewManager) {
  72. treeViewManager->switchConfig("Home_config.json");
  73. }
  74. }
  75. void OriginalWnd::switchToProductionConfig()
  76. {
  77. if (treeViewManager) {
  78. treeViewManager->switchConfig("Production_config.json");
  79. }
  80. }
  81. void OriginalWnd::switchToSystemConfig()
  82. {
  83. if (treeViewManager) {
  84. treeViewManager->switchConfig("System_config.json");
  85. }
  86. }
  87. void OriginalWnd::switchToProgramConfig()
  88. {
  89. if (treeViewManager) {
  90. treeViewManager->switchConfig("Program_config.json");
  91. }
  92. }
  93. void OriginalWnd::switchToModuleConfig()
  94. {
  95. if (treeViewManager) {
  96. treeViewManager->switchConfig("Module_config.json");
  97. }
  98. }
  99. void OriginalWnd::switchToDispositionConfig()
  100. {
  101. if (treeViewManager) {
  102. treeViewManager->switchConfig("Disposition_config.json");
  103. }
  104. }
  105. void OriginalWnd::switchToDiagnosisConfig()
  106. {
  107. if (treeViewManager) {
  108. treeViewManager->switchConfig("Diagnosis_config.json");
  109. }
  110. }
  111. void OriginalWnd::initForm()
  112. {
  113. QTimer *timer = new QTimer(this);
  114. connect(timer, &QTimer::timeout, this, &OriginalWnd::updateTime);
  115. timer->start(100);
  116. ui->toolButton->setIcon(QIcon(":/images/light/stop.png"));
  117. ui->toolButton_2->setIcon(QIcon(":/images/light/start.png"));
  118. ui->toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  119. ui->toolButton_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  120. ui->label_userlogo->setPixmap(QPixmap(":/images/light/user.png").scaled(20, 20, Qt::KeepAspectRatio));
  121. QSettings settings("OrganizationName__", "ApplicationName__");
  122. int lastSavedIndex = settings.value("lastIndex", 1).toInt();
  123. if (lastSavedIndex == 1) {
  124. ChartsAndCamerasWnd *demo001_1 = new ChartsAndCamerasWnd;
  125. ui->switchShowPageUI->addWidget(demo001_1);
  126. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
  127. }
  128. if (lastSavedIndex == 2) {
  129. MainAndSecondaryCamerasWnd *demo001_2 = new MainAndSecondaryCamerasWnd;
  130. ui->switchShowPageUI->addWidget(demo001_2);
  131. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
  132. }
  133. if (lastSavedIndex == 3) {
  134. SingleCameraOperationWnd *demo001_3 = new SingleCameraOperationWnd;
  135. ui->switchShowPageUI->addWidget(demo001_3);
  136. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
  137. }
  138. ui->label_userlogo->installEventFilter(this);
  139. ui->label_username->installEventFilter(this);
  140. }
  141. void OriginalWnd::updateTime() {
  142. QDateTime currentDateTime = QDateTime::currentDateTime();
  143. QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
  144. ui->label_time->setText(timeStr);
  145. QSettings settings("YourCompany_2", "YourAppName_2");
  146. QString userName = settings.value("userName", "???").toString();
  147. ui->label_username->setText(userName);
  148. }
  149. bool OriginalWnd::eventFilter(QObject *obj,QEvent *event)
  150. {
  151. if (obj == this->ui->label_userlogo || obj == this->ui->label_username)
  152. {
  153. //判断事件类型是否为鼠标事件
  154. if (event->type() == QEvent::MouseButtonPress)
  155. {
  156. //转换为鼠标事件
  157. QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
  158. //判断鼠标左键点击
  159. if (mouseenevt->button() == Qt::LeftButton)
  160. {
  161. if (QStackedWidget *parent = qobject_cast<QStackedWidget*>(this->parentWidget())) {
  162. Login *loginWnd = new Login(this);
  163. parent->addWidget(loginWnd);
  164. parent->setCurrentIndex(parent->indexOf(loginWnd));
  165. }
  166. }
  167. }
  168. }
  169. return QWidget::eventFilter(obj, event);
  170. }
  171. void OriginalWnd::on_ChartButton_clicked()
  172. {
  173. ChartsAndCamerasWnd *demo001_1 = new ChartsAndCamerasWnd;
  174. ui->switchShowPageUI->addWidget(demo001_1);
  175. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
  176. QSettings settings("OrganizationName__", "ApplicationName__");
  177. settings.setValue("lastIndex", 1);
  178. }
  179. void OriginalWnd::on_DoubleImageButton_clicked()
  180. {
  181. MainAndSecondaryCamerasWnd *demo001_2 = new MainAndSecondaryCamerasWnd;
  182. ui->switchShowPageUI->addWidget(demo001_2);
  183. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
  184. QSettings settings("OrganizationName__", "ApplicationName__");
  185. settings.setValue("lastIndex", 2);
  186. }
  187. void OriginalWnd::on_SingleImageButton_clicked()
  188. {
  189. SingleCameraOperationWnd *demo001_3 = new SingleCameraOperationWnd;
  190. ui->switchShowPageUI->addWidget(demo001_3);
  191. ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
  192. QSettings settings("OrganizationName__", "ApplicationName__");
  193. settings.setValue("lastIndex", 3);
  194. }