From d6ca4245d9539c6de61d6625ff5eda62e997433e Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 8 Sep 2017 21:40:11 +0800 Subject: [PATCH] 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; --- src/dialog/vnewnotebookdialog.cpp | 14 ++++++++++++++ src/vfilelist.cpp | 25 ++++++++++++++++++++++++- src/vmainwindow.cpp | 2 ++ src/vnotebookselector.cpp | 20 ++++++++++++++++++++ src/vnotebookselector.h | 5 +++++ 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/dialog/vnewnotebookdialog.cpp b/src/dialog/vnewnotebookdialog.cpp index c1f0a9dd..0c484e3c 100644 --- a/src/dialog/vnewnotebookdialog.cpp +++ b/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; } diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index b2ce447b..143c0516 100644 --- a/src/vfilelist.cpp +++ b/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(wid); + if (edit && edit->getFile() == file) { + QKeyEvent *downEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, + Qt::NoModifier); + QCoreApplication::postEvent(edit, downEvent); + } + } } } diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 0f533ab2..ba58247d 100644 --- a/src/vmainwindow.cpp +++ b/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); diff --git a/src/vnotebookselector.cpp b/src/vnotebookselector.cpp index ba6685f3..b1c67437 100644 --- a/src/vnotebookselector.cpp +++ b/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 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)); diff --git a/src/vnotebookselector.h b/src/vnotebookselector.h index 04bd0311..eddcfa7c 100644 --- a/src/vnotebookselector.h +++ b/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.