veditor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <QtWidgets>
  2. #include <QTextBrowser>
  3. #include "veditor.h"
  4. #include "vedit.h"
  5. VEditor::VEditor(const QString &path, const QString &name, bool modifiable,
  6. QWidget *parent)
  7. : QStackedWidget(parent)
  8. {
  9. DocType docType = isMarkdown(name) ? DocType::Markdown : DocType::Html;
  10. QString fileText = readFileFromDisk(QDir(path).filePath(name));
  11. noteFile = new VNoteFile(path, name, fileText, docType, modifiable);
  12. isEditMode = false;
  13. setupUI();
  14. showFileReadMode();
  15. }
  16. VEditor::~VEditor()
  17. {
  18. delete noteFile;
  19. }
  20. void VEditor::setupUI()
  21. {
  22. textEditor = new VEdit(noteFile);
  23. textBrowser = new QTextBrowser();
  24. addWidget(textBrowser);
  25. addWidget(textEditor);
  26. }
  27. bool VEditor::isMarkdown(const QString &name)
  28. {
  29. const QVector<QString> mdPostfix({"md", "markdown", "mkd"});
  30. QStringList list = name.split('.', QString::SkipEmptyParts);
  31. if (list.isEmpty()) {
  32. return false;
  33. }
  34. const QString &postfix = list.last();
  35. for (int i = 0; i < mdPostfix.size(); ++i) {
  36. if (postfix == mdPostfix[i]) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. QString VEditor::readFileFromDisk(const QString &filePath)
  43. {
  44. QFile file(filePath);
  45. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  46. qWarning() << "error: fail to read file" << filePath;
  47. return QString();
  48. }
  49. QString fileText(file.readAll());
  50. file.close();
  51. qDebug() << "read file content:" << filePath;
  52. return fileText;
  53. }
  54. bool VEditor::writeFileToDisk(const QString &filePath, const QString &text)
  55. {
  56. QFile file(filePath);
  57. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  58. qWarning() << "error: fail to open file" << filePath << "to write to";
  59. return false;
  60. }
  61. QTextStream stream(&file);
  62. stream << text;
  63. file.close();
  64. qDebug() << "write file content:" << filePath;
  65. return true;
  66. }
  67. void VEditor::showFileReadMode()
  68. {
  69. isEditMode = false;
  70. switch (noteFile->docType) {
  71. case DocType::Html:
  72. textBrowser->setHtml(noteFile->content);
  73. break;
  74. case DocType::Markdown:
  75. break;
  76. default:
  77. qWarning() << "error: unknown doc type" << int(noteFile->docType);
  78. }
  79. setCurrentWidget(textBrowser);
  80. }
  81. void VEditor::showFileEditMode()
  82. {
  83. isEditMode = true;
  84. textEditor->beginEdit();
  85. setCurrentWidget(textEditor);
  86. }
  87. bool VEditor::requestClose()
  88. {
  89. readFile();
  90. return !isEditMode;
  91. }
  92. void VEditor::editFile()
  93. {
  94. if (isEditMode || !noteFile->modifiable) {
  95. return;
  96. }
  97. showFileEditMode();
  98. }
  99. void VEditor::readFile()
  100. {
  101. if (!isEditMode) {
  102. return;
  103. }
  104. bool canExit = textEditor->tryEndEdit();
  105. if (!canExit) {
  106. // Need to save the changes
  107. QMessageBox msgBox;
  108. msgBox.setText("The note has been modified.");
  109. msgBox.setInformativeText("Do you want to save your changes?");
  110. msgBox.setIcon(QMessageBox::Information);
  111. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  112. msgBox.setDefaultButton(QMessageBox::Save);
  113. int ret = msgBox.exec();
  114. switch (ret) {
  115. case QMessageBox::Save:
  116. saveFile();
  117. // Fall through
  118. case QMessageBox::Discard:
  119. textEditor->reloadFile();
  120. break;
  121. case QMessageBox::Cancel:
  122. // Nothing to do if user cancel this action
  123. return;
  124. default:
  125. qWarning() << "error: wrong return value from QMessageBox:" << ret;
  126. return;
  127. }
  128. }
  129. textEditor->setReadOnly(true);
  130. showFileReadMode();
  131. }
  132. bool VEditor::saveFile()
  133. {
  134. if (!isEditMode || !noteFile->modifiable) {
  135. return true;
  136. }
  137. textEditor->beginSave();
  138. bool ret = writeFileToDisk(QDir(noteFile->path).filePath(noteFile->name),
  139. noteFile->content);
  140. if (!ret) {
  141. QMessageBox msgBox(QMessageBox::Warning, tr("Fail to save to file"),
  142. QString("Fail to write to disk when saving a note. Please try it again."));
  143. msgBox.setStandardButtons(QMessageBox::Ok);
  144. msgBox.setDefaultButton(QMessageBox::Ok);
  145. msgBox.exec();
  146. return false;
  147. }
  148. textEditor->endSave();
  149. return true;
  150. }