vmainwindow.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. #include <QtWidgets>
  2. #include <QList>
  3. #include <QPrinter>
  4. #include <QPrintDialog>
  5. #include <QPainter>
  6. #include "vmainwindow.h"
  7. #include "vdirectorytree.h"
  8. #include "vnote.h"
  9. #include "vfilelist.h"
  10. #include "vconfigmanager.h"
  11. #include "utils/vutils.h"
  12. #include "veditarea.h"
  13. #include "voutline.h"
  14. #include "vnotebookselector.h"
  15. #include "vavatar.h"
  16. #include "dialog/vfindreplacedialog.h"
  17. #include "dialog/vsettingsdialog.h"
  18. #include "vcaptain.h"
  19. #include "vedittab.h"
  20. #include "vwebview.h"
  21. #include "vexporter.h"
  22. #include "vmdtab.h"
  23. extern VConfigManager vconfig;
  24. VNote *g_vnote;
  25. #if defined(QT_NO_DEBUG)
  26. extern QFile g_logFile;
  27. #endif
  28. VMainWindow::VMainWindow(QWidget *parent)
  29. : QMainWindow(parent), m_onePanel(false)
  30. {
  31. setWindowIcon(QIcon(":/resources/icons/vnote.ico"));
  32. vnote = new VNote(this);
  33. g_vnote = vnote;
  34. vnote->initPalette(palette());
  35. initPredefinedColorPixmaps();
  36. setupUI();
  37. initMenuBar();
  38. initToolBar();
  39. initDockWindows();
  40. initAvatar();
  41. restoreStateAndGeometry();
  42. setContextMenuPolicy(Qt::NoContextMenu);
  43. notebookSelector->update();
  44. initCaptain();
  45. }
  46. void VMainWindow::initCaptain()
  47. {
  48. // VCaptain should be visible to accpet key focus. But VCaptain
  49. // may hide other widgets.
  50. m_captain = new VCaptain(this);
  51. connect(m_captain, &VCaptain::captainModeChanged,
  52. this, &VMainWindow::handleCaptainModeChanged);
  53. m_captain->registerNavigationTarget(notebookSelector);
  54. m_captain->registerNavigationTarget(directoryTree);
  55. m_captain->registerNavigationTarget(fileList);
  56. m_captain->registerNavigationTarget(editArea);
  57. m_captain->registerNavigationTarget(outline);
  58. }
  59. void VMainWindow::setupUI()
  60. {
  61. QWidget *directoryPanel = setupDirectoryPanel();
  62. fileList = new VFileList();
  63. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  64. editArea = new VEditArea(vnote);
  65. editArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  66. m_findReplaceDialog = editArea->getFindReplaceDialog();
  67. fileList->setEditArea(editArea);
  68. directoryTree->setEditArea(editArea);
  69. notebookSelector->setEditArea(editArea);
  70. // Main Splitter
  71. mainSplitter = new QSplitter();
  72. mainSplitter->setObjectName("MainSplitter");
  73. mainSplitter->addWidget(directoryPanel);
  74. mainSplitter->addWidget(fileList);
  75. mainSplitter->addWidget(editArea);
  76. mainSplitter->setStretchFactor(0, 0);
  77. mainSplitter->setStretchFactor(1, 0);
  78. mainSplitter->setStretchFactor(2, 1);
  79. // Signals
  80. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  81. fileList, &VFileList::setDirectory);
  82. connect(directoryTree, &VDirectoryTree::directoryUpdated,
  83. editArea, &VEditArea::handleDirectoryUpdated);
  84. connect(notebookSelector, &VNotebookSelector::notebookUpdated,
  85. editArea, &VEditArea::handleNotebookUpdated);
  86. connect(fileList, &VFileList::fileClicked,
  87. editArea, &VEditArea::openFile);
  88. connect(fileList, &VFileList::fileCreated,
  89. editArea, &VEditArea::openFile);
  90. connect(fileList, &VFileList::fileUpdated,
  91. editArea, &VEditArea::handleFileUpdated);
  92. connect(editArea, &VEditArea::curTabStatusChanged,
  93. this, &VMainWindow::handleCurTabStatusChanged);
  94. connect(editArea, &VEditArea::statusMessage,
  95. this, &VMainWindow::showStatusMessage);
  96. connect(m_findReplaceDialog, &VFindReplaceDialog::findTextChanged,
  97. this, &VMainWindow::handleFindDialogTextChanged);
  98. setCentralWidget(mainSplitter);
  99. // Create and show the status bar
  100. statusBar();
  101. }
  102. QWidget *VMainWindow::setupDirectoryPanel()
  103. {
  104. notebookLabel = new QLabel(tr("Notebooks"));
  105. notebookLabel->setProperty("TitleLabel", true);
  106. notebookLabel->setProperty("NotebookPanel", true);
  107. directoryLabel = new QLabel(tr("Folders"));
  108. directoryLabel->setProperty("TitleLabel", true);
  109. directoryLabel->setProperty("NotebookPanel", true);
  110. notebookSelector = new VNotebookSelector(vnote);
  111. notebookSelector->setObjectName("NotebookSelector");
  112. notebookSelector->setProperty("NotebookPanel", true);
  113. notebookSelector->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
  114. directoryTree = new VDirectoryTree(vnote);
  115. directoryTree->setProperty("NotebookPanel", true);
  116. QVBoxLayout *nbLayout = new QVBoxLayout;
  117. nbLayout->addWidget(notebookLabel);
  118. nbLayout->addWidget(notebookSelector);
  119. nbLayout->addWidget(directoryLabel);
  120. nbLayout->addWidget(directoryTree);
  121. nbLayout->setContentsMargins(0, 0, 0, 0);
  122. nbLayout->setSpacing(0);
  123. QWidget *nbContainer = new QWidget();
  124. nbContainer->setObjectName("NotebookPanel");
  125. nbContainer->setLayout(nbLayout);
  126. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  127. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  128. directoryTree, &VDirectoryTree::setNotebook);
  129. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  130. this, &VMainWindow::handleCurrentNotebookChanged);
  131. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  132. this, &VMainWindow::handleCurrentDirectoryChanged);
  133. return nbContainer;
  134. }
  135. void VMainWindow::initToolBar()
  136. {
  137. initFileToolBar();
  138. initViewToolBar();
  139. }
  140. void VMainWindow::initViewToolBar()
  141. {
  142. QToolBar *viewToolBar = addToolBar(tr("View"));
  143. viewToolBar->setObjectName("ViewToolBar");
  144. viewToolBar->setMovable(false);
  145. QAction *onePanelViewAct = new QAction(QIcon(":/resources/icons/one_panel.svg"),
  146. tr("&Single Panel"), this);
  147. onePanelViewAct->setStatusTip(tr("Display only the notes list panel"));
  148. connect(onePanelViewAct, &QAction::triggered,
  149. this, &VMainWindow::onePanelView);
  150. QAction *twoPanelViewAct = new QAction(QIcon(":/resources/icons/two_panels.svg"),
  151. tr("&Two Panels"), this);
  152. twoPanelViewAct->setStatusTip(tr("Display both the folders and notes list panel"));
  153. connect(twoPanelViewAct, &QAction::triggered,
  154. this, &VMainWindow::twoPanelView);
  155. QMenu *panelMenu = new QMenu(this);
  156. panelMenu->addAction(onePanelViewAct);
  157. panelMenu->addAction(twoPanelViewAct);
  158. expandViewAct = new QAction(QIcon(":/resources/icons/expand.svg"),
  159. tr("Expand"), this);
  160. expandViewAct->setStatusTip(tr("Expand the edit area"));
  161. expandViewAct->setCheckable(true);
  162. expandViewAct->setShortcut(QKeySequence("Ctrl+T"));
  163. expandViewAct->setMenu(panelMenu);
  164. connect(expandViewAct, &QAction::triggered,
  165. this, &VMainWindow::expandPanelView);
  166. viewToolBar->addAction(expandViewAct);
  167. }
  168. void VMainWindow::initFileToolBar()
  169. {
  170. QToolBar *fileToolBar = addToolBar(tr("Note"));
  171. fileToolBar->setObjectName("NoteToolBar");
  172. fileToolBar->setMovable(false);
  173. newRootDirAct = new QAction(QIcon(":/resources/icons/create_rootdir_tb.svg"),
  174. tr("New &Root Folder"), this);
  175. newRootDirAct->setStatusTip(tr("Create a root folder in current notebook"));
  176. connect(newRootDirAct, &QAction::triggered,
  177. directoryTree, &VDirectoryTree::newRootDirectory);
  178. newNoteAct = new QAction(QIcon(":/resources/icons/create_note_tb.svg"),
  179. tr("New &Note"), this);
  180. newNoteAct->setStatusTip(tr("Create a note in current folder"));
  181. newNoteAct->setShortcut(QKeySequence::New);
  182. connect(newNoteAct, &QAction::triggered,
  183. fileList, &VFileList::newFile);
  184. noteInfoAct = new QAction(QIcon(":/resources/icons/note_info_tb.svg"),
  185. tr("Note &Info"), this);
  186. noteInfoAct->setStatusTip(tr("View and edit current note's information"));
  187. connect(noteInfoAct, &QAction::triggered,
  188. this, &VMainWindow::curEditFileInfo);
  189. deleteNoteAct = new QAction(QIcon(":/resources/icons/delete_note_tb.svg"),
  190. tr("&Delete Note"), this);
  191. deleteNoteAct->setStatusTip(tr("Delete current note"));
  192. connect(deleteNoteAct, &QAction::triggered,
  193. this, &VMainWindow::deleteCurNote);
  194. editNoteAct = new QAction(QIcon(":/resources/icons/edit_note.svg"),
  195. tr("&Edit"), this);
  196. editNoteAct->setStatusTip(tr("Edit current note"));
  197. editNoteAct->setShortcut(QKeySequence("Ctrl+W"));
  198. connect(editNoteAct, &QAction::triggered,
  199. editArea, &VEditArea::editFile);
  200. discardExitAct = new QAction(QIcon(":/resources/icons/discard_exit.svg"),
  201. tr("Discard Changes And Read"), this);
  202. discardExitAct->setStatusTip(tr("Discard changes and exit edit mode"));
  203. connect(discardExitAct, &QAction::triggered,
  204. editArea, &VEditArea::readFile);
  205. QMenu *exitEditMenu = new QMenu(this);
  206. exitEditMenu->addAction(discardExitAct);
  207. saveExitAct = new QAction(QIcon(":/resources/icons/save_exit.svg"),
  208. tr("Save Changes And Read"), this);
  209. saveExitAct->setStatusTip(tr("Save changes and exit edit mode"));
  210. saveExitAct->setMenu(exitEditMenu);
  211. saveExitAct->setShortcut(QKeySequence("Ctrl+R"));
  212. connect(saveExitAct, &QAction::triggered,
  213. editArea, &VEditArea::saveAndReadFile);
  214. saveNoteAct = new QAction(QIcon(":/resources/icons/save_note.svg"),
  215. tr("Save"), this);
  216. saveNoteAct->setStatusTip(tr("Save changes to current note"));
  217. saveNoteAct->setShortcut(QKeySequence::Save);
  218. connect(saveNoteAct, &QAction::triggered,
  219. editArea, &VEditArea::saveFile);
  220. newRootDirAct->setEnabled(false);
  221. newNoteAct->setEnabled(false);
  222. noteInfoAct->setEnabled(false);
  223. deleteNoteAct->setEnabled(false);
  224. editNoteAct->setVisible(false);
  225. saveExitAct->setVisible(false);
  226. discardExitAct->setVisible(false);
  227. saveNoteAct->setVisible(false);
  228. fileToolBar->addAction(newRootDirAct);
  229. fileToolBar->addAction(newNoteAct);
  230. fileToolBar->addAction(noteInfoAct);
  231. fileToolBar->addAction(deleteNoteAct);
  232. fileToolBar->addSeparator();
  233. fileToolBar->addAction(editNoteAct);
  234. fileToolBar->addAction(saveExitAct);
  235. fileToolBar->addAction(saveNoteAct);
  236. fileToolBar->addSeparator();
  237. }
  238. void VMainWindow::initMenuBar()
  239. {
  240. initFileMenu();
  241. initEditMenu();
  242. initViewMenu();
  243. initMarkdownMenu();
  244. initHelpMenu();
  245. }
  246. void VMainWindow::initHelpMenu()
  247. {
  248. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  249. helpMenu->setToolTipsVisible(true);
  250. #if defined(QT_NO_DEBUG)
  251. QAction *logAct = new QAction(tr("View &Log"), this);
  252. logAct->setToolTip(tr("View VNote's debug log (%1)").arg(vconfig.getLogFilePath()));
  253. connect(logAct, &QAction::triggered,
  254. this, [](){
  255. QUrl url = QUrl::fromLocalFile(vconfig.getLogFilePath());
  256. QDesktopServices::openUrl(url);
  257. });
  258. #endif
  259. QAction *shortcutAct = new QAction(tr("&Shortcuts Help"), this);
  260. shortcutAct->setToolTip(tr("View information about shortcut keys"));
  261. connect(shortcutAct, &QAction::triggered,
  262. this, &VMainWindow::shortcutHelp);
  263. QAction *aboutAct = new QAction(tr("&About VNote"), this);
  264. aboutAct->setToolTip(tr("View information about VNote"));
  265. connect(aboutAct, &QAction::triggered,
  266. this, &VMainWindow::aboutMessage);
  267. QAction *aboutQtAct = new QAction(tr("About &Qt"), this);
  268. aboutQtAct->setToolTip(tr("View information about Qt"));
  269. connect(aboutQtAct, &QAction::triggered,
  270. qApp, &QApplication::aboutQt);
  271. #if defined(QT_NO_DEBUG)
  272. helpMenu->addAction(logAct);
  273. #endif
  274. helpMenu->addAction(shortcutAct);
  275. helpMenu->addAction(aboutQtAct);
  276. helpMenu->addAction(aboutAct);
  277. }
  278. void VMainWindow::initMarkdownMenu()
  279. {
  280. QMenu *markdownMenu = menuBar()->addMenu(tr("&Markdown"));
  281. markdownMenu->setToolTipsVisible(true);
  282. QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter"));
  283. converterMenu->setToolTipsVisible(true);
  284. QActionGroup *converterAct = new QActionGroup(this);
  285. QAction *markedAct = new QAction(tr("Marked"), converterAct);
  286. markedAct->setToolTip(tr("Use Marked to convert Markdown to HTML (re-open current tabs to make it work)"));
  287. markedAct->setCheckable(true);
  288. markedAct->setData(int(MarkdownConverterType::Marked));
  289. QAction *hoedownAct = new QAction(tr("Hoedown"), converterAct);
  290. hoedownAct->setToolTip(tr("Use Hoedown to convert Markdown to HTML (re-open current tabs to make it work)"));
  291. hoedownAct->setCheckable(true);
  292. hoedownAct->setData(int(MarkdownConverterType::Hoedown));
  293. QAction *markdownitAct = new QAction(tr("Markdown-it"), converterAct);
  294. markdownitAct->setToolTip(tr("Use Markdown-it to convert Markdown to HTML (re-open current tabs to make it work)"));
  295. markdownitAct->setCheckable(true);
  296. markdownitAct->setData(int(MarkdownConverterType::MarkdownIt));
  297. QAction *showdownAct = new QAction(tr("Showdown"), converterAct);
  298. showdownAct->setToolTip(tr("Use Showdown to convert Markdown to HTML (re-open current tabs to make it work)"));
  299. showdownAct->setCheckable(true);
  300. showdownAct->setData(int(MarkdownConverterType::Showdown));
  301. connect(converterAct, &QActionGroup::triggered,
  302. this, &VMainWindow::changeMarkdownConverter);
  303. converterMenu->addAction(hoedownAct);
  304. converterMenu->addAction(markedAct);
  305. converterMenu->addAction(markdownitAct);
  306. converterMenu->addAction(showdownAct);
  307. MarkdownConverterType converterType = vconfig.getMdConverterType();
  308. switch (converterType) {
  309. case MarkdownConverterType::Marked:
  310. markedAct->setChecked(true);
  311. break;
  312. case MarkdownConverterType::Hoedown:
  313. hoedownAct->setChecked(true);
  314. break;
  315. case MarkdownConverterType::MarkdownIt:
  316. markdownitAct->setChecked(true);
  317. break;
  318. case MarkdownConverterType::Showdown:
  319. showdownAct->setChecked(true);
  320. break;
  321. default:
  322. Q_ASSERT(false);
  323. }
  324. initRenderStyleMenu(markdownMenu);
  325. initRenderBackgroundMenu(markdownMenu);
  326. QAction *constrainImageAct = new QAction(tr("Constrain The Width of Images"), this);
  327. constrainImageAct->setToolTip(tr("Constrain the width of images to the window in read mode (re-open current tabs to make it work)"));
  328. constrainImageAct->setCheckable(true);
  329. connect(constrainImageAct, &QAction::triggered,
  330. this, &VMainWindow::enableImageConstraint);
  331. markdownMenu->addAction(constrainImageAct);
  332. constrainImageAct->setChecked(vconfig.getEnableImageConstraint());
  333. QAction *imageCaptionAct = new QAction(tr("Enable Image Caption"), this);
  334. imageCaptionAct->setToolTip(tr("Center the images and display the alt text as caption (re-open current tabs to make it work)"));
  335. imageCaptionAct->setCheckable(true);
  336. connect(imageCaptionAct, &QAction::triggered,
  337. this, &VMainWindow::enableImageCaption);
  338. markdownMenu->addAction(imageCaptionAct);
  339. imageCaptionAct->setChecked(vconfig.getEnableImageCaption());
  340. markdownMenu->addSeparator();
  341. QAction *mermaidAct = new QAction(tr("&Mermaid Diagram"), this);
  342. mermaidAct->setToolTip(tr("Enable Mermaid for graph and diagram"));
  343. mermaidAct->setCheckable(true);
  344. connect(mermaidAct, &QAction::triggered,
  345. this, &VMainWindow::enableMermaid);
  346. markdownMenu->addAction(mermaidAct);
  347. mermaidAct->setChecked(vconfig.getEnableMermaid());
  348. QAction *mathjaxAct = new QAction(tr("Math&Jax"), this);
  349. mathjaxAct->setToolTip(tr("Enable MathJax for math support in Markdown"));
  350. mathjaxAct->setCheckable(true);
  351. connect(mathjaxAct, &QAction::triggered,
  352. this, &VMainWindow::enableMathjax);
  353. markdownMenu->addAction(mathjaxAct);
  354. mathjaxAct->setChecked(vconfig.getEnableMathjax());
  355. markdownMenu->addSeparator();
  356. QAction *codeBlockAct = new QAction(tr("Highlight Code Blocks In Edit Mode"), this);
  357. codeBlockAct->setToolTip(tr("Enable syntax highlight within code blocks in edit mode"));
  358. codeBlockAct->setCheckable(true);
  359. connect(codeBlockAct, &QAction::triggered,
  360. this, &VMainWindow::enableCodeBlockHighlight);
  361. markdownMenu->addAction(codeBlockAct);
  362. codeBlockAct->setChecked(vconfig.getEnableCodeBlockHighlight());
  363. QAction *previewImageAct = new QAction(tr("Preview Images In Edit Mode"), this);
  364. previewImageAct->setToolTip(tr("Enable image preview in edit mode"));
  365. previewImageAct->setCheckable(true);
  366. connect(previewImageAct, &QAction::triggered,
  367. this, &VMainWindow::enableImagePreview);
  368. // TODO: add the action to the menu after handling the UNDO history well.
  369. // markdownMenu->addAction(previewImageAct);
  370. previewImageAct->setChecked(vconfig.getEnablePreviewImages());
  371. QAction *previewWidthAct = new QAction(tr("Constrain The Width Of Previewed Images"), this);
  372. previewWidthAct->setToolTip(tr("Constrain the width of previewed images to the edit window in edit mode"));
  373. previewWidthAct->setCheckable(true);
  374. connect(previewWidthAct, &QAction::triggered,
  375. this, &VMainWindow::enableImagePreviewConstraint);
  376. markdownMenu->addAction(previewWidthAct);
  377. previewWidthAct->setChecked(vconfig.getEnablePreviewImageConstraint());
  378. }
  379. void VMainWindow::initViewMenu()
  380. {
  381. viewMenu = menuBar()->addMenu(tr("&View"));
  382. }
  383. void VMainWindow::initFileMenu()
  384. {
  385. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  386. fileMenu->setToolTipsVisible(true);
  387. // Import notes from files.
  388. m_importNoteAct = newAction(QIcon(":/resources/icons/import_note.svg"),
  389. tr("&Import Notes From Files"), this);
  390. m_importNoteAct->setToolTip(tr("Import notes from external files into current folder"));
  391. connect(m_importNoteAct, &QAction::triggered,
  392. this, &VMainWindow::importNoteFromFile);
  393. m_importNoteAct->setEnabled(false);
  394. fileMenu->addAction(m_importNoteAct);
  395. fileMenu->addSeparator();
  396. // Export as PDF.
  397. m_exportAsPDFAct = new QAction(tr("Export As &PDF"), this);
  398. m_exportAsPDFAct->setToolTip(tr("Export current note as PDF file"));
  399. connect(m_exportAsPDFAct, &QAction::triggered,
  400. this, &VMainWindow::exportAsPDF);
  401. m_exportAsPDFAct->setEnabled(false);
  402. fileMenu->addAction(m_exportAsPDFAct);
  403. fileMenu->addSeparator();
  404. // Print.
  405. m_printAct = new QAction(QIcon(":/resources/icons/print.svg"),
  406. tr("&Print"), this);
  407. m_printAct->setToolTip(tr("Print current note"));
  408. connect(m_printAct, &QAction::triggered,
  409. this, &VMainWindow::printNote);
  410. m_printAct->setEnabled(false);
  411. // Settings.
  412. QAction *settingsAct = newAction(QIcon(":/resources/icons/settings.svg"),
  413. tr("&Settings"), this);
  414. settingsAct->setToolTip(tr("View and change settings for VNote"));
  415. connect(settingsAct, &QAction::triggered,
  416. this, &VMainWindow::viewSettings);
  417. fileMenu->addAction(settingsAct);
  418. fileMenu->addSeparator();
  419. // Exit.
  420. QAction *exitAct = new QAction(tr("&Exit"), this);
  421. exitAct->setToolTip(tr("Exit VNote"));
  422. exitAct->setShortcut(QKeySequence("Ctrl+Q"));
  423. connect(exitAct, &QAction::triggered,
  424. this, &VMainWindow::close);
  425. fileMenu->addAction(exitAct);
  426. }
  427. void VMainWindow::initEditMenu()
  428. {
  429. QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
  430. editMenu->setToolTipsVisible(true);
  431. // Insert image.
  432. m_insertImageAct = newAction(QIcon(":/resources/icons/insert_image.svg"),
  433. tr("Insert &Image"), this);
  434. m_insertImageAct->setToolTip(tr("Insert an image from file into current note"));
  435. connect(m_insertImageAct, &QAction::triggered,
  436. this, &VMainWindow::insertImage);
  437. // Find/Replace.
  438. m_findReplaceAct = newAction(QIcon(":/resources/icons/find_replace.svg"),
  439. tr("Find/Replace"), this);
  440. m_findReplaceAct->setToolTip(tr("Open Find/Replace dialog to search in current note"));
  441. m_findReplaceAct->setShortcut(QKeySequence::Find);
  442. connect(m_findReplaceAct, &QAction::triggered,
  443. this, &VMainWindow::openFindDialog);
  444. m_findNextAct = new QAction(tr("Find Next"), this);
  445. m_findNextAct->setToolTip(tr("Find next occurence"));
  446. m_findNextAct->setShortcut(QKeySequence::FindNext);
  447. connect(m_findNextAct, SIGNAL(triggered(bool)),
  448. m_findReplaceDialog, SLOT(findNext()));
  449. m_findPreviousAct = new QAction(tr("Find Previous"), this);
  450. m_findPreviousAct->setToolTip(tr("Find previous occurence"));
  451. m_findPreviousAct->setShortcut(QKeySequence::FindPrevious);
  452. connect(m_findPreviousAct, SIGNAL(triggered(bool)),
  453. m_findReplaceDialog, SLOT(findPrevious()));
  454. m_replaceAct = new QAction(tr("Replace"), this);
  455. m_replaceAct->setToolTip(tr("Replace current occurence"));
  456. m_replaceAct->setShortcut(QKeySequence::Replace);
  457. connect(m_replaceAct, SIGNAL(triggered(bool)),
  458. m_findReplaceDialog, SLOT(replace()));
  459. m_replaceFindAct = new QAction(tr("Replace && Find"), this);
  460. m_replaceFindAct->setToolTip(tr("Replace current occurence and find the next one"));
  461. connect(m_replaceFindAct, SIGNAL(triggered(bool)),
  462. m_findReplaceDialog, SLOT(replaceFind()));
  463. m_replaceAllAct = new QAction(tr("Replace All"), this);
  464. m_replaceAllAct->setToolTip(tr("Replace all occurences in current note"));
  465. connect(m_replaceAllAct, SIGNAL(triggered(bool)),
  466. m_findReplaceDialog, SLOT(replaceAll()));
  467. QAction *searchedWordAct = new QAction(tr("Highlight Searched Pattern"), this);
  468. searchedWordAct->setToolTip(tr("Highlight all occurences of searched pattern"));
  469. searchedWordAct->setCheckable(true);
  470. connect(searchedWordAct, &QAction::triggered,
  471. this, &VMainWindow::changeHighlightSearchedWord);
  472. // Expand Tab into spaces.
  473. QAction *expandTabAct = new QAction(tr("&Expand Tab"), this);
  474. expandTabAct->setToolTip(tr("Expand entered Tab to spaces"));
  475. expandTabAct->setCheckable(true);
  476. connect(expandTabAct, &QAction::triggered,
  477. this, &VMainWindow::changeExpandTab);
  478. // Tab stop width.
  479. QActionGroup *tabStopWidthAct = new QActionGroup(this);
  480. QAction *twoSpaceTabAct = new QAction(tr("2 Spaces"), tabStopWidthAct);
  481. twoSpaceTabAct->setToolTip(tr("Expand Tab to 2 spaces"));
  482. twoSpaceTabAct->setCheckable(true);
  483. twoSpaceTabAct->setData(2);
  484. QAction *fourSpaceTabAct = new QAction(tr("4 Spaces"), tabStopWidthAct);
  485. fourSpaceTabAct->setToolTip(tr("Expand Tab to 4 spaces"));
  486. fourSpaceTabAct->setCheckable(true);
  487. fourSpaceTabAct->setData(4);
  488. QAction *eightSpaceTabAct = new QAction(tr("8 Spaces"), tabStopWidthAct);
  489. eightSpaceTabAct->setToolTip(tr("Expand Tab to 8 spaces"));
  490. eightSpaceTabAct->setCheckable(true);
  491. eightSpaceTabAct->setData(8);
  492. connect(tabStopWidthAct, &QActionGroup::triggered,
  493. this, &VMainWindow::setTabStopWidth);
  494. // Auto Indent.
  495. m_autoIndentAct = new QAction(tr("Auto Indent"), this);
  496. m_autoIndentAct->setToolTip(tr("Indent automatically when inserting a new line"));
  497. m_autoIndentAct->setCheckable(true);
  498. connect(m_autoIndentAct, &QAction::triggered,
  499. this, &VMainWindow::changeAutoIndent);
  500. // Auto List.
  501. QAction *autoListAct = new QAction(tr("Auto List"), this);
  502. autoListAct->setToolTip(tr("Continue the list automatically when inserting a new line"));
  503. autoListAct->setCheckable(true);
  504. connect(autoListAct, &QAction::triggered,
  505. this, &VMainWindow::changeAutoList);
  506. // Vim Mode.
  507. QAction *vimAct = new QAction(tr("Vim Mode"), this);
  508. vimAct->setToolTip(tr("Enable Vim mode for editing (re-enter edit mode to make it work)"));
  509. vimAct->setCheckable(true);
  510. connect(vimAct, &QAction::triggered,
  511. this, &VMainWindow::changeVimMode);
  512. // Highlight current cursor line.
  513. QAction *cursorLineAct = new QAction(tr("Highlight Cursor Line"), this);
  514. cursorLineAct->setToolTip(tr("Highlight current cursor line"));
  515. cursorLineAct->setCheckable(true);
  516. connect(cursorLineAct, &QAction::triggered,
  517. this, &VMainWindow::changeHighlightCursorLine);
  518. // Highlight selected word.
  519. QAction *selectedWordAct = new QAction(tr("Highlight Selected Words"), this);
  520. selectedWordAct->setToolTip(tr("Highlight all occurences of selected words"));
  521. selectedWordAct->setCheckable(true);
  522. connect(selectedWordAct, &QAction::triggered,
  523. this, &VMainWindow::changeHighlightSelectedWord);
  524. // Highlight trailing space.
  525. QAction *trailingSapceAct = new QAction(tr("Highlight Trailing Sapces"), this);
  526. trailingSapceAct->setToolTip(tr("Highlight all the spaces at the end of a line"));
  527. trailingSapceAct->setCheckable(true);
  528. connect(trailingSapceAct, &QAction::triggered,
  529. this, &VMainWindow::changeHighlightTrailingSapce);
  530. editMenu->addAction(m_insertImageAct);
  531. editMenu->addSeparator();
  532. m_insertImageAct->setEnabled(false);
  533. QMenu *findReplaceMenu = editMenu->addMenu(tr("Find/Replace"));
  534. findReplaceMenu->setToolTipsVisible(true);
  535. findReplaceMenu->addAction(m_findReplaceAct);
  536. findReplaceMenu->addAction(m_findNextAct);
  537. findReplaceMenu->addAction(m_findPreviousAct);
  538. findReplaceMenu->addAction(m_replaceAct);
  539. findReplaceMenu->addAction(m_replaceFindAct);
  540. findReplaceMenu->addAction(m_replaceAllAct);
  541. findReplaceMenu->addSeparator();
  542. findReplaceMenu->addAction(searchedWordAct);
  543. searchedWordAct->setChecked(vconfig.getHighlightSearchedWord());
  544. m_findReplaceAct->setEnabled(false);
  545. m_findNextAct->setEnabled(false);
  546. m_findPreviousAct->setEnabled(false);
  547. m_replaceAct->setEnabled(false);
  548. m_replaceFindAct->setEnabled(false);
  549. m_replaceAllAct->setEnabled(false);
  550. editMenu->addSeparator();
  551. editMenu->addAction(expandTabAct);
  552. if (vconfig.getIsExpandTab()) {
  553. expandTabAct->setChecked(true);
  554. } else {
  555. expandTabAct->setChecked(false);
  556. }
  557. QMenu *tabStopWidthMenu = editMenu->addMenu(tr("Tab Stop Width"));
  558. tabStopWidthMenu->setToolTipsVisible(true);
  559. tabStopWidthMenu->addAction(twoSpaceTabAct);
  560. tabStopWidthMenu->addAction(fourSpaceTabAct);
  561. tabStopWidthMenu->addAction(eightSpaceTabAct);
  562. int tabStopWidth = vconfig.getTabStopWidth();
  563. switch (tabStopWidth) {
  564. case 2:
  565. twoSpaceTabAct->setChecked(true);
  566. break;
  567. case 4:
  568. fourSpaceTabAct->setChecked(true);
  569. break;
  570. case 8:
  571. eightSpaceTabAct->setChecked(true);
  572. break;
  573. default:
  574. qWarning() << "unsupported tab stop width" << tabStopWidth << "in config";
  575. }
  576. editMenu->addAction(m_autoIndentAct);
  577. m_autoIndentAct->setChecked(vconfig.getAutoIndent());
  578. editMenu->addAction(autoListAct);
  579. if (vconfig.getAutoList()) {
  580. // Let the trigger handler to trigger m_autoIndentAct, too.
  581. autoListAct->trigger();
  582. }
  583. Q_ASSERT(!(autoListAct->isChecked() && !m_autoIndentAct->isChecked()));
  584. editMenu->addAction(vimAct);
  585. vimAct->setChecked(vconfig.getEnableVimMode());
  586. editMenu->addSeparator();
  587. initEditorStyleMenu(editMenu);
  588. initEditorBackgroundMenu(editMenu);
  589. editMenu->addAction(cursorLineAct);
  590. cursorLineAct->setChecked(vconfig.getHighlightCursorLine());
  591. editMenu->addAction(selectedWordAct);
  592. selectedWordAct->setChecked(vconfig.getHighlightSelectedWord());
  593. editMenu->addAction(trailingSapceAct);
  594. trailingSapceAct->setChecked(vconfig.getEnableTrailingSpaceHighlight());
  595. }
  596. void VMainWindow::initDockWindows()
  597. {
  598. toolDock = new QDockWidget(tr("Tools"), this);
  599. toolDock->setObjectName("tools_dock");
  600. toolDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
  601. toolBox = new QToolBox(this);
  602. outline = new VOutline(this);
  603. connect(editArea, &VEditArea::outlineChanged,
  604. outline, &VOutline::updateOutline);
  605. connect(outline, &VOutline::outlineItemActivated,
  606. editArea, &VEditArea::handleOutlineItemActivated);
  607. connect(editArea, &VEditArea::curHeaderChanged,
  608. outline, &VOutline::updateCurHeader);
  609. toolBox->addItem(outline, QIcon(":/resources/icons/outline.svg"), tr("Outline"));
  610. toolDock->setWidget(toolBox);
  611. addDockWidget(Qt::RightDockWidgetArea, toolDock);
  612. viewMenu->addAction(toolDock->toggleViewAction());
  613. }
  614. void VMainWindow::initAvatar()
  615. {
  616. m_avatar = new VAvatar(this);
  617. m_avatar->setAvatarPixmap(":/resources/icons/vnote.svg");
  618. m_avatar->setColor(vnote->getColorFromPalette("base-color"), vnote->getColorFromPalette("Indigo4"),
  619. vnote->getColorFromPalette("Teal4"));
  620. m_avatar->hide();
  621. }
  622. void VMainWindow::importNoteFromFile()
  623. {
  624. static QString lastPath = QDir::homePath();
  625. QStringList files = QFileDialog::getOpenFileNames(this,
  626. tr("Select Files (HTML or Markdown) To Import"),
  627. lastPath);
  628. if (files.isEmpty()) {
  629. return;
  630. }
  631. // Update lastPath
  632. lastPath = QFileInfo(files[0]).path();
  633. int failedFiles = 0;
  634. for (int i = 0; i < files.size(); ++i) {
  635. bool ret = fileList->importFile(files[i]);
  636. if (!ret) {
  637. ++failedFiles;
  638. }
  639. }
  640. QMessageBox msgBox(QMessageBox::Information, tr("Import Notes From File"),
  641. tr("Imported notes: %1 succeed, %2 failed.")
  642. .arg(files.size() - failedFiles).arg(failedFiles),
  643. QMessageBox::Ok, this);
  644. if (failedFiles > 0) {
  645. msgBox.setInformativeText(tr("Fail to import files maybe due to name conflicts."));
  646. }
  647. msgBox.exec();
  648. }
  649. void VMainWindow::changeMarkdownConverter(QAction *action)
  650. {
  651. if (!action) {
  652. return;
  653. }
  654. MarkdownConverterType type = (MarkdownConverterType)action->data().toInt();
  655. qDebug() << "switch to converter" << type;
  656. vconfig.setMarkdownConverterType(type);
  657. }
  658. void VMainWindow::aboutMessage()
  659. {
  660. QString info = tr("v%1").arg(VConfigManager::c_version);
  661. info += "\n\n";
  662. info += tr("VNote is a Vim-inspired note-taking application for Markdown.");
  663. info += "\n";
  664. info += tr("Visit https://github.com/tamlok/vnote.git for more information.");
  665. QMessageBox::about(this, tr("About VNote"), info);
  666. }
  667. void VMainWindow::changeExpandTab(bool checked)
  668. {
  669. vconfig.setIsExpandTab(checked);
  670. }
  671. void VMainWindow::enableMermaid(bool p_checked)
  672. {
  673. vconfig.setEnableMermaid(p_checked);
  674. }
  675. void VMainWindow::enableMathjax(bool p_checked)
  676. {
  677. vconfig.setEnableMathjax(p_checked);
  678. }
  679. void VMainWindow::changeHighlightCursorLine(bool p_checked)
  680. {
  681. vconfig.setHighlightCursorLine(p_checked);
  682. }
  683. void VMainWindow::changeHighlightSelectedWord(bool p_checked)
  684. {
  685. vconfig.setHighlightSelectedWord(p_checked);
  686. }
  687. void VMainWindow::changeHighlightSearchedWord(bool p_checked)
  688. {
  689. vconfig.setHighlightSearchedWord(p_checked);
  690. }
  691. void VMainWindow::changeHighlightTrailingSapce(bool p_checked)
  692. {
  693. vconfig.setEnableTrailingSapceHighlight(p_checked);
  694. }
  695. void VMainWindow::setTabStopWidth(QAction *action)
  696. {
  697. if (!action) {
  698. return;
  699. }
  700. vconfig.setTabStopWidth(action->data().toInt());
  701. }
  702. void VMainWindow::setEditorBackgroundColor(QAction *action)
  703. {
  704. if (!action) {
  705. return;
  706. }
  707. vconfig.setCurBackgroundColor(action->data().toString());
  708. }
  709. void VMainWindow::initPredefinedColorPixmaps()
  710. {
  711. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  712. predefinedColorPixmaps.clear();
  713. int size = 256;
  714. for (int i = 0; i < bgColors.size(); ++i) {
  715. // Generate QPixmap filled in this color
  716. QColor color(VUtils::QRgbFromString(bgColors[i].rgb));
  717. QPixmap pixmap(size, size);
  718. pixmap.fill(color);
  719. predefinedColorPixmaps.append(pixmap);
  720. }
  721. }
  722. void VMainWindow::initRenderBackgroundMenu(QMenu *menu)
  723. {
  724. QActionGroup *renderBackgroundAct = new QActionGroup(this);
  725. connect(renderBackgroundAct, &QActionGroup::triggered,
  726. this, &VMainWindow::setRenderBackgroundColor);
  727. QMenu *renderBgMenu = menu->addMenu(tr("&Rendering Background"));
  728. renderBgMenu->setToolTipsVisible(true);
  729. const QString &curBgColor = vconfig.getCurRenderBackgroundColor();
  730. QAction *tmpAct = new QAction(tr("System"), renderBackgroundAct);
  731. tmpAct->setToolTip(tr("Use system's background color configuration for Markdown rendering"));
  732. tmpAct->setCheckable(true);
  733. tmpAct->setData("System");
  734. if (curBgColor == "System") {
  735. tmpAct->setChecked(true);
  736. }
  737. renderBgMenu->addAction(tmpAct);
  738. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  739. for (int i = 0; i < bgColors.size(); ++i) {
  740. tmpAct = new QAction(bgColors[i].name, renderBackgroundAct);
  741. tmpAct->setToolTip(tr("Set as the background color for Markdown rendering"));
  742. tmpAct->setCheckable(true);
  743. tmpAct->setData(bgColors[i].name);
  744. #if !defined(Q_OS_MACOS)
  745. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  746. #endif
  747. if (curBgColor == bgColors[i].name) {
  748. tmpAct->setChecked(true);
  749. }
  750. renderBgMenu->addAction(tmpAct);
  751. }
  752. }
  753. void VMainWindow::updateRenderStyleMenu()
  754. {
  755. QMenu *menu = dynamic_cast<QMenu *>(sender());
  756. V_ASSERT(menu);
  757. QList<QAction *> actions = menu->actions();
  758. // Remove all other actions except the first one.
  759. for (int i = 1; i < actions.size(); ++i) {
  760. menu->removeAction(actions[i]);
  761. m_renderStyleActs->removeAction(actions[i]);
  762. delete actions[i];
  763. }
  764. // Update the menu actions with styles.
  765. QVector<QString> styles = vconfig.getCssStyles();
  766. for (auto const &style : styles) {
  767. QAction *act = new QAction(style, m_renderStyleActs);
  768. act->setToolTip(tr("Set as the CSS style for Markdown rendering"));
  769. act->setCheckable(true);
  770. act->setData(style);
  771. // Add it to the menu.
  772. menu->addAction(act);
  773. if (vconfig.getTemplateCss() == style) {
  774. act->setChecked(true);
  775. }
  776. }
  777. }
  778. void VMainWindow::initRenderStyleMenu(QMenu *p_menu)
  779. {
  780. QMenu *styleMenu = p_menu->addMenu(tr("Rendering &Style"));
  781. styleMenu->setToolTipsVisible(true);
  782. connect(styleMenu, &QMenu::aboutToShow,
  783. this, &VMainWindow::updateRenderStyleMenu);
  784. m_renderStyleActs = new QActionGroup(this);
  785. connect(m_renderStyleActs, &QActionGroup::triggered,
  786. this, &VMainWindow::setRenderStyle);
  787. QAction *addAct = newAction(QIcon(":/resources/icons/add_style.svg"),
  788. tr("&Add Style"), m_renderStyleActs);
  789. addAct->setToolTip(tr("Open the folder to add your custom CSS style files"));
  790. addAct->setCheckable(true);
  791. addAct->setData("AddStyle");
  792. styleMenu->addAction(addAct);
  793. }
  794. void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
  795. {
  796. QMenu *backgroundColorMenu = menu->addMenu(tr("&Background Color"));
  797. backgroundColorMenu->setToolTipsVisible(true);
  798. QActionGroup *backgroundColorAct = new QActionGroup(this);
  799. connect(backgroundColorAct, &QActionGroup::triggered,
  800. this, &VMainWindow::setEditorBackgroundColor);
  801. // System background color
  802. const QString &curBgColor = vconfig.getCurBackgroundColor();
  803. QAction *tmpAct = new QAction(tr("System"), backgroundColorAct);
  804. tmpAct->setToolTip(tr("Use system's background color configuration for editor"));
  805. tmpAct->setCheckable(true);
  806. tmpAct->setData("System");
  807. if (curBgColor == "System") {
  808. tmpAct->setChecked(true);
  809. }
  810. backgroundColorMenu->addAction(tmpAct);
  811. const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
  812. for (int i = 0; i < bgColors.size(); ++i) {
  813. tmpAct = new QAction(bgColors[i].name, backgroundColorAct);
  814. tmpAct->setToolTip(tr("Set as the background color for editor"));
  815. tmpAct->setCheckable(true);
  816. tmpAct->setData(bgColors[i].name);
  817. #if !defined(Q_OS_MACOS)
  818. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  819. #endif
  820. if (curBgColor == bgColors[i].name) {
  821. tmpAct->setChecked(true);
  822. }
  823. backgroundColorMenu->addAction(tmpAct);
  824. }
  825. }
  826. void VMainWindow::updateEditorStyleMenu()
  827. {
  828. QMenu *menu = dynamic_cast<QMenu *>(sender());
  829. V_ASSERT(menu);
  830. QList<QAction *> actions = menu->actions();
  831. // Remove all other actions except the first one.
  832. for (int i = 1; i < actions.size(); ++i) {
  833. menu->removeAction(actions[i]);
  834. m_editorStyleActs->removeAction(actions[i]);
  835. delete actions[i];
  836. }
  837. // Update the menu actions with styles.
  838. QVector<QString> styles = vconfig.getEditorStyles();
  839. for (auto const &style : styles) {
  840. QAction *act = new QAction(style, m_editorStyleActs);
  841. act->setToolTip(tr("Set as the editor style"));
  842. act->setCheckable(true);
  843. act->setData(style);
  844. // Add it to the menu.
  845. menu->addAction(act);
  846. if (vconfig.getEditorStyle() == style) {
  847. act->setChecked(true);
  848. }
  849. }
  850. }
  851. void VMainWindow::initEditorStyleMenu(QMenu *p_menu)
  852. {
  853. QMenu *styleMenu = p_menu->addMenu(tr("Editor &Style"));
  854. styleMenu->setToolTipsVisible(true);
  855. connect(styleMenu, &QMenu::aboutToShow,
  856. this, &VMainWindow::updateEditorStyleMenu);
  857. m_editorStyleActs = new QActionGroup(this);
  858. connect(m_editorStyleActs, &QActionGroup::triggered,
  859. this, &VMainWindow::setEditorStyle);
  860. QAction *addAct = newAction(QIcon(":/resources/icons/add_style.svg"),
  861. tr("&Add Style"), m_editorStyleActs);
  862. addAct->setToolTip(tr("Open the folder to add your custom MDHL style files"));
  863. addAct->setCheckable(true);
  864. addAct->setData("AddStyle");
  865. styleMenu->addAction(addAct);
  866. }
  867. void VMainWindow::setRenderBackgroundColor(QAction *action)
  868. {
  869. if (!action) {
  870. return;
  871. }
  872. vconfig.setCurRenderBackgroundColor(action->data().toString());
  873. vnote->updateTemplate();
  874. }
  875. void VMainWindow::setRenderStyle(QAction *p_action)
  876. {
  877. if (!p_action) {
  878. return;
  879. }
  880. QString data = p_action->data().toString();
  881. if (data == "AddStyle") {
  882. // Add custom style.
  883. QUrl url = QUrl::fromLocalFile(vconfig.getStyleConfigFolder());
  884. QDesktopServices::openUrl(url);
  885. } else {
  886. vconfig.setTemplateCss(data);
  887. vnote->updateTemplate();
  888. }
  889. }
  890. void VMainWindow::setEditorStyle(QAction *p_action)
  891. {
  892. if (!p_action) {
  893. return;
  894. }
  895. QString data = p_action->data().toString();
  896. if (data == "AddStyle") {
  897. // Add custom style.
  898. QUrl url = QUrl::fromLocalFile(vconfig.getStyleConfigFolder());
  899. QDesktopServices::openUrl(url);
  900. } else {
  901. vconfig.setEditorStyle(data);
  902. }
  903. }
  904. void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file,
  905. bool p_editMode)
  906. {
  907. m_printAct->setEnabled(p_file && p_file->getDocType() == DocType::Markdown);
  908. m_exportAsPDFAct->setEnabled(p_file && p_file->getDocType() == DocType::Markdown);
  909. editNoteAct->setVisible(p_file && p_file->isModifiable() && !p_editMode);
  910. discardExitAct->setVisible(p_file && p_editMode);
  911. saveExitAct->setVisible(p_file && p_editMode);
  912. saveNoteAct->setVisible(p_file && p_editMode);
  913. deleteNoteAct->setEnabled(p_file && p_file->isModifiable());
  914. noteInfoAct->setEnabled(p_file && p_file->getType() == FileType::Normal);
  915. m_insertImageAct->setEnabled(p_file && p_editMode);
  916. // Find/Replace
  917. m_findReplaceAct->setEnabled(p_file);
  918. m_findNextAct->setEnabled(p_file);
  919. m_findPreviousAct->setEnabled(p_file);
  920. m_replaceAct->setEnabled(p_file && p_editMode);
  921. m_replaceFindAct->setEnabled(p_file && p_editMode);
  922. m_replaceAllAct->setEnabled(p_file && p_editMode);
  923. if (!p_file) {
  924. m_findReplaceDialog->closeDialog();
  925. }
  926. }
  927. void VMainWindow::handleCurTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode)
  928. {
  929. updateActionStateFromTabStatusChange(p_file, p_editMode);
  930. if (p_file) {
  931. m_findReplaceDialog->updateState(p_file->getDocType(), p_editMode);
  932. }
  933. QString title;
  934. if (p_file) {
  935. title = QString("[%1] %2").arg(p_file->getNotebookName()).arg(p_file->retrivePath());
  936. if (p_file->isModifiable()) {
  937. if (p_file->isModified()) {
  938. title.append('*');
  939. }
  940. } else {
  941. title.append('#');
  942. }
  943. }
  944. updateWindowTitle(title);
  945. m_curFile = const_cast<VFile *>(p_file);
  946. m_curTab = const_cast<VEditTab *>(p_editTab);
  947. }
  948. void VMainWindow::onePanelView()
  949. {
  950. changeSplitterView(1);
  951. expandViewAct->setChecked(false);
  952. m_onePanel = true;
  953. }
  954. void VMainWindow::twoPanelView()
  955. {
  956. changeSplitterView(2);
  957. expandViewAct->setChecked(false);
  958. m_onePanel = false;
  959. }
  960. void VMainWindow::toggleOnePanelView()
  961. {
  962. if (m_onePanel) {
  963. twoPanelView();
  964. } else {
  965. onePanelView();
  966. }
  967. }
  968. void VMainWindow::expandPanelView(bool p_checked)
  969. {
  970. int nrSplits = 0;
  971. if (p_checked) {
  972. nrSplits = 0;
  973. } else {
  974. if (m_onePanel) {
  975. nrSplits = 1;
  976. } else {
  977. nrSplits = 2;
  978. }
  979. }
  980. changeSplitterView(nrSplits);
  981. }
  982. void VMainWindow::changeSplitterView(int nrPanel)
  983. {
  984. switch (nrPanel) {
  985. case 0:
  986. // Expand
  987. mainSplitter->widget(0)->hide();
  988. mainSplitter->widget(1)->hide();
  989. mainSplitter->widget(2)->show();
  990. break;
  991. case 1:
  992. // Single panel
  993. mainSplitter->widget(0)->hide();
  994. mainSplitter->widget(1)->show();
  995. mainSplitter->widget(2)->show();
  996. break;
  997. case 2:
  998. // Two panels
  999. mainSplitter->widget(0)->show();
  1000. mainSplitter->widget(1)->show();
  1001. mainSplitter->widget(2)->show();
  1002. break;
  1003. default:
  1004. qWarning() << "invalid panel number" << nrPanel;
  1005. }
  1006. }
  1007. void VMainWindow::updateWindowTitle(const QString &str)
  1008. {
  1009. QString title = "VNote";
  1010. if (!str.isEmpty()) {
  1011. title = title + " - " + str;
  1012. }
  1013. setWindowTitle(title);
  1014. }
  1015. void VMainWindow::curEditFileInfo()
  1016. {
  1017. if (!m_curFile || m_curFile->getType() != FileType::Normal) {
  1018. return;
  1019. }
  1020. fileList->fileInfo(m_curFile);
  1021. }
  1022. void VMainWindow::deleteCurNote()
  1023. {
  1024. if (!m_curFile || !m_curFile->isModifiable()) {
  1025. return;
  1026. }
  1027. fileList->deleteFile(m_curFile);
  1028. }
  1029. void VMainWindow::closeEvent(QCloseEvent *event)
  1030. {
  1031. if (!editArea->closeAllFiles(false)) {
  1032. // Fail to close all the opened files, cancel closing app
  1033. event->ignore();
  1034. return;
  1035. }
  1036. saveStateAndGeometry();
  1037. QMainWindow::closeEvent(event);
  1038. }
  1039. void VMainWindow::saveStateAndGeometry()
  1040. {
  1041. vconfig.setMainWindowGeometry(saveGeometry());
  1042. vconfig.setMainWindowState(saveState());
  1043. vconfig.setToolsDockChecked(toolDock->isVisible());
  1044. vconfig.setMainSplitterState(mainSplitter->saveState());
  1045. }
  1046. void VMainWindow::restoreStateAndGeometry()
  1047. {
  1048. const QByteArray &geometry = vconfig.getMainWindowGeometry();
  1049. if (!geometry.isEmpty()) {
  1050. restoreGeometry(geometry);
  1051. }
  1052. const QByteArray &state = vconfig.getMainWindowState();
  1053. if (!state.isEmpty()) {
  1054. restoreState(state);
  1055. }
  1056. toolDock->setVisible(vconfig.getToolsDockChecked());
  1057. const QByteArray &splitterState = vconfig.getMainSplitterState();
  1058. if (!splitterState.isEmpty()) {
  1059. mainSplitter->restoreState(splitterState);
  1060. }
  1061. }
  1062. const QVector<QPair<QString, QString> >& VMainWindow::getPalette() const
  1063. {
  1064. return vnote->getPalette();
  1065. }
  1066. void VMainWindow::handleCurrentDirectoryChanged(const VDirectory *p_dir)
  1067. {
  1068. newNoteAct->setEnabled(p_dir);
  1069. m_importNoteAct->setEnabled(p_dir);
  1070. }
  1071. void VMainWindow::handleCurrentNotebookChanged(const VNotebook *p_notebook)
  1072. {
  1073. newRootDirAct->setEnabled(p_notebook);
  1074. }
  1075. void VMainWindow::resizeEvent(QResizeEvent *event)
  1076. {
  1077. repositionAvatar();
  1078. QMainWindow::resizeEvent(event);
  1079. }
  1080. void VMainWindow::keyPressEvent(QKeyEvent *event)
  1081. {
  1082. int key = event->key();
  1083. Qt::KeyboardModifiers modifiers = event->modifiers();
  1084. if (key == Qt::Key_Escape
  1085. || (key == Qt::Key_BracketLeft
  1086. && modifiers == Qt::ControlModifier)) {
  1087. m_findReplaceDialog->closeDialog();
  1088. event->accept();
  1089. return;
  1090. }
  1091. QMainWindow::keyPressEvent(event);
  1092. }
  1093. void VMainWindow::repositionAvatar()
  1094. {
  1095. int diameter = mainSplitter->pos().y();
  1096. int x = width() - diameter - 5;
  1097. int y = 0;
  1098. qDebug() << "avatar:" << diameter << x << y;
  1099. m_avatar->setDiameter(diameter);
  1100. m_avatar->move(x, y);
  1101. m_avatar->show();
  1102. }
  1103. void VMainWindow::insertImage()
  1104. {
  1105. if (!m_curTab) {
  1106. return;
  1107. }
  1108. Q_ASSERT(m_curTab == editArea->currentEditTab());
  1109. m_curTab->insertImage();
  1110. }
  1111. void VMainWindow::locateFile(VFile *p_file)
  1112. {
  1113. if (!p_file || p_file->getType() != FileType::Normal) {
  1114. return;
  1115. }
  1116. qDebug() << "locate file" << p_file->retrivePath();
  1117. VNotebook *notebook = p_file->getNotebook();
  1118. if (notebookSelector->locateNotebook(notebook)) {
  1119. while (directoryTree->currentNotebook() != notebook) {
  1120. QCoreApplication::sendPostedEvents();
  1121. }
  1122. VDirectory *dir = p_file->getDirectory();
  1123. if (directoryTree->locateDirectory(dir)) {
  1124. while (fileList->currentDirectory() != dir) {
  1125. QCoreApplication::sendPostedEvents();
  1126. }
  1127. fileList->locateFile(p_file);
  1128. }
  1129. }
  1130. // Open the directory and file panels after location.
  1131. twoPanelView();
  1132. }
  1133. void VMainWindow::locateCurrentFile()
  1134. {
  1135. if (m_curFile) {
  1136. locateFile(m_curFile);
  1137. }
  1138. }
  1139. void VMainWindow::handleFindDialogTextChanged(const QString &p_text, uint /* p_options */)
  1140. {
  1141. bool enabled = true;
  1142. if (p_text.isEmpty()) {
  1143. enabled = false;
  1144. }
  1145. m_findNextAct->setEnabled(enabled);
  1146. m_findPreviousAct->setEnabled(enabled);
  1147. m_replaceAct->setEnabled(enabled);
  1148. m_replaceFindAct->setEnabled(enabled);
  1149. m_replaceAllAct->setEnabled(enabled);
  1150. }
  1151. void VMainWindow::openFindDialog()
  1152. {
  1153. m_findReplaceDialog->openDialog(editArea->getSelectedText());
  1154. }
  1155. void VMainWindow::viewSettings()
  1156. {
  1157. VSettingsDialog settingsDialog(this);
  1158. settingsDialog.exec();
  1159. }
  1160. void VMainWindow::handleCaptainModeChanged(bool p_enabled)
  1161. {
  1162. static QString normalBaseColor = m_avatar->getBaseColor();
  1163. static QString captainModeColor = vnote->getColorFromPalette("Purple5");
  1164. if (p_enabled) {
  1165. m_avatar->updateBaseColor(captainModeColor);
  1166. } else {
  1167. m_avatar->updateBaseColor(normalBaseColor);
  1168. }
  1169. }
  1170. void VMainWindow::closeCurrentFile()
  1171. {
  1172. if (m_curFile) {
  1173. editArea->closeFile(m_curFile, false);
  1174. }
  1175. }
  1176. void VMainWindow::changeAutoIndent(bool p_checked)
  1177. {
  1178. vconfig.setAutoIndent(p_checked);
  1179. }
  1180. void VMainWindow::changeAutoList(bool p_checked)
  1181. {
  1182. vconfig.setAutoList(p_checked);
  1183. if (p_checked) {
  1184. if (!m_autoIndentAct->isChecked()) {
  1185. m_autoIndentAct->trigger();
  1186. }
  1187. m_autoIndentAct->setEnabled(false);
  1188. } else {
  1189. m_autoIndentAct->setEnabled(true);
  1190. }
  1191. }
  1192. void VMainWindow::changeVimMode(bool p_checked)
  1193. {
  1194. vconfig.setEnableVimMode(p_checked);
  1195. }
  1196. void VMainWindow::enableCodeBlockHighlight(bool p_checked)
  1197. {
  1198. vconfig.setEnableCodeBlockHighlight(p_checked);
  1199. }
  1200. void VMainWindow::enableImagePreview(bool p_checked)
  1201. {
  1202. vconfig.setEnablePreviewImages(p_checked);
  1203. }
  1204. void VMainWindow::enableImagePreviewConstraint(bool p_checked)
  1205. {
  1206. vconfig.setEnablePreviewImageConstraint(p_checked);
  1207. }
  1208. void VMainWindow::enableImageConstraint(bool p_checked)
  1209. {
  1210. vconfig.setEnableImageConstraint(p_checked);
  1211. vnote->updateTemplate();
  1212. }
  1213. void VMainWindow::enableImageCaption(bool p_checked)
  1214. {
  1215. vconfig.setEnableImageCaption(p_checked);
  1216. }
  1217. void VMainWindow::shortcutHelp()
  1218. {
  1219. QString locale = VUtils::getLocale();
  1220. QString docName = VNote::c_shortcutsDocFile_en;
  1221. if (locale == "zh_CN") {
  1222. docName = VNote::c_shortcutsDocFile_zh;
  1223. }
  1224. VFile *file = vnote->getOrphanFile(docName);
  1225. editArea->openFile(file, OpenFileMode::Read);
  1226. }
  1227. void VMainWindow::printNote()
  1228. {
  1229. QPrinter printer;
  1230. QPrintDialog dialog(&printer, this);
  1231. dialog.setWindowTitle(tr("Print Note"));
  1232. V_ASSERT(m_curTab);
  1233. if (m_curFile->getDocType() == DocType::Markdown) {
  1234. VMdTab *mdTab = dynamic_cast<VMdTab *>((VEditTab *)m_curTab);
  1235. VWebView *webView = mdTab->getWebViewer();
  1236. V_ASSERT(webView);
  1237. if (webView->hasSelection()) {
  1238. dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
  1239. }
  1240. if (dialog.exec() != QDialog::Accepted) {
  1241. return;
  1242. }
  1243. }
  1244. }
  1245. void VMainWindow::exportAsPDF()
  1246. {
  1247. V_ASSERT(m_curTab);
  1248. V_ASSERT(m_curFile);
  1249. if (m_curFile->getDocType() == DocType::Markdown) {
  1250. VMdTab *mdTab = dynamic_cast<VMdTab *>((VEditTab *)m_curTab);
  1251. VExporter exporter(mdTab->getMarkdownConverterType(), this);
  1252. exporter.exportNote(m_curFile, ExportType::PDF);
  1253. exporter.exec();
  1254. }
  1255. }
  1256. QAction *VMainWindow::newAction(const QIcon &p_icon,
  1257. const QString &p_text,
  1258. QObject *p_parent)
  1259. {
  1260. #if defined(Q_OS_MACOS)
  1261. Q_UNUSED(p_icon);
  1262. return new QAction(p_text, p_parent);
  1263. #else
  1264. return new QAction(p_icon, p_text, p_parent);
  1265. #endif
  1266. }
  1267. void VMainWindow::showStatusMessage(const QString &p_msg)
  1268. {
  1269. const int timeout = 3000;
  1270. statusBar()->showMessage(p_msg, timeout);
  1271. }