veditwindow.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef VEDITWINDOW_H
  2. #define VEDITWINDOW_H
  3. #include <QTabWidget>
  4. #include <QJsonObject>
  5. #include <QString>
  6. #include <QFileInfo>
  7. #include <QDir>
  8. #include "vnotebook.h"
  9. #include "vedittab.h"
  10. #include "vtoc.h"
  11. #include "vconstants.h"
  12. class VNote;
  13. class QPushButton;
  14. class QActionGroup;
  15. class VFile;
  16. class VEditArea;
  17. class VEditWindow : public QTabWidget
  18. {
  19. Q_OBJECT
  20. public:
  21. explicit VEditWindow(VNote *vnote, VEditArea *editArea, QWidget *parent = 0);
  22. int findTabByFile(const VFile *p_file) const;
  23. int openFile(VFile *p_file, OpenFileMode p_mode);
  24. bool closeFile(const VFile *p_file, bool p_forced);
  25. bool closeFile(const VDirectory *p_dir, bool p_forced);
  26. bool closeFile(const VNotebook *p_notebook, bool p_forced);
  27. void editFile();
  28. void saveFile();
  29. void readFile();
  30. void saveAndReadFile();
  31. bool closeAllFiles(bool p_forced);
  32. void requestUpdateOutline();
  33. void requestUpdateCurHeader();
  34. // Focus to current tab's editor
  35. void focusWindow();
  36. void scrollCurTab(const VAnchor &p_anchor);
  37. void updateFileInfo(const VFile *p_file);
  38. void updateDirectoryInfo(const VDirectory *p_dir);
  39. void updateNotebookInfo(const VNotebook *p_notebook);
  40. VEditTab *currentEditTab();
  41. // Insert a tab with @p_widget. @p_widget is a fully initialized VEditTab.
  42. bool addEditTab(QWidget *p_widget);
  43. // Set whether it is the current window.
  44. void setCurrentWindow(bool p_current);
  45. void clearSearchedWordHighlight();
  46. void moveCurrentTabOneSplit(bool p_right);
  47. void focusNextTab(bool p_right);
  48. // Return true if the file list is shown.
  49. bool showOpenedFileList();
  50. bool activateTab(int p_sequence);
  51. // Switch to previous activated tab.
  52. bool alternateTab();
  53. VEditTab *getTab(int tabIndex) const;
  54. // Ask tab @p_index to update its status and propogate.
  55. // The status here means tab status, outline, current header.
  56. // If @p_index is -1, it is current tab.
  57. void updateTabStatus(int p_index = -1);
  58. protected:
  59. void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  60. // To accept specific drop.
  61. void dragEnterEvent(QDragEnterEvent *p_event) Q_DECL_OVERRIDE;
  62. // Drop the data.
  63. void dropEvent(QDropEvent *p_event) Q_DECL_OVERRIDE;
  64. signals:
  65. // Status of current VEditTab has update.
  66. void tabStatusUpdated(const VEditTabInfo &p_info);
  67. // Requst VEditArea to split this window.
  68. void requestSplitWindow(VEditWindow *p_window, bool p_right = true);
  69. void requestRemoveSplit(VEditWindow *curWindow);
  70. // This widget or its children get the focus
  71. void getFocused();
  72. void outlineChanged(const VToc &toc);
  73. void curHeaderChanged(const VAnchor &anchor);
  74. // Emit when want to show message in status bar.
  75. void statusMessage(const QString &p_msg);
  76. // Emit when Vim mode status changed.
  77. void vimStatusUpdated(const VVim *p_vim);
  78. private slots:
  79. // Close tab @p_index.
  80. bool closeTab(int p_index);
  81. // Split this window on the right/left.
  82. void splitWindow(bool p_right = true);
  83. void removeSplit();
  84. void handleTabbarClicked(int p_index);
  85. void handleCurrentIndexChanged(int p_index);
  86. void contextMenuRequested(QPoint pos);
  87. void tabListJump(VFile *p_file);
  88. void handleOutlineChanged(const VToc &p_toc);
  89. void handleCurHeaderChanged(const VAnchor &p_anchor);
  90. void updateSplitMenu();
  91. void tabbarContextMenuRequested(QPoint p_pos);
  92. void handleLocateAct();
  93. void handleMoveLeftAct();
  94. void handleMoveRightAct();
  95. // Handle the statusMessage signal of VEditTab.
  96. void handleTabStatusMessage(const QString &p_msg);
  97. // Handle the vimStatusUpdated() signal of VEditTab.
  98. void handleTabVimStatusUpdated(const VVim *p_vim);
  99. // Handle the statusUpdated signal of VEditTab.
  100. void handleTabStatusUpdated(const VEditTabInfo &p_info);
  101. private:
  102. void initTabActions();
  103. void setupCornerWidget();
  104. void removeEditTab(int p_index);
  105. int insertEditTab(int p_index, VFile *p_file, QWidget *p_page);
  106. int appendEditTab(VFile *p_file, QWidget *p_page);
  107. int openFileInTab(VFile *p_file, OpenFileMode p_mode);
  108. inline QString generateTooltip(const VFile *p_file) const;
  109. inline QString generateTabText(int p_index, const QString &p_name,
  110. bool p_modified, bool p_modifiable) const;
  111. bool canRemoveSplit();
  112. // Move tab at @p_tabIdx one split window.
  113. // @p_right: move right or left.
  114. // If there is only one split window, it will request to split current window
  115. // and move the tab to the new split.
  116. void moveTabOneSplit(int p_tabIdx, bool p_right);
  117. void updateTabInfo(int p_idx);
  118. // Update the sequence number of all the tabs.
  119. void updateAllTabsSequence();
  120. // Connect the signals of VEditTab to this VEditWindow.
  121. void connectEditTab(const VEditTab *p_tab);
  122. VNote *vnote;
  123. VEditArea *m_editArea;
  124. // These two members are only used for alternateTab().
  125. QWidget *m_curTabWidget;
  126. QWidget *m_lastTabWidget;
  127. // Button in the right corner
  128. QPushButton *rightBtn;
  129. // Button in the left corner
  130. QPushButton *leftBtn;
  131. // Actions
  132. QAction *splitAct;
  133. QAction *removeSplitAct;
  134. // Locate current note in the directory and file list
  135. QAction *m_locateAct;
  136. QAction *m_moveLeftAct;
  137. QAction *m_moveRightAct;
  138. // Close current tab action in tab menu.
  139. QAction *m_closeTabAct;
  140. // Close other tabs action in tab menu.
  141. QAction *m_closeOthersAct;
  142. // Close tabs to the right in tab menu.
  143. QAction *m_closeRightAct;
  144. // View and edit info about this note.
  145. QAction *m_noteInfoAct;
  146. // Open the location (the folder containing this file) of this note.
  147. QAction *m_openLocationAct;
  148. };
  149. inline QString VEditWindow::generateTooltip(const VFile *p_file) const
  150. {
  151. if (!p_file) {
  152. return "";
  153. }
  154. // [Notebook]path
  155. return QString("[%1] %2").arg(p_file->getNotebookName()).arg(p_file->fetchPath());
  156. }
  157. inline QString VEditWindow::generateTabText(int p_index, const QString &p_name,
  158. bool p_modified, bool p_modifiable) const
  159. {
  160. QString seq = QString::number(p_index + c_tabSequenceBase, 10);
  161. return seq + ". " + p_name + (p_modifiable ? (p_modified ? "*" : "") : "#");
  162. }
  163. #endif // VEDITWINDOW_H