diff --git a/vedit.cpp b/vedit.cpp index c23d4893..939e2d8f 100644 --- a/vedit.cpp +++ b/vedit.cpp @@ -38,12 +38,7 @@ void VEdit::beginEdit() } } -bool VEdit::tryEndEdit() -{ - return !document()->isModified(); -} - -void VEdit::beginSave() +void VEdit::saveFile() { if (!document()->isModified()) { return; @@ -61,11 +56,6 @@ void VEdit::beginSave() } } -void VEdit::endSave() -{ - document()->setModified(false); -} - void VEdit::reloadFile() { switch (noteFile->docType) { diff --git a/vedit.h b/vedit.h index 6e6d9d1e..a368db78 100644 --- a/vedit.h +++ b/vedit.h @@ -14,12 +14,12 @@ class VEdit : public QTextEdit public: VEdit(VNoteFile *noteFile, QWidget *parent = 0); void beginEdit(); - bool tryEndEdit(); - // begin: sync the buffer to noteFile->content; - // end: setModified(false) - void beginSave(); - void endSave(); + // Save buffer content to noteFile->content. + void saveFile(); + + inline void setModified(bool modified); + inline bool isModified() const; void reloadFile(); @@ -33,4 +33,15 @@ private: HGMarkdownHighlighter *mdHighlighter; }; + +inline bool VEdit::isModified() const +{ + return document()->isModified(); +} + +inline void VEdit::setModified(bool modified) +{ + document()->setModified(modified); +} + #endif // VEDIT_H diff --git a/veditor.cpp b/veditor.cpp index 2cf5fb07..e8e29e17 100644 --- a/veditor.cpp +++ b/veditor.cpp @@ -118,8 +118,8 @@ void VEditor::readFile() if (!isEditMode) { return; } - bool canExit = textEditor->tryEndEdit(); - if (!canExit) { + + if (textEditor->isModified()) { // Need to save the changes QMessageBox msgBox; msgBox.setText("The note has been modified."); @@ -149,21 +149,22 @@ void VEditor::readFile() bool VEditor::saveFile() { - if (!isEditMode || !noteFile->modifiable) { + if (!isEditMode || !noteFile->modifiable || !textEditor->isModified()) { return true; } - textEditor->beginSave(); + textEditor->saveFile(); bool ret = VUtils::writeFileToDisk(QDir(noteFile->path).filePath(noteFile->name), - noteFile->content); + noteFile->content); if (!ret) { QMessageBox msgBox(QMessageBox::Warning, tr("Fail to save to file"), QString("Fail to write to disk when saving a note. Please try it again.")); msgBox.setStandardButtons(QMessageBox::Ok); msgBox.setDefaultButton(QMessageBox::Ok); msgBox.exec(); + textEditor->setModified(true); return false; } - textEditor->endSave(); + textEditor->setModified(false); return true; } diff --git a/vmainwindow.cpp b/vmainwindow.cpp index 4bf78350..002790de 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -110,6 +110,7 @@ void VMainWindow::initActions() saveNoteAct = new QAction(tr("&Save"), this); saveNoteAct->setStatusTip(tr("Save current note")); + saveNoteAct->setShortcut(QKeySequence::Save); connect(saveNoteAct, &QAction::triggered, tabs, &VTabWidget::saveFile);