Sfoglia il codice sorgente

update mainwindow reload canvas

xuqiang 4 mesi fa
parent
commit
f17e633fca
3 ha cambiato i file con 64 aggiunte e 122 eliminazioni
  1. 3 1
      include/mainwindow.h
  2. 1 12
      src/main.cpp
  3. 60 109
      src/mainwindow.cpp

+ 3 - 1
include/mainwindow.h

@@ -16,7 +16,9 @@ public:
 
 protected:
     void init_ui();
-    void reloadCanvas();
+    void reload();
+    void clear();
+    void setCurrentCanvas(bool isNewComfigure = false);
 
 private:
     QStackedWidget *m_pStackedWidget;

+ 1 - 12
src/main.cpp

@@ -29,18 +29,7 @@ int main(int argc, char *argv[])
     LOG_INFO("PROJECT_BUILD_TIME:   {}", PROJECT_BUILD_TIME);
 
     AppContext &appCtx = AppContext::instance();
-    ProjectManager &projectMgr = appCtx.projectManager();
-    QString fileName = QString("%1/%2").arg(QDir::currentPath(), "conf/monitor.xml");
-    bool result = projectMgr.openProject(fileName);
-    if(result)
-    {
-        LOG_INFO("open monitor config xml success: {}, canvas count: {}", fileName.toUtf8().data(), projectMgr.canvasCount());
-    }
-    else
-    {
-        LOG_ERROR("open monitor config xml failed: {}", fileName.toUtf8().data());
-    }
-    
+
     MainWindow w;
     w.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
     w.showFullScreen();

+ 60 - 109
src/mainwindow.cpp

@@ -45,7 +45,23 @@ void MainWindow::init_ui()
     pMainLayout->setSpacing(0);
     pMainLayout->setContentsMargins(0, 0, 0, 0);
 
+    reload();
+    setCurrentCanvas();
+}
+
+void MainWindow::reload()
+{
     ProjectManager &projectMgr = AppContext::instance().projectManager();
+    QString fileName = QString("%1/%2").arg(QDir::currentPath(), "conf/monitor.xml");
+    bool result = projectMgr.openProject(fileName);
+    if(result) {
+        LOG_INFO("open monitor config xml success: {}, canvas count: {}", fileName.toUtf8().data(), projectMgr.canvasCount());
+    } else {
+        LOG_ERROR("open monitor config xml failed: {}", fileName.toUtf8().data());
+        return;
+    }
+
+    LOG_INFO("start reload monitor configure");
     int canvasCount = projectMgr.canvasCount();
     for(int i = 0; i < canvasCount; i++)
     {
@@ -54,24 +70,11 @@ void MainWindow::init_ui()
         pCanvas->resize(1280, 720);
         m_pStackedWidget->insertWidget(i, pCanvas);
     }
-
-    int index = 0;
-    QSettings settings("storage/conf/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);
+    LOG_INFO("reload monitor configure complete");
 }
 
-void MainWindow::reloadCanvas()
+void MainWindow::clear()
 {
-    ProjectManager &projectMgr = AppContext::instance().projectManager();
     LOG_INFO("begin release exist canvas");
     {
         QList<QWidget*> widgets;
@@ -86,42 +89,39 @@ void MainWindow::reloadCanvas()
             delete w;
         }
     }
+    ProjectManager &projectMgr = AppContext::instance().projectManager();
+    projectMgr.closeProject();
     LOG_INFO("release exist canvas complete, widget count: {}", m_pStackedWidget->count());
+}
 
