vmainwindow.cpp 47 KB

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