viewarea.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef VIEWAREA_H
  2. #define VIEWAREA_H
  3. #include <QWidget>
  4. #include <QSharedPointer>
  5. #include <functional>
  6. #include <buffer/buffer.h>
  7. #include "global.h"
  8. #include "navigationmode.h"
  9. #include "viewsplit.h"
  10. #include "viewareasession.h"
  11. class QLayout;
  12. class QSplitter;
  13. namespace vnotex
  14. {
  15. class Buffer;
  16. class ViewWindow;
  17. class Event;
  18. class Notebook;
  19. struct FileOpenParameters;
  20. // Hold a list of ViewWindow. A ViewSplit could display any ViewWorkspace.
  21. // A ViewWorkspace could only be displayed by one ViewSplit at a time.
  22. // If a ViewWorkspace is visible, it is managed by its ViewSplit and
  23. // its state may not reflect the real fact.
  24. struct ViewWorkspace
  25. {
  26. explicit ViewWorkspace(ID p_id)
  27. : m_id(p_id)
  28. {
  29. }
  30. ~ViewWorkspace()
  31. {
  32. Q_ASSERT(m_viewWindows.isEmpty());
  33. }
  34. void clear()
  35. {
  36. m_visible = false;
  37. m_viewWindows.clear();
  38. m_currentViewWindowIndex = 0;
  39. }
  40. const ID m_id = 0;
  41. // Whether it is displayed by a ViewSplit now.
  42. bool m_visible = false;
  43. QVector<ViewWindow *> m_viewWindows;
  44. int m_currentViewWindowIndex = 0;
  45. };
  46. // ViewArea -- ViewSplit -- ViewWindow.
  47. // ViewSplit may be put within a splitter.
  48. // There may be multiple ViewWindow for the same buffer.
  49. class ViewArea : public QWidget, public NavigationMode
  50. {
  51. Q_OBJECT
  52. public:
  53. explicit ViewArea(QWidget *p_parent = nullptr);
  54. ~ViewArea();
  55. QSize sizeHint() const Q_DECL_OVERRIDE;
  56. // Not all Workspace. Just all ViewSplits.
  57. QList<Buffer *> getAllBuffersInViewSplits() const;
  58. ViewWindow *getCurrentViewWindow() const;
  59. public slots:
  60. void openBuffer(Buffer *p_buffer, const QSharedPointer<FileOpenParameters> &p_paras);
  61. bool close(const Notebook *p_notebook, bool p_force);
  62. void focus();
  63. // NavigationMode.
  64. protected:
  65. QVector<void *> getVisibleNavigationItems() Q_DECL_OVERRIDE;
  66. void placeNavigationLabel(int p_idx, void *p_item, QLabel *p_label) Q_DECL_OVERRIDE;
  67. void handleTargetHit(void *p_item) Q_DECL_OVERRIDE;
  68. void clearNavigation() Q_DECL_OVERRIDE;
  69. protected:
  70. // To accept specific drop.
  71. void dragEnterEvent(QDragEnterEvent *p_event) Q_DECL_OVERRIDE;
  72. // Drop the data.
  73. void dropEvent(QDropEvent *p_event) Q_DECL_OVERRIDE;
  74. signals:
  75. // Status widget of ViewArea is changed.
  76. // MainWindow should set the corresponding status widget accordingly.
  77. void statusWidgetChanged(QWidget *p_widget);
  78. // Count of ViewSplit is chagned.
  79. // Used internally.
  80. void viewSplitsCountChanged();
  81. void currentViewWindowChanged();
  82. // State of current view window has update.
  83. void currentViewWindowUpdated();
  84. private slots:
  85. // Return true if @p_win is closed.
  86. // @p_removeSplitIfEmpty: whether remove the workspace and split if @p_win is that only ViewWindow left.
  87. bool closeViewWindow(ViewWindow *p_win, bool p_force, bool p_removeSplitIfEmpty);
  88. void maximizeViewSplit(ViewSplit *p_split);
  89. void distributeViewSplits();
  90. void newWorkspaceInViewSplit(ViewSplit *p_split);
  91. // @p_insertNew: whether to find an available workspace for @p_split after removal.
  92. // Return true if removed.
  93. bool removeWorkspaceInViewSplit(ViewSplit *p_split, bool p_insertNew);
  94. void handleViewSplitsCountChange();
  95. bool close(bool p_force);
  96. void handleNodeChange(Node *p_node, const QSharedPointer<Event> &p_event);
  97. void loadSession();
  98. void saveSession() const;
  99. private:
  100. enum class SplitType
  101. {
  102. Vertical,
  103. Horizontal
  104. };
  105. enum class Direction
  106. {
  107. Left,
  108. Down,
  109. Up,
  110. Right
  111. };
  112. void setupUI();
  113. // Find given @p_buffer among all view splits.
  114. // Does not search invisible work spaces.
  115. QVector<ViewWindow *> findBufferInViewSplits(const Buffer *p_buffer) const;
  116. ViewSplit *createViewSplit(QWidget *p_parent, ID p_viewSplitId = InvalidViewSplitId);
  117. // A Scene widget will be used when there is no split.
  118. // Usually it is used to show some help message.
  119. void showSceneWidget();
  120. void hideSceneWidget();
  121. void addFirstViewSplit();
  122. // Close all the ViewWindows in @p_split if @p_removeWorkspace is true.
  123. // Otherwise, detach the Workpace from it and remove the split.
  124. void removeViewSplit(ViewSplit *p_split, bool p_removeWorkspace);
  125. void setCurrentViewWindow(ViewWindow *p_win);
  126. ViewSplit *getCurrentViewSplit() const;
  127. void setCurrentViewSplit(ViewSplit *p_split, bool p_focus);
  128. QSharedPointer<ViewWorkspace> createWorkspace();
  129. void splitViewSplit(ViewSplit *p_split, SplitType p_type);
  130. QSplitter *tryGetParentSplitter(const QWidget *p_widget) const;
  131. // Distribute all splits within @p_splitter recursively.
  132. void distributeViewSplitsOfSplitter(QSplitter *p_splitter);
  133. void unwrapSplitter(QSplitter *p_splitter);
  134. void maximizeWidgetOfSplitter(QWidget *p_widget);
  135. QSplitter *createSplitter(Qt::Orientation p_orientation, QWidget *p_parent) const;
  136. // Do not use the reference since it will remove the value in @m_workspaces.
  137. // Should have done the clean up of ViewWindow before calling this.
  138. void removeWorkspace(QSharedPointer<ViewWorkspace> p_workspace);
  139. bool shouldUseGlobalStatusWidget() const;
  140. // Iterate through all ViewWindows including both ViewSplits and Workspaces.
  141. // Should NOT use this function to close ViewWindow.
  142. void forEachViewWindow(const ViewSplit::ViewWindowSelector &p_func);
  143. void setupShortcuts();
  144. // Close all ViewWindows related to @p_node.
  145. bool close(Node *p_node, bool p_force);
  146. // Go through all ViewWindows and judge whether to close it by @p_func.
  147. bool closeIf(bool p_force,
  148. const ViewSplit::ViewWindowSelector &p_func,
  149. bool p_closeEmptySplit);
  150. void checkCurrentViewWindowChange();
  151. QVector<ViewWindow *> getAllViewWindows(ViewSplit *p_split, const ViewSplit::ViewWindowSelector &p_func) const;
  152. QVector<ViewWindow *> getAllViewWindows(ViewSplit *p_split) const;
  153. void takeSnapshot(ViewAreaSession &p_session) const;
  154. void postFirstViewSplit();
  155. void loadSplitterFromSession(const ViewAreaSession::Node &p_node, QSplitter *p_splitter);
  156. void openViewWindowFromSession(const ViewWindowSession &p_session);
  157. void focusSplitByDirection(Direction p_direction);
  158. SplitType checkSplitType(const QSplitter *p_splitter) const;
  159. void flashViewSplit(ViewSplit *p_split);
  160. QLayout *m_mainLayout = nullptr;
  161. QWidget *m_sceneWidget = nullptr;
  162. QVector<ViewSplit *> m_splits;
  163. QVector<QSharedPointer<ViewWorkspace>> m_workspaces;
  164. ViewSplit *m_currentSplit = nullptr;
  165. ViewWindow *m_currentWindow = nullptr;
  166. // Current global status widget.
  167. // When there is no split window, we will make the status widget global.
  168. // Otherwise, we will display the status widget at the bottom of each view window.
  169. QSharedPointer<QWidget> m_currentStatusWidget;
  170. QVector<ViewSplit::ViewWindowNavigationModeInfo> m_navigationItems;
  171. // Timer to check file change outside periodically.
  172. QTimer *m_fileCheckTimer = nullptr;
  173. ID m_nextViewSplitId = InvalidViewSplitId + 1;
  174. };
  175. } // ns vnotex
  176. #endif // VIEWAREA_H