vmainwindow.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #ifndef VMAINWINDOW_H
  2. #define VMAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QVector>
  5. #include <QPair>
  6. #include <QPointer>
  7. #include <QString>
  8. #include "vfile.h"
  9. #include "vedittab.h"
  10. #include "utils/vwebutils.h"
  11. class QLabel;
  12. class QComboBox;
  13. class VDirectoryTree;
  14. class QSplitter;
  15. class QListWidget;
  16. class QTabWidget;
  17. class QToolBar;
  18. class VNote;
  19. class QAction;
  20. class QPushButton;
  21. class VNotebook;
  22. class QActionGroup;
  23. class VFileList;
  24. class VEditArea;
  25. class VToolBox;
  26. class VOutline;
  27. class VNotebookSelector;
  28. class VFindReplaceDialog;
  29. class VCaptain;
  30. class VVimIndicator;
  31. class VVimCmdLineEdit;
  32. class VTabIndicator;
  33. class VSingleInstanceGuard;
  34. class QTimer;
  35. class QSystemTrayIcon;
  36. class VButtonWithWidget;
  37. class VAttachmentList;
  38. class VSnippetList;
  39. class VCart;
  40. class VSearcher;
  41. class QPrinter;
  42. class VUniversalEntry;
  43. class VHistoryList;
  44. class VExplorer;
  45. class VTagExplorer;
  46. enum class PanelViewState
  47. {
  48. ExpandMode = 0,
  49. HorizontalMode,
  50. VerticalMode,
  51. Invalid
  52. };
  53. class VMainWindow : public QMainWindow
  54. {
  55. Q_OBJECT
  56. public:
  57. VMainWindow(VSingleInstanceGuard *p_guard, QWidget *p_parent = 0);
  58. // Returns true if the location succeeds.
  59. bool locateFile(VFile *p_file);
  60. // Returns true if the location succeeds.
  61. bool locateDirectory(VDirectory *p_directory);
  62. // Returns true if the location succeeds.
  63. bool locateNotebook(VNotebook *p_notebook);
  64. VFileList *getFileList() const;
  65. VEditArea *getEditArea() const;
  66. VSnippetList *getSnippetList() const;
  67. VCart *getCart() const;
  68. VDirectoryTree *getDirectoryTree() const;
  69. VNotebookSelector *getNotebookSelector() const;
  70. VHistoryList *getHistoryList() const;
  71. // View and edit the information of @p_file, which is an orphan file.
  72. void editOrphanFileInfo(VFile *p_file);
  73. // Open files @p_files as orphan files or internal note files.
  74. // If @p_forceOrphan is false, for each file, VNote will try to find out if
  75. // it is a note inside VNote. If yes, VNote will open it as internal file.
  76. QVector<VFile *> openFiles(const QStringList &p_files,
  77. bool p_forceOrphan = false,
  78. OpenFileMode p_mode = OpenFileMode::Read,
  79. bool p_forceMode = false,
  80. bool p_oneByOne = false);
  81. // Try to open @p_filePath as internal note.
  82. bool tryOpenInternalFile(const QString &p_filePath);
  83. // Show a temporary message in status bar.
  84. void showStatusMessage(const QString &p_msg);
  85. // Open startup pages according to configuration.
  86. void openStartupPages();
  87. VCaptain *getCaptain() const;
  88. // Prompt user for new notebook if there is no notebook.
  89. void promptNewNotebookIfEmpty();
  90. // Check invalid notebooks. Set current notebook according to config.
  91. void checkNotebooks();
  92. VFile *getCurrentFile() const;
  93. VEditTab *getCurrentTab() const;
  94. VNotebook *getCurrentNotebook() const;
  95. // Kick off timer to do things after start.
  96. void kickOffStartUpTimer(const QStringList &p_files);
  97. void focusEditArea() const;
  98. void showExplorerPanel(bool p_focus = false);
  99. VExplorer *getExplorer() const;
  100. void setCaptainModeEnabled(bool p_enabled);
  101. signals:
  102. // Emit when editor related configurations were changed by user.
  103. void editorConfigUpdated();
  104. private slots:
  105. void importNoteFromFile();
  106. void viewSettings();
  107. void changeMarkdownConverter(QAction *action);
  108. void aboutMessage();
  109. // Display shortcuts help.
  110. void shortcutsHelp();
  111. void changeExpandTab(bool checked);
  112. void setTabStopWidth(QAction *action);
  113. void setEditorBackgroundColor(QAction *action);
  114. void setRenderBackgroundColor(QAction *action);
  115. void changeHighlightCursorLine(bool p_checked);
  116. void changeHighlightSelectedWord(bool p_checked);
  117. void changeHighlightSearchedWord(bool p_checked);
  118. void changeHighlightTrailingSapce(bool p_checked);
  119. void curEditFileInfo();
  120. void deleteCurNote();
  121. void handleCurrentDirectoryChanged(const VDirectory *p_dir);
  122. void handleCurrentNotebookChanged(const VNotebook *p_notebook);
  123. void handleFindDialogTextChanged(const QString &p_text, uint p_options);
  124. void openFindDialog();
  125. void enableMermaid(bool p_checked);
  126. void enableMathjax(bool p_checked);
  127. void changeAutoIndent(bool p_checked);
  128. void changeAutoList(bool p_checked);
  129. void changeVimMode(bool p_checked);
  130. void enableCodeBlockHighlight(bool p_checked);
  131. void enableImagePreview(bool p_checked);
  132. void enableImagePreviewConstraint(bool p_checked);
  133. void enableImageConstraint(bool p_checked);
  134. void enableImageCaption(bool p_checked);
  135. void printNote();
  136. // Open export dialog.
  137. void handleExportAct();
  138. // Handle Vim status updated.
  139. void handleVimStatusUpdated(const VVim *p_vim);
  140. // Handle the status update of the current tab of VEditArea.
  141. // Will be called frequently.
  142. void handleAreaTabStatusUpdated(const VEditTabInfo &p_info);
  143. // Check the shared memory between different instances to see if we have
  144. // files to open.
  145. void checkSharedMemory();
  146. void quitApp();
  147. // Restore main window.
  148. void showMainWindow();
  149. // Close current note.
  150. void closeCurrentFile();
  151. // Open flash page in edit mode.
  152. void openFlashPage();
  153. void customShortcut();
  154. void toggleEditReadMode();
  155. // Activate Universal Entry.
  156. void activateUniversalEntry();
  157. void stayOnTop(bool p_enabled);
  158. protected:
  159. void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
  160. void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  161. void changeEvent(QEvent *p_event) Q_DECL_OVERRIDE;
  162. private:
  163. void setupUI();
  164. void setupNaviBox();
  165. void setupNotebookPanel();
  166. void initToolBar();
  167. QToolBar *initFileToolBar(QSize p_iconSize = QSize());
  168. QToolBar *initViewToolBar(QSize p_iconSize = QSize());
  169. QToolBar *initNoteToolBar(QSize p_iconSize = QSize());
  170. QToolBar *initEditToolBar(QSize p_iconSize = QSize());
  171. void initMenuBar();
  172. void initFileMenu();
  173. void initEditMenu();
  174. void initViewMenu();
  175. void initMarkdownMenu();
  176. void initHelpMenu();
  177. void initDockWindows();
  178. void initToolsDock();
  179. void initSearchDock();
  180. void initRenderBackgroundMenu(QMenu *menu);
  181. void initRenderStyleMenu(QMenu *p_menu);
  182. void initCodeBlockStyleMenu(QMenu *p_menu);
  183. void initConverterMenu(QMenu *p_menu);
  184. void initMarkdownitOptionMenu(QMenu *p_menu);
  185. void initEditorBackgroundMenu(QMenu *menu);
  186. // Init the Line Number submenu in Edit menu.
  187. void initEditorLineNumberMenu(QMenu *p_menu);
  188. void initEditorStyleMenu(QMenu *p_emnu);
  189. void updateWindowTitle(const QString &str);
  190. void initVimCmd();
  191. // Update state of actions according to @p_tab.
  192. void updateActionsStateFromTab(const VEditTab *p_tab);
  193. void saveStateAndGeometry();
  194. void restoreStateAndGeometry();
  195. // Should init VCaptain before setupUI().
  196. void initCaptain();
  197. void registerCaptainAndNavigationTargets();
  198. // Update status bar information.
  199. void updateStatusInfo(const VEditTabInfo &p_info);
  200. // Wrapper to create a QAction.
  201. QAction *newAction(const QIcon &p_icon,
  202. const QString &p_text,
  203. QObject *p_parent = nullptr);
  204. // Init a timer to watch the change of the shared memory between instances of
  205. // VNote.
  206. void initSharedMemoryWatcher();
  207. // Init system tray icon and correspondign context menu.
  208. void initTrayIcon();
  209. // Change the panel view according to @p_state.
  210. void changePanelView(PanelViewState p_state);
  211. // Whether heading sequence is applicable to current tab.
  212. // Only available for writable Markdown file.
  213. bool isHeadingSequenceApplicable() const;
  214. void initShortcuts();
  215. void initHeadingButton(QToolBar *p_tb);
  216. void initThemeMenu(QMenu *p_emnu);
  217. void updateEditReadAct(const VEditTab *p_tab);
  218. void initUniversalEntry();
  219. void setMenuBarVisible(bool p_visible);
  220. void setToolBarVisible(bool p_visible);
  221. void showNotebookPanel();
  222. // Captain mode functions.
  223. // Popup the attachment list if it is enabled.
  224. static bool showAttachmentListByCaptain(void *p_target, void *p_data);
  225. static bool locateCurrentFileByCaptain(void *p_target, void *p_data);
  226. static bool toggleExpandModeByCaptain(void *p_target, void *p_data);
  227. static bool discardAndReadByCaptain(void *p_target, void *p_data);
  228. static bool toggleToolBarByCaptain(void *p_target, void *p_data);
  229. static bool toggleToolsDockByCaptain(void *p_target, void *p_data);
  230. static bool toggleSearchDockByCaptain(void *p_target, void *p_data);
  231. static bool closeFileByCaptain(void *p_target, void *p_data);
  232. static bool shortcutsHelpByCaptain(void *p_target, void *p_data);
  233. static bool flushLogFileByCaptain(void *p_target, void *p_data);
  234. static bool exportByCaptain(void *p_target, void *p_data);
  235. static bool focusEditAreaByCaptain(void *p_target, void *p_data);
  236. // End Captain mode functions.
  237. VNote *vnote;
  238. QPointer<VFile> m_curFile;
  239. QPointer<VEditTab> m_curTab;
  240. VCaptain *m_captain;
  241. VNotebookSelector *m_notebookSelector;
  242. VFileList *m_fileList;
  243. VDirectoryTree *m_dirTree;
  244. VToolBox *m_naviBox;
  245. // Splitter for directory | files | edit.
  246. QSplitter *m_mainSplitter;
  247. // Splitter for folders/notes.
  248. QSplitter *m_nbSplitter;
  249. VEditArea *m_editArea;
  250. QDockWidget *m_toolDock;
  251. QDockWidget *m_searchDock;
  252. // Tool box in the dock widget.
  253. VToolBox *m_toolBox;
  254. VOutline *outline;
  255. // View and manage snippets.
  256. VSnippetList *m_snippetList;
  257. // View and manage cart.
  258. VCart *m_cart;
  259. // Advanced search.
  260. VSearcher *m_searcher;
  261. VFindReplaceDialog *m_findReplaceDialog;
  262. VVimCmdLineEdit *m_vimCmd;
  263. VVimIndicator *m_vimIndicator;
  264. VTabIndicator *m_tabIndicator;
  265. // Actions
  266. QAction *newRootDirAct;
  267. QAction *newNoteAct;
  268. QAction *noteInfoAct;
  269. QAction *deleteNoteAct;
  270. // Toggle read and edit note.
  271. QAction *m_editReadAct;
  272. QAction *saveNoteAct;
  273. QAction *m_discardExitAct;
  274. QAction *expandViewAct;
  275. QAction *m_importNoteAct;
  276. QAction *m_printAct;
  277. QAction *m_exportAct;
  278. QAction *m_findReplaceAct;
  279. QAction *m_findNextAct;
  280. QAction *m_findPreviousAct;
  281. QAction *m_replaceAct;
  282. QAction *m_replaceFindAct;
  283. QAction *m_replaceAllAct;
  284. QAction *m_autoIndentAct;
  285. // Enable heading sequence for current note.
  286. QAction *m_headingSequenceAct;
  287. QAction *m_toolBarAct;
  288. // Act group for render styles.
  289. QActionGroup *m_renderStyleActs;
  290. // Act group for code block render styles.
  291. QActionGroup *m_codeBlockStyleActs;
  292. // Menus
  293. QMenu *m_viewMenu;
  294. // Edit ToolBar.
  295. QToolBar *m_editToolBar;
  296. // All the ToolBar.
  297. QVector<QToolBar *> m_toolBars;
  298. // Attachment button.
  299. VButtonWithWidget *m_attachmentBtn;
  300. // Attachment list.
  301. VAttachmentList *m_attachmentList;
  302. QPushButton *m_headingBtn;
  303. QPushButton *m_avatarBtn;
  304. // Single instance guard.
  305. VSingleInstanceGuard *m_guard;
  306. // Timer to check the shared memory between instances of VNote.
  307. QTimer *m_sharedMemTimer;
  308. // Tray icon.
  309. QSystemTrayIcon *m_trayIcon;
  310. // The old state of window.
  311. Qt::WindowStates m_windowOldState;
  312. // Whether user request VNote to quit.
  313. bool m_requestQuit;
  314. VWebUtils m_webUtils;
  315. QPrinter *m_printer;
  316. VUniversalEntry *m_ue;
  317. VHistoryList *m_historyList;
  318. VExplorer *m_explorer;
  319. VTagExplorer *m_tagExplorer;
  320. // Interval of the shared memory timer in ms.
  321. static const int c_sharedMemTimerInterval;
  322. };
  323. inline VFileList *VMainWindow::getFileList() const
  324. {
  325. return m_fileList;
  326. }
  327. inline VEditArea *VMainWindow::getEditArea() const
  328. {
  329. return m_editArea;
  330. }
  331. inline VCaptain *VMainWindow::getCaptain() const
  332. {
  333. return m_captain;
  334. }
  335. inline VFile *VMainWindow::getCurrentFile() const
  336. {
  337. return m_curFile;
  338. }
  339. inline VEditTab *VMainWindow::getCurrentTab() const
  340. {
  341. return m_curTab;
  342. }
  343. inline VSnippetList *VMainWindow::getSnippetList() const
  344. {
  345. return m_snippetList;
  346. }
  347. inline VCart *VMainWindow::getCart() const
  348. {
  349. return m_cart;
  350. }
  351. inline VHistoryList *VMainWindow::getHistoryList() const
  352. {
  353. return m_historyList;
  354. }
  355. inline VDirectoryTree *VMainWindow::getDirectoryTree() const
  356. {
  357. return m_dirTree;
  358. }
  359. inline VNotebookSelector *VMainWindow::getNotebookSelector() const
  360. {
  361. return m_notebookSelector;
  362. }
  363. inline VExplorer *VMainWindow::getExplorer() const
  364. {
  365. return m_explorer;
  366. }
  367. #endif // VMAINWINDOW_H