mainwindow.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "mainwindow.h"
  2. #include <QVBoxLayout>
  3. #include <QDir>
  4. #include <QElapsedTimer>
  5. #include "appcontext.h"
  6. #include "canvas.h"
  7. #include "logger.h"
  8. #include "utils.h"
  9. #include "abstractwidget.h"
  10. #include <QSettings>
  11. MainWindow::MainWindow(QWidget *parent)
  12. : QMainWindow(parent)
  13. , m_pStackedWidget(nullptr)
  14. {
  15. setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
  16. init_ui();
  17. Network &network = AppContext::instance().network();
  18. connect(&network, &Network::updateScreen, this, &MainWindow::onUpdateScreen);
  19. SerialPort &serialPort = AppContext::instance().serialPort();
  20. connect(&serialPort, &SerialPort::pageSwitch, this, &MainWindow::onPageSwitdh);
  21. FileServer &fileServer = AppContext::instance().fileserver();
  22. connect(&fileServer, &FileServer::monitorConfigureUpdate, this, &MainWindow::onConfigureUpdate);
  23. }
  24. MainWindow::~MainWindow()
  25. {
  26. }
  27. void MainWindow::init_ui()
  28. {
  29. QWidget *pCentralWidget = new QWidget(this);
  30. setCentralWidget(pCentralWidget);
  31. QVBoxLayout *pMainLayout = new QVBoxLayout(pCentralWidget);
  32. m_pStackedWidget = new QStackedWidget(pCentralWidget);
  33. pMainLayout->addWidget(m_pStackedWidget);
  34. pMainLayout->setSpacing(0);
  35. pMainLayout->setContentsMargins(0, 0, 0, 0);
  36. ProjectManager &projectMgr = AppContext::instance().projectManager();
  37. int canvasCount = projectMgr.canvasCount();
  38. for(int i = 0; i < canvasCount; i++)
  39. {
  40. LOG_DEBUG("----------------canvas {}----------------", i);
  41. Canvas *pCanvas = new Canvas(projectMgr.canvas(i), m_pStackedWidget);
  42. pCanvas->resize(1280, 720);
  43. m_pStackedWidget->insertWidget(i, pCanvas);
  44. }
  45. int index = 0;
  46. QSettings settings("settings.ini", QSettings::IniFormat);
  47. settings.setIniCodec("UTF-8");
  48. settings.beginGroup("CANVAS");
  49. if(settings.contains("INDEX")) {
  50. index = settings.value("INDEX").toInt();
  51. LOG_INFO("find saved canvas index, set index {}", index);
  52. } else {
  53. LOG_INFO("not find saved canvas index, set index 0");
  54. }
  55. settings.endGroup();
  56. m_pStackedWidget->setCurrentIndex(index);
  57. }
  58. void MainWindow::onUpdateScreen(const QJsonObject &obj)
  59. {
  60. QElapsedTimer timer;
  61. timer.start(); // 开始计时
  62. ProjectManager &projectMgr = AppContext::instance().projectManager();
  63. int canvasCount = projectMgr.canvasCount();
  64. QList<QWidget *> widgets;
  65. QList<QStringList> parameters;
  66. for(int i = 0; i < canvasCount; i++)
  67. {
  68. Canvas *pCanvas = static_cast<Canvas *>(m_pStackedWidget->widget(i));
  69. if(!pCanvas) {
  70. LOG_ERROR("error current canvas");
  71. return;
  72. }
  73. widgets = pCanvas->widgetList();
  74. parameters = pCanvas->parameterList();
  75. if(widgets.count() != parameters.count()) {
  76. LOG_ERROR("error widget count and parameters count");
  77. return;
  78. }
  79. int count = widgets.count();
  80. for(int j = 0; j < count; j++)
  81. {
  82. AbstractWidget *pAbstractWidget = static_cast<AbstractWidget *>(widgets[j]);
  83. QStringList widgetParameters;
  84. QMetaObject::invokeMethod(pAbstractWidget, "parameters",
  85. Qt::AutoConnection, Q_RETURN_ARG(QStringList, widgetParameters));
  86. for(auto parameter : widgetParameters)
  87. {
  88. QMetaObject::invokeMethod(
  89. pAbstractWidget,
  90. "setValue",
  91. Qt::AutoConnection,
  92. Q_ARG(QString, parameter),
  93. Q_ARG(QVariant, obj[parameter].toVariant()),
  94. Q_ARG(QString, Utils::timestamp()));
  95. }
  96. if(pCanvas == m_pStackedWidget->currentWidget()) {
  97. QMetaObject::invokeMethod(pAbstractWidget, "redraw", Qt::AutoConnection);
  98. }
  99. }
  100. }
  101. qint64 duration = timer.elapsed(); // 获取经过的毫秒数
  102. LOG_DEBUG("update screen duration: {}ms", duration);
  103. }
  104. void MainWindow::onPageSwitdh(int state)
  105. {
  106. ProjectManager &projectMgr = AppContext::instance().projectManager();
  107. int canvasCount = projectMgr.canvasCount();
  108. if (canvasCount <= 0)
  109. return;
  110. int currentIndex = m_pStackedWidget->currentIndex();
  111. int nextIndex = currentIndex;
  112. if (state < 0)
  113. {
  114. // 上一页
  115. nextIndex = (currentIndex - 1 + canvasCount) % canvasCount;
  116. LOG_INFO("page up: {}", nextIndex);
  117. }
  118. else if (state > 0)
  119. {
  120. // 下一页
  121. nextIndex = (currentIndex + 1) % canvasCount;
  122. LOG_INFO("page down: {}", nextIndex);
  123. }
  124. else
  125. {
  126. return; // state == 0 不翻页
  127. }
  128. m_pStackedWidget->setCurrentIndex(nextIndex);
  129. QSettings settings("settings.ini", QSettings::IniFormat);
  130. settings.setIniCodec("UTF-8");
  131. settings.beginGroup("CANVAS");
  132. settings.setValue("INDEX", nextIndex);
  133. settings.endGroup();
  134. }
  135. void MainWindow::onConfigureUpdate(const QString &filename)
  136. {
  137. ProjectManager &projectMgr = AppContext::instance().projectManager();
  138. projectMgr.closeProject();
  139. QString srcFileName = QString("%1/%2").arg(QDir::currentPath(), "update/monitor.xml");
  140. QString dstFileName = QString("%1/%2").arg(QDir::currentPath(), "conf/monitor.xml");
  141. bool ret;
  142. // bool result = projectMgr.openProject(fileName);
  143. LOG_INFO("begin delete current monitor configure...");
  144. QFile dstFile = QFile(dstFileName);
  145. if(dstFile.exists())
  146. {
  147. ret = dstFile.remove();
  148. if(ret) {
  149. LOG_INFO("remove current monitor configure successfully");
  150. } else {
  151. LOG_ERROR("remove current monitor configure failed");
  152. return;
  153. }
  154. }
  155. LOG_INFO("begin copy new monitor configure...");
  156. QFile srcFile = QFile(srcFileName);
  157. if(srcFile.exists())
  158. {
  159. ret = QFile::copy(srcFileName, dstFileName);
  160. if(ret) {
  161. LOG_INFO("update monitor configure successfully");
  162. } else {
  163. LOG_ERROR("update monitor configure failed");
  164. return;
  165. }
  166. }
  167. else
  168. {
  169. LOG_ERROR("update monitor configure file not exist");
  170. return;
  171. }
  172. bool result = projectMgr.openProject(dstFileName);
  173. if(result)
  174. {
  175. LOG_INFO("open monitor config xml success: {}, canvas count: {}", dstFileName.toUtf8().data(), projectMgr.canvasCount());
  176. }
  177. else
  178. {
  179. LOG_ERROR("open monitor config xml failed: {}", dstFileName.toUtf8().data());
  180. return;
  181. }
  182. LOG_INFO("start reload monitor configure");
  183. int canvasCount = projectMgr.canvasCount();
  184. int index = -1;
  185. for(int i = 0; i < canvasCount; i++)
  186. {
  187. LOG_DEBUG("----------------canvas {}----------------", i);
  188. Canvas *pCanvas = new Canvas(projectMgr.canvas(i), m_pStackedWidget);
  189. pCanvas->resize(1280, 720);
  190. if(pCanvas->isDefault()) {
  191. index = i;
  192. }
  193. m_pStackedWidget->insertWidget(i, pCanvas);
  194. }
  195. LOG_INFO("reload monitor configure complete");
  196. if(index > 0 && index < canvasCount) {
  197. m_pStackedWidget->setCurrentIndex(index);
  198. LOG_INFO("update: default canvas index is {}", index);
  199. }
  200. else {
  201. m_pStackedWidget->setCurrentIndex(0);
  202. LOG_INFO("update: not set default canvas index, set index 0");
  203. }
  204. }