From e4ae4fbeb792d382b553f8a15eddc4e1c70aa2cf Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 26 Aug 2021 10:33:37 +0200
Subject: [PATCH] LIB: Documents must emit them self the documentChanged signal
 on rename and copy

---
 src/Lib/Document/VivyDocument.cc |  8 ++++++--
 src/Lib/Script/ScriptDocument.cc |  2 ++
 src/UI/MainWindow.cc             | 19 ++++++++++---------
 src/UI/ScriptDocumentView.cc     |  2 ++
 src/UI/VivyDocumentView.cc       |  1 +
 5 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/Lib/Document/VivyDocument.cc b/src/Lib/Document/VivyDocument.cc
index 0bc030af..377425de 100644
--- a/src/Lib/Document/VivyDocument.cc
+++ b/src/Lib/Document/VivyDocument.cc
@@ -237,7 +237,7 @@ VivyDocument::copy(const QString &newName)
 
     // Rename + create the disk on disk as it's a memory document
     if (documentOptions & MemoryDocumentCreation)
-        return saveMemoryFile(newPath);
+        saveMemoryFile(newPath);
 
     // Compute new paths, the document is really on disk initially
     else {
@@ -247,6 +247,8 @@ VivyDocument::copy(const QString &newName)
             documentName     = newPath.baseName();
         });
     }
+
+    emit documentChanged();
 }
 
 void
@@ -256,7 +258,7 @@ VivyDocument::rename(const QString &newName)
 
     // Rename + create the disk on disk as it's a memory document
     if (documentOptions & MemoryDocumentCreation)
-        return saveMemoryFile(newPath);
+        saveMemoryFile(newPath);
 
     // Compute new paths, the document is really on disk initially
     else {
@@ -266,6 +268,8 @@ VivyDocument::rename(const QString &newName)
             documentName     = newPath.baseName();
         });
     }
+
+    emit documentChanged();
 }
 
 QFileInfo
diff --git a/src/Lib/Script/ScriptDocument.cc b/src/Lib/Script/ScriptDocument.cc
index 715451b6..6294656c 100644
--- a/src/Lib/Script/ScriptDocument.cc
+++ b/src/Lib/Script/ScriptDocument.cc
@@ -20,6 +20,7 @@ ScriptDocument::copy(const QString &newName)
     /* Compute new paths */
     const QFileInfo newPath(newName);
     copyWith(newPath, [=, this]() noexcept { name = newName; });
+    emit documentChanged();
 }
 
 void
@@ -28,6 +29,7 @@ ScriptDocument::rename(const QString &newName)
     /* Compute new paths */
     const QFileInfo newPath(newName);
     renameWith(newPath, [=, this]() noexcept { name = newName; });
+    emit documentChanged();
 }
 
 void
diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc
index f4d0af80..7430255d 100644
--- a/src/UI/MainWindow.cc
+++ b/src/UI/MainWindow.cc
@@ -261,10 +261,8 @@ MainWindow::renameFile() noexcept
         if (filename.isEmpty())
             throw std::runtime_error("No filename passed");
 
-        else {
+        else
             document->rename(filename); // Kubat: Save is called by rename!
-            emit docView->documentPropertyChanged();
-        }
     }
 
     catch (const std::runtime_error &e) {
@@ -285,10 +283,8 @@ MainWindow::saveFileAs() noexcept
         if (filename.isEmpty())
             throw std::runtime_error("No filename passed");
 
-        else {
+        else
             document->copy(filename); // Kubat: Save is called by copy!
-            emit docView->documentPropertyChanged();
-        }
     }
 
     catch (const std::runtime_error &e) {
@@ -434,9 +430,14 @@ MainWindow::addTab(AbstractDocumentView *const tab)
             &MainWindow::documentViewActionsChanged);
     connect(tab, &AbstractDocumentView::documentPropertyChanged, this,
             [this, tab]() noexcept -> void {
-                int tabIndex = documents->indexOf(tab);
-                documents->setTabToolTip(tabIndex, tab->getDocumentTabToolTip());
-                documents->setTabText(tabIndex, tab->getDocumentTabName());
+                int tabIndex = getIndexOfTab(tab);
+                if (tabIndex < 0) {
+                    qDebug() << "Tab was not found!";
+                } else {
+                    qDebug() << "Tab was found at index" << tabIndex;
+                    documents->setTabToolTip(tabIndex, tab->getDocumentTabToolTip());
+                    documents->setTabText(tabIndex, tab->getDocumentTabName());
+                }
             });
     documentViewActionsChanged();
     qDebug() << "View constructed successfully";
diff --git a/src/UI/ScriptDocumentView.cc b/src/UI/ScriptDocumentView.cc
index a7ab4938..e1b50cc3 100644
--- a/src/UI/ScriptDocumentView.cc
+++ b/src/UI/ScriptDocumentView.cc
@@ -26,6 +26,8 @@ ScriptDocumentView::ScriptDocumentView(std::shared_ptr<ScriptDocument> ptr, QWid
     setUseFakeVimEditor(vivyApp->getUseFakeVimEditor());
 
     connect(this, &ScriptDocumentView::luaErrorFound, editor, &ScriptEditor::updateLastLuaError);
+    connect(document.get(), &AbstractDocument::documentChanged, this,
+            [=, this]() noexcept -> void { emit documentPropertyChanged(); });
 
     // Same style as the editor
     setStyleSheet(QStringLiteral("* {"
diff --git a/src/UI/VivyDocumentView.cc b/src/UI/VivyDocumentView.cc
index 0962ecff..0f36c52b 100644
--- a/src/UI/VivyDocumentView.cc
+++ b/src/UI/VivyDocumentView.cc
@@ -35,6 +35,7 @@ VivyDocumentView::VivyDocumentView(std::shared_ptr<VivyDocument> doc, QWidget *p
     connect(document.get(), &AbstractDocument::documentChanged, this, [=, this]() noexcept -> void {
         if (property)
             openProperties();
+        emit documentPropertyChanged();
     });
     viewsActions.append(openPropertiesAct);
 
-- 
GitLab