vmainwindow.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. #include <QtWidgets>
  2. #include <QList>
  3. #include "vmainwindow.h"
  4. #include "vdirectorytree.h"
  5. #include "vnote.h"
  6. #include "vfilelist.h"
  7. #include "vconfigmanager.h"
  8. #include "utils/vutils.h"
  9. #include "veditarea.h"
  10. #include "voutline.h"
  11. #include "vnotebookselector.h"
  12. #include "vavatar.h"
  13. #include "dialog/vfindreplacedialog.h"
  14. #include "dialog/vsettingsdialog.h"
  15. #include "vcaptain.h"
  16. extern VConfigManager vconfig;
  17. VNote *g_vnote;
  18. VMainWindow::VMainWindow(QWidget *parent)
  19. : QMainWindow(parent), m_onePanel(false)
  20. {
  21. setWindowIcon(QIcon(":/resources/icons/vnote.ico"));
  22. vnote = new VNote(this);
  23. g_vnote = vnote;
  24. vnote->initPalette(palette());
  25. initPredefinedColorPixmaps();
  26. setupUI();
  27. initMenuBar();
  28. initToolBar();
  29. initDockWindows();
  30. initAvatar();
  31. restoreStateAndGeometry();
  32. setContextMenuPolicy(Qt::NoContextMenu);
  33. notebookSelector->update();
  34. initCaptain();
  35. }
  36. void VMainWindow::initCaptain()
  37. {
  38. // VCaptain should be visible to accpet key focus. But VCaptain
  39. // may hide other widgets.
  40. m_captain = new VCaptain(this);
  41. connect(m_captain, &VCaptain::captainModeChanged,
  42. this, &VMainWindow::handleCaptainModeChanged);
  43. }
  44. void VMainWindow::setupUI()
  45. {
  46. QWidget *directoryPanel = setupDirectoryPanel();
  47. fileList = new VFileList();
  48. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  49. editArea = new VEditArea(vnote);
  50. editArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  51. m_findReplaceDialog = editArea->getFindReplaceDialog();
  52. fileList->setEditArea(editArea);
  53. directoryTree->setEditArea(editArea);
  54. notebookSelector->setEditArea(editArea);
  55. // Main Splitter
  56. mainSplitter = new QSplitter();
  57. mainSplitter->setObjectName("MainSplitter");
  58. mainSplitter->addWidget(directoryPanel);
  59. mainSplitter->addWidget(fileList);
  60. mainSplitter->addWidget(editArea);
  61. mainSplitter->setStretchFactor(0, 0);
  62. mainSplitter->setStretchFactor(1, 0);
  63. mainSplitter->setStretchFactor(2, 1);
  64. // Signals
  65. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  66. fileList, &VFileList::setDirectory);
  67. connect(directoryTree, &VDirectoryTree::directoryUpdated,
  68. editArea, &VEditArea::handleDirectoryUpdated);
  69. connect(notebookSelector, &VNotebookSelector::notebookUpdated,
  70. editArea, &VEditArea::handleNotebookUpdated);
  71. connect(fileList, &VFileList::fileClicked,
  72. editArea, &VEditArea::openFile);
  73. connect(fileList, &VFileList::fileCreated,
  74. editArea, &VEditArea::openFile);
  75. connect(fileList, &VFileList::fileUpdated,
  76. editArea, &VEditArea::handleFileUpdated);
  77. connect(editArea, &VEditArea::curTabStatusChanged,
  78. this, &VMainWindow::handleCurTabStatusChanged);
  79. connect(m_findReplaceDialog, &VFindReplaceDialog::findTextChanged,
  80. this, &VMainWindow::handleFindDialogTextChanged);
  81. setCentralWidget(mainSplitter);
  82. // Create and show the status bar
  83. statusBar();
  84. }
  85. QWidget *VMainWindow::setupDirectoryPanel()
  86. {
  87. notebookLabel = new QLabel(tr("Notebook"));
  88. notebookLabel->setProperty("TitleLabel", true);
  89. notebookLabel->setProperty("NotebookPanel", true);
  90. directoryLabel = new QLabel(tr("Directory"));
  91. directoryLabel->setProperty("TitleLabel", true);
  92. directoryLabel->setProperty("NotebookPanel", true);
  93. notebookSelector = new VNotebookSelector(vnote);
  94. notebookSelector->setObjectName("NotebookSelector");
  95. notebookSelector->setProperty("NotebookPanel", true);
  96. notebookSelector->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
  97. directoryTree = new VDirectoryTree(vnote);
  98. directoryTree->setProperty("NotebookPanel", true);
  99. QVBoxLayout *nbLayout = new QVBoxLayout;
  100. nbLayout->addWidget(notebookLabel);
  101. nbLayout->addWidget(notebookSelector);
  102. nbLayout->addWidget(directoryLabel);
  103. nbLayout->addWidget(directoryTree);
  104. nbLayout->setContentsMargins(0, 0, 0, 0);
  105. nbLayout->setSpacing(0);
  106. QWidget *nbContainer = new QWidget();
  107. nbContainer->setObjectName("NotebookPanel");
  108. nbContainer->setLayout(nbLayout);
  109. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  110. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  111. directoryTree, &VDirectoryTree::setNotebook);
  112. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  113. this, &VMainWindow::handleCurrentNotebookChanged);
  114. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  115. this, &VMainWindow::handleCurrentDirectoryChanged);
  116. return nbContainer;
  117. }
  118. void VMainWindow::initToolBar()
  119. {
  120. initFileToolBar();
  121. initViewToolBar();
  122. }
  123. void VMainWindow::initViewToolBar()
  124. {
  125. QToolBar *viewToolBar = addToolBar(tr("View"));
  126. viewToolBar->setObjectName("ViewToolBar");
  127. viewToolBar->setMovable(false);
  128. QAction *onePanelViewAct = new QAction(QIcon(":/resources/icons/one_panel.svg"),
  129. tr("&Single Panel"), this);
  130. onePanelViewAct->setStatusTip(tr("Display only the note list panel"));
  131. connect(onePanelViewAct, &QAction::triggered,
  132. this, &VMainWindow::onePanelView);
  133. QAction *twoPanelViewAct = new QAction(QIcon(":/resources/icons/two_panels.svg"),
  134. tr("&Two Panels"), this);
  135. twoPanelViewAct->setStatusTip(tr("Display both the directory and note list panel"));
  136. connect(twoPanelViewAct, &QAction::triggered,
  137. this, &VMainWindow::twoPanelView);
  138. QMenu *panelMenu = new QMenu(this);
  139. panelMenu->addAction(onePanelViewAct);
  140. panelMenu->addAction(twoPanelViewAct);
  141. expandViewAct = new QAction(QIcon(":/resources/icons/expand.svg"),
  142. tr("Expand"), this);
  143. expandViewAct->setStatusTip(tr("Expand the edit area"));
  144. expandViewAct->setCheckable(true);
  145. expandViewAct->setShortcut(QKeySequence("Ctrl+T"));
  146. expandViewAct->setMenu(panelMenu);
  147. connect(expandViewAct, &QAction::triggered,
  148. this, &VMainWindow::expandPanelView);
  149. viewToolBar->addAction(expandViewAct);
  150. }
  151. void VMainWindow::initFileToolBar()
  152. {
  153. QToolBar *fileToolBar = addToolBar(tr("Note"));
  154. fileToolBar->setObjectName("NoteToolBar");
  155. fileToolBar->setMovable(false);
  156. newRootDirAct = new QAction(QIcon(":/resources/icons/create_rootdir_tb.svg"),
  157. tr("New &Root Directory"), this);
  158. newRootDirAct->setStatusTip(tr("Create a root directory in current notebook"));
  159. connect(newRootDirAct, &QAction::triggered,
  160. directoryTree, &VDirectoryTree::newRootDirectory);
  161. newNoteAct = new QAction(QIcon(":/resources/icons/create_note_tb.svg"),
  162. tr("New &Note"), this);
  163. newNoteAct->setStatusTip(tr("Create a note in current directory"));
  164. newNoteAct->setShortcut(QKeySequence::New);
  165. connect(newNoteAct, &QAction::triggered,
  166. fileList, &VFileList::newFile);
  167. noteInfoAct = new QAction(QIcon(":/resources/icons/note_info_tb.svg"),
  168. tr("Note &Info"), this);
  169. noteInfoAct->setStatusTip(tr("View and edit current note's information"));
  170. connect(noteInfoAct, &QAction::triggered,
  171. this, &VMainWindow::curEditFileInfo);
  172. deleteNoteAct = new QAction(QIcon(":/resources/icons/delete_note_tb.svg"),
  173. tr("&Delete Note"), this);
  174. deleteNoteAct->setStatusTip(tr("Delete current note"));
  175. connect(deleteNoteAct, &QAction::triggered,
  176. this, &VMainWindow::deleteCurNote);
  177. editNoteAct = new QAction(QIcon(":/resources/icons/edit_note.svg"),
  178. tr("&Edit"), this);
  179. editNoteAct->setStatusTip(tr("Edit current note"));
  180. editNoteAct->setShortcut(QKeySequence("Ctrl+W"));
  181. connect(editNoteAct, &QAction::triggered,
  182. editArea, &VEditArea::editFile);
  183. discardExitAct = new QAction(QIcon(":/resources/icons/discard_exit.svg"),
  184. tr("Discard Changes And Exit"), this);
  185. discardExitAct->setStatusTip(tr("Discard changes and exit edit mode"));
  186. connect(discardExitAct, &QAction::triggered,
  187. editArea, &VEditArea::readFile);
  188. QMenu *exitEditMenu = new QMenu(this);
  189. exitEditMenu->addAction(discardExitAct);
  190. saveExitAct = new QAction(QIcon(":/resources/icons/save_exit.svg"),
  191. tr("Save Changes And Exit"), this);
  192. saveExitAct->setStatusTip(tr("Save changes and exit edit mode"));
  193. saveExitAct->setMenu(exitEditMenu);
  194. saveExitAct->setShortcut(QKeySequence("Ctrl+R"));
  195. connect(saveExitAct, &QAction::triggered,
  196. editArea, &VEditArea::saveAndReadFile);
  197. saveNoteAct = new QAction(QIcon(":/resources/icons/save_note.svg"),
  198. tr("Save"), this);
  199. saveNoteAct->setStatusTip(tr("Save changes to current note"));
  200. saveNoteAct->setShortcut(QKeySequence::Save);
  201. connect(saveNoteAct, &QAction::triggered,
  202. editArea, &VEditArea::saveFile);
  203. newRootDirAct->setEnabled(false);
  204. newNoteAct->setEnabled(false);
  205. noteInfoAct->setEnabled(false);
  206. deleteNoteAct->setEnabled(false);
  207. editNoteAct->setVisible(false);
  208. saveExitAct->setVisible(false);
  209. saveNoteAct->setVisible(false);
  210. fileToolBar->addAction(newRootDirAct);
  211. fileToolBar->addAction(newNoteAct);
  212. fileToolBar->addAction(noteInfoAct);
  213. fileToolBar->addAction(deleteNoteAct);
  214. fileToolBar->addSeparator();
  215. fileToolBar->addAction(editNoteAct);
  216. fileToolBar->addAction(saveExitAct);
  217. fileToolBar->addAction(saveNoteAct);
  218. fileToolBar->addSeparator();
  219. }
  220. void VMainWindow::initMenuBar()
  221. {
  222. initFileMenu();
  223. initEditMenu();
  224. initViewMenu();
  225. initMarkdownMenu();
  226. initHelpMenu();
  227. }
  228. void VMainWindow::initHelpMenu()
  229. {
  230. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  231. QAction *aboutAct = new QAction(tr("&About VNote"), this);
  232. aboutAct->setStatusTip(tr("View information about VNote"));
  233. connect(aboutAct, &QAction::triggered,
  234. this, &VMainWindow::aboutMessage);
  235. QAction *aboutQtAct = new QAction(tr("About &Qt"), this);
  236. aboutQtAct->setStatusTip(tr("View information about Qt"));
  237. connect(aboutQtAct, &QAction::triggered,
  238. qApp, &QApplication::aboutQt);
  239. helpMenu->addAction(aboutQtAct);
  240. helpMenu->addAction(aboutAct);
  241. }
  242. void VMainWindow::initMarkdownMenu()
  243. {
  244. QMenu *markdownMenu = menuBar()->addMenu(tr("&Markdown"));
  245. QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter"));
  246. QActionGroup *converterAct = new QActionGroup(this);
  247. QAction *markedAct = new QAction(tr("Marked"), converterAct);
  248. markedAct->setStatusTip(tr("Use Marked to convert Markdown to HTML (re-open current tabs to make it work)"));
  249. markedAct->setCheckable(true);
  250. markedAct->setData(int(MarkdownConverterType::Marked));
  251. QAction *hoedownAct = new QAction(tr("Hoedown"), converterAct);
  252. hoedownAct->setStatusTip(tr("Use Hoedown to convert Markdown to HTML (re-open current tabs to make it work)"));
  253. hoedownAct->setCheckable(true);
  254. hoedownAct->setData(int(MarkdownConverterType::Hoedown));
  255. QAction *markdownitAct = new QAction(tr("Markdown-it"), converterAct);
  256. markdownitAct->setStatusTip(tr("Use Markdown-it to convert Markdown to HTML (re-open current tabs to make it work)"));
  257. markdownitAct->setCheckable(true);
  258. markdownitAct->setData(int(MarkdownConverterType::MarkdownIt));
  259. connect(converterAct, &QActionGroup::triggered,
  260. this, &VMainWindow::changeMarkdownConverter);
  261. converterMenu->addAction(hoedownAct);
  262. converterMenu->addAction(markedAct);
  263. converterMenu->addAction(markdownitAct);
  264. MarkdownConverterType converterType = vconfig.getMdConverterType();
  265. switch (converterType) {
  266. case MarkdownConverterType::Marked:
  267. markedAct->setChecked(true);
  268. break;
  269. case MarkdownConverterType::Hoedown:
  270. hoedownAct->setChecked(true);
  271. break;
  272. case MarkdownConverterType::MarkdownIt:
  273. markdownitAct->setChecked(true);
  274. break;
  275. default:
  276. Q_ASSERT(false);
  277. }
  278. initRenderBackgroundMenu(markdownMenu);
  279. QAction *mermaidAct = new QAction(tr("&Mermaid Diagram"), this);
  280. mermaidAct->setStatusTip(tr("Enable Mermaid for graph and diagram"));
  281. mermaidAct->setCheckable(true);
  282. connect(mermaidAct, &QAction::triggered,
  283. this, &VMainWindow::enableMermaid);
  284. markdownMenu->addAction(mermaidAct);
  285. mermaidAct->setChecked(vconfig.getEnableMermaid());
  286. QAction *mathjaxAct = new QAction(tr("Math&jax"), this);
  287. mathjaxAct->setStatusTip(tr("Enable Mathjax for math support in Markdown"));
  288. mathjaxAct->setCheckable(true);
  289. connect(mathjaxAct, &QAction::triggered,
  290. this, &VMainWindow::enableMathjax);
  291. markdownMenu->addAction(mathjaxAct);
  292. mathjaxAct->setChecked(vconfig.getEnableMathjax());
  293. }
  294. void VMainWindow::initViewMenu()
  295. {
  296. viewMenu = menuBar()->addMenu(tr("&View"));
  297. }
  298. void VMainWindow::initFileMenu()
  299. {
  300. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  301. // Import notes from files.
  302. m_importNoteAct = new QAction(QIcon(":/resources/icons/import_note.svg"),
  303. tr("&Import Notes From Files"), this);
  304. m_importNoteAct->setStatusTip(tr("Import notes from files into current directory"));
  305. connect(m_importNoteAct, &QAction::triggered,
  306. this, &VMainWindow::importNoteFromFile);
  307. m_importNoteAct->setEnabled(false);
  308. // Settings.
  309. QAction *settingsAct = new QAction(QIcon(":/resources/icons/settings.svg"),
  310. tr("Settings"), this);
  311. settingsAct->setStatusTip(tr("View and change settings for VNote"));
  312. connect(settingsAct, &QAction::triggered,
  313. this, &VMainWindow::viewSettings);
  314. // Exit.
  315. QAction *exitAct = new QAction(tr("Exit"), this);
  316. exitAct->setStatusTip(tr("Exit VNote"));
  317. exitAct->setShortcut(QKeySequence("Ctrl+Q"));
  318. connect(exitAct, &QAction::triggered,
  319. this, &VMainWindow::close);
  320. fileMenu->addAction(m_importNoteAct);
  321. fileMenu->addSeparator();
  322. fileMenu->addAction(settingsAct);
  323. fileMenu->addSeparator();
  324. fileMenu->addAction(exitAct);
  325. }
  326. void VMainWindow::initEditMenu()
  327. {
  328. QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
  329. // Insert image.
  330. m_insertImageAct = new QAction(QIcon(":/resources/icons/insert_image.svg"),
  331. tr("Insert &Image"), this);
  332. m_insertImageAct->setStatusTip(tr("Insert an image from file into current note"));
  333. connect(m_insertImageAct, &QAction::triggered,
  334. this, &VMainWindow::insertImage);
  335. // Find/Replace.
  336. m_findReplaceAct = new QAction(QIcon(":/resources/icons/find_replace.svg"),
  337. tr("Find/Replace"), this);
  338. m_findReplaceAct->setStatusTip(tr("Open Find/Replace dialog to search in current note"));
  339. m_findReplaceAct->setShortcut(QKeySequence::Find);
  340. connect(m_findReplaceAct, &QAction::triggered,
  341. this, &VMainWindow::openFindDialog);
  342. m_findNextAct = new QAction(tr("Find Next"), this);
  343. m_findNextAct->setStatusTip(tr("Find next occurence"));
  344. m_findNextAct->setShortcut(QKeySequence::FindNext);
  345. connect(m_findNextAct, SIGNAL(triggered(bool)),
  346. m_findReplaceDialog, SLOT(findNext()));
  347. m_findPreviousAct = new QAction(tr("Find Previous"), this);
  348. m_findPreviousAct->setStatusTip(tr("Find previous occurence"));
  349. m_findPreviousAct->setShortcut(QKeySequence::FindPrevious);
  350. connect(m_findPreviousAct, SIGNAL(triggered(bool)),
  351. m_findReplaceDialog, SLOT(findPrevious()));
  352. m_replaceAct = new QAction(tr("Replace"), this);
  353. m_replaceAct->setStatusTip(tr("Replace current occurence"));
  354. m_replaceAct->setShortcut(QKeySequence::Replace);
  355. connect(m_replaceAct, SIGNAL(triggered(bool)),
  356. m_findReplaceDialog, SLOT(replace()));
  357. m_replaceFindAct = new QAction(tr("Replace && Find"), this);
  358. m_replaceFindAct->setStatusTip(tr("Replace current occurence and find the next one"));
  359. connect(m_replaceFindAct, SIGNAL(triggered(bool)),
  360. m_findReplaceDialog, SLOT(replaceFind()));
  361. m_replaceAllAct = new QAction(tr("Replace All"), this);
  362. m_replaceAllAct->setStatusTip(tr("Replace all occurences in current note"));
  363. connect(m_replaceAllAct, SIGNAL(triggered(bool)),
  364. m_findReplaceDialog, SLOT(replaceAll()));
  365. QAction *searchedWordAct = new QAction(tr("Highlight Searched Pattern"), this);
  366. searchedWordAct->setStatusTip(tr("Highlight all occurences of searched pattern"));
  367. searchedWordAct->setCheckable(true);
  368. connect(searchedWordAct, &QAction::triggered,
  369. this, &VMainWindow::changeHighlightSearchedWord);
  370. // Expand Tab into spaces.
  371. QAction *expandTabAct = new QAction(tr("&Expand Tab"), this);
  372. expandTabAct->setStatusTip(tr("Expand entered Tab to spaces"));
  373. expandTabAct->setCheckable(true);
  374. connect(expandTabAct, &QAction::triggered,
  375. this, &VMainWindow::changeExpandTab);
  376. // Tab stop width.
  377. QActionGroup *tabStopWidthAct = new QActionGroup(this);
  378. QAction *twoSpaceTabAct = new QAction(tr("2 Spaces"), tabStopWidthAct);
  379. twoSpaceTabAct->setStatusTip(tr("Expand Tab to 2 spaces"));
  380. twoSpaceTabAct->setCheckable(true);
  381. twoSpaceTabAct->setData(2);
  382. QAction *fourSpaceTabAct = new QAction(tr("4 Spaces"), tabStopWidthAct);
  383. fourSpaceTabAct->setStatusTip(tr("Expand Tab to 4 spaces"));
  384. fourSpaceTabAct->setCheckable(true);
  385. fourSpaceTabAct->setData(4);
  386. QAction *eightSpaceTabAct = new QAction(tr("8 Spaces"), tabStopWidthAct);
  387. eightSpaceTabAct->setStatusTip(tr("Expand Tab to 8 spaces"));
  388. eightSpaceTabAct->setCheckable(true);
  389. eightSpaceTabAct->setData(8);
  390. connect(tabStopWidthAct, &QActionGroup::triggered,
  391. this, &VMainWindow::setTabStopWidth);
  392. // Highlight current cursor line.
  393. QAction *cursorLineAct = new QAction(tr("Highlight Cursor Line"), this);
  394. cursorLineAct->setStatusTip(tr("Highlight current cursor line"));
  395. cursorLineAct->setCheckable(true);
  396. connect(cursorLineAct, &QAction::triggered,
  397. this, &VMainWindow::changeHighlightCursorLine);
  398. // Highlight selected word.
  399. QAction *selectedWordAct = new QAction(tr("Highlight Selected Words"), this);
  400. selectedWordAct->setStatusTip(tr("Highlight all occurences of selected words"));
  401. selectedWordAct->setCheckable(true);
  402. connect(selectedWordAct, &QAction::triggered,
  403. this, &VMainWindow::changeHighlightSelectedWord);
  404. editMenu->addAction(m_insertImageAct);
  405. editMenu->addSeparator();
  406. m_insertImageAct->setEnabled(false);
  407. QMenu *findReplaceMenu = editMenu->addMenu(tr("Find/Replace"));
  408. findReplaceMenu->addAction(m_findReplaceAct);
  409. findReplaceMenu->addAction(m_findNextAct);
  410. findReplaceMenu->addAction(m_findPreviousAct);
  411. findReplaceMenu->addAction(m_replaceAct);
  412. findReplaceMenu->addAction(m_replaceFindAct);
  413. findReplaceMenu->addAction(m_replaceAllAct);
  414. findReplaceMenu->addSeparator();
  415. findReplaceMenu->addAction(searchedWordAct);
  416. if (vconfig.getHighlightSearchedWord()) {
  417. searchedWordAct->setChecked(true);
  418. } else {
  419. searchedWordAct->setChecked(false);
  420. }
  421. editMenu->addSeparator();
  422. m_findReplaceAct->setEnabled(false);
  423. m_findNextAct->setEnabled(false);
  424. m_findPreviousAct->setEnabled(false);
  425. m_replaceAct->setEnabled(false);
  426. m_replaceFindAct->setEnabled(false);
  427. m_replaceAllAct->setEnabled(false);
  428. editMenu->addAction(expandTabAct);
  429. if (vconfig.getIsExpandTab()) {
  430. expandTabAct->setChecked(true);
  431. } else {
  432. expandTabAct->setChecked(false);
  433. }
  434. QMenu *tabStopWidthMenu = editMenu->addMenu(tr("Tab Stop Width"));
  435. tabStopWidthMenu->addAction(twoSpaceTabAct);
  436. tabStopWidthMenu->addAction(fourSpaceTabAct);
  437. tabStopWidthMenu->addAction(eightSpaceTabAct);
  438. int tabStopWidth = vconfig.getTabStopWidth();
  439. switch (tabStopWidth) {
  440. case 2:
  441. twoSpaceTabAct->setChecked(true);
  442. break;
  443. case 4:
  444. fourSpaceTabAct->setChecked(true);
  445. break;
  446. case 8:
  447. eightSpaceTabAct->setChecked(true);
  448. break;
  449. default:
  450. qWarning() << "unsupported tab stop width" << tabStopWidth << "in config";
  451. }
  452. initEditorBackgroundMenu(editMenu);
  453. editMenu->addSeparator();
  454. editMenu->addAction(cursorLineAct);
  455. if (vconfig.getHighlightCursorLine()) {
  456. cursorLineAct->setChecked(true);
  457. } else {
  458. cursorLineAct->setChecked(false);
  459. }
  460. editMenu->addAction(selectedWordAct);
  461. if (vconfig.getHighlightSelectedWord()) {
  462. selectedWordAct->setChecked(true);
  463. } else {
  464. selectedWordAct->setChecked(false);
  465. }
  466. }
  467. void VMainWindow::initDockWindows()
  468. {
  469. toolDock = new QDockWidget(tr("Tools"), this);
  470. toolDock->setObjectName("tools_dock");
  471. toolDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
  472. toolBox = new QToolBox(this);
  473. outline = new VOutline(this);
  474. connect(editArea, &VEditArea::outlineChanged,
  475. outline, &VOutline::updateOutline);
  476. connect(outline, &VOutline::outlineItemActivated,
  477. editArea, &VEditArea::handleOutlineItemActivated);
  478. connect(editArea, &VEditArea::curHeaderChanged,
  479. outline, &VOutline::updateCurHeader);
  480. toolBox->addItem(outline, QIcon(":/resources/icons/outline.svg"), tr("Outline"));
  481. toolDock->setWidget(toolBox);
  482. addDockWidget(Qt::RightDockWidgetArea, toolDock);
  483. viewMenu->addAction(toolDock->toggleViewAction());
  484. }
  485. void VMainWindow::initAvatar()
  486. {
  487. m_avatar = new VAvatar(this);
  488. m_avatar->setAvatarPixmap(":/resources/icons/vnote.svg");
  489. m_avatar->setColor(vnote->getColorFromPalette("base-color"), vnote->getColorFromPalette("Indigo4"),
  490. vnote->getColorFromPalette("Teal4"));
  491. m_avatar->hide();
  492. }
  493. void VMainWindow::importNoteFromFile()
  494. {
  495. static QString lastPath = QDir::homePath();
  496. QStringList files = QFileDialog::getOpenFileNames(this,
  497. tr("Select Files(HTML or Markdown) To Import"),
  498. lastPath);
  499. if (files.isEmpty()) {
  500. return;
  501. }
  502. // Update lastPath
  503. lastPath = QFileInfo(files[0]).path();
  504. int failedFiles = 0;
  505. for (int i = 0; i < files.size(); ++i) {
  506. bool ret = fileList->importFile(files[i]);
  507. if (!ret) {
  508. ++failedFiles;
  509. }
  510. }
  511. QMessageBox msgBox(QMessageBox::Information, tr("Import Notes From File"),
  512. tr("Imported notes: %1 succeed, %2 failed.")
  513. .arg(files.size() - failedFiles).arg(failedFiles),
  514. QMessageBox::Ok, this);
  515. if (failedFiles > 0) {
  516. msgBox.setInformativeText(tr("Fail to import files maybe due to name conflicts."));
  517. }
  518. msgBox.exec();
  519. }
  520. void VMainWindow::changeMarkdownConverter(QAction *action)
  521. {
  522. if (!action) {
  523. return;
  524. }
  525. MarkdownConverterType type = (MarkdownConverterType)action->data().toInt();
  526. qDebug() << "switch to converter" << type;
  527. vconfig.setMarkdownConverterType(type);
  528. }
  529. void VMainWindow::aboutMessage()
  530. {
  531. QString info = tr("v%1").arg(VConfigManager::c_version);
  532. info += "\n\n";
  533. info += tr("VNote is a Vim-inspired note-taking application for Markdown.");
  534. info += "\n";
  535. info += tr("Visit https://github.com/tamlok/vnote.git for more information.");
  536. QMessageBox::about(this, tr("About VNote"), info);
  537. }
  538. void VMainWindow::changeExpandTab(bool checked)
  539. {
  540. vconfig.setIsExpandTab(checked);
  541. }
  542. void VMainWindow::enableMermaid(bool p_checked)
  543. {
  544. vconfig.setEnableMermaid(p_checked);
  545. }
  546. void VMainWindow::enableMathjax(bool p_checked)
  547. {
  548. vconfig.setEnableMathjax(p_checked);
  549. }
  550. void VMainWindow::changeHighlightCursorLine(bool p_checked)
  551. {
  552. vconfig.setHighlightCursorLine(p_checked);
  553. }
  554. void VMainWindow::changeHighlightSelectedWord(bool p_checked)
  555. {
  556. vconfig.setHighlightSelectedWord(p_checked);
  557. }
  558. void VMainWindow::changeHighlightSearchedWord(bool p_checked)
  559. {
  560. vconfig.setHighlightSearchedWord(p_checked);
  561. }
  562. void VMainWindow::setTabStopWidth(QAction *action)
  563. {
  564. if (!action) {
  565. return;
  566. }
  567. vconfig.setTabStopWidth(action->data().toInt());
  568. }
  569. void VMainWindow::setEditorBackgroundColor(QAction *action)
  570. {
  571. if (!action) {
  572. return;
  573. }
  574. vconfig.setCurBackgroundColor(action->data().toString());
  575. }
  576. void VMainWindow::initPredefinedColorPixmaps()
  577. {
  578. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  579. predefinedColorPixmaps.clear();
  580. int size = 256;
  581. for (int i = 0; i < bgColors.size(); ++i) {
  582. // Generate QPixmap filled in this color
  583. QColor color(VUtils::QRgbFromString(bgColors[i].rgb));
  584. QPixmap pixmap(size, size);
  585. pixmap.fill(color);
  586. predefinedColorPixmaps.append(pixmap);
  587. }
  588. }
  589. void VMainWindow::initRenderBackgroundMenu(QMenu *menu)
  590. {
  591. QActionGroup *renderBackgroundAct = new QActionGroup(this);
  592. connect(renderBackgroundAct, &QActionGroup::triggered,
  593. this, &VMainWindow::setRenderBackgroundColor);
  594. QMenu *renderBgMenu = menu->addMenu(tr("&Rendering Background"));
  595. const QString &curBgColor = vconfig.getCurRenderBackgroundColor();
  596. QAction *tmpAct = new QAction(tr("System"), renderBackgroundAct);
  597. tmpAct->setStatusTip(tr("Use system's background color configuration for Markdown rendering"));
  598. tmpAct->setCheckable(true);
  599. tmpAct->setData("System");
  600. if (curBgColor == "System") {
  601. tmpAct->setChecked(true);
  602. }
  603. renderBgMenu->addAction(tmpAct);
  604. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  605. for (int i = 0; i < bgColors.size(); ++i) {
  606. tmpAct = new QAction(bgColors[i].name, renderBackgroundAct);
  607. tmpAct->setStatusTip(tr("Set as the background color for Markdown rendering"));
  608. tmpAct->setCheckable(true);
  609. tmpAct->setData(bgColors[i].name);
  610. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  611. if (curBgColor == bgColors[i].name) {
  612. tmpAct->setChecked(true);
  613. }
  614. renderBgMenu->addAction(tmpAct);
  615. }
  616. }
  617. void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
  618. {
  619. QMenu *backgroundColorMenu = menu->addMenu(tr("&Background Color"));
  620. QActionGroup *backgroundColorAct = new QActionGroup(this);
  621. connect(backgroundColorAct, &QActionGroup::triggered,
  622. this, &VMainWindow::setEditorBackgroundColor);
  623. // System background color
  624. const QString &curBgColor = vconfig.getCurBackgroundColor();
  625. QAction *tmpAct = new QAction(tr("System"), backgroundColorAct);
  626. tmpAct->setStatusTip(tr("Use system's background color configuration for editor"));
  627. tmpAct->setCheckable(true);
  628. tmpAct->setData("System");
  629. if (curBgColor == "System") {
  630. tmpAct->setChecked(true);
  631. }
  632. backgroundColorMenu->addAction(tmpAct);
  633. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  634. for (int i = 0; i < bgColors.size(); ++i) {
  635. tmpAct = new QAction(bgColors[i].name, backgroundColorAct);
  636. tmpAct->setStatusTip(tr("Set as the background color for editor"));
  637. tmpAct->setCheckable(true);
  638. tmpAct->setData(bgColors[i].name);
  639. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  640. if (curBgColor == bgColors[i].name) {
  641. tmpAct->setChecked(true);
  642. }
  643. backgroundColorMenu->addAction(tmpAct);
  644. }
  645. }
  646. void VMainWindow::setRenderBackgroundColor(QAction *action)
  647. {
  648. if (!action) {
  649. return;
  650. }
  651. vconfig.setCurRenderBackgroundColor(action->data().toString());
  652. vnote->updateTemplate();
  653. }
  654. void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file,
  655. bool p_editMode)
  656. {
  657. if (p_file) {
  658. if (p_editMode) {
  659. editNoteAct->setVisible(false);
  660. saveExitAct->setVisible(true);
  661. saveNoteAct->setVisible(true);
  662. deleteNoteAct->setEnabled(true);
  663. m_insertImageAct->setEnabled(true);
  664. } else {
  665. editNoteAct->setVisible(true);
  666. saveExitAct->setVisible(false);
  667. saveNoteAct->setVisible(false);
  668. deleteNoteAct->setEnabled(true);
  669. m_insertImageAct->setEnabled(false);
  670. m_replaceAct->setEnabled(false);
  671. m_replaceFindAct->setEnabled(false);
  672. m_replaceAllAct->setEnabled(false);
  673. }
  674. noteInfoAct->setEnabled(true);
  675. m_findReplaceAct->setEnabled(true);
  676. } else {
  677. editNoteAct->setVisible(false);
  678. saveExitAct->setVisible(false);
  679. saveNoteAct->setVisible(false);
  680. deleteNoteAct->setEnabled(false);
  681. noteInfoAct->setEnabled(false);
  682. m_insertImageAct->setEnabled(false);
  683. // Find/Replace
  684. m_findReplaceAct->setEnabled(false);
  685. m_findNextAct->setEnabled(false);
  686. m_findPreviousAct->setEnabled(false);
  687. m_replaceAct->setEnabled(false);
  688. m_replaceFindAct->setEnabled(false);
  689. m_replaceAllAct->setEnabled(false);
  690. m_findReplaceDialog->closeDialog();
  691. }
  692. }
  693. void VMainWindow::handleCurTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode)
  694. {
  695. updateActionStateFromTabStatusChange(p_file, p_editMode);
  696. if (p_file) {
  697. m_findReplaceDialog->updateState(p_file->getDocType(), p_editMode);
  698. }
  699. QString title;
  700. if (p_file) {
  701. title = QString("[%1] %2").arg(p_file->getNotebookName()).arg(p_file->retrivePath());
  702. if (p_file->isModified()) {
  703. title.append('*');
  704. }
  705. }
  706. updateWindowTitle(title);
  707. m_curFile = const_cast<VFile *>(p_file);
  708. m_curTab = const_cast<VEditTab *>(p_editTab);
  709. }
  710. void VMainWindow::onePanelView()
  711. {
  712. changeSplitterView(1);
  713. expandViewAct->setChecked(false);
  714. m_onePanel = true;
  715. }
  716. void VMainWindow::twoPanelView()
  717. {
  718. changeSplitterView(2);
  719. expandViewAct->setChecked(false);
  720. m_onePanel = false;
  721. }
  722. void VMainWindow::toggleOnePanelView()
  723. {
  724. if (m_onePanel) {
  725. twoPanelView();
  726. } else {
  727. onePanelView();
  728. }
  729. }
  730. void VMainWindow::expandPanelView(bool p_checked)
  731. {
  732. int nrSplits = 0;
  733. if (p_checked) {
  734. nrSplits = 0;
  735. } else {
  736. if (m_onePanel) {
  737. nrSplits = 1;
  738. } else {
  739. nrSplits = 2;
  740. }
  741. }
  742. changeSplitterView(nrSplits);
  743. }
  744. void VMainWindow::changeSplitterView(int nrPanel)
  745. {
  746. switch (nrPanel) {
  747. case 0:
  748. // Expand
  749. mainSplitter->widget(0)->hide();
  750. mainSplitter->widget(1)->hide();
  751. mainSplitter->widget(2)->show();
  752. break;
  753. case 1:
  754. // Single panel
  755. mainSplitter->widget(0)->hide();
  756. mainSplitter->widget(1)->show();
  757. mainSplitter->widget(2)->show();
  758. break;
  759. case 2:
  760. // Two panels
  761. mainSplitter->widget(0)->show();
  762. mainSplitter->widget(1)->show();
  763. mainSplitter->widget(2)->show();
  764. break;
  765. default:
  766. qWarning() << "invalid panel number" << nrPanel;
  767. }
  768. }
  769. void VMainWindow::updateWindowTitle(const QString &str)
  770. {
  771. QString title = "VNote";
  772. if (!str.isEmpty()) {
  773. title = title + " - " + str;
  774. }
  775. setWindowTitle(title);
  776. }
  777. void VMainWindow::curEditFileInfo()
  778. {
  779. Q_ASSERT(m_curFile);
  780. fileList->fileInfo(m_curFile);
  781. }
  782. void VMainWindow::deleteCurNote()
  783. {
  784. Q_ASSERT(m_curFile);
  785. fileList->deleteFile(m_curFile);
  786. }
  787. void VMainWindow::closeEvent(QCloseEvent *event)
  788. {
  789. if (!editArea->closeAllFiles(false)) {
  790. // Fail to close all the opened files, cancel closing app
  791. event->ignore();
  792. return;
  793. }
  794. saveStateAndGeometry();
  795. QMainWindow::closeEvent(event);
  796. }
  797. void VMainWindow::saveStateAndGeometry()
  798. {
  799. vconfig.setMainWindowGeometry(saveGeometry());
  800. vconfig.setMainWindowState(saveState());
  801. vconfig.setToolsDockChecked(toolDock->isVisible());
  802. vconfig.setMainSplitterState(mainSplitter->saveState());
  803. }
  804. void VMainWindow::restoreStateAndGeometry()
  805. {
  806. const QByteArray &geometry = vconfig.getMainWindowGeometry();
  807. if (!geometry.isEmpty()) {
  808. restoreGeometry(geometry);
  809. }
  810. const QByteArray &state = vconfig.getMainWindowState();
  811. if (!state.isEmpty()) {
  812. restoreState(state);
  813. }
  814. toolDock->setVisible(vconfig.getToolsDockChecked());
  815. const QByteArray &splitterState = vconfig.getMainSplitterState();
  816. if (!splitterState.isEmpty()) {
  817. mainSplitter->restoreState(splitterState);
  818. }
  819. }
  820. const QVector<QPair<QString, QString> >& VMainWindow::getPalette() const
  821. {
  822. return vnote->getPalette();
  823. }
  824. void VMainWindow::handleCurrentDirectoryChanged(const VDirectory *p_dir)
  825. {
  826. newNoteAct->setEnabled(p_dir);
  827. m_importNoteAct->setEnabled(p_dir);
  828. }
  829. void VMainWindow::handleCurrentNotebookChanged(const VNotebook *p_notebook)
  830. {
  831. newRootDirAct->setEnabled(p_notebook);
  832. }
  833. void VMainWindow::resizeEvent(QResizeEvent *event)
  834. {
  835. repositionAvatar();
  836. QMainWindow::resizeEvent(event);
  837. }
  838. void VMainWindow::keyPressEvent(QKeyEvent *event)
  839. {
  840. int key = event->key();
  841. Qt::KeyboardModifiers modifiers = event->modifiers();
  842. if (key == Qt::Key_Escape
  843. || (key == Qt::Key_BracketLeft
  844. && modifiers == Qt::ControlModifier)) {
  845. m_findReplaceDialog->closeDialog();
  846. event->accept();
  847. return;
  848. }
  849. QMainWindow::keyPressEvent(event);
  850. }
  851. void VMainWindow::repositionAvatar()
  852. {
  853. int diameter = mainSplitter->pos().y();
  854. int x = width() - diameter - 5;
  855. int y = 0;
  856. qDebug() << "avatar:" << diameter << x << y;
  857. m_avatar->setDiameter(diameter);
  858. m_avatar->move(x, y);
  859. m_avatar->show();
  860. }
  861. void VMainWindow::insertImage()
  862. {
  863. if (!m_curTab) {
  864. return;
  865. }
  866. Q_ASSERT(m_curTab == editArea->currentEditTab());
  867. m_curTab->insertImage();
  868. }
  869. void VMainWindow::locateFile(VFile *p_file)
  870. {
  871. if (!p_file) {
  872. return;
  873. }
  874. qDebug() << "locate file" << p_file->retrivePath();
  875. VNotebook *notebook = p_file->getNotebook();
  876. if (notebookSelector->locateNotebook(notebook)) {
  877. while (directoryTree->currentNotebook() != notebook) {
  878. QCoreApplication::sendPostedEvents();
  879. }
  880. VDirectory *dir = p_file->getDirectory();
  881. if (directoryTree->locateDirectory(dir)) {
  882. while (fileList->currentDirectory() != dir) {
  883. QCoreApplication::sendPostedEvents();
  884. }
  885. fileList->locateFile(p_file);
  886. }
  887. }
  888. // Open the directory and file panels after location.
  889. twoPanelView();
  890. }
  891. void VMainWindow::locateCurrentFile()
  892. {
  893. if (m_curFile) {
  894. locateFile(m_curFile);
  895. }
  896. }
  897. void VMainWindow::handleFindDialogTextChanged(const QString &p_text, uint /* p_options */)
  898. {
  899. bool enabled = true;
  900. if (p_text.isEmpty()) {
  901. enabled = false;
  902. }
  903. m_findNextAct->setEnabled(enabled);
  904. m_findPreviousAct->setEnabled(enabled);
  905. m_replaceAct->setEnabled(enabled);
  906. m_replaceFindAct->setEnabled(enabled);
  907. m_replaceAllAct->setEnabled(enabled);
  908. }
  909. void VMainWindow::openFindDialog()
  910. {
  911. m_findReplaceDialog->openDialog(editArea->getSelectedText());
  912. }
  913. void VMainWindow::viewSettings()
  914. {
  915. VSettingsDialog settingsDialog(this);
  916. settingsDialog.exec();
  917. }
  918. void VMainWindow::handleCaptainModeChanged(bool p_enabled)
  919. {
  920. static QString normalBaseColor = m_avatar->getBaseColor();
  921. static QString captainModeColor = vnote->getColorFromPalette("Purple5");
  922. if (p_enabled) {
  923. m_avatar->updateBaseColor(captainModeColor);
  924. } else {
  925. m_avatar->updateBaseColor(normalBaseColor);
  926. }
  927. }
  928. void VMainWindow::closeCurrentFile()
  929. {
  930. if (m_curFile) {
  931. editArea->closeFile(m_curFile, false);
  932. }
  933. }