vhtmltab.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <QtWidgets>
  2. #include <QFileInfo>
  3. #include "vhtmltab.h"
  4. #include "vedit.h"
  5. #include "utils/vutils.h"
  6. #include "vconfigmanager.h"
  7. #include "vnotebook.h"
  8. #include "dialog/vfindreplacedialog.h"
  9. #include "veditarea.h"
  10. #include "vconstants.h"
  11. extern VConfigManager vconfig;
  12. VHtmlTab::VHtmlTab(VFile *p_file, VEditArea *p_editArea,
  13. OpenFileMode p_mode, QWidget *p_parent)
  14. : VEditTab(p_file, p_editArea, p_parent)
  15. {
  16. V_ASSERT(m_file->getDocType() == DocType::Html);
  17. m_file->open();
  18. setupUI();
  19. if (p_mode == OpenFileMode::Edit) {
  20. showFileEditMode();
  21. } else {
  22. showFileReadMode();
  23. }
  24. }
  25. void VHtmlTab::setupUI()
  26. {
  27. m_editor = new VEdit(m_file, this);
  28. connect(m_editor, &VEdit::textChanged,
  29. this, &VHtmlTab::handleTextChanged);
  30. connect(m_editor, &VEdit::saveAndRead,
  31. this, &VHtmlTab::saveAndRead);
  32. connect(m_editor, &VEdit::discardAndRead,
  33. this, &VHtmlTab::discardAndRead);
  34. connect(m_editor, &VEdit::editNote,
  35. this, &VHtmlTab::editFile);
  36. connect(m_editor, &VEdit::statusMessage,
  37. this, &VEditTab::statusMessage);
  38. m_editor->reloadFile();
  39. QVBoxLayout *mainLayout = new QVBoxLayout();
  40. mainLayout->addWidget(m_editor);
  41. setLayout(mainLayout);
  42. }
  43. void VHtmlTab::handleTextChanged()
  44. {
  45. V_ASSERT(m_file->isModifiable());
  46. if (m_modified) {
  47. return;
  48. }
  49. noticeStatusChanged();
  50. }
  51. void VHtmlTab::noticeStatusChanged()
  52. {
  53. m_modified = m_file->isModified();
  54. emit statusChanged();
  55. }
  56. void VHtmlTab::showFileReadMode()
  57. {
  58. m_isEditMode = false;
  59. m_editor->setReadOnly(true);
  60. noticeStatusChanged();
  61. }
  62. void VHtmlTab::showFileEditMode()
  63. {
  64. if (!m_file->isModifiable()) {
  65. return;
  66. }
  67. m_isEditMode = true;
  68. m_editor->beginEdit();
  69. m_editor->setFocus();
  70. noticeStatusChanged();
  71. }
  72. bool VHtmlTab::closeFile(bool p_forced)
  73. {
  74. if (p_forced && m_isEditMode) {
  75. // Discard buffer content
  76. m_editor->reloadFile();
  77. m_editor->endEdit();
  78. showFileReadMode();
  79. } else {
  80. readFile();
  81. }
  82. return !m_isEditMode;
  83. }
  84. void VHtmlTab::editFile()
  85. {
  86. if (m_isEditMode || !m_file->isModifiable()) {
  87. return;
  88. }
  89. showFileEditMode();
  90. }
  91. void VHtmlTab::readFile()
  92. {
  93. if (!m_isEditMode) {
  94. return;
  95. }
  96. if (m_editor && m_editor->isModified()) {
  97. // Prompt to save the changes.
  98. int ret = VUtils::showMessage(QMessageBox::Information, tr("Information"),
  99. tr("Note <span style=\"%1\">%2</span> has been modified.")
  100. .arg(vconfig.c_dataTextStyle).arg(m_file->getName()),
  101. tr("Do you want to save your changes?"),
  102. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
  103. QMessageBox::Save, this);
  104. switch (ret) {
  105. case QMessageBox::Save:
  106. saveFile();
  107. // Fall through
  108. case QMessageBox::Discard:
  109. m_editor->reloadFile();
  110. break;
  111. case QMessageBox::Cancel:
  112. // Nothing to do if user cancel this action
  113. return;
  114. default:
  115. qWarning() << "wrong return value from QMessageBox:" << ret;
  116. return;
  117. }
  118. }
  119. if (m_editor) {
  120. m_editor->endEdit();
  121. }
  122. showFileReadMode();
  123. }
  124. bool VHtmlTab::saveFile()
  125. {
  126. if (!m_isEditMode || !m_editor->isModified()) {
  127. return true;
  128. }
  129. bool ret;
  130. // Make sure the file already exists. Temporary deal with cases when user delete or move
  131. // a file.
  132. QString filePath = m_file->retrivePath();
  133. if (!QFileInfo::exists(filePath)) {
  134. qWarning() << filePath << "being written has been removed";
  135. VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to save note."),
  136. tr("File <span style=\"%1\">%2</span> being written has been removed.")
  137. .arg(vconfig.c_dataTextStyle).arg(filePath),
  138. QMessageBox::Ok, QMessageBox::Ok, this);
  139. return false;
  140. }
  141. m_editor->saveFile();
  142. ret = m_file->save();
  143. if (!ret) {
  144. VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to save note."),
  145. tr("Fail to write to disk when saving a note. Please try it again."),
  146. QMessageBox::Ok, QMessageBox::Ok, this);
  147. m_editor->setModified(true);
  148. }
  149. noticeStatusChanged();
  150. return ret;
  151. }
  152. void VHtmlTab::saveAndRead()
  153. {
  154. saveFile();
  155. readFile();
  156. }
  157. void VHtmlTab::discardAndRead()
  158. {
  159. readFile();
  160. }
  161. void VHtmlTab::scrollToAnchor(const VAnchor & /* p_anchor */)
  162. {
  163. }
  164. void VHtmlTab::insertImage()
  165. {
  166. }
  167. void VHtmlTab::findText(const QString &p_text, uint p_options, bool p_peek,
  168. bool p_forward)
  169. {
  170. if (p_peek) {
  171. m_editor->peekText(p_text, p_options);
  172. } else {
  173. m_editor->findText(p_text, p_options, p_forward);
  174. }
  175. }
  176. void VHtmlTab::replaceText(const QString &p_text, uint p_options,
  177. const QString &p_replaceText, bool p_findNext)
  178. {
  179. if (m_isEditMode) {
  180. m_editor->replaceText(p_text, p_options, p_replaceText, p_findNext);
  181. }
  182. }
  183. void VHtmlTab::replaceTextAll(const QString &p_text, uint p_options,
  184. const QString &p_replaceText)
  185. {
  186. if (m_isEditMode) {
  187. m_editor->replaceTextAll(p_text, p_options, p_replaceText);
  188. }
  189. }
  190. QString VHtmlTab::getSelectedText() const
  191. {
  192. QTextCursor cursor = m_editor->textCursor();
  193. return cursor.selectedText();
  194. }
  195. void VHtmlTab::clearSearchedWordHighlight()
  196. {
  197. m_editor->clearSearchedWordHighlight();
  198. }
  199. void VHtmlTab::zoom(bool /* p_zoomIn */, qreal /* p_step */)
  200. {
  201. }
  202. void VHtmlTab::focusChild()
  203. {
  204. m_editor->setFocus();
  205. }