| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "mainwindow.h"
- #include <QVBoxLayout>
- #include <QDir>
- #include <QElapsedTimer>
- #include "appcontext.h"
- #include "canvas.h"
- #include "logger.h"
- #include "utils.h"
- #include "abstractwidget.h"
- #include <QSettings>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , m_pStackedWidget(nullptr)
- {
- setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
- init_ui();
- Network &network = AppContext::instance().network();
- connect(&network, &Network::updateScreen, this, &MainWindow::onUpdateScreen);
- SerialPort &serialPort = AppContext::instance().serialPort();
- connect(&serialPort, &SerialPort::pageSwitch, this, &MainWindow::onPageSwitdh);
- FileServer &fileServer = AppContext::instance().fileserver();
- connect(&fileServer, &FileServer::monitorConfigureUpdate, this, &MainWindow::onConfigureUpdate);
- }
- MainWindow::~MainWindow()
- {
- }
- void MainWindow::init_ui()
- {
- QWidget *pCentralWidget = new QWidget(this);
- setCentralWidget(pCentralWidget);
- QVBoxLayout *pMainLayout = new QVBoxLayout(pCentralWidget);
- m_pStackedWidget = new QStackedWidget(pCentralWidget);
- pMainLayout->addWidget(m_pStackedWidget);
- pMainLayout->setSpacing(0);
- pMainLayout->setContentsMargins(0, 0, 0, 0);
- ProjectManager &projectMgr = AppContext::instance().projectManager();
- int canvasCount = projectMgr.canvasCount();
- for(int i = 0; i < canvasCount; i++)
- {
- LOG_DEBUG("----------------canvas {}----------------", i);
- Canvas *pCanvas = new Canvas(projectMgr.canvas(i), m_pStackedWidget);
- pCanvas->resize(1280, 720);
- m_pStackedWidget->insertWidget(i, pCanvas);
- }
- int index = 0;
- QSettings settings("settings.ini", QSettings::IniFormat);
- settings.setIniCodec("UTF-8");
- settings.beginGroup("CANVAS");
- if(settings.contains("INDEX")) {
- index = settings.value("INDEX").toInt();
- LOG_INFO("find saved canvas index, set index {}", index);
- } else {
- LOG_INFO("not find saved canvas index, set index 0");
- }
- settings.endGroup();
- m_pStackedWidget->setCurrentIndex(index);
- }
- void MainWindow::onUpdateScreen(const QJsonObject &obj)
- {
- QElapsedTimer timer;
- timer.start(); // 开始计时
- ProjectManager &projectMgr = AppContext::instance().projectManager();
- int canvasCount = projectMgr.canvasCount();
- QList<QWidget *> widgets;
- QList<QStringList> parameters;
- for(int i = 0; i < canvasCount; i++)
- {
- Canvas *pCanvas = static_cast<Canvas *>(m_pStackedWidget->widget(i));
- if(!pCanvas) {
- LOG_ERROR("error current canvas");
- return;
- }
- widgets = pCanvas->widgetList();
- parameters = pCanvas->parameterList();
- if(widgets.count() != parameters.count()) {
- LOG_ERROR("error widget count and parameters count");
- return;
- }
- int count = widgets.count();
- for(int j = 0; j < count; j++)
- {
- AbstractWidget *pAbstractWidget = static_cast<AbstractWidget *>(widgets[j]);
- QStringList widgetParameters;
- QMetaObject::invokeMethod(pAbstractWidget, "parameters",
- Qt::AutoConnection, Q_RETURN_ARG(QStringList, widgetParameters));
- for(auto parameter : widgetParameters)
- {
- QMetaObject::invokeMethod(
- pAbstractWidget,
- "setValue",
- Qt::AutoConnection,
- Q_ARG(QString, parameter),
- Q_ARG(QVariant, obj[parameter].toVariant()),
- Q_ARG(QString, Utils::timestamp()));
- }
- if(pCanvas == m_pStackedWidget->currentWidget()) {
- QMetaObject::invokeMethod(pAbstractWidget, "redraw", Qt::AutoConnection);
- }
- }
- }
- qint64 duration = timer.elapsed(); // 获取经过的毫秒数
- LOG_DEBUG("update screen duration: {}ms", duration);
- }
- void MainWindow::onPageSwitdh(int state)
- {
- ProjectManager &projectMgr = AppContext::instance().projectManager();
- int canvasCount = projectMgr.canvasCount();
- if (canvasCount <= 0)
- return;
- int currentIndex = m_pStackedWidget->currentIndex();
- int nextIndex = currentIndex;
- if (state < 0)
- {
- // 上一页
- nextIndex = (currentIndex - 1 + canvasCount) % canvasCount;
- LOG_INFO("page up: {}", nextIndex);
- }
- else if (state > 0)
- {
- // 下一页
- nextIndex = (currentIndex + 1) % canvasCount;
- LOG_INFO("page down: {}", nextIndex);
- }
- else
- {
- return; // state == 0 不翻页
- }
- m_pStackedWidget->setCurrentIndex(nextIndex);
- QSettings settings("settings.ini", QSettings::IniFormat);
- settings.setIniCodec("UTF-8");
- settings.beginGroup("CANVAS");
- settings.setValue("INDEX", nextIndex);
- settings.endGroup();
- }
- void MainWindow::onConfigureUpdate(const QString &filename)
- {
- ProjectManager &projectMgr = AppContext::instance().projectManager();
- projectMgr.closeProject();
- QString srcFileName = QString("%1/%2").arg(QDir::currentPath(), "update/monitor.xml");
- QString dstFileName = QString("%1/%2").arg(QDir::currentPath(), "conf/monitor.xml");
- bool ret;
- // bool result = projectMgr.openProject(fileName);
- LOG_INFO("begin delete current monitor configure...");
- QFile dstFile = QFile(dstFileName);
- if(dstFile.exists())
- {
- ret = dstFile.remove();
- if(ret) {
- LOG_INFO("remove current monitor configure successfully");
- } else {
- LOG_ERROR("remove current monitor configure failed");
- return;
- }
- }
- LOG_INFO("begin copy new monitor configure...");
- QFile srcFile = QFile(srcFileName);
- if(srcFile.exists())
- {
- ret = QFile::copy(srcFileName, dstFileName);
- if(ret) {
- LOG_INFO("update monitor configure successfully");
- } else {
- LOG_ERROR("update monitor configure failed");
- return;
- }
- }
- else
- {
- LOG_ERROR("update monitor configure file not exist");
- return;
- }
- bool result = projectMgr.openProject(dstFileName);
- if(result)
- {
- LOG_INFO("open monitor config xml success: {}, canvas count: {}", dstFileName.toUtf8().data(), projectMgr.canvasCount());
- }
- else
- {
- LOG_ERROR("open monitor config xml failed: {}", dstFileName.toUtf8().data());
- return;
- }
- LOG_INFO("start reload monitor configure");
- int canvasCount = projectMgr.canvasCount();
- int index = -1;
- for(int i = 0; i < canvasCount; i++)
- {
- LOG_DEBUG("----------------canvas {}----------------", i);
- Canvas *pCanvas = new Canvas(projectMgr.canvas(i), m_pStackedWidget);
- pCanvas->resize(1280, 720);
- if(pCanvas->isDefault()) {
- index = i;
- }
- m_pStackedWidget->insertWidget(i, pCanvas);
- }
- LOG_INFO("reload monitor configure complete");
- if(index > 0 && index < canvasCount) {
- m_pStackedWidget->setCurrentIndex(index);
- LOG_INFO("update: default canvas index is {}", index);
- }
- else {
- m_pStackedWidget->setCurrentIndex(0);
- LOG_INFO("update: not set default canvas index, set index 0");
- }
- }
|