Browse Source

support reloading content of notebook and folder from disk

Le Tan 8 years ago
parent
commit
72970cd404
4 changed files with 99 additions and 7 deletions
  1. 89 3
      src/vdirectorytree.cpp
  2. 6 0
      src/vdirectorytree.h
  3. 2 2
      src/vnotebook.cpp
  4. 2 2
      src/vnotebook.h

+ 89 - 3
src/vdirectorytree.cpp

@@ -115,6 +115,11 @@ void VDirectoryTree::initActions()
     m_openLocationAct->setToolTip(tr("Open the folder containing this folder in operating system"));
     connect(m_openLocationAct, &QAction::triggered,
             this, &VDirectoryTree::openDirectoryLocation);
+
+    m_reloadAct = new QAction(tr("&Reload From Disk"), this);
+    m_reloadAct->setToolTip(tr("Reload the content of this folder (or notebook) from disk"));
+    connect(m_reloadAct, &QAction::triggered,
+            this, &VDirectoryTree::reloadFromDisk);
 }
 
 void VDirectoryTree::setNotebook(VNotebook *p_notebook)
@@ -140,7 +145,9 @@ void VDirectoryTree::setNotebook(VNotebook *p_notebook)
     if (!m_notebook->open()) {
         VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
                             tr("Fail to open notebook <span style=\"%1\">%2</span>.")
-                              .arg(g_config->c_dataTextStyle).arg(m_notebook->getName()), "",
+                              .arg(g_config->c_dataTextStyle).arg(m_notebook->getName()),
+                            tr("Please check if path <span style=\"%1\">%2</span> exists.")
+                              .arg(g_config->c_dataTextStyle).arg(m_notebook->getPath()),
                             QMessageBox::Ok, QMessageBox::Ok, this);
         clear();
         return;
@@ -202,7 +209,9 @@ void VDirectoryTree::buildSubTree(QTreeWidgetItem *p_parent, int p_depth)
     if (!dir->open()) {
         VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
                             tr("Fail to open folder <span style=\"%1\">%2</span>.")
-                              .arg(g_config->c_dataTextStyle).arg(dir->getName()), "",
+                              .arg(g_config->c_dataTextStyle).arg(dir->getName()),
+                            tr("Please check if path <span style=\"%1\">%2</span> exists.")
+                              .arg(g_config->c_dataTextStyle).arg(dir->retrivePath()),
                             QMessageBox::Ok, QMessageBox::Ok, this);
         return;
     }
@@ -355,8 +364,10 @@ void VDirectoryTree::contextMenuRequested(QPoint pos)
         menu.addAction(pasteAct);
     }
 
+    menu.addSeparator();
+    menu.addAction(m_reloadAct);
+
     if (item) {
-        menu.addSeparator();
         menu.addAction(m_openLocationAct);
         menu.addAction(dirInfoAct);
     }
@@ -532,6 +543,81 @@ void VDirectoryTree::openDirectoryLocation() const
     QDesktopServices::openUrl(url);
 }
 
+void VDirectoryTree::reloadFromDisk()
+{
+    if (!m_notebook) {
+        return;
+    }
+
+    QString info;
+    VDirectory *curDir = NULL;
+    QTreeWidgetItem *curItem = currentItem();
+    if (curItem) {
+        // Reload current directory.
+        curDir = getVDirectory(curItem);
+        info = tr("Are you sure to reload folder <span style=\"%1\">%2</span>?")
+                 .arg(g_config->c_dataTextStyle).arg(curDir->getName());
+    } else {
+        // Reload notebook.
+        info = tr("Are you sure to reload notebook <span style=\"%1\">%2</span>?")
+                 .arg(g_config->c_dataTextStyle).arg(m_notebook->getName());
+    }
+
+    int ret = VUtils::showMessage(QMessageBox::Information, tr("Information"),
+                                  info,
+                                  tr("VNote will close all the related notes before reload."),
+                                  QMessageBox::Ok | QMessageBox::Cancel,
+                                  QMessageBox::Ok, this);
+
+    if (ret != QMessageBox::Ok) {
+        return;
+    }
+
+    m_notebookCurrentDirMap.remove(m_notebook);
+
+    if (curItem) {
+        if (!m_editArea->closeFile(curDir, false)) {
+            return;
+        }
+
+        setCurrentItem(NULL);
+
+        curItem->setExpanded(false);
+        curDir->setExpanded(false);
+
+        curDir->close();
+
+        // Remove all its children.
+        QList<QTreeWidgetItem *> children = curItem->takeChildren();
+        for (int i = 0; i < children.size(); ++i) {
+            delete children[i];
+        }
+
+        buildSubTree(curItem, 1);
+
+        setCurrentItem(curItem);
+    } else {
+        if (!m_editArea->closeFile(m_notebook, false)) {
+            return;
+        }
+
+        m_notebook->close();
+
+        if (!m_notebook->open()) {
+            VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
+                                tr("Fail to open notebook <span style=\"%1\">%2</span>.")
+                                  .arg(g_config->c_dataTextStyle).arg(m_notebook->getName()),
+                                tr("Please check if path <span style=\"%1\">%2</span> exists.")
+                                  .arg(g_config->c_dataTextStyle).arg(m_notebook->getPath()),
+                                QMessageBox::Ok, QMessageBox::Ok, this);
+            clear();
+            return;
+        }
+
+        updateDirectoryTree();
+    }
+}
+
 void VDirectoryTree::copySelectedDirectories(bool p_cut)
 {
     QList<QTreeWidgetItem *> items = selectedItems();

+ 6 - 0
src/vdirectorytree.h

@@ -56,6 +56,9 @@ private slots:
     void pasteDirectoriesInCurDir();
     void openDirectoryLocation() const;
 
+    // Reload the content of current directory.
+    void reloadFromDisk();
+
 protected:
     void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
     void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
@@ -123,6 +126,9 @@ private:
     QAction *pasteAct;
     QAction *m_openLocationAct;
 
+    // Reload content from disk.
+    QAction *m_reloadAct;
+
     // Navigation Mode.
     // Map second key to QTreeWidgetItem.
     QMap<QChar, QTreeWidgetItem *> m_keyMap;

+ 2 - 2
src/vnotebook.cpp

@@ -82,12 +82,12 @@ bool VNotebook::writeConfig() const
     return VConfigManager::writeDirectoryConfig(m_path, json);
 }
 
-QString VNotebook::getName() const
+const QString &VNotebook::getName() const
 {
     return m_name;
 }
 
-QString VNotebook::getPath() const
+const QString &VNotebook::getPath() const
 {
     return m_path;
 }

+ 2 - 2
src/vnotebook.h

@@ -33,8 +33,8 @@ public:
     // if @p_path is not inside this notebook.
     VFile *tryLoadFile(const QString &p_path);
 
-    QString getName() const;
-    QString getPath() const;
+    const QString &getName() const;
+    const QString &getPath() const;
 
     VDirectory *getRootDir() const;