vedittab.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "vedittab.h"
  2. #include <QApplication>
  3. #include <QWheelEvent>
  4. #include "utils/vutils.h"
  5. #include "vconfigmanager.h"
  6. extern VConfigManager *g_config;
  7. VEditTab::VEditTab(VFile *p_file, VEditArea *p_editArea, QWidget *p_parent)
  8. : QWidget(p_parent),
  9. m_file(p_file),
  10. m_isEditMode(false),
  11. m_outline(p_file),
  12. m_currentHeader(p_file, -1),
  13. m_editArea(p_editArea),
  14. m_checkFileChange(true),
  15. m_fileDiverged(false)
  16. {
  17. connect(qApp, &QApplication::focusChanged,
  18. this, &VEditTab::handleFocusChanged);
  19. }
  20. VEditTab::~VEditTab()
  21. {
  22. if (m_file) {
  23. m_file->close();
  24. }
  25. }
  26. void VEditTab::focusTab()
  27. {
  28. focusChild();
  29. emit getFocused();
  30. updateStatus();
  31. }
  32. bool VEditTab::isEditMode() const
  33. {
  34. return m_isEditMode;
  35. }
  36. bool VEditTab::isModified() const
  37. {
  38. return false;
  39. }
  40. VFile *VEditTab::getFile() const
  41. {
  42. return m_file;
  43. }
  44. void VEditTab::handleFocusChanged(QWidget * /* p_old */, QWidget *p_now)
  45. {
  46. if (p_now == this) {
  47. // When VEditTab get focus, it should focus to current widget.
  48. focusChild();
  49. emit getFocused();
  50. updateStatus();
  51. } else if (isAncestorOf(p_now)) {
  52. emit getFocused();
  53. updateStatus();
  54. }
  55. }
  56. void VEditTab::wheelEvent(QWheelEvent *p_event)
  57. {
  58. QPoint angle = p_event->angleDelta();
  59. Qt::KeyboardModifiers modifiers = p_event->modifiers();
  60. if (!angle.isNull() && (angle.y() != 0) && (modifiers & Qt::ControlModifier)) {
  61. // Zoom in/out current tab.
  62. zoom(angle.y() > 0);
  63. p_event->accept();
  64. return;
  65. }
  66. p_event->ignore();
  67. }
  68. VEditTabInfo VEditTab::fetchTabInfo() const
  69. {
  70. VEditTabInfo info;
  71. info.m_editTab = const_cast<VEditTab *>(this);
  72. return info;
  73. }
  74. const VHeaderPointer &VEditTab::getCurrentHeader() const
  75. {
  76. return m_currentHeader;
  77. }
  78. const VTableOfContent &VEditTab::getOutline() const
  79. {
  80. return m_outline;
  81. }
  82. void VEditTab::tryRestoreFromTabInfo(const VEditTabInfo &p_info)
  83. {
  84. if (p_info.m_editTab != this) {
  85. m_infoToRestore.clear();
  86. return;
  87. }
  88. if (restoreFromTabInfo(p_info)) {
  89. m_infoToRestore.clear();
  90. return;
  91. }
  92. // Save it and restore later.
  93. m_infoToRestore = p_info;
  94. }
  95. void VEditTab::updateStatus()
  96. {
  97. emit statusUpdated(fetchTabInfo());
  98. }
  99. void VEditTab::evaluateMagicWords()
  100. {
  101. }
  102. bool VEditTab::tabHasFocus() const
  103. {
  104. QWidget *wid = QApplication::focusWidget();
  105. return wid == this || isAncestorOf(wid);
  106. }
  107. void VEditTab::insertLink()
  108. {
  109. }
  110. void VEditTab::applySnippet(const VSnippet *p_snippet)
  111. {
  112. Q_UNUSED(p_snippet);
  113. }
  114. void VEditTab::applySnippet()
  115. {
  116. }
  117. void VEditTab::checkFileChangeOutside()
  118. {
  119. if (!m_checkFileChange) {
  120. return;
  121. }
  122. if (m_file->isChangedOutside()) {
  123. int ret = VUtils::showMessage(QMessageBox::Information,
  124. tr("Information"),
  125. tr("Note <span style=\"%1\">%2</span> has been modified by another program.")
  126. .arg(g_config->c_dataTextStyle).arg(m_file->fetchPath()),
  127. tr("Do you want to reload it?"),
  128. QMessageBox::Yes | QMessageBox::No,
  129. QMessageBox::Yes,
  130. this);
  131. switch (ret) {
  132. case QMessageBox::Yes:
  133. reloadFromDisk();
  134. break;
  135. case QMessageBox::No:
  136. m_checkFileChange = false;
  137. m_fileDiverged = true;
  138. updateStatus();
  139. break;
  140. default:
  141. Q_ASSERT(false);
  142. break;
  143. }
  144. }
  145. }
  146. void VEditTab::reloadFromDisk()
  147. {
  148. m_file->reload();
  149. m_fileDiverged = false;
  150. m_checkFileChange = true;
  151. reload();
  152. }