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;
This commit is contained in:
Le Tan 2017-09-08 21:40:11 +08:00
parent 4ad79e4d92
commit d6ca4245d9
5 changed files with 65 additions and 1 deletions

View File

@ -258,6 +258,20 @@ void VNewNotebookDialog::handleInputChanged()
bool VNewNotebookDialog::autoComplete() bool VNewNotebookDialog::autoComplete()
{ {
if (m_manualPath) { 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; return false;
} }

View File

@ -10,6 +10,7 @@
#include "utils/vutils.h" #include "utils/vutils.h"
#include "vfile.h" #include "vfile.h"
#include "vconfigmanager.h" #include "vconfigmanager.h"
#include "vmdedit.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
extern VNote *g_vnote; extern VNote *g_vnote;
@ -131,9 +132,17 @@ void VFileList::initActions()
void VFileList::setDirectory(VDirectory *p_directory) 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 == p_directory) {
if (!m_directory) {
fileList->clear();
}
return; return;
} }
m_directory = p_directory; m_directory = p_directory;
if (!m_directory) { if (!m_directory) {
fileList->clear(); fileList->clear();
@ -280,7 +289,8 @@ void VFileList::newFile()
} }
// Write title if needed. // Write title if needed.
if (dialog.getInsertTitleInput()) { bool contentInserted = false;
if (dialog.getInsertTitleInput() && file->getDocType() == DocType::Markdown) {
if (!file->open()) { if (!file->open()) {
qWarning() << "fail to open newly-created note" << file->getName(); qWarning() << "fail to open newly-created note" << file->getName();
} else { } else {
@ -289,6 +299,8 @@ void VFileList::newFile()
file->setContent(content); file->setContent(content);
if (!file->save()) { if (!file->save()) {
qWarning() << "fail to write to newly-created note" << file->getName(); qWarning() << "fail to write to newly-created note" << file->getName();
} else {
contentInserted = true;
} }
file->close(); file->close();
@ -303,6 +315,17 @@ void VFileList::newFile()
// Open it in edit mode // Open it in edit mode
emit fileCreated(file, OpenFileMode::Edit); 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);
}
}
} }
} }

View File

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

View File

@ -64,6 +64,23 @@ void VNotebookSelector::initActions()
m_notebookInfoAct->setToolTip(tr("View and edit current notebook's information")); m_notebookInfoAct->setToolTip(tr("View and edit current notebook's information"));
connect(m_notebookInfoAct, SIGNAL(triggered(bool)), connect(m_notebookInfoAct, SIGNAL(triggered(bool)),
this, SLOT(editNotebookInfo())); 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() void VNotebookSelector::updateComboBox()
@ -183,6 +200,8 @@ bool VNotebookSelector::newNotebook()
dialog.getPathInput(), dialog.getPathInput(),
dialog.isImportExistingNotebook(), dialog.isImportExistingNotebook(),
dialog.getImageFolder()); dialog.getImageFolder());
emit notebookCreated();
return true; return true;
} }
@ -350,6 +369,7 @@ void VNotebookSelector::requestPopupListContextMenu(QPoint p_pos)
QMenu menu(this); QMenu menu(this);
menu.setToolTipsVisible(true); menu.setToolTipsVisible(true);
menu.addAction(m_deleteNotebookAct); menu.addAction(m_deleteNotebookAct);
menu.addAction(m_openLocationAct);
menu.addAction(m_notebookInfoAct); menu.addAction(m_notebookInfoAct);
menu.exec(m_listWidget->mapToGlobal(p_pos)); menu.exec(m_listWidget->mapToGlobal(p_pos));

View File

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