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()
{
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) {

21
vedit.h
View File

@ -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

View File

@ -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,10 +149,10 @@ 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);
if (!ret) {
@ -161,9 +161,10 @@ bool VEditor::saveFile()
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
textEditor->setModified(true);
return false;
}
textEditor->endSave();
textEditor->setModified(false);
return true;
}

View File

@ -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);