Bläddra i källkod

add Ctrl+S to save file

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 år sedan
förälder
incheckning
2dd84bdc93
4 ändrade filer med 25 tillägg och 22 borttagningar
  1. 1 11
      vedit.cpp
  2. 16 5
      vedit.h
  3. 7 6
      veditor.cpp
  4. 1 0
      vmainwindow.cpp

+ 1 - 11
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) {

+ 16 - 5
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

+ 7 - 6
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;
 }
 

+ 1 - 0
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);