add Ctrl+S to save file

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-20 22:19:16 +08:00
parent 17c136f11d
commit 2dd84bdc93
4 changed files with 25 additions and 22 deletions

View File

@ -38,12 +38,7 @@ void VEdit::beginEdit()
} }
} }
bool VEdit::tryEndEdit() void VEdit::saveFile()
{
return !document()->isModified();
}
void VEdit::beginSave()
{ {
if (!document()->isModified()) { if (!document()->isModified()) {
return; return;
@ -61,11 +56,6 @@ void VEdit::beginSave()
} }
} }
void VEdit::endSave()
{
document()->setModified(false);
}
void VEdit::reloadFile() void VEdit::reloadFile()
{ {
switch (noteFile->docType) { switch (noteFile->docType) {

21
vedit.h
View File

@ -14,12 +14,12 @@ class VEdit : public QTextEdit
public: public:
VEdit(VNoteFile *noteFile, QWidget *parent = 0); VEdit(VNoteFile *noteFile, QWidget *parent = 0);
void beginEdit(); void beginEdit();
bool tryEndEdit();
// begin: sync the buffer to noteFile->content; // Save buffer content to noteFile->content.
// end: setModified(false) void saveFile();
void beginSave();
void endSave(); inline void setModified(bool modified);
inline bool isModified() const;
void reloadFile(); void reloadFile();
@ -33,4 +33,15 @@ private:
HGMarkdownHighlighter *mdHighlighter; HGMarkdownHighlighter *mdHighlighter;
}; };
inline bool VEdit::isModified() const
{
return document()->isModified();
}
inline void VEdit::setModified(bool modified)
{
document()->setModified(modified);
}
#endif // VEDIT_H #endif // VEDIT_H

View File

@ -118,8 +118,8 @@ void VEditor::readFile()
if (!isEditMode) { if (!isEditMode) {
return; return;
} }
bool canExit = textEditor->tryEndEdit();
if (!canExit) { if (textEditor->isModified()) {
// Need to save the changes // Need to save the changes
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("The note has been modified."); msgBox.setText("The note has been modified.");
@ -149,21 +149,22 @@ void VEditor::readFile()
bool VEditor::saveFile() bool VEditor::saveFile()
{ {
if (!isEditMode || !noteFile->modifiable) { if (!isEditMode || !noteFile->modifiable || !textEditor->isModified()) {
return true; return true;
} }
textEditor->beginSave(); textEditor->saveFile();
bool ret = VUtils::writeFileToDisk(QDir(noteFile->path).filePath(noteFile->name), bool ret = VUtils::writeFileToDisk(QDir(noteFile->path).filePath(noteFile->name),
noteFile->content); noteFile->content);
if (!ret) { if (!ret) {
QMessageBox msgBox(QMessageBox::Warning, tr("Fail to save to file"), QMessageBox msgBox(QMessageBox::Warning, tr("Fail to save to file"),
QString("Fail to write to disk when saving a note. Please try it again.")); QString("Fail to write to disk when saving a note. Please try it again."));
msgBox.setStandardButtons(QMessageBox::Ok); msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok); msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec(); msgBox.exec();
textEditor->setModified(true);
return false; return false;
} }
textEditor->endSave(); textEditor->setModified(false);
return true; return true;
} }

View File

@ -110,6 +110,7 @@ void VMainWindow::initActions()
saveNoteAct = new QAction(tr("&Save"), this); saveNoteAct = new QAction(tr("&Save"), this);
saveNoteAct->setStatusTip(tr("Save current note")); saveNoteAct->setStatusTip(tr("Save current note"));
saveNoteAct->setShortcut(QKeySequence::Save);
connect(saveNoteAct, &QAction::triggered, connect(saveNoteAct, &QAction::triggered,
tabs, &VTabWidget::saveFile); tabs, &VTabWidget::saveFile);