vmainwindow.h 13 KB

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