Browse Source

minor-fix: VFileList and VNotebookSelector

1. Bug fix: after deleting last folder, the file list won't be cleared;
2. Move cursor one line down after inserting title automatically;
3. Prompt user to create a root folder after creating a notebook;
4. Autocomplete the folder name from the chosen path when creating a
notebook;
5. Add "Open Notebook Location" action in the context menu of
VNotebookSelector;
Le Tan 8 years ago
parent
commit
d6ca4245d9
5 changed files with 65 additions and 1 deletions
  1. 14 0
      src/dialog/vnewnotebookdialog.cpp
  2. 24 1
      src/vfilelist.cpp
  3. 2 0
      src/vmainwindow.cpp
  4. 20 0
      src/vnotebookselector.cpp
  5. 5 0
      src/vnotebookselector.h

+ 14 - 0
src/dialog/vnewnotebookdialog.cpp

@@ -258,6 +258,20 @@ void VNewNotebookDialog::handleInputChanged()
 bool VNewNotebookDialog::autoComplete()
 {
     if (m_manualPath) {
+        if (m_manualName) {
+            return false;
+        }
+
+        // Set the name according to user-chosen path.
+        QString pathText = pathEdit->text();
+        if (!pathText.isEmpty()) {
+            QString autoName = VUtils::directoryNameFromPath(pathText);
+            if (autoName != nameEdit->text()) {
+                nameEdit->setText(autoName);
+                return true;
+            }
+        }
+
         return false;
     }
 

+ 24 - 1
src/vfilelist.cpp

@@ -10,6 +10,7 @@
 #include "utils/vutils.h"
 #include "vfile.h"
 #include "vconfigmanager.h"
+#include "vmdedit.h"
 
 extern VConfigManager *g_config;
 extern VNote *g_vnote;
@@ -131,9 +132,17 @@ void VFileList::initActions()
 
 void VFileList::setDirectory(VDirectory *p_directory)
 {
+    // QPointer will be set to NULL automatically once the directory was deleted.
+    // If the last directory is deleted, m_directory and p_directory will both
+    // be NULL.
     if (m_directory == p_directory) {
+        if (!m_directory) {
+            fileList->clear();
+        }
+
         return;
     }
+
     m_directory = p_directory;
     if (!m_directory) {
         fileList->clear();
@@ -280,7 +289,8 @@ void VFileList::newFile()
         }
 
         // Write title if needed.
-        if (dialog.getInsertTitleInput()) {
+        bool contentInserted = false;
+        if (dialog.getInsertTitleInput() && file->getDocType() == DocType::Markdown) {
             if (!file->open()) {
                 qWarning() << "fail to open newly-created note" << file->getName();
             } else {
@@ -289,6 +299,8 @@ void VFileList::newFile()
                 file->setContent(content);
                 if (!file->save()) {
                     qWarning() << "fail to write to newly-created note" << file->getName();
+                } else {
+                    contentInserted = true;
                 }
 
                 file->close();
@@ -303,6 +315,17 @@ void VFileList::newFile()
 
         // Open it in edit mode
         emit fileCreated(file, OpenFileMode::Edit);
+
+        // Move cursor down if content has been inserted.
+        if (contentInserted) {
+            QWidget *wid = QApplication::focusWidget();
+            VMdEdit *edit = dynamic_cast<VMdEdit *>(wid);
+            if (edit && edit->getFile() == file) {
+                QKeyEvent *downEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Down,
+                                                     Qt::NoModifier);
+                QCoreApplication::postEvent(edit, downEvent);
+            }
+        }
     }
 }
 

+ 2 - 0
src/vmainwindow.cpp

@@ -122,6 +122,8 @@ void VMainWindow::setupUI()
 
     connect(notebookSelector, &VNotebookSelector::notebookUpdated,
             editArea, &VEditArea::handleNotebookUpdated);
+    connect(notebookSelector, &VNotebookSelector::notebookCreated,
+            directoryTree, &VDirectoryTree::newRootDirectory);
 
     connect(fileList, &VFileList::fileClicked,
             editArea, &VEditArea::openFile);

+ 20 - 0
src/vnotebookselector.cpp

@@ -64,6 +64,23 @@ void VNotebookSelector::initActions()
     m_notebookInfoAct->setToolTip(tr("View and edit current notebook's information"));
     connect(m_notebookInfoAct, SIGNAL(triggered(bool)),
             this, SLOT(editNotebookInfo()));
+
+    m_openLocationAct = new QAction(tr("&Open Notebook Location"), this);
+    m_openLocationAct->setToolTip(tr("Open the root folder of this notebook in operating system"));
+    connect(m_openLocationAct, &QAction::triggered,
+            this, [this]() {
+                QList<QListWidgetItem *> items = this->m_listWidget->selectedItems();
+                if (items.isEmpty()) {
+                    return;
+                }
+
+                Q_ASSERT(items.size() == 1);
+                QListWidgetItem *item = items[0];
+                int index = this->indexOfListItem(item);
+                VNotebook *notebook = this->getNotebookFromComboIndex(index);
+                QUrl url = QUrl::fromLocalFile(notebook->getPath());
+                QDesktopServices::openUrl(url);
+            });
 }
 
 void VNotebookSelector::updateComboBox()
@@ -183,6 +200,8 @@ bool VNotebookSelector::newNotebook()
                        dialog.getPathInput(),
                        dialog.isImportExistingNotebook(),
                        dialog.getImageFolder());
+
+        emit notebookCreated();
         return true;
     }
 
@@ -350,6 +369,7 @@ void VNotebookSelector::requestPopupListContextMenu(QPoint p_pos)
     QMenu menu(this);
     menu.setToolTipsVisible(true);
     menu.addAction(m_deleteNotebookAct);
+    menu.addAction(m_openLocationAct);
     menu.addAction(m_notebookInfoAct);
 
     menu.exec(m_listWidget->mapToGlobal(p_pos));

+ 5 - 0
src/vnotebookselector.h

@@ -33,9 +33,13 @@ public:
 
 signals:
     void curNotebookChanged(VNotebook *p_notebook);
+
     // Info of current notebook was changed.
     void notebookUpdated(const VNotebook *p_notebook);
 
+    // Emit after creating a new notebook.
+    void notebookCreated();
+
 public slots:
     bool newNotebook();
 
@@ -85,6 +89,7 @@ private:
     // Actions
     QAction *m_deleteNotebookAct;
     QAction *m_notebookInfoAct;
+    QAction *m_openLocationAct;
 
     // We will add several special action item in the combobox. This is the start index
     // of the real notebook items related to m_notebooks.