viewwindow.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #ifndef VIEWWINDOW_H
  2. #define VIEWWINDOW_H
  3. #include <QFrame>
  4. #include <QIcon>
  5. #include <QSharedPointer>
  6. #include <buffer/buffer.h>
  7. #include "viewwindowtoolbarhelper.h"
  8. class QVBoxLayout;
  9. class QTimer;
  10. class QToolBar;
  11. namespace vnotex
  12. {
  13. class ViewSplit;
  14. struct FileOpenParameters;
  15. class DragDropAreaIndicator;
  16. class DragDropAreaIndicatorInterface;
  17. class OutlineProvider;
  18. class EditReadDiscardAction;
  19. class FindAndReplaceWidget;
  20. class StatusWidget;
  21. class ViewWindow : public QFrame
  22. {
  23. Q_OBJECT
  24. public:
  25. enum Mode
  26. {
  27. Read,
  28. Edit,
  29. FullPreview,
  30. FocusPreview,
  31. Invalid
  32. };
  33. explicit ViewWindow(QWidget *p_parent = nullptr);
  34. virtual ~ViewWindow();
  35. Buffer *getBuffer() const;
  36. void attachToBuffer(Buffer *p_buffer);
  37. void detachFromBuffer(bool p_quiet = false);
  38. virtual const QIcon &getIcon() const;
  39. virtual QString getName() const;
  40. QString getTitle() const;
  41. ViewSplit *getViewSplit() const;
  42. void setViewSplit(ViewSplit *p_split);
  43. QSharedPointer<QWidget> statusWidget();
  44. // Whether should show standalone status widget.
  45. void setStatusWidgetVisible(bool p_visible);
  46. // Get latest content from editor instead of buffer.
  47. virtual QString getLatestContent() const = 0;
  48. // Will be called before close.
  49. // Return true if it is OK to proceed.
  50. bool aboutToClose(bool p_force);
  51. ViewWindow::Mode getMode() const;
  52. virtual void setMode(Mode p_mode) = 0;
  53. virtual QSharedPointer<OutlineProvider> getOutlineProvider();
  54. // Called by upside.
  55. void checkFileMissingOrChangedOutsidePeriodically();
  56. public slots:
  57. virtual void handleEditorConfigChange() = 0;
  58. void findNext(const QString &p_text, FindOptions p_options);
  59. void replace(const QString &p_text, FindOptions p_options, const QString &p_replaceText);
  60. void replaceAll(const QString &p_text, FindOptions p_options, const QString &p_replaceText);
  61. signals:
  62. // Emit when the attached buffer is changed.
  63. void bufferChanged();
  64. // Emit when this ViewWindow get focused.
  65. void focused(ViewWindow *p_win);
  66. // Emit when the status of this ViewWindow has changed,
  67. // such as modification state.
  68. void statusChanged();
  69. void modeChanged();
  70. void nameChanged();
  71. void attachmentChanged();
  72. protected:
  73. enum TypeAction
  74. {
  75. Heading1,
  76. Heading2,
  77. Heading3,
  78. Heading4,
  79. Heading5,
  80. Heading6,
  81. HeadingNone,
  82. // Make sure the order is identical with ViewWindowToolBarHelper::Action.
  83. Bold,
  84. Italic,
  85. Strikethrough,
  86. UnorderedList,
  87. OrderedList,
  88. TodoList,
  89. CheckedTodoList,
  90. Code,
  91. CodeBlock,
  92. Math,
  93. MathBlock,
  94. Quote,
  95. Link,
  96. Image,
  97. Table,
  98. Mark
  99. };
  100. protected slots:
  101. // Handle current buffer change.
  102. virtual void handleBufferChangedInternal() = 0;
  103. // Handle all kinds of type action.
  104. virtual void handleTypeAction(TypeAction p_action);
  105. virtual void handleSectionNumberOverride(OverrideState p_state);
  106. virtual void handleFindTextChanged(const QString &p_text, FindOptions p_options);
  107. virtual void handleFindNext(const QString &p_text, FindOptions p_options);
  108. virtual void handleReplace(const QString &p_text, FindOptions p_options, const QString &p_replaceText);
  109. virtual void handleReplaceAll(const QString &p_text, FindOptions p_options, const QString &p_replaceText);
  110. virtual void handleFindAndReplaceWidgetClosed();
  111. virtual void handleFindAndReplaceWidgetOpened();
  112. // Show message in status widget if exists. Otherwise, show it in the mainwindow's status widget.
  113. void showMessage(const QString p_msg);
  114. protected:
  115. void setCentralWidget(QWidget *p_widget);
  116. void addTopWidget(QWidget *p_widget);
  117. void addToolBar(QToolBar *p_bar);
  118. void addBottomWidget(QWidget *p_widget);
  119. void setStatusWidget(const QSharedPointer<StatusWidget> &p_widget);
  120. bool eventFilter(QObject *p_obj, QEvent *p_event) Q_DECL_OVERRIDE;
  121. void wheelEvent(QWheelEvent *p_event) Q_DECL_OVERRIDE;
  122. void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
  123. // Provide some common actions of tool bar for ViewWindow.
  124. QAction *addAction(QToolBar *p_toolBar, ViewWindowToolBarHelper::Action p_action);
  125. // ViewWindow should set editor's modification state.
  126. virtual void setModified(bool p_modified) = 0;
  127. // Return true if it is OK to proceed.
  128. virtual bool aboutToCloseInternal(bool p_force);
  129. // Sync buffer changes to editor.
  130. virtual void syncEditorFromBuffer() = 0;
  131. // Sync buffer content changes to editor.
  132. virtual void syncEditorFromBufferContent() = 0;
  133. // Whether we are in a mode that enable us to insert text.
  134. bool inModeCanInsert() const;
  135. virtual void detachFromBufferInternal();
  136. virtual void scrollUp() = 0;
  137. virtual void scrollDown() = 0;
  138. virtual void zoom(bool p_zoomIn) = 0;
  139. void showZoomFactor(qreal p_factor);
  140. void showZoomDelta(int p_delta);
  141. void showFindAndReplaceWidget();
  142. void hideFindAndReplaceWidget();
  143. bool findAndReplaceWidgetVisible() const;
  144. // @p_currentMatchIndex: 0-based.
  145. void showFindResult(const QString &p_text, int p_totalMatches, int p_currentMatchIndex);
  146. void showReplaceResult(const QString &p_text, int p_totalReplaces);
  147. void edit();
  148. void read(bool p_save);
  149. static ViewWindow::Mode modeFromOpenParameters(const FileOpenParameters &p_paras);
  150. static QToolBar *createToolBar(QWidget *p_parent = nullptr);
  151. // The revision of the buffer of the last sync content.
  152. int m_bufferRevision = 0;
  153. // Whether there is change of editor config since last update.
  154. // Subclass should maintain it.
  155. int m_editorConfigRevision = 0;
  156. Mode m_mode = Mode::Invalid;
  157. // Managed by QObject.
  158. FindAndReplaceWidget *m_findAndReplace = nullptr;
  159. private:
  160. struct FindInfo
  161. {
  162. QString m_text;
  163. FindOptions m_options;
  164. };
  165. void setupUI();
  166. void initIcons();
  167. void setupShortcuts();
  168. void discardChangesAndRead();
  169. void checkBackupFileOfPreviousSession();
  170. DragDropAreaIndicator *getAttachmentDragDropArea();
  171. const QIcon &getAttachmentIcon(Buffer *p_buffer) const;
  172. // A wrapper of saveInternal().
  173. bool save(bool p_force = false);
  174. // Save buffer content to file.
  175. bool saveInternal(bool p_force = false);
  176. // Discard changes and reload buffer content from file.
  177. bool reload();
  178. void updateEditReadDiscardActionState(EditReadDiscardAction *p_act);
  179. // Return code of checkFileMissingOrChangedOutside().
  180. enum
  181. {
  182. // File is not missing or changed outside.
  183. Normal,
  184. // Force save the buffer to file or reload the buffer from file.
  185. SavedOrReloaded,
  186. // Discard the buffer.
  187. Discarded,
  188. // User do not handle it.
  189. Failed
  190. };
  191. int checkFileMissingOrChangedOutside();
  192. void findNextOnLastFind(bool p_forward = true);
  193. static ViewWindow::TypeAction toolBarActionToTypeAction(ViewWindowToolBarHelper::Action p_action);
  194. Buffer *m_buffer = nullptr;
  195. // Null if this window has not been added to any split.
  196. ViewSplit *m_viewSplit = nullptr;
  197. // Managed by QObject.
  198. QWidget *m_centralWidget = nullptr;
  199. // Managed by QObject.
  200. QVBoxLayout *m_mainLayout = nullptr;
  201. // Managed by QObject.
  202. QVBoxLayout *m_topLayout = nullptr;
  203. // Managed by QObject.
  204. QVBoxLayout *m_bottomLayout = nullptr;
  205. QTimer *m_syncBufferContentTimer = nullptr;
  206. // Managed by QObject.
  207. // Allocated on necessary. Use getAttachmentDragDropArea() to access.
  208. DragDropAreaIndicator *m_attachmentDragDropIndicator = nullptr;
  209. QScopedPointer<DragDropAreaIndicatorInterface> m_attachmentDragDropIndicatorInterface;
  210. // Managed by QObject.
  211. QToolBar *m_toolBar = nullptr;
  212. // Whether check file missing or changed outside.
  213. bool m_fileChangeCheckEnabled = true;
  214. // Last find info.
  215. FindInfo m_findInfo;
  216. QSharedPointer<StatusWidget> m_statusWidget;
  217. EditReadDiscardAction *m_editReadDiscardAct = nullptr;
  218. static QIcon s_savedIcon;
  219. static QIcon s_modifiedIcon;
  220. };
  221. } // ns vnotex
  222. #endif // VIEWWINDOW_H