-    QString fileName = QString("%1/%2").arg(QDir::currentPath(), "conf/monitor.xml");
-    bool result = projectMgr.openProject(fileName);
-    if(result)
-    {
-        LOG_INFO("open monitor config xml success: {}, canvas count: {}", fileName.toUtf8().data(), projectMgr.canvasCount());
-    }
-    else
-    {
-        LOG_ERROR("open monitor config xml failed: {}", fileName.toUtf8().data());
-        return;
-    }
-
-    LOG_INFO("start reload monitor configure");
-    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);
-    }
-    LOG_INFO("reload monitor configure complete");
-
+void MainWindow::setCurrentCanvas(bool isNewComfigure)
+{
     int index = 0;
-    QSettings settings("storage/conf/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);
+    if(isNewComfigure) {
+        int canvasCount = m_pStackedWidget->count();
+        for(int i = 0; i < canvasCount; i++)
+        {
+            QWidget *pWidget = m_pStackedWidget->widget(i);
+            Canvas *pCanvas = static_cast<Canvas *>(pWidget);
+            if(pCanvas->isDefault()) {
+                index = i;
+                break;
+            }
+        }
     } else {
-        LOG_INFO("not find saved canvas index, set index 0");
+        QSettings settings("storage/conf/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.setValue("INDEX", 0);
+        }
+        settings.endGroup();
     }
-    settings.endGroup();
+
     m_pStackedWidget->setCurrentIndex(index);
 }
 
@@ -195,20 +195,15 @@ void MainWindow::onPageSwitdh(int state)
     int currentIndex = m_pStackedWidget->currentIndex();
     int nextIndex = currentIndex;
 
-    if (state < 0)
-    {
+    if (state < 0) {
         // 上一页
         nextIndex = (currentIndex - 1 + canvasCount) % canvasCount;
         LOG_INFO("page up: {}", nextIndex);
-    }
-    else if (state > 0)
-    {
+    } else if (state > 0) {
         // 下一页
         nextIndex = (currentIndex + 1) % canvasCount;
         LOG_INFO("page down: {}", nextIndex);
-    }
-    else
-    {
+    } else {
         return; // state == 0 不翻页
     }
 
@@ -223,16 +218,11 @@ void MainWindow::onPageSwitdh(int state)
 
 void MainWindow::onConfigureUpdate(const QString &filename)
 {
-    int canvasCount;
-    ProjectManager &projectMgr = AppContext::instance().projectManager();
-    canvasCount = projectMgr.canvasCount();
-    projectMgr.closeProject();
+    clear();
 
-    // QString srcFileName = QString("%1/%2").arg(QDir::currentPath(), "update/monitor.xml");
     QString srcFileName = filename;
     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);
@@ -249,8 +239,7 @@ void MainWindow::onConfigureUpdate(const QString &filename)
 
     LOG_INFO("begin copy new monitor configure...");
     QFile srcFile = QFile(srcFileName);
-    if(srcFile.exists())
-    {
+    if(srcFile.exists()) {
         ret = QFile::copy(srcFileName, dstFileName);
         if(ret) {
             LOG_INFO("update monitor configure successfully");
@@ -258,66 +247,28 @@ void MainWindow::onConfigureUpdate(const QString &filename)
             LOG_ERROR("update monitor configure failed");
             return;
         }
-    }
-    else
-    {
+    } else {
         LOG_ERROR("update monitor configure file not exist");
         return;
     }
 
-    LOG_INFO("begin release exist canvas");
-    {
-        QList<QWidget*> widgets;
-        for (int i = 0; i < m_pStackedWidget->count(); ++i) {
-            widgets << m_pStackedWidget->widget(i);
-        }
 
-        for (QWidget *w : widgets) {
-            Canvas *pCanvas = static_cast<Canvas *>(w);
-            pCanvas->clear();
-            m_pStackedWidget->removeWidget(w);
-            delete w;
-        }
-    }
-    LOG_INFO("release exist canvas complete, widget count: {}", m_pStackedWidget->count());
+    reload();
 
-    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");
-    canvasCount = projectMgr.canvasCount();
-    int index = 0;
-    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");
-
-    m_pStackedWidget->setCurrentIndex(index);
-    LOG_INFO("update: default canvas index is {}", index);
+    setCurrentCanvas(true);
 
+    LOG_INFO("update: default canvas index is {}", m_pStackedWidget->currentIndex());
     QSettings settings("storage/conf/settings.ini", QSettings::IniFormat);
     settings.setIniCodec("UTF-8");
     settings.beginGroup("CANVAS");
-    settings.setValue("INDEX", index);
+    settings.setValue("INDEX", m_pStackedWidget->currentIndex());
     settings.endGroup();
 }
 
 void MainWindow::onReloadCanvas()
 {
-    reloadCanvas();
+    // reloadCanvas();
+    clear();
+    reload();
+    setCurrentCanvas();
 }