vmainwindow.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805
  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. #include "vvimindicator.h"
  24. #include "vtabindicator.h"
  25. #include "dialog/vupdater.h"
  26. #include "vorphanfile.h"
  27. #include "dialog/vorphanfileinfodialog.h"
  28. #include "vsingleinstanceguard.h"
  29. #include "vnotefile.h"
  30. #include "vbuttonwithwidget.h"
  31. #include "vattachmentlist.h"
  32. #include "vfilesessioninfo.h"
  33. #include "vsnippetlist.h"
  34. #include "vtoolbox.h"
  35. #include "vbuttonmenuitem.h"
  36. #include "vpalette.h"
  37. #include "utils/viconutils.h"
  38. #include "dialog/vtipsdialog.h"
  39. VMainWindow *g_mainWin;
  40. extern VConfigManager *g_config;
  41. extern VPalette *g_palette;
  42. VNote *g_vnote;
  43. const int VMainWindow::c_sharedMemTimerInterval = 1000;
  44. #if defined(QT_NO_DEBUG)
  45. extern QFile g_logFile;
  46. #endif
  47. VMainWindow::VMainWindow(VSingleInstanceGuard *p_guard, QWidget *p_parent)
  48. : QMainWindow(p_parent), m_guard(p_guard),
  49. m_windowOldState(Qt::WindowNoState), m_requestQuit(false)
  50. {
  51. qsrand(QDateTime::currentDateTime().toTime_t());
  52. g_mainWin = this;
  53. setWindowIcon(QIcon(":/resources/icons/vnote.ico"));
  54. vnote = new VNote(this);
  55. g_vnote = vnote;
  56. initPredefinedColorPixmaps();
  57. if (g_config->getEnableCompactMode()) {
  58. m_panelViewState = PanelViewState::CompactMode;
  59. } else {
  60. m_panelViewState = PanelViewState::TwoPanels;
  61. }
  62. initCaptain();
  63. setupUI();
  64. initMenuBar();
  65. initToolBar();
  66. initShortcuts();
  67. initDockWindows();
  68. initAvatar();
  69. changePanelView(m_panelViewState);
  70. restoreStateAndGeometry();
  71. setContextMenuPolicy(Qt::NoContextMenu);
  72. notebookSelector->update();
  73. initSharedMemoryWatcher();
  74. registerCaptainAndNavigationTargets();
  75. }
  76. void VMainWindow::initSharedMemoryWatcher()
  77. {
  78. m_sharedMemTimer = new QTimer(this);
  79. m_sharedMemTimer->setSingleShot(false);
  80. m_sharedMemTimer->setInterval(c_sharedMemTimerInterval);
  81. connect(m_sharedMemTimer, &QTimer::timeout,
  82. this, &VMainWindow::checkSharedMemory);
  83. m_sharedMemTimer->start();
  84. }
  85. void VMainWindow::initCaptain()
  86. {
  87. // VCaptain should be visible to accpet key focus. But VCaptain
  88. // may hide other widgets.
  89. m_captain = new VCaptain(this);
  90. connect(m_captain, &VCaptain::captainModeChanged,
  91. this, [this](bool p_captainMode) {
  92. static QString normalBaseColor = m_avatar->getBaseColor();
  93. static QString captainModeColor = g_palette->color("avatar_captain_mode_border_bg");
  94. if (p_captainMode) {
  95. m_avatar->updateBaseColor(captainModeColor);
  96. } else {
  97. m_avatar->updateBaseColor(normalBaseColor);
  98. }
  99. });
  100. }
  101. void VMainWindow::registerCaptainAndNavigationTargets()
  102. {
  103. m_captain->registerNavigationTarget(notebookSelector);
  104. m_captain->registerNavigationTarget(directoryTree);
  105. m_captain->registerNavigationTarget(m_fileList);
  106. m_captain->registerNavigationTarget(editArea);
  107. m_captain->registerNavigationTarget(m_toolBox);
  108. m_captain->registerNavigationTarget(outline);
  109. m_captain->registerNavigationTarget(m_snippetList);
  110. // Register Captain mode targets.
  111. m_captain->registerCaptainTarget(tr("AttachmentList"),
  112. g_config->getCaptainShortcutKeySequence("AttachmentList"),
  113. this,
  114. showAttachmentListByCaptain);
  115. m_captain->registerCaptainTarget(tr("LocateCurrentFile"),
  116. g_config->getCaptainShortcutKeySequence("LocateCurrentFile"),
  117. this,
  118. locateCurrentFileByCaptain);
  119. m_captain->registerCaptainTarget(tr("ExpandMode"),
  120. g_config->getCaptainShortcutKeySequence("ExpandMode"),
  121. this,
  122. toggleExpandModeByCaptain);
  123. m_captain->registerCaptainTarget(tr("OnePanelView"),
  124. g_config->getCaptainShortcutKeySequence("OnePanelView"),
  125. this,
  126. toggleOnePanelViewByCaptain);
  127. m_captain->registerCaptainTarget(tr("DiscardAndRead"),
  128. g_config->getCaptainShortcutKeySequence("DiscardAndRead"),
  129. this,
  130. discardAndReadByCaptain);
  131. m_captain->registerCaptainTarget(tr("ToolsDock"),
  132. g_config->getCaptainShortcutKeySequence("ToolsDock"),
  133. this,
  134. toggleToolsDockByCaptain);
  135. m_captain->registerCaptainTarget(tr("CloseNote"),
  136. g_config->getCaptainShortcutKeySequence("CloseNote"),
  137. this,
  138. closeFileByCaptain);
  139. m_captain->registerCaptainTarget(tr("ShortcutsHelp"),
  140. g_config->getCaptainShortcutKeySequence("ShortcutsHelp"),
  141. this,
  142. shortcutsHelpByCaptain);
  143. m_captain->registerCaptainTarget(tr("FlushLogFile"),
  144. g_config->getCaptainShortcutKeySequence("FlushLogFile"),
  145. this,
  146. flushLogFileByCaptain);
  147. }
  148. void VMainWindow::setupUI()
  149. {
  150. QWidget *directoryPanel = setupDirectoryPanel();
  151. m_fileList = new VFileList();
  152. m_fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  153. editArea = new VEditArea();
  154. editArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  155. m_findReplaceDialog = editArea->getFindReplaceDialog();
  156. m_fileList->setEditArea(editArea);
  157. directoryTree->setEditArea(editArea);
  158. // Main Splitter
  159. m_mainSplitter = new QSplitter();
  160. m_mainSplitter->setObjectName("MainSplitter");
  161. m_mainSplitter->addWidget(directoryPanel);
  162. m_mainSplitter->addWidget(m_fileList);
  163. setTabOrder(directoryTree, m_fileList->getContentWidget());
  164. m_mainSplitter->addWidget(editArea);
  165. m_mainSplitter->setStretchFactor(0, 0);
  166. m_mainSplitter->setStretchFactor(1, 0);
  167. m_mainSplitter->setStretchFactor(2, 1);
  168. // Signals
  169. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  170. m_fileList, &VFileList::setDirectory);
  171. connect(directoryTree, &VDirectoryTree::directoryUpdated,
  172. editArea, &VEditArea::handleDirectoryUpdated);
  173. connect(notebookSelector, &VNotebookSelector::notebookUpdated,
  174. editArea, &VEditArea::handleNotebookUpdated);
  175. connect(notebookSelector, &VNotebookSelector::notebookCreated,
  176. directoryTree, [this](const QString &p_name, bool p_import) {
  177. Q_UNUSED(p_name);
  178. if (!p_import) {
  179. directoryTree->newRootDirectory();
  180. }
  181. });
  182. connect(m_fileList, &VFileList::fileClicked,
  183. editArea, &VEditArea::openFile);
  184. connect(m_fileList, &VFileList::fileCreated,
  185. editArea, &VEditArea::openFile);
  186. connect(m_fileList, &VFileList::fileUpdated,
  187. editArea, &VEditArea::handleFileUpdated);
  188. connect(editArea, &VEditArea::tabStatusUpdated,
  189. this, &VMainWindow::handleAreaTabStatusUpdated);
  190. connect(editArea, &VEditArea::statusMessage,
  191. this, &VMainWindow::showStatusMessage);
  192. connect(editArea, &VEditArea::vimStatusUpdated,
  193. this, &VMainWindow::handleVimStatusUpdated);
  194. connect(m_findReplaceDialog, &VFindReplaceDialog::findTextChanged,
  195. this, &VMainWindow::handleFindDialogTextChanged);
  196. setCentralWidget(m_mainSplitter);
  197. m_vimIndicator = new VVimIndicator(this);
  198. m_vimIndicator->hide();
  199. m_tabIndicator = new VTabIndicator(this);
  200. m_tabIndicator->hide();
  201. // Create and show the status bar
  202. statusBar()->addPermanentWidget(m_vimIndicator);
  203. statusBar()->addPermanentWidget(m_tabIndicator);
  204. initTrayIcon();
  205. }
  206. QWidget *VMainWindow::setupDirectoryPanel()
  207. {
  208. // Notebook selector.
  209. notebookLabel = new QLabel(tr("Notebooks"));
  210. notebookLabel->setProperty("TitleLabel", true);
  211. notebookLabel->setProperty("NotebookPanel", true);
  212. notebookSelector = new VNotebookSelector();
  213. notebookSelector->setObjectName("NotebookSelector");
  214. notebookSelector->setProperty("NotebookPanel", true);
  215. notebookSelector->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
  216. // Navigation panel.
  217. directoryLabel = new QLabel(tr("Folders"));
  218. directoryLabel->setProperty("TitleLabel", true);
  219. directoryLabel->setProperty("NotebookPanel", true);
  220. directoryTree = new VDirectoryTree;
  221. directoryTree->setProperty("NotebookPanel", true);
  222. QVBoxLayout *naviLayout = new QVBoxLayout;
  223. naviLayout->addWidget(directoryLabel);
  224. naviLayout->addWidget(directoryTree);
  225. naviLayout->setContentsMargins(0, 0, 0, 0);
  226. naviLayout->setSpacing(0);
  227. QWidget *naviWidget = new QWidget();
  228. naviWidget->setLayout(naviLayout);
  229. QWidget *tmpWidget = new QWidget();
  230. // Compact splitter.
  231. m_naviSplitter = new QSplitter();
  232. m_naviSplitter->setOrientation(Qt::Vertical);
  233. m_naviSplitter->setObjectName("NaviSplitter");
  234. m_naviSplitter->addWidget(naviWidget);
  235. m_naviSplitter->addWidget(tmpWidget);
  236. m_naviSplitter->setStretchFactor(0, 0);
  237. m_naviSplitter->setStretchFactor(1, 1);
  238. tmpWidget->hide();
  239. QVBoxLayout *nbLayout = new QVBoxLayout;
  240. nbLayout->addWidget(notebookLabel);
  241. nbLayout->addWidget(notebookSelector);
  242. nbLayout->addWidget(m_naviSplitter);
  243. nbLayout->setContentsMargins(0, 0, 0, 0);
  244. nbLayout->setSpacing(0);
  245. QWidget *nbContainer = new QWidget();
  246. nbContainer->setObjectName("NotebookPanel");
  247. nbContainer->setLayout(nbLayout);
  248. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  249. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  250. this, [this](VNotebook *p_notebook) {
  251. directoryTree->setNotebook(p_notebook);
  252. directoryTree->setFocus();
  253. });
  254. connect(notebookSelector, &VNotebookSelector::curNotebookChanged,
  255. this, &VMainWindow::handleCurrentNotebookChanged);
  256. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  257. this, &VMainWindow::handleCurrentDirectoryChanged);
  258. return nbContainer;
  259. }
  260. void VMainWindow::initToolBar()
  261. {
  262. const int tbIconSize = g_config->getToolBarIconSize() * VUtils::calculateScaleFactor();
  263. QSize iconSize(tbIconSize, tbIconSize);
  264. initFileToolBar(iconSize);
  265. initViewToolBar(iconSize);
  266. initEditToolBar(iconSize);
  267. initNoteToolBar(iconSize);
  268. }
  269. void VMainWindow::initViewToolBar(QSize p_iconSize)
  270. {
  271. QToolBar *viewToolBar = addToolBar(tr("View"));
  272. viewToolBar->setObjectName("ViewToolBar");
  273. viewToolBar->setMovable(false);
  274. if (p_iconSize.isValid()) {
  275. viewToolBar->setIconSize(p_iconSize);
  276. }
  277. m_viewActGroup = new QActionGroup(this);
  278. QAction *onePanelViewAct = new QAction(VIconUtils::menuIcon(":/resources/icons/one_panel.svg"),
  279. tr("&Single Panel"),
  280. m_viewActGroup);
  281. onePanelViewAct->setStatusTip(tr("Display only the notes list panel"));
  282. onePanelViewAct->setToolTip(tr("Single Panel"));
  283. onePanelViewAct->setCheckable(true);
  284. onePanelViewAct->setData((int)PanelViewState::SinglePanel);
  285. QAction *twoPanelViewAct = new QAction(VIconUtils::menuIcon(":/resources/icons/two_panels.svg"),
  286. tr("&Two Panels"),
  287. m_viewActGroup);
  288. twoPanelViewAct->setStatusTip(tr("Display both the folders and notes list panel"));
  289. twoPanelViewAct->setToolTip(tr("Two Panels"));
  290. twoPanelViewAct->setCheckable(true);
  291. twoPanelViewAct->setData((int)PanelViewState::TwoPanels);
  292. QAction *compactViewAct = new QAction(VIconUtils::menuIcon(":/resources/icons/compact_mode.svg"),
  293. tr("&Compact Mode"),
  294. m_viewActGroup);
  295. compactViewAct->setStatusTip(tr("Integrate the folders and notes list panel in one column"));
  296. compactViewAct->setCheckable(true);
  297. compactViewAct->setData((int)PanelViewState::CompactMode);
  298. connect(m_viewActGroup, &QActionGroup::triggered,
  299. this, [this](QAction *p_action) {
  300. if (!p_action) {
  301. return;
  302. }
  303. int act = p_action->data().toInt();
  304. switch (act) {
  305. case (int)PanelViewState::SinglePanel:
  306. onePanelView();
  307. break;
  308. case (int)PanelViewState::TwoPanels:
  309. twoPanelView();
  310. break;
  311. case (int)PanelViewState::CompactMode:
  312. compactModeView();
  313. break;
  314. default:
  315. break;
  316. }
  317. });
  318. QMenu *panelMenu = new QMenu(this);
  319. panelMenu->setToolTipsVisible(true);
  320. panelMenu->addAction(onePanelViewAct);
  321. panelMenu->addAction(twoPanelViewAct);
  322. panelMenu->addAction(compactViewAct);
  323. expandViewAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/expand.svg"),
  324. tr("Expand"), this);
  325. expandViewAct->setStatusTip(tr("Expand the edit area"));
  326. expandViewAct->setCheckable(true);
  327. expandViewAct->setMenu(panelMenu);
  328. connect(expandViewAct, &QAction::triggered,
  329. this, [this](bool p_checked) {
  330. // Recover m_panelViewState or change to expand mode.
  331. changePanelView(p_checked ? PanelViewState::ExpandMode
  332. : m_panelViewState);
  333. });
  334. viewToolBar->addAction(expandViewAct);
  335. }
  336. // Enable/disable all actions of @p_widget.
  337. static void setActionsEnabled(QWidget *p_widget, bool p_enabled)
  338. {
  339. Q_ASSERT(p_widget);
  340. QList<QAction *> actions = p_widget->actions();
  341. for (auto const & act : actions) {
  342. act->setEnabled(p_enabled);
  343. }
  344. }
  345. void VMainWindow::initEditToolBar(QSize p_iconSize)
  346. {
  347. m_editToolBar = addToolBar(tr("Edit Toolbar"));
  348. m_editToolBar->setObjectName("EditToolBar");
  349. m_editToolBar->setMovable(false);
  350. if (p_iconSize.isValid()) {
  351. m_editToolBar->setIconSize(p_iconSize);
  352. }
  353. m_editToolBar->addSeparator();
  354. m_headingSequenceAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/heading_sequence.svg"),
  355. tr("Heading Sequence"),
  356. this);
  357. m_headingSequenceAct->setStatusTip(tr("Enable heading sequence in current note in edit mode"));
  358. m_headingSequenceAct->setCheckable(true);
  359. connect(m_headingSequenceAct, &QAction::triggered,
  360. this, [this](bool p_checked){
  361. if (isHeadingSequenceApplicable()) {
  362. VMdTab *tab = dynamic_cast<VMdTab *>(m_curTab.data());
  363. Q_ASSERT(tab);
  364. tab->enableHeadingSequence(p_checked);
  365. }
  366. });
  367. m_editToolBar->addAction(m_headingSequenceAct);
  368. initHeadingButton(m_editToolBar);
  369. QAction *boldAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/bold.svg"),
  370. tr("Bold\t%1").arg(VUtils::getShortcutText("Ctrl+B")),
  371. this);
  372. boldAct->setStatusTip(tr("Insert bold text or change selected text to bold"));
  373. connect(boldAct, &QAction::triggered,
  374. this, [this](){
  375. if (m_curTab) {
  376. m_curTab->decorateText(TextDecoration::Bold);
  377. }
  378. });
  379. m_editToolBar->addAction(boldAct);
  380. QAction *italicAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/italic.svg"),
  381. tr("Italic\t%1").arg(VUtils::getShortcutText("Ctrl+I")),
  382. this);
  383. italicAct->setStatusTip(tr("Insert italic text or change selected text to italic"));
  384. connect(italicAct, &QAction::triggered,
  385. this, [this](){
  386. if (m_curTab) {
  387. m_curTab->decorateText(TextDecoration::Italic);
  388. }
  389. });
  390. m_editToolBar->addAction(italicAct);
  391. QAction *strikethroughAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/strikethrough.svg"),
  392. tr("Strikethrough\t%1").arg(VUtils::getShortcutText("Ctrl+D")),
  393. this);
  394. strikethroughAct->setStatusTip(tr("Insert strikethrough text or change selected text to strikethroughed"));
  395. connect(strikethroughAct, &QAction::triggered,
  396. this, [this](){
  397. if (m_curTab) {
  398. m_curTab->decorateText(TextDecoration::Strikethrough);
  399. }
  400. });
  401. m_editToolBar->addAction(strikethroughAct);
  402. QAction *inlineCodeAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/inline_code.svg"),
  403. tr("Inline Code\t%1").arg(VUtils::getShortcutText("Ctrl+K")),
  404. this);
  405. inlineCodeAct->setStatusTip(tr("Insert inline-code text or change selected text to inline-coded"));
  406. connect(inlineCodeAct, &QAction::triggered,
  407. this, [this](){
  408. if (m_curTab) {
  409. m_curTab->decorateText(TextDecoration::InlineCode);
  410. }
  411. });
  412. m_editToolBar->addAction(inlineCodeAct);
  413. QAction *codeBlockAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/code_block.svg"),
  414. tr("Code Block\t%1").arg(VUtils::getShortcutText("Ctrl+M")),
  415. this);
  416. codeBlockAct->setStatusTip(tr("Insert fenced code block text or wrap selected text into a fenced code block"));
  417. connect(codeBlockAct, &QAction::triggered,
  418. this, [this](){
  419. if (m_curTab) {
  420. m_curTab->decorateText(TextDecoration::CodeBlock);
  421. }
  422. });
  423. m_editToolBar->addAction(codeBlockAct);
  424. // Insert link.
  425. QAction *insetLinkAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/link.svg"),
  426. tr("Insert Link\t%1").arg(VUtils::getShortcutText("Ctrl+L")),
  427. this);
  428. insetLinkAct->setStatusTip(tr("Insert a link"));
  429. connect(insetLinkAct, &QAction::triggered,
  430. this, [this]() {
  431. if (m_curTab) {
  432. m_curTab->insertLink();
  433. }
  434. });
  435. m_editToolBar->addAction(insetLinkAct);
  436. // Insert image.
  437. QAction *insertImageAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/insert_image.svg"),
  438. tr("Insert Image\t%1").arg(VUtils::getShortcutText("Ctrl+'")),
  439. this);
  440. insertImageAct->setStatusTip(tr("Insert an image from file or URL"));
  441. connect(insertImageAct, &QAction::triggered,
  442. this, [this]() {
  443. if (m_curTab) {
  444. m_curTab->insertImage();
  445. }
  446. });
  447. m_editToolBar->addAction(insertImageAct);
  448. QAction *toggleAct = m_editToolBar->toggleViewAction();
  449. toggleAct->setToolTip(tr("Toggle the edit toolbar"));
  450. viewMenu->addAction(toggleAct);
  451. setActionsEnabled(m_editToolBar, false);
  452. }
  453. void VMainWindow::initNoteToolBar(QSize p_iconSize)
  454. {
  455. QToolBar *noteToolBar = addToolBar(tr("Note Toolbar"));
  456. noteToolBar->setObjectName("NoteToolBar");
  457. noteToolBar->setMovable(false);
  458. if (p_iconSize.isValid()) {
  459. noteToolBar->setIconSize(p_iconSize);
  460. }
  461. noteToolBar->addSeparator();
  462. // Attachment.
  463. m_attachmentList = new VAttachmentList(this);
  464. m_attachmentBtn = new VButtonWithWidget(VIconUtils::toolButtonIcon(":/resources/icons/attachment.svg"),
  465. "",
  466. m_attachmentList,
  467. this);
  468. m_attachmentBtn->setBubbleColor(g_palette->color("bubble_fg"),
  469. g_palette->color("bubble_bg"));
  470. m_attachmentBtn->setToolTip(tr("Attachments (drag files here to add attachments)"));
  471. m_attachmentBtn->setProperty("CornerBtn", true);
  472. m_attachmentBtn->setFocusPolicy(Qt::NoFocus);
  473. m_attachmentBtn->setEnabled(false);
  474. QAction *flashPageAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/flash_page.svg"),
  475. tr("Flash Page"),
  476. this);
  477. flashPageAct->setStatusTip(tr("Open the Flash Page to edit"));
  478. QString keySeq = g_config->getShortcutKeySequence("FlashPage");
  479. qDebug() << "set FlashPage shortcut to" << keySeq;
  480. flashPageAct->setShortcut(QKeySequence(keySeq));
  481. connect(flashPageAct, &QAction::triggered,
  482. this, &VMainWindow::openFlashPage);
  483. noteToolBar->addWidget(m_attachmentBtn);
  484. noteToolBar->addAction(flashPageAct);
  485. }
  486. void VMainWindow::initFileToolBar(QSize p_iconSize)
  487. {
  488. QToolBar *fileToolBar = addToolBar(tr("Note"));
  489. fileToolBar->setObjectName("NoteToolBar");
  490. fileToolBar->setMovable(false);
  491. if (p_iconSize.isValid()) {
  492. fileToolBar->setIconSize(p_iconSize);
  493. }
  494. newRootDirAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/create_rootdir_tb.svg"),
  495. tr("New &Root Folder"),
  496. this);
  497. newRootDirAct->setStatusTip(tr("Create a root folder in current notebook"));
  498. connect(newRootDirAct, &QAction::triggered,
  499. directoryTree, &VDirectoryTree::newRootDirectory);
  500. newNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/create_note_tb.svg"),
  501. tr("New &Note"), this);
  502. newNoteAct->setStatusTip(tr("Create a note in current folder"));
  503. QString keySeq = g_config->getShortcutKeySequence("NewNote");
  504. qDebug() << "set NewNote shortcut to" << keySeq;
  505. newNoteAct->setShortcut(QKeySequence(keySeq));
  506. connect(newNoteAct, &QAction::triggered,
  507. m_fileList, &VFileList::newFile);
  508. noteInfoAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/note_info_tb.svg"),
  509. tr("Note &Info"), this);
  510. noteInfoAct->setStatusTip(tr("View and edit current note's information"));
  511. connect(noteInfoAct, &QAction::triggered,
  512. this, &VMainWindow::curEditFileInfo);
  513. deleteNoteAct = new QAction(VIconUtils::toolButtonDangerIcon(":/resources/icons/delete_note_tb.svg"),
  514. tr("&Delete Note"), this);
  515. deleteNoteAct->setStatusTip(tr("Delete current note"));
  516. connect(deleteNoteAct, &QAction::triggered,
  517. this, &VMainWindow::deleteCurNote);
  518. editNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/edit_note.svg"),
  519. tr("&Edit"), this);
  520. editNoteAct->setStatusTip(tr("Edit current note"));
  521. keySeq = g_config->getShortcutKeySequence("EditNote");
  522. qDebug() << "set EditNote shortcut to" << keySeq;
  523. editNoteAct->setShortcut(QKeySequence(keySeq));
  524. connect(editNoteAct, &QAction::triggered,
  525. editArea, &VEditArea::editFile);
  526. discardExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"),
  527. tr("Discard Changes And Read"), this);
  528. discardExitAct->setStatusTip(tr("Discard changes and exit edit mode"));
  529. discardExitAct->setToolTip(tr("Discard Changes And Read"));
  530. connect(discardExitAct, &QAction::triggered,
  531. editArea, &VEditArea::readFile);
  532. QMenu *exitEditMenu = new QMenu(this);
  533. exitEditMenu->setToolTipsVisible(true);
  534. exitEditMenu->addAction(discardExitAct);
  535. saveExitAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/save_exit.svg"),
  536. tr("Save Changes And Read"), this);
  537. saveExitAct->setStatusTip(tr("Save changes and exit edit mode"));
  538. saveExitAct->setMenu(exitEditMenu);
  539. keySeq = g_config->getShortcutKeySequence("SaveAndRead");
  540. qDebug() << "set SaveAndRead shortcut to" << keySeq;
  541. saveExitAct->setShortcut(QKeySequence(keySeq));
  542. connect(saveExitAct, &QAction::triggered,
  543. editArea, &VEditArea::saveAndReadFile);
  544. saveNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/save_note.svg"),
  545. tr("Save"), this);
  546. saveNoteAct->setStatusTip(tr("Save changes to current note"));
  547. keySeq = g_config->getShortcutKeySequence("SaveNote");
  548. qDebug() << "set SaveNote shortcut to" << keySeq;
  549. saveNoteAct->setShortcut(QKeySequence(keySeq));
  550. connect(saveNoteAct, &QAction::triggered,
  551. editArea, &VEditArea::saveFile);
  552. newRootDirAct->setEnabled(false);
  553. newNoteAct->setEnabled(false);
  554. noteInfoAct->setEnabled(false);
  555. deleteNoteAct->setEnabled(false);
  556. editNoteAct->setEnabled(false);
  557. saveExitAct->setVisible(false);
  558. discardExitAct->setVisible(false);
  559. saveNoteAct->setEnabled(false);
  560. fileToolBar->addAction(newRootDirAct);
  561. fileToolBar->addAction(newNoteAct);
  562. fileToolBar->addAction(noteInfoAct);
  563. fileToolBar->addAction(deleteNoteAct);
  564. fileToolBar->addAction(editNoteAct);
  565. fileToolBar->addAction(saveExitAct);
  566. fileToolBar->addAction(saveNoteAct);
  567. }
  568. void VMainWindow::initMenuBar()
  569. {
  570. initFileMenu();
  571. initEditMenu();
  572. initViewMenu();
  573. initMarkdownMenu();
  574. initHelpMenu();
  575. }
  576. void VMainWindow::initHelpMenu()
  577. {
  578. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  579. helpMenu->setToolTipsVisible(true);
  580. #if defined(QT_NO_DEBUG)
  581. QAction *logAct = new QAction(tr("View &Log"), this);
  582. logAct->setToolTip(tr("View VNote's debug log (%1)").arg(g_config->getLogFilePath()));
  583. connect(logAct, &QAction::triggered,
  584. this, [](){
  585. QUrl url = QUrl::fromLocalFile(g_config->getLogFilePath());
  586. QDesktopServices::openUrl(url);
  587. });
  588. #endif
  589. QAction *shortcutAct = new QAction(tr("&Shortcuts Help"), this);
  590. shortcutAct->setToolTip(tr("View information about shortcut keys"));
  591. connect(shortcutAct, &QAction::triggered,
  592. this, &VMainWindow::shortcutsHelp);
  593. QAction *mdGuideAct = new QAction(tr("&Markdown Guide"), this);
  594. mdGuideAct->setToolTip(tr("A quick guide of Markdown syntax"));
  595. connect(mdGuideAct, &QAction::triggered,
  596. this, [this](){
  597. QString docFile = VUtils::getDocFile(VNote::c_markdownGuideDocFile);
  598. VFile *file = vnote->getOrphanFile(docFile, false, true);
  599. editArea->openFile(file, OpenFileMode::Read);
  600. });
  601. QAction *wikiAct = new QAction(tr("&Wiki"), this);
  602. wikiAct->setToolTip(tr("View VNote's wiki on Github"));
  603. connect(wikiAct, &QAction::triggered,
  604. this, [this]() {
  605. QString url("https://github.com/tamlok/vnote/wiki");
  606. QDesktopServices::openUrl(url);
  607. });
  608. QAction *updateAct = new QAction(tr("Check For &Updates"), this);
  609. updateAct->setToolTip(tr("Check for updates of VNote"));
  610. connect(updateAct, &QAction::triggered,
  611. this, [this](){
  612. VUpdater updater(this);
  613. updater.exec();
  614. });
  615. QAction *starAct = new QAction(tr("Star VNote on &Github"), this);
  616. starAct->setToolTip(tr("Give a star to VNote on Github project"));
  617. connect(starAct, &QAction::triggered,
  618. this, [this]() {
  619. QString url("https://github.com/tamlok/vnote");
  620. QDesktopServices::openUrl(url);
  621. });
  622. QAction *feedbackAct = new QAction(tr("&Feedback"), this);
  623. feedbackAct->setToolTip(tr("Open an issue on Github"));
  624. connect(feedbackAct, &QAction::triggered,
  625. this, [this]() {
  626. QString url("https://github.com/tamlok/vnote/issues");
  627. QDesktopServices::openUrl(url);
  628. });
  629. QAction *aboutAct = new QAction(tr("&About VNote"), this);
  630. aboutAct->setToolTip(tr("View information about VNote"));
  631. aboutAct->setMenuRole(QAction::AboutRole);
  632. connect(aboutAct, &QAction::triggered,
  633. this, &VMainWindow::aboutMessage);
  634. QAction *aboutQtAct = new QAction(tr("About &Qt"), this);
  635. aboutQtAct->setToolTip(tr("View information about Qt"));
  636. aboutQtAct->setMenuRole(QAction::AboutQtRole);
  637. connect(aboutQtAct, &QAction::triggered,
  638. qApp, &QApplication::aboutQt);
  639. helpMenu->addAction(shortcutAct);
  640. helpMenu->addAction(mdGuideAct);
  641. helpMenu->addAction(wikiAct);
  642. helpMenu->addAction(updateAct);
  643. helpMenu->addAction(starAct);
  644. helpMenu->addAction(feedbackAct);
  645. #if defined(QT_NO_DEBUG)
  646. helpMenu->addAction(logAct);
  647. #endif
  648. helpMenu->addAction(aboutQtAct);
  649. helpMenu->addAction(aboutAct);
  650. }
  651. void VMainWindow::initMarkdownMenu()
  652. {
  653. QMenu *markdownMenu = menuBar()->addMenu(tr("&Markdown"));
  654. markdownMenu->setToolTipsVisible(true);
  655. initConverterMenu(markdownMenu);
  656. initMarkdownitOptionMenu(markdownMenu);
  657. markdownMenu->addSeparator();
  658. initRenderStyleMenu(markdownMenu);
  659. initRenderBackgroundMenu(markdownMenu);
  660. initCodeBlockStyleMenu(markdownMenu);
  661. QAction *constrainImageAct = new QAction(tr("Constrain The Width of Images"), this);
  662. constrainImageAct->setToolTip(tr("Constrain the width of images to the window in read mode (re-open current tabs to make it work)"));
  663. constrainImageAct->setCheckable(true);
  664. connect(constrainImageAct, &QAction::triggered,
  665. this, &VMainWindow::enableImageConstraint);
  666. markdownMenu->addAction(constrainImageAct);
  667. constrainImageAct->setChecked(g_config->getEnableImageConstraint());
  668. QAction *imageCaptionAct = new QAction(tr("Enable Image Caption"), this);
  669. imageCaptionAct->setToolTip(tr("Center the images and display the alt text as caption (re-open current tabs to make it work)"));
  670. imageCaptionAct->setCheckable(true);
  671. connect(imageCaptionAct, &QAction::triggered,
  672. this, &VMainWindow::enableImageCaption);
  673. markdownMenu->addAction(imageCaptionAct);
  674. imageCaptionAct->setChecked(g_config->getEnableImageCaption());
  675. markdownMenu->addSeparator();
  676. QAction *mermaidAct = new QAction(tr("&Mermaid Diagram"), this);
  677. mermaidAct->setToolTip(tr("Enable Mermaid for graph and diagram"));
  678. mermaidAct->setCheckable(true);
  679. connect(mermaidAct, &QAction::triggered,
  680. this, &VMainWindow::enableMermaid);
  681. markdownMenu->addAction(mermaidAct);
  682. mermaidAct->setChecked(g_config->getEnableMermaid());
  683. QAction *flowchartAct = new QAction(tr("&Flowchart.js"), this);
  684. flowchartAct->setToolTip(tr("Enable Flowchart.js for flowchart diagram"));
  685. flowchartAct->setCheckable(true);
  686. connect(flowchartAct, &QAction::triggered,
  687. this, [this](bool p_enabled){
  688. g_config->setEnableFlowchart(p_enabled);
  689. });
  690. markdownMenu->addAction(flowchartAct);
  691. flowchartAct->setChecked(g_config->getEnableFlowchart());
  692. QAction *mathjaxAct = new QAction(tr("Math&Jax"), this);
  693. mathjaxAct->setToolTip(tr("Enable MathJax for math support in Markdown"));
  694. mathjaxAct->setCheckable(true);
  695. connect(mathjaxAct, &QAction::triggered,
  696. this, &VMainWindow::enableMathjax);
  697. markdownMenu->addAction(mathjaxAct);
  698. mathjaxAct->setChecked(g_config->getEnableMathjax());
  699. markdownMenu->addSeparator();
  700. QAction *codeBlockAct = new QAction(tr("Highlight Code Blocks In Edit Mode"), this);
  701. codeBlockAct->setToolTip(tr("Enable syntax highlight within code blocks in edit mode"));
  702. codeBlockAct->setCheckable(true);
  703. connect(codeBlockAct, &QAction::triggered,
  704. this, &VMainWindow::enableCodeBlockHighlight);
  705. markdownMenu->addAction(codeBlockAct);
  706. codeBlockAct->setChecked(g_config->getEnableCodeBlockHighlight());
  707. QAction *lineNumberAct = new QAction(tr("Display Line Number In Code Blocks"), this);
  708. lineNumberAct->setToolTip(tr("Enable line number in code blocks in read mode"));
  709. lineNumberAct->setCheckable(true);
  710. connect(lineNumberAct, &QAction::triggered,
  711. this, [this](bool p_checked){
  712. g_config->setEnableCodeBlockLineNumber(p_checked);
  713. });
  714. markdownMenu->addAction(lineNumberAct);
  715. lineNumberAct->setChecked(g_config->getEnableCodeBlockLineNumber());
  716. QAction *previewImageAct = new QAction(tr("Preview Images In Edit Mode"), this);
  717. previewImageAct->setToolTip(tr("Enable image preview in edit mode (re-open current tabs to make it work)"));
  718. previewImageAct->setCheckable(true);
  719. connect(previewImageAct, &QAction::triggered,
  720. this, &VMainWindow::enableImagePreview);
  721. markdownMenu->addAction(previewImageAct);
  722. previewImageAct->setChecked(g_config->getEnablePreviewImages());
  723. QAction *previewWidthAct = new QAction(tr("Constrain The Width Of Previewed Images"), this);
  724. previewWidthAct->setToolTip(tr("Constrain the width of previewed images to the edit window in edit mode"));
  725. previewWidthAct->setCheckable(true);
  726. connect(previewWidthAct, &QAction::triggered,
  727. this, &VMainWindow::enableImagePreviewConstraint);
  728. markdownMenu->addAction(previewWidthAct);
  729. previewWidthAct->setChecked(g_config->getEnablePreviewImageConstraint());
  730. }
  731. void VMainWindow::initViewMenu()
  732. {
  733. viewMenu = menuBar()->addMenu(tr("&View"));
  734. viewMenu->setToolTipsVisible(true);
  735. }
  736. void VMainWindow::initFileMenu()
  737. {
  738. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  739. fileMenu->setToolTipsVisible(true);
  740. // Open external files.
  741. QAction *openAct = new QAction(tr("&Open"), this);
  742. openAct->setToolTip(tr("Open external file to edit"));
  743. connect(openAct, &QAction::triggered,
  744. this, [this](){
  745. static QString lastPath = QDir::homePath();
  746. QStringList files = QFileDialog::getOpenFileNames(this,
  747. tr("Select External Files To Open"),
  748. lastPath);
  749. if (files.isEmpty()) {
  750. return;
  751. }
  752. // Update lastPath
  753. lastPath = QFileInfo(files[0]).path();
  754. openFiles(VUtils::filterFilePathsToOpen(files));
  755. });
  756. fileMenu->addAction(openAct);
  757. // Import notes from files.
  758. m_importNoteAct = newAction(VIconUtils::menuIcon(":/resources/icons/import_note.svg"),
  759. tr("&New Notes From Files"), this);
  760. m_importNoteAct->setToolTip(tr("Create notes from external files in current folder by copy"));
  761. connect(m_importNoteAct, &QAction::triggered,
  762. this, &VMainWindow::importNoteFromFile);
  763. m_importNoteAct->setEnabled(false);
  764. fileMenu->addAction(m_importNoteAct);
  765. fileMenu->addSeparator();
  766. // Export as PDF.
  767. m_exportAsPDFAct = new QAction(tr("Export As &PDF"), this);
  768. m_exportAsPDFAct->setToolTip(tr("Export current note as PDF file"));
  769. connect(m_exportAsPDFAct, &QAction::triggered,
  770. this, &VMainWindow::exportAsPDF);
  771. m_exportAsPDFAct->setEnabled(false);
  772. fileMenu->addAction(m_exportAsPDFAct);
  773. fileMenu->addSeparator();
  774. // Print.
  775. m_printAct = new QAction(VIconUtils::menuIcon(":/resources/icons/print.svg"),
  776. tr("&Print"), this);
  777. m_printAct->setToolTip(tr("Print current note"));
  778. connect(m_printAct, &QAction::triggered,
  779. this, &VMainWindow::printNote);
  780. m_printAct->setEnabled(false);
  781. // Themes.
  782. initThemeMenu(fileMenu);
  783. // Settings.
  784. QAction *settingsAct = newAction(VIconUtils::menuIcon(":/resources/icons/settings.svg"),
  785. tr("&Settings"), this);
  786. settingsAct->setToolTip(tr("View and change settings for VNote"));
  787. settingsAct->setMenuRole(QAction::PreferencesRole);
  788. connect(settingsAct, &QAction::triggered,
  789. this, &VMainWindow::viewSettings);
  790. fileMenu->addAction(settingsAct);
  791. QAction *openConfigAct = new QAction(tr("Open Configuration Folder"), this);
  792. openConfigAct->setToolTip(tr("Open configuration folder of VNote"));
  793. connect(openConfigAct, &QAction::triggered,
  794. this, [this](){
  795. QUrl url = QUrl::fromLocalFile(g_config->getConfigFolder());
  796. QDesktopServices::openUrl(url);
  797. });
  798. fileMenu->addAction(openConfigAct);
  799. QAction *customShortcutAct = new QAction(tr("Custom Shortcuts"), this);
  800. customShortcutAct->setToolTip(tr("Custom some standard shortcuts"));
  801. connect(customShortcutAct, &QAction::triggered,
  802. this, &VMainWindow::customShortcut);
  803. fileMenu->addAction(customShortcutAct);
  804. fileMenu->addSeparator();
  805. // Exit.
  806. QAction *exitAct = new QAction(tr("&Quit"), this);
  807. exitAct->setToolTip(tr("Quit VNote"));
  808. exitAct->setShortcut(QKeySequence("Ctrl+Q"));
  809. exitAct->setMenuRole(QAction::QuitRole);
  810. connect(exitAct, &QAction::triggered,
  811. this, &VMainWindow::quitApp);
  812. fileMenu->addAction(exitAct);
  813. }
  814. void VMainWindow::quitApp()
  815. {
  816. m_requestQuit = true;
  817. close();
  818. }
  819. void VMainWindow::initEditMenu()
  820. {
  821. QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
  822. editMenu->setToolTipsVisible(true);
  823. // Find/Replace.
  824. m_findReplaceAct = newAction(VIconUtils::menuIcon(":/resources/icons/find_replace.svg"),
  825. tr("Find/Replace"), this);
  826. m_findReplaceAct->setToolTip(tr("Open Find/Replace dialog to search in current note"));
  827. QString keySeq = g_config->getShortcutKeySequence("Find");
  828. qDebug() << "set Find shortcut to" << keySeq;
  829. m_findReplaceAct->setShortcut(QKeySequence(keySeq));
  830. connect(m_findReplaceAct, &QAction::triggered,
  831. this, &VMainWindow::openFindDialog);
  832. m_findNextAct = new QAction(tr("Find Next"), this);
  833. m_findNextAct->setToolTip(tr("Find next occurence"));
  834. keySeq = g_config->getShortcutKeySequence("FindNext");
  835. qDebug() << "set FindNext shortcut to" << keySeq;
  836. m_findNextAct->setShortcut(QKeySequence(keySeq));
  837. connect(m_findNextAct, SIGNAL(triggered(bool)),
  838. m_findReplaceDialog, SLOT(findNext()));
  839. m_findPreviousAct = new QAction(tr("Find Previous"), this);
  840. m_findPreviousAct->setToolTip(tr("Find previous occurence"));
  841. keySeq = g_config->getShortcutKeySequence("FindPrevious");
  842. qDebug() << "set FindPrevious shortcut to" << keySeq;
  843. m_findPreviousAct->setShortcut(QKeySequence(keySeq));
  844. connect(m_findPreviousAct, SIGNAL(triggered(bool)),
  845. m_findReplaceDialog, SLOT(findPrevious()));
  846. m_replaceAct = new QAction(tr("Replace"), this);
  847. m_replaceAct->setToolTip(tr("Replace current occurence"));
  848. connect(m_replaceAct, SIGNAL(triggered(bool)),
  849. m_findReplaceDialog, SLOT(replace()));
  850. m_replaceFindAct = new QAction(tr("Replace && Find"), this);
  851. m_replaceFindAct->setToolTip(tr("Replace current occurence and find the next one"));
  852. connect(m_replaceFindAct, SIGNAL(triggered(bool)),
  853. m_findReplaceDialog, SLOT(replaceFind()));
  854. m_replaceAllAct = new QAction(tr("Replace All"), this);
  855. m_replaceAllAct->setToolTip(tr("Replace all occurences in current note"));
  856. connect(m_replaceAllAct, SIGNAL(triggered(bool)),
  857. m_findReplaceDialog, SLOT(replaceAll()));
  858. QAction *searchedWordAct = new QAction(tr("Highlight Searched Pattern"), this);
  859. searchedWordAct->setToolTip(tr("Highlight all occurences of searched pattern"));
  860. searchedWordAct->setCheckable(true);
  861. connect(searchedWordAct, &QAction::triggered,
  862. this, &VMainWindow::changeHighlightSearchedWord);
  863. // Expand Tab into spaces.
  864. QAction *expandTabAct = new QAction(tr("&Expand Tab"), this);
  865. expandTabAct->setToolTip(tr("Expand entered Tab to spaces"));
  866. expandTabAct->setCheckable(true);
  867. connect(expandTabAct, &QAction::triggered,
  868. this, &VMainWindow::changeExpandTab);
  869. // Tab stop width.
  870. QActionGroup *tabStopWidthAct = new QActionGroup(this);
  871. QAction *twoSpaceTabAct = new QAction(tr("2 Spaces"), tabStopWidthAct);
  872. twoSpaceTabAct->setToolTip(tr("Expand Tab to 2 spaces"));
  873. twoSpaceTabAct->setCheckable(true);
  874. twoSpaceTabAct->setData(2);
  875. QAction *fourSpaceTabAct = new QAction(tr("4 Spaces"), tabStopWidthAct);
  876. fourSpaceTabAct->setToolTip(tr("Expand Tab to 4 spaces"));
  877. fourSpaceTabAct->setCheckable(true);
  878. fourSpaceTabAct->setData(4);
  879. QAction *eightSpaceTabAct = new QAction(tr("8 Spaces"), tabStopWidthAct);
  880. eightSpaceTabAct->setToolTip(tr("Expand Tab to 8 spaces"));
  881. eightSpaceTabAct->setCheckable(true);
  882. eightSpaceTabAct->setData(8);
  883. connect(tabStopWidthAct, &QActionGroup::triggered,
  884. this, &VMainWindow::setTabStopWidth);
  885. // Auto Indent.
  886. m_autoIndentAct = new QAction(tr("Auto Indent"), this);
  887. m_autoIndentAct->setToolTip(tr("Indent automatically when inserting a new line"));
  888. m_autoIndentAct->setCheckable(true);
  889. connect(m_autoIndentAct, &QAction::triggered,
  890. this, &VMainWindow::changeAutoIndent);
  891. // Auto List.
  892. QAction *autoListAct = new QAction(tr("Auto List"), this);
  893. autoListAct->setToolTip(tr("Continue the list automatically when inserting a new line"));
  894. autoListAct->setCheckable(true);
  895. connect(autoListAct, &QAction::triggered,
  896. this, &VMainWindow::changeAutoList);
  897. // Vim Mode.
  898. QAction *vimAct = new QAction(tr("Vim Mode"), this);
  899. vimAct->setToolTip(tr("Enable Vim mode for editing (re-open current tabs to make it work)"));
  900. vimAct->setCheckable(true);
  901. connect(vimAct, &QAction::triggered,
  902. this, &VMainWindow::changeVimMode);
  903. // Smart input method in Vim mode.
  904. QAction *smartImAct = new QAction(tr("Smart Input Method In Vim Mode"), this);
  905. smartImAct->setToolTip(tr("Disable input method when leaving Insert mode in Vim mode"));
  906. smartImAct->setCheckable(true);
  907. connect(smartImAct, &QAction::triggered,
  908. this, [this](bool p_checked){
  909. g_config->setEnableSmartImInVimMode(p_checked);
  910. });
  911. // Highlight current cursor line.
  912. QAction *cursorLineAct = new QAction(tr("Highlight Cursor Line"), this);
  913. cursorLineAct->setToolTip(tr("Highlight current cursor line"));
  914. cursorLineAct->setCheckable(true);
  915. connect(cursorLineAct, &QAction::triggered,
  916. this, &VMainWindow::changeHighlightCursorLine);
  917. // Highlight selected word.
  918. QAction *selectedWordAct = new QAction(tr("Highlight Selected Words"), this);
  919. selectedWordAct->setToolTip(tr("Highlight all occurences of selected words"));
  920. selectedWordAct->setCheckable(true);
  921. connect(selectedWordAct, &QAction::triggered,
  922. this, &VMainWindow::changeHighlightSelectedWord);
  923. // Highlight trailing space.
  924. QAction *trailingSapceAct = new QAction(tr("Highlight Trailing Sapces"), this);
  925. trailingSapceAct->setToolTip(tr("Highlight all the spaces at the end of a line"));
  926. trailingSapceAct->setCheckable(true);
  927. connect(trailingSapceAct, &QAction::triggered,
  928. this, &VMainWindow::changeHighlightTrailingSapce);
  929. QMenu *findReplaceMenu = editMenu->addMenu(tr("Find/Replace"));
  930. findReplaceMenu->setToolTipsVisible(true);
  931. findReplaceMenu->addAction(m_findReplaceAct);
  932. findReplaceMenu->addAction(m_findNextAct);
  933. findReplaceMenu->addAction(m_findPreviousAct);
  934. findReplaceMenu->addAction(m_replaceAct);
  935. findReplaceMenu->addAction(m_replaceFindAct);
  936. findReplaceMenu->addAction(m_replaceAllAct);
  937. findReplaceMenu->addSeparator();
  938. findReplaceMenu->addAction(searchedWordAct);
  939. searchedWordAct->setChecked(g_config->getHighlightSearchedWord());
  940. m_findReplaceAct->setEnabled(false);
  941. m_findNextAct->setEnabled(false);
  942. m_findPreviousAct->setEnabled(false);
  943. m_replaceAct->setEnabled(false);
  944. m_replaceFindAct->setEnabled(false);
  945. m_replaceAllAct->setEnabled(false);
  946. editMenu->addSeparator();
  947. editMenu->addAction(expandTabAct);
  948. if (g_config->getIsExpandTab()) {
  949. expandTabAct->setChecked(true);
  950. } else {
  951. expandTabAct->setChecked(false);
  952. }
  953. QMenu *tabStopWidthMenu = editMenu->addMenu(tr("Tab Stop Width"));
  954. tabStopWidthMenu->setToolTipsVisible(true);
  955. tabStopWidthMenu->addAction(twoSpaceTabAct);
  956. tabStopWidthMenu->addAction(fourSpaceTabAct);
  957. tabStopWidthMenu->addAction(eightSpaceTabAct);
  958. int tabStopWidth = g_config->getTabStopWidth();
  959. switch (tabStopWidth) {
  960. case 2:
  961. twoSpaceTabAct->setChecked(true);
  962. break;
  963. case 4:
  964. fourSpaceTabAct->setChecked(true);
  965. break;
  966. case 8:
  967. eightSpaceTabAct->setChecked(true);
  968. break;
  969. default:
  970. qWarning() << "unsupported tab stop width" << tabStopWidth << "in config";
  971. }
  972. editMenu->addAction(m_autoIndentAct);
  973. m_autoIndentAct->setChecked(g_config->getAutoIndent());
  974. editMenu->addAction(autoListAct);
  975. if (g_config->getAutoList()) {
  976. // Let the trigger handler to trigger m_autoIndentAct, too.
  977. autoListAct->trigger();
  978. }
  979. Q_ASSERT(!(autoListAct->isChecked() && !m_autoIndentAct->isChecked()));
  980. editMenu->addAction(vimAct);
  981. vimAct->setChecked(g_config->getEnableVimMode());
  982. editMenu->addAction(smartImAct);
  983. smartImAct->setChecked(g_config->getEnableSmartImInVimMode());
  984. editMenu->addSeparator();
  985. initEditorStyleMenu(editMenu);
  986. initEditorBackgroundMenu(editMenu);
  987. initEditorLineNumberMenu(editMenu);
  988. editMenu->addAction(cursorLineAct);
  989. cursorLineAct->setChecked(g_config->getHighlightCursorLine());
  990. editMenu->addAction(selectedWordAct);
  991. selectedWordAct->setChecked(g_config->getHighlightSelectedWord());
  992. editMenu->addAction(trailingSapceAct);
  993. trailingSapceAct->setChecked(g_config->getEnableTrailingSpaceHighlight());
  994. }
  995. void VMainWindow::initDockWindows()
  996. {
  997. toolDock = new QDockWidget(tr("Tools"), this);
  998. toolDock->setObjectName("ToolsDock");
  999. toolDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
  1000. // Outline tree.
  1001. outline = new VOutline(this);
  1002. connect(editArea, &VEditArea::outlineChanged,
  1003. outline, &VOutline::updateOutline);
  1004. connect(editArea, &VEditArea::currentHeaderChanged,
  1005. outline, &VOutline::updateCurrentHeader);
  1006. connect(outline, &VOutline::outlineItemActivated,
  1007. editArea, &VEditArea::scrollToHeader);
  1008. // Snippets.
  1009. m_snippetList = new VSnippetList(this);
  1010. m_toolBox = new VToolBox(this);
  1011. m_toolBox->addItem(outline,
  1012. ":/resources/icons/outline.svg",
  1013. tr("Outline"));
  1014. m_toolBox->addItem(m_snippetList,
  1015. ":/resources/icons/snippets.svg",
  1016. tr("Snippets"));
  1017. toolDock->setWidget(m_toolBox);
  1018. addDockWidget(Qt::RightDockWidgetArea, toolDock);
  1019. QAction *toggleAct = toolDock->toggleViewAction();
  1020. toggleAct->setToolTip(tr("Toggle the tools dock widget"));
  1021. viewMenu->addAction(toggleAct);
  1022. }
  1023. void VMainWindow::initAvatar()
  1024. {
  1025. m_avatar = new VAvatar(this);
  1026. m_avatar->setAvatarPixmap(":/resources/icons/vnote.svg");
  1027. m_avatar->setColor(g_palette->color("avatar_border_bg"),
  1028. g_palette->color("avatar_fg"),
  1029. g_palette->color("avatar_bg"));
  1030. m_avatar->hide();
  1031. }
  1032. void VMainWindow::importNoteFromFile()
  1033. {
  1034. static QString lastPath = QDir::homePath();
  1035. QStringList files = QFileDialog::getOpenFileNames(this,
  1036. tr("Select Files To Create Notes"),
  1037. lastPath);
  1038. if (files.isEmpty()) {
  1039. return;
  1040. }
  1041. // Update lastPath
  1042. lastPath = QFileInfo(files[0]).path();
  1043. QString msg;
  1044. if (!m_fileList->importFiles(files, &msg)) {
  1045. VUtils::showMessage(QMessageBox::Warning,
  1046. tr("Warning"),
  1047. tr("Fail to create notes for all the files."),
  1048. msg,
  1049. QMessageBox::Ok,
  1050. QMessageBox::Ok,
  1051. this);
  1052. } else {
  1053. int cnt = files.size();
  1054. showStatusMessage(tr("%1 %2 created from external files")
  1055. .arg(cnt)
  1056. .arg(cnt > 1 ? tr("notes") : tr("note")));
  1057. }
  1058. }
  1059. void VMainWindow::changeMarkdownConverter(QAction *action)
  1060. {
  1061. if (!action) {
  1062. return;
  1063. }
  1064. MarkdownConverterType type = (MarkdownConverterType)action->data().toInt();
  1065. qDebug() << "switch to converter" << type;
  1066. g_config->setMarkdownConverterType(type);
  1067. }
  1068. void VMainWindow::aboutMessage()
  1069. {
  1070. QString info = tr("<span style=\"font-weight: bold;\">v%1</span>").arg(VConfigManager::c_version);
  1071. info += "<br/><br/>";
  1072. info += tr("VNote is a Vim-inspired note-taking application for Markdown.");
  1073. info += "<br/>";
  1074. info += tr("Please visit <a href=\"https://github.com/tamlok/vnote.git\">Github</a> for more information.");
  1075. QMessageBox::about(this, tr("About VNote"), info);
  1076. }
  1077. void VMainWindow::changeExpandTab(bool checked)
  1078. {
  1079. g_config->setIsExpandTab(checked);
  1080. }
  1081. void VMainWindow::enableMermaid(bool p_checked)
  1082. {
  1083. g_config->setEnableMermaid(p_checked);
  1084. }
  1085. void VMainWindow::enableMathjax(bool p_checked)
  1086. {
  1087. g_config->setEnableMathjax(p_checked);
  1088. }
  1089. void VMainWindow::changeHighlightCursorLine(bool p_checked)
  1090. {
  1091. g_config->setHighlightCursorLine(p_checked);
  1092. }
  1093. void VMainWindow::changeHighlightSelectedWord(bool p_checked)
  1094. {
  1095. g_config->setHighlightSelectedWord(p_checked);
  1096. }
  1097. void VMainWindow::changeHighlightSearchedWord(bool p_checked)
  1098. {
  1099. g_config->setHighlightSearchedWord(p_checked);
  1100. }
  1101. void VMainWindow::changeHighlightTrailingSapce(bool p_checked)
  1102. {
  1103. g_config->setEnableTrailingSapceHighlight(p_checked);
  1104. }
  1105. void VMainWindow::setTabStopWidth(QAction *action)
  1106. {
  1107. if (!action) {
  1108. return;
  1109. }
  1110. g_config->setTabStopWidth(action->data().toInt());
  1111. }
  1112. void VMainWindow::setEditorBackgroundColor(QAction *action)
  1113. {
  1114. if (!action) {
  1115. return;
  1116. }
  1117. g_config->setCurBackgroundColor(action->data().toString());
  1118. }
  1119. void VMainWindow::initPredefinedColorPixmaps()
  1120. {
  1121. const QVector<VColor> &bgColors = g_config->getPredefinedColors();
  1122. predefinedColorPixmaps.clear();
  1123. int size = 256;
  1124. for (int i = 0; i < bgColors.size(); ++i) {
  1125. // Generate QPixmap filled in this color
  1126. QColor color(VUtils::QRgbFromString(bgColors[i].rgb));
  1127. QPixmap pixmap(size, size);
  1128. pixmap.fill(color);
  1129. predefinedColorPixmaps.append(pixmap);
  1130. }
  1131. }
  1132. void VMainWindow::initConverterMenu(QMenu *p_menu)
  1133. {
  1134. QMenu *converterMenu = p_menu->addMenu(tr("&Converter"));
  1135. converterMenu->setToolTipsVisible(true);
  1136. QActionGroup *converterAct = new QActionGroup(this);
  1137. QAction *markedAct = new QAction(tr("Marked"), converterAct);
  1138. markedAct->setToolTip(tr("Use Marked to convert Markdown to HTML (re-open current tabs to make it work)"));
  1139. markedAct->setCheckable(true);
  1140. markedAct->setData(int(MarkdownConverterType::Marked));
  1141. QAction *hoedownAct = new QAction(tr("Hoedown"), converterAct);
  1142. hoedownAct->setToolTip(tr("Use Hoedown to convert Markdown to HTML (re-open current tabs to make it work)"));
  1143. hoedownAct->setCheckable(true);
  1144. hoedownAct->setData(int(MarkdownConverterType::Hoedown));
  1145. QAction *markdownitAct = new QAction(tr("Markdown-it"), converterAct);
  1146. markdownitAct->setToolTip(tr("Use Markdown-it to convert Markdown to HTML (re-open current tabs to make it work)"));
  1147. markdownitAct->setCheckable(true);
  1148. markdownitAct->setData(int(MarkdownConverterType::MarkdownIt));
  1149. QAction *showdownAct = new QAction(tr("Showdown"), converterAct);
  1150. showdownAct->setToolTip(tr("Use Showdown to convert Markdown to HTML (re-open current tabs to make it work)"));
  1151. showdownAct->setCheckable(true);
  1152. showdownAct->setData(int(MarkdownConverterType::Showdown));
  1153. connect(converterAct, &QActionGroup::triggered,
  1154. this, &VMainWindow::changeMarkdownConverter);
  1155. converterMenu->addAction(hoedownAct);
  1156. converterMenu->addAction(markedAct);
  1157. converterMenu->addAction(markdownitAct);
  1158. converterMenu->addAction(showdownAct);
  1159. MarkdownConverterType converterType = g_config->getMdConverterType();
  1160. switch (converterType) {
  1161. case MarkdownConverterType::Marked:
  1162. markedAct->setChecked(true);
  1163. break;
  1164. case MarkdownConverterType::Hoedown:
  1165. hoedownAct->setChecked(true);
  1166. break;
  1167. case MarkdownConverterType::MarkdownIt:
  1168. markdownitAct->setChecked(true);
  1169. break;
  1170. case MarkdownConverterType::Showdown:
  1171. showdownAct->setChecked(true);
  1172. break;
  1173. default:
  1174. Q_ASSERT(false);
  1175. }
  1176. }
  1177. void VMainWindow::initMarkdownitOptionMenu(QMenu *p_menu)
  1178. {
  1179. QMenu *optMenu = p_menu->addMenu(tr("Markdown-it Options"));
  1180. optMenu->setToolTipsVisible(true);
  1181. MarkdownitOption opt = g_config->getMarkdownitOption();
  1182. QAction *htmlAct = new QAction(tr("HTML"), this);
  1183. htmlAct->setToolTip(tr("Enable HTML tags in source"));
  1184. htmlAct->setCheckable(true);
  1185. htmlAct->setChecked(opt.m_html);
  1186. connect(htmlAct, &QAction::triggered,
  1187. this, [this](bool p_checked) {
  1188. MarkdownitOption opt = g_config->getMarkdownitOption();
  1189. opt.m_html = p_checked;
  1190. g_config->setMarkdownitOption(opt);
  1191. });
  1192. QAction *breaksAct = new QAction(tr("Line Break"), this);
  1193. breaksAct->setToolTip(tr("Convert '\\n' in paragraphs into line break"));
  1194. breaksAct->setCheckable(true);
  1195. breaksAct->setChecked(opt.m_breaks);
  1196. connect(breaksAct, &QAction::triggered,
  1197. this, [this](bool p_checked) {
  1198. MarkdownitOption opt = g_config->getMarkdownitOption();
  1199. opt.m_breaks = p_checked;
  1200. g_config->setMarkdownitOption(opt);
  1201. });
  1202. QAction *linkifyAct = new QAction(tr("Linkify"), this);
  1203. linkifyAct->setToolTip(tr("Convert URL-like text into links"));
  1204. linkifyAct->setCheckable(true);
  1205. linkifyAct->setChecked(opt.m_linkify);
  1206. connect(linkifyAct, &QAction::triggered,
  1207. this, [this](bool p_checked) {
  1208. MarkdownitOption opt = g_config->getMarkdownitOption();
  1209. opt.m_linkify = p_checked;
  1210. g_config->setMarkdownitOption(opt);
  1211. });
  1212. optMenu->addAction(htmlAct);
  1213. optMenu->addAction(breaksAct);
  1214. optMenu->addAction(linkifyAct);
  1215. }
  1216. void VMainWindow::initRenderBackgroundMenu(QMenu *menu)
  1217. {
  1218. QActionGroup *renderBackgroundAct = new QActionGroup(this);
  1219. connect(renderBackgroundAct, &QActionGroup::triggered,
  1220. this, &VMainWindow::setRenderBackgroundColor);
  1221. QMenu *renderBgMenu = menu->addMenu(tr("&Rendering Background"));
  1222. renderBgMenu->setToolTipsVisible(true);
  1223. const QString &curBgColor = g_config->getCurRenderBackgroundColor();
  1224. QAction *tmpAct = new QAction(tr("System"), renderBackgroundAct);
  1225. tmpAct->setToolTip(tr("Use system's background color configuration for Markdown rendering"));
  1226. tmpAct->setCheckable(true);
  1227. tmpAct->setData("System");
  1228. if (curBgColor == "System") {
  1229. tmpAct->setChecked(true);
  1230. }
  1231. renderBgMenu->addAction(tmpAct);
  1232. const QVector<VColor> &bgColors = g_config->getPredefinedColors();
  1233. for (int i = 0; i < bgColors.size(); ++i) {
  1234. tmpAct = new QAction(bgColors[i].name, renderBackgroundAct);
  1235. tmpAct->setToolTip(tr("Set as the background color for Markdown rendering"));
  1236. tmpAct->setCheckable(true);
  1237. tmpAct->setData(bgColors[i].name);
  1238. #if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
  1239. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  1240. #endif
  1241. if (curBgColor == bgColors[i].name) {
  1242. tmpAct->setChecked(true);
  1243. }
  1244. renderBgMenu->addAction(tmpAct);
  1245. }
  1246. }
  1247. void VMainWindow::initRenderStyleMenu(QMenu *p_menu)
  1248. {
  1249. QMenu *styleMenu = p_menu->addMenu(tr("Rendering &Style"));
  1250. styleMenu->setToolTipsVisible(true);
  1251. QAction *addAct = newAction(VIconUtils::menuIcon(":/resources/icons/add_style.svg"),
  1252. tr("Add Style"),
  1253. styleMenu);
  1254. addAct->setToolTip(tr("Add custom style of read mode"));
  1255. connect(addAct, &QAction::triggered,
  1256. this, [this]() {
  1257. VTipsDialog dialog(VUtils::getDocFile("tips_add_style.md"),
  1258. tr("Add Style"),
  1259. []() {
  1260. QUrl url = QUrl::fromLocalFile(g_config->getStyleConfigFolder());
  1261. QDesktopServices::openUrl(url);
  1262. },
  1263. this);
  1264. dialog.exec();
  1265. });
  1266. styleMenu->addAction(addAct);
  1267. QActionGroup *ag = new QActionGroup(this);
  1268. connect(ag, &QActionGroup::triggered,
  1269. this, [this](QAction *p_action) {
  1270. if (!p_action) {
  1271. return;
  1272. }
  1273. QString data = p_action->data().toString();
  1274. g_config->setCssStyle(data);
  1275. vnote->updateTemplate();
  1276. });
  1277. QList<QString> styles = g_config->getCssStyles();
  1278. QString curStyle = g_config->getCssStyle();
  1279. for (auto const &style : styles) {
  1280. QAction *act = new QAction(style, ag);
  1281. act->setToolTip(tr("Set as the CSS style for Markdown rendering "
  1282. "(re-open current tabs to make it work)"));
  1283. act->setCheckable(true);
  1284. act->setData(style);
  1285. // Add it to the menu.
  1286. styleMenu->addAction(act);
  1287. if (curStyle == style) {
  1288. act->setChecked(true);
  1289. }
  1290. }
  1291. }
  1292. void VMainWindow::initCodeBlockStyleMenu(QMenu *p_menu)
  1293. {
  1294. QMenu *styleMenu = p_menu->addMenu(tr("Code Block Style"));
  1295. styleMenu->setToolTipsVisible(true);
  1296. QAction *addAct = newAction(VIconUtils::menuIcon(":/resources/icons/add_style.svg"),
  1297. tr("Add Style"),
  1298. styleMenu);
  1299. addAct->setToolTip(tr("Add custom style of code block in read mode"));
  1300. connect(addAct, &QAction::triggered,
  1301. this, [this]() {
  1302. VTipsDialog dialog(VUtils::getDocFile("tips_add_style.md"),
  1303. tr("Add Style"),
  1304. []() {
  1305. QUrl url = QUrl::fromLocalFile(g_config->getCodeBlockStyleConfigFolder());
  1306. QDesktopServices::openUrl(url);
  1307. },
  1308. this);
  1309. dialog.exec();
  1310. });
  1311. styleMenu->addAction(addAct);
  1312. QActionGroup *ag = new QActionGroup(this);
  1313. connect(ag, &QActionGroup::triggered,
  1314. this, [this](QAction *p_action) {
  1315. if (!p_action) {
  1316. return;
  1317. }
  1318. QString data = p_action->data().toString();
  1319. g_config->setCodeBlockCssStyle(data);
  1320. vnote->updateTemplate();
  1321. });
  1322. QList<QString> styles = g_config->getCodeBlockCssStyles();
  1323. QString curStyle = g_config->getCodeBlockCssStyle();
  1324. for (auto const &style : styles) {
  1325. QAction *act = new QAction(style, ag);
  1326. act->setToolTip(tr("Set as the code block CSS style for Markdown rendering "
  1327. "(re-open current tabs to make it work)"));
  1328. act->setCheckable(true);
  1329. act->setData(style);
  1330. // Add it to the menu.
  1331. styleMenu->addAction(act);
  1332. if (curStyle == style) {
  1333. act->setChecked(true);
  1334. }
  1335. }
  1336. }
  1337. void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
  1338. {
  1339. QMenu *backgroundColorMenu = menu->addMenu(tr("&Background Color"));
  1340. backgroundColorMenu->setToolTipsVisible(true);
  1341. QActionGroup *backgroundColorAct = new QActionGroup(this);
  1342. connect(backgroundColorAct, &QActionGroup::triggered,
  1343. this, &VMainWindow::setEditorBackgroundColor);
  1344. // System background color
  1345. const QString &curBgColor = g_config->getCurBackgroundColor();
  1346. QAction *tmpAct = new QAction(tr("System"), backgroundColorAct);
  1347. tmpAct->setToolTip(tr("Use system's background color configuration for editor"));
  1348. tmpAct->setCheckable(true);
  1349. tmpAct->setData("System");
  1350. if (curBgColor == "System") {
  1351. tmpAct->setChecked(true);
  1352. }
  1353. backgroundColorMenu->addAction(tmpAct);
  1354. const QVector<VColor> &bgColors = g_config->getPredefinedColors();
  1355. for (int i = 0; i < bgColors.size(); ++i) {
  1356. tmpAct = new QAction(bgColors[i].name, backgroundColorAct);
  1357. tmpAct->setToolTip(tr("Set as the background color for editor"));
  1358. tmpAct->setCheckable(true);
  1359. tmpAct->setData(bgColors[i].name);
  1360. #if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
  1361. tmpAct->setIcon(QIcon(predefinedColorPixmaps[i]));
  1362. #endif
  1363. if (curBgColor == bgColors[i].name) {
  1364. tmpAct->setChecked(true);
  1365. }
  1366. backgroundColorMenu->addAction(tmpAct);
  1367. }
  1368. }
  1369. void VMainWindow::initEditorLineNumberMenu(QMenu *p_menu)
  1370. {
  1371. QMenu *lineNumMenu = p_menu->addMenu(tr("Line Number"));
  1372. lineNumMenu->setToolTipsVisible(true);
  1373. QActionGroup *lineNumAct = new QActionGroup(lineNumMenu);
  1374. connect(lineNumAct, &QActionGroup::triggered,
  1375. this, [this](QAction *p_action){
  1376. if (!p_action) {
  1377. return;
  1378. }
  1379. g_config->setEditorLineNumber(p_action->data().toInt());
  1380. emit editorConfigUpdated();
  1381. });
  1382. int lineNumberMode = g_config->getEditorLineNumber();
  1383. QAction *act = lineNumAct->addAction(tr("None"));
  1384. act->setToolTip(tr("Do not display line number in edit mode"));
  1385. act->setCheckable(true);
  1386. act->setData(0);
  1387. lineNumMenu->addAction(act);
  1388. if (lineNumberMode == 0) {
  1389. act->setChecked(true);
  1390. }
  1391. act = lineNumAct->addAction(tr("Absolute"));
  1392. act->setToolTip(tr("Display absolute line number in edit mode"));
  1393. act->setCheckable(true);
  1394. act->setData(1);
  1395. lineNumMenu->addAction(act);
  1396. if (lineNumberMode == 1) {
  1397. act->setChecked(true);
  1398. }
  1399. act = lineNumAct->addAction(tr("Relative"));
  1400. act->setToolTip(tr("Display line number relative to current cursor line in edit mode"));
  1401. act->setCheckable(true);
  1402. act->setData(2);
  1403. lineNumMenu->addAction(act);
  1404. if (lineNumberMode == 2) {
  1405. act->setChecked(true);
  1406. }
  1407. act = lineNumAct->addAction(tr("CodeBlock"));
  1408. act->setToolTip(tr("Display line number in code block in edit mode (for Markdown only)"));
  1409. act->setCheckable(true);
  1410. act->setData(3);
  1411. lineNumMenu->addAction(act);
  1412. if (lineNumberMode == 3) {
  1413. act->setChecked(true);
  1414. }
  1415. }
  1416. void VMainWindow::initEditorStyleMenu(QMenu *p_menu)
  1417. {
  1418. QMenu *styleMenu = p_menu->addMenu(tr("Editor &Style"));
  1419. styleMenu->setToolTipsVisible(true);
  1420. QAction *addAct = newAction(VIconUtils::menuIcon(":/resources/icons/add_style.svg"),
  1421. tr("Add Style"),
  1422. styleMenu);
  1423. addAct->setToolTip(tr("Add custom style of editor"));
  1424. connect(addAct, &QAction::triggered,
  1425. this, [this]() {
  1426. VTipsDialog dialog(VUtils::getDocFile("tips_add_style.md"),
  1427. tr("Add Style"),
  1428. []() {
  1429. QUrl url = QUrl::fromLocalFile(g_config->getStyleConfigFolder());
  1430. QDesktopServices::openUrl(url);
  1431. },
  1432. this);
  1433. dialog.exec();
  1434. });
  1435. styleMenu->addAction(addAct);
  1436. QActionGroup *ag = new QActionGroup(this);
  1437. connect(ag, &QActionGroup::triggered,
  1438. this, [this](QAction *p_action) {
  1439. if (!p_action) {
  1440. return;
  1441. }
  1442. QString data = p_action->data().toString();
  1443. g_config->setEditorStyle(data);
  1444. });
  1445. QList<QString> styles = g_config->getEditorStyles();
  1446. QString style = g_config->getEditorStyle();
  1447. for (auto const &item : styles) {
  1448. QAction *act = new QAction(item, ag);
  1449. act->setToolTip(tr("Set as the editor style (re-open current tabs to make it work)"));
  1450. act->setCheckable(true);
  1451. act->setData(item);
  1452. // Add it to the menu.
  1453. styleMenu->addAction(act);
  1454. if (style == item) {
  1455. act->setChecked(true);
  1456. }
  1457. }
  1458. }
  1459. void VMainWindow::setRenderBackgroundColor(QAction *action)
  1460. {
  1461. if (!action) {
  1462. return;
  1463. }
  1464. g_config->setCurRenderBackgroundColor(action->data().toString());
  1465. vnote->updateTemplate();
  1466. }
  1467. void VMainWindow::updateActionsStateFromTab(const VEditTab *p_tab)
  1468. {
  1469. const VFile *file = p_tab ? p_tab->getFile() : NULL;
  1470. bool editMode = p_tab ? p_tab->isEditMode() : false;
  1471. bool systemFile = file
  1472. && file->getType() == FileType::Orphan
  1473. && dynamic_cast<const VOrphanFile *>(file)->isSystemFile();
  1474. m_printAct->setEnabled(file && file->getDocType() == DocType::Markdown);
  1475. m_exportAsPDFAct->setEnabled(file && file->getDocType() == DocType::Markdown);
  1476. discardExitAct->setVisible(file && editMode);
  1477. saveExitAct->setVisible(file && editMode);
  1478. editNoteAct->setEnabled(file && !editMode);
  1479. editNoteAct->setVisible(!saveExitAct->isVisible());
  1480. saveNoteAct->setEnabled(file && editMode && file->isModifiable());
  1481. deleteNoteAct->setEnabled(file && file->getType() == FileType::Note);
  1482. noteInfoAct->setEnabled(file && !systemFile);
  1483. m_attachmentBtn->setEnabled(file && file->getType() == FileType::Note);
  1484. m_headingBtn->setEnabled(file && editMode);
  1485. setActionsEnabled(m_editToolBar, file && editMode);
  1486. // Handle heading sequence act independently.
  1487. m_headingSequenceAct->setEnabled(isHeadingSequenceApplicable());
  1488. const VMdTab *mdTab = dynamic_cast<const VMdTab *>(p_tab);
  1489. m_headingSequenceAct->setChecked(mdTab && mdTab->isHeadingSequenceEnabled());
  1490. // Find/Replace
  1491. m_findReplaceAct->setEnabled(file);
  1492. m_findNextAct->setEnabled(file);
  1493. m_findPreviousAct->setEnabled(file);
  1494. m_replaceAct->setEnabled(file && editMode);
  1495. m_replaceFindAct->setEnabled(file && editMode);
  1496. m_replaceAllAct->setEnabled(file && editMode);
  1497. if (!file) {
  1498. m_findReplaceDialog->closeDialog();
  1499. }
  1500. }
  1501. void VMainWindow::handleAreaTabStatusUpdated(const VEditTabInfo &p_info)
  1502. {
  1503. m_curTab = p_info.m_editTab;
  1504. if (m_curTab) {
  1505. m_curFile = m_curTab->getFile();
  1506. } else {
  1507. m_curFile = NULL;
  1508. }
  1509. if (p_info.m_type == VEditTabInfo::InfoType::All) {
  1510. updateActionsStateFromTab(m_curTab);
  1511. m_attachmentList->setFile(dynamic_cast<VNoteFile *>(m_curFile.data()));
  1512. QString title;
  1513. if (m_curFile) {
  1514. m_findReplaceDialog->updateState(m_curFile->getDocType(),
  1515. m_curTab->isEditMode());
  1516. if (m_curFile->getType() == FileType::Note) {
  1517. const VNoteFile *tmpFile = dynamic_cast<const VNoteFile *>((VFile *)m_curFile);
  1518. title = QString("[%1] %2").arg(tmpFile->getNotebookName()).arg(tmpFile->fetchPath());
  1519. } else {
  1520. title = QString("%1").arg(m_curFile->fetchPath());
  1521. }
  1522. if (!m_curFile->isModifiable()) {
  1523. title.append('#');
  1524. }
  1525. if (m_curTab->isModified()) {
  1526. title.append('*');
  1527. }
  1528. }
  1529. updateWindowTitle(title);
  1530. }
  1531. updateStatusInfo(p_info);
  1532. }
  1533. void VMainWindow::onePanelView()
  1534. {
  1535. m_panelViewState = PanelViewState::SinglePanel;
  1536. g_config->setEnableCompactMode(false);
  1537. changePanelView(m_panelViewState);
  1538. }
  1539. void VMainWindow::twoPanelView()
  1540. {
  1541. m_panelViewState = PanelViewState::TwoPanels;
  1542. g_config->setEnableCompactMode(false);
  1543. changePanelView(m_panelViewState);
  1544. }
  1545. void VMainWindow::compactModeView()
  1546. {
  1547. m_panelViewState = PanelViewState::CompactMode;
  1548. g_config->setEnableCompactMode(true);
  1549. changePanelView(m_panelViewState);
  1550. }
  1551. void VMainWindow::enableCompactMode(bool p_enabled)
  1552. {
  1553. const int fileListIdx = 1;
  1554. bool isCompactMode = m_naviSplitter->indexOf(m_fileList) != -1;
  1555. if (p_enabled) {
  1556. // Change to compact mode.
  1557. if (isCompactMode) {
  1558. return;
  1559. }
  1560. // Take m_fileList out of m_mainSplitter.
  1561. QWidget *tmpWidget = new QWidget(this);
  1562. Q_ASSERT(fileListIdx == m_mainSplitter->indexOf(m_fileList));
  1563. m_fileList->hide();
  1564. m_mainSplitter->replaceWidget(fileListIdx, tmpWidget);
  1565. tmpWidget->hide();
  1566. // Insert m_fileList into m_naviSplitter.
  1567. QWidget *wid = m_naviSplitter->replaceWidget(fileListIdx, m_fileList);
  1568. delete wid;
  1569. m_fileList->show();
  1570. } else {
  1571. // Disable compact mode and go back to two panels view.
  1572. if (!isCompactMode) {
  1573. return;
  1574. }
  1575. // Take m_fileList out of m_naviSplitter.
  1576. Q_ASSERT(fileListIdx == m_naviSplitter->indexOf(m_fileList));
  1577. QWidget *tmpWidget = new QWidget(this);
  1578. m_fileList->hide();
  1579. m_naviSplitter->replaceWidget(fileListIdx, tmpWidget);
  1580. tmpWidget->hide();
  1581. // Insert m_fileList into m_mainSplitter.
  1582. QWidget *wid = m_mainSplitter->replaceWidget(fileListIdx, m_fileList);
  1583. delete wid;
  1584. m_fileList->show();
  1585. }
  1586. // Set Tab order.
  1587. setTabOrder(directoryTree, m_fileList->getContentWidget());
  1588. }
  1589. void VMainWindow::changePanelView(PanelViewState p_state)
  1590. {
  1591. switch (p_state) {
  1592. case PanelViewState::ExpandMode:
  1593. m_mainSplitter->widget(0)->hide();
  1594. m_mainSplitter->widget(1)->hide();
  1595. m_mainSplitter->widget(2)->show();
  1596. break;
  1597. case PanelViewState::SinglePanel:
  1598. enableCompactMode(false);
  1599. m_mainSplitter->widget(0)->hide();
  1600. m_mainSplitter->widget(1)->show();
  1601. m_mainSplitter->widget(2)->show();
  1602. break;
  1603. case PanelViewState::TwoPanels:
  1604. enableCompactMode(false);
  1605. m_mainSplitter->widget(0)->show();
  1606. m_mainSplitter->widget(1)->show();
  1607. m_mainSplitter->widget(2)->show();
  1608. break;
  1609. case PanelViewState::CompactMode:
  1610. m_mainSplitter->widget(0)->show();
  1611. m_mainSplitter->widget(1)->hide();
  1612. m_mainSplitter->widget(2)->show();
  1613. enableCompactMode(true);
  1614. break;
  1615. default:
  1616. break;
  1617. }
  1618. // Change the action state.
  1619. QList<QAction *> acts = m_viewActGroup->actions();
  1620. for (auto & act : acts) {
  1621. if (act->data().toInt() == (int)p_state) {
  1622. act->setChecked(true);
  1623. } else {
  1624. act->setChecked(false);
  1625. }
  1626. }
  1627. if (p_state != PanelViewState::ExpandMode) {
  1628. expandViewAct->setChecked(false);
  1629. }
  1630. }
  1631. void VMainWindow::updateWindowTitle(const QString &str)
  1632. {
  1633. QString title = "VNote";
  1634. if (!str.isEmpty()) {
  1635. title = title + " - " + str;
  1636. }
  1637. setWindowTitle(title);
  1638. }
  1639. void VMainWindow::curEditFileInfo()
  1640. {
  1641. Q_ASSERT(m_curFile);
  1642. if (m_curFile->getType() == FileType::Note) {
  1643. VNoteFile *file = dynamic_cast<VNoteFile *>((VFile *)m_curFile);
  1644. Q_ASSERT(file);
  1645. m_fileList->fileInfo(file);
  1646. } else if (m_curFile->getType() == FileType::Orphan) {
  1647. VOrphanFile *file = dynamic_cast<VOrphanFile *>((VFile *)m_curFile);
  1648. Q_ASSERT(file);
  1649. if (!file->isSystemFile()) {
  1650. editOrphanFileInfo(m_curFile);
  1651. }
  1652. }
  1653. }
  1654. void VMainWindow::deleteCurNote()
  1655. {
  1656. if (!m_curFile || m_curFile->getType() != FileType::Note) {
  1657. return;
  1658. }
  1659. VNoteFile *file = dynamic_cast<VNoteFile *>((VFile *)m_curFile);
  1660. m_fileList->deleteFile(file);
  1661. }
  1662. void VMainWindow::closeEvent(QCloseEvent *event)
  1663. {
  1664. bool isExit = m_requestQuit || !g_config->getMinimizeToStystemTray();
  1665. m_requestQuit = false;
  1666. #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
  1667. // Do not support minimized to tray on macOS.
  1668. isExit = true;
  1669. #endif
  1670. if (!isExit && g_config->getMinimizeToStystemTray() == -1) {
  1671. // Not initialized yet. Prompt for user.
  1672. int ret = VUtils::showMessage(QMessageBox::Information,
  1673. tr("Close VNote"),
  1674. tr("Do you want to minimize VNote to system tray "
  1675. "instead of quitting it when closing VNote?"),
  1676. tr("You could change the option in Settings later."),
  1677. QMessageBox::Ok | QMessageBox::No | QMessageBox::Cancel,
  1678. QMessageBox::Ok,
  1679. this);
  1680. if (ret == QMessageBox::Ok) {
  1681. g_config->setMinimizeToSystemTray(1);
  1682. } else if (ret == QMessageBox::No) {
  1683. g_config->setMinimizeToSystemTray(0);
  1684. isExit = true;
  1685. } else {
  1686. event->ignore();
  1687. return;
  1688. }
  1689. }
  1690. if (isVisible()) {
  1691. saveStateAndGeometry();
  1692. }
  1693. if (isExit || !m_trayIcon->isVisible()) {
  1694. // Get all the opened tabs.
  1695. bool saveOpenedNotes = g_config->getStartupPageType() == StartupPageType::ContinueLeftOff;
  1696. QVector<VFileSessionInfo> fileInfos;
  1697. QVector<VEditTabInfo> tabs;
  1698. if (saveOpenedNotes) {
  1699. tabs = editArea->getAllTabsInfo();
  1700. fileInfos.reserve(tabs.size());
  1701. for (auto const & tab : tabs) {
  1702. // Skip system file.
  1703. VFile *file = tab.m_editTab->getFile();
  1704. if (file->getType() == FileType::Orphan
  1705. && dynamic_cast<VOrphanFile *>(file)->isSystemFile()) {
  1706. continue;
  1707. }
  1708. VFileSessionInfo info = VFileSessionInfo::fromEditTabInfo(&tab);
  1709. fileInfos.push_back(info);
  1710. qDebug() << "file session:" << info.m_file << (info.m_mode == OpenFileMode::Edit);
  1711. }
  1712. }
  1713. if (!editArea->closeAllFiles(false)) {
  1714. // Fail to close all the opened files, cancel closing app.
  1715. event->ignore();
  1716. return;
  1717. }
  1718. if (saveOpenedNotes) {
  1719. g_config->setLastOpenedFiles(fileInfos);
  1720. }
  1721. QMainWindow::closeEvent(event);
  1722. } else {
  1723. hide();
  1724. event->ignore();
  1725. }
  1726. }
  1727. void VMainWindow::saveStateAndGeometry()
  1728. {
  1729. g_config->setMainWindowGeometry(saveGeometry());
  1730. g_config->setMainWindowState(saveState());
  1731. g_config->setToolsDockChecked(toolDock->isVisible());
  1732. if (m_panelViewState == PanelViewState::CompactMode) {
  1733. g_config->setNaviSplitterState(m_naviSplitter->saveState());
  1734. g_config->setMainSplitterState(m_mainSplitter->saveState());
  1735. } else {
  1736. // In one panel view, it will save the wrong state that the directory tree
  1737. // panel has a width of zero.
  1738. changePanelView(PanelViewState::TwoPanels);
  1739. g_config->setMainSplitterState(m_mainSplitter->saveState());
  1740. }
  1741. }
  1742. void VMainWindow::restoreStateAndGeometry()
  1743. {
  1744. const QByteArray &geometry = g_config->getMainWindowGeometry();
  1745. if (!geometry.isEmpty()) {
  1746. restoreGeometry(geometry);
  1747. }
  1748. const QByteArray &state = g_config->getMainWindowState();
  1749. if (!state.isEmpty()) {
  1750. restoreState(state);
  1751. }
  1752. toolDock->setVisible(g_config->getToolsDockChecked());
  1753. const QByteArray &splitterState = g_config->getMainSplitterState();
  1754. if (!splitterState.isEmpty()) {
  1755. m_mainSplitter->restoreState(splitterState);
  1756. }
  1757. const QByteArray &naviSplitterState = g_config->getNaviSplitterState();
  1758. if (!naviSplitterState.isEmpty()) {
  1759. m_naviSplitter->restoreState(naviSplitterState);
  1760. }
  1761. }
  1762. void VMainWindow::handleCurrentDirectoryChanged(const VDirectory *p_dir)
  1763. {
  1764. newNoteAct->setEnabled(p_dir);
  1765. m_importNoteAct->setEnabled(p_dir);
  1766. }
  1767. void VMainWindow::handleCurrentNotebookChanged(const VNotebook *p_notebook)
  1768. {
  1769. newRootDirAct->setEnabled(p_notebook);
  1770. }
  1771. void VMainWindow::resizeEvent(QResizeEvent *event)
  1772. {
  1773. repositionAvatar();
  1774. QMainWindow::resizeEvent(event);
  1775. }
  1776. void VMainWindow::keyPressEvent(QKeyEvent *event)
  1777. {
  1778. int key = event->key();
  1779. Qt::KeyboardModifiers modifiers = event->modifiers();
  1780. if (key == Qt::Key_Escape
  1781. || (key == Qt::Key_BracketLeft
  1782. && modifiers == Qt::ControlModifier)) {
  1783. m_findReplaceDialog->closeDialog();
  1784. event->accept();
  1785. return;
  1786. }
  1787. QMainWindow::keyPressEvent(event);
  1788. }
  1789. void VMainWindow::repositionAvatar()
  1790. {
  1791. int diameter = m_mainSplitter->pos().y();
  1792. int x = width() - diameter - 5;
  1793. int y = 0;
  1794. qDebug() << "avatar:" << diameter << x << y;
  1795. m_avatar->setDiameter(diameter);
  1796. m_avatar->move(x, y);
  1797. m_avatar->show();
  1798. }
  1799. bool VMainWindow::locateFile(VFile *p_file)
  1800. {
  1801. bool ret = false;
  1802. if (!p_file || p_file->getType() != FileType::Note) {
  1803. return ret;
  1804. }
  1805. VNoteFile *file = dynamic_cast<VNoteFile *>(p_file);
  1806. VNotebook *notebook = file->getNotebook();
  1807. if (notebookSelector->locateNotebook(notebook)) {
  1808. while (directoryTree->currentNotebook() != notebook) {
  1809. QCoreApplication::sendPostedEvents();
  1810. }
  1811. VDirectory *dir = file->getDirectory();
  1812. if (directoryTree->locateDirectory(dir)) {
  1813. while (m_fileList->currentDirectory() != dir) {
  1814. QCoreApplication::sendPostedEvents();
  1815. }
  1816. if (m_fileList->locateFile(file)) {
  1817. ret = true;
  1818. m_fileList->setFocus();
  1819. }
  1820. }
  1821. }
  1822. // Open the directory and file panels after location.
  1823. if (m_panelViewState == PanelViewState::CompactMode) {
  1824. compactModeView();
  1825. } else {
  1826. twoPanelView();
  1827. }
  1828. return ret;
  1829. }
  1830. void VMainWindow::handleFindDialogTextChanged(const QString &p_text, uint /* p_options */)
  1831. {
  1832. bool enabled = true;
  1833. if (p_text.isEmpty()) {
  1834. enabled = false;
  1835. }
  1836. m_findNextAct->setEnabled(enabled);
  1837. m_findPreviousAct->setEnabled(enabled);
  1838. m_replaceAct->setEnabled(enabled);
  1839. m_replaceFindAct->setEnabled(enabled);
  1840. m_replaceAllAct->setEnabled(enabled);
  1841. }
  1842. void VMainWindow::openFindDialog()
  1843. {
  1844. m_findReplaceDialog->openDialog(editArea->getSelectedText());
  1845. }
  1846. void VMainWindow::viewSettings()
  1847. {
  1848. VSettingsDialog settingsDialog(this);
  1849. settingsDialog.exec();
  1850. }
  1851. void VMainWindow::closeCurrentFile()
  1852. {
  1853. if (m_curFile) {
  1854. editArea->closeFile(m_curFile, false);
  1855. }
  1856. }
  1857. void VMainWindow::changeAutoIndent(bool p_checked)
  1858. {
  1859. g_config->setAutoIndent(p_checked);
  1860. }
  1861. void VMainWindow::changeAutoList(bool p_checked)
  1862. {
  1863. g_config->setAutoList(p_checked);
  1864. if (p_checked) {
  1865. if (!m_autoIndentAct->isChecked()) {
  1866. m_autoIndentAct->trigger();
  1867. }
  1868. m_autoIndentAct->setEnabled(false);
  1869. } else {
  1870. m_autoIndentAct->setEnabled(true);
  1871. }
  1872. }
  1873. void VMainWindow::changeVimMode(bool p_checked)
  1874. {
  1875. g_config->setEnableVimMode(p_checked);
  1876. }
  1877. void VMainWindow::enableCodeBlockHighlight(bool p_checked)
  1878. {
  1879. g_config->setEnableCodeBlockHighlight(p_checked);
  1880. }
  1881. void VMainWindow::enableImagePreview(bool p_checked)
  1882. {
  1883. g_config->setEnablePreviewImages(p_checked);
  1884. emit editorConfigUpdated();
  1885. }
  1886. void VMainWindow::enableImagePreviewConstraint(bool p_checked)
  1887. {
  1888. g_config->setEnablePreviewImageConstraint(p_checked);
  1889. emit editorConfigUpdated();
  1890. }
  1891. void VMainWindow::enableImageConstraint(bool p_checked)
  1892. {
  1893. g_config->setEnableImageConstraint(p_checked);
  1894. vnote->updateTemplate();
  1895. }
  1896. void VMainWindow::enableImageCaption(bool p_checked)
  1897. {
  1898. g_config->setEnableImageCaption(p_checked);
  1899. }
  1900. void VMainWindow::shortcutsHelp()
  1901. {
  1902. QString docFile = VUtils::getDocFile(VNote::c_shortcutsDocFile);
  1903. VFile *file = vnote->getOrphanFile(docFile, false, true);
  1904. editArea->openFile(file, OpenFileMode::Read);
  1905. }
  1906. void VMainWindow::printNote()
  1907. {
  1908. QPrinter printer;
  1909. QPrintDialog dialog(&printer, this);
  1910. dialog.setWindowTitle(tr("Print Note"));
  1911. V_ASSERT(m_curTab);
  1912. if (m_curFile->getDocType() == DocType::Markdown) {
  1913. VMdTab *mdTab = dynamic_cast<VMdTab *>((VEditTab *)m_curTab);
  1914. VWebView *webView = mdTab->getWebViewer();
  1915. V_ASSERT(webView);
  1916. if (webView->hasSelection()) {
  1917. dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
  1918. }
  1919. if (dialog.exec() != QDialog::Accepted) {
  1920. return;
  1921. }
  1922. }
  1923. }
  1924. void VMainWindow::exportAsPDF()
  1925. {
  1926. V_ASSERT(m_curTab);
  1927. V_ASSERT(m_curFile);
  1928. if (m_curFile->getDocType() == DocType::Markdown) {
  1929. VMdTab *mdTab = dynamic_cast<VMdTab *>((VEditTab *)m_curTab);
  1930. VExporter exporter(mdTab->getMarkdownConverterType(), this);
  1931. exporter.exportNote(m_curFile, ExportType::PDF);
  1932. exporter.exec();
  1933. }
  1934. }
  1935. QAction *VMainWindow::newAction(const QIcon &p_icon,
  1936. const QString &p_text,
  1937. QObject *p_parent)
  1938. {
  1939. #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
  1940. Q_UNUSED(p_icon);
  1941. return new QAction(p_text, p_parent);
  1942. #else
  1943. return new QAction(p_icon, p_text, p_parent);
  1944. #endif
  1945. }
  1946. void VMainWindow::showStatusMessage(const QString &p_msg)
  1947. {
  1948. const int timeout = 3000;
  1949. statusBar()->showMessage(p_msg, timeout);
  1950. }
  1951. void VMainWindow::updateStatusInfo(const VEditTabInfo &p_info)
  1952. {
  1953. if (m_curTab) {
  1954. m_tabIndicator->update(p_info);
  1955. m_tabIndicator->show();
  1956. if (m_curTab->isEditMode()) {
  1957. if (p_info.m_type == VEditTabInfo::InfoType::All) {
  1958. m_curTab->requestUpdateVimStatus();
  1959. }
  1960. } else {
  1961. m_vimIndicator->hide();
  1962. }
  1963. } else {
  1964. m_tabIndicator->hide();
  1965. m_vimIndicator->hide();
  1966. }
  1967. }
  1968. void VMainWindow::handleVimStatusUpdated(const VVim *p_vim)
  1969. {
  1970. m_vimIndicator->update(p_vim, m_curTab);
  1971. if (!p_vim || !m_curTab || !m_curTab->isEditMode()) {
  1972. m_vimIndicator->hide();
  1973. } else {
  1974. m_vimIndicator->show();
  1975. }
  1976. }
  1977. bool VMainWindow::tryOpenInternalFile(const QString &p_filePath)
  1978. {
  1979. if (p_filePath.isEmpty()) {
  1980. return false;
  1981. }
  1982. if (QFileInfo::exists(p_filePath)) {
  1983. VFile *file = vnote->getInternalFile(p_filePath);
  1984. if (file) {
  1985. editArea->openFile(file, OpenFileMode::Read);
  1986. return true;
  1987. }
  1988. }
  1989. return false;
  1990. }
  1991. void VMainWindow::openFiles(const QStringList &p_files,
  1992. bool p_forceOrphan,
  1993. OpenFileMode p_mode,
  1994. bool p_forceMode)
  1995. {
  1996. for (int i = 0; i < p_files.size(); ++i) {
  1997. VFile *file = NULL;
  1998. if (!p_forceOrphan) {
  1999. file = vnote->getInternalFile(p_files[i]);
  2000. }
  2001. if (!file) {
  2002. file = vnote->getOrphanFile(p_files[i], true);
  2003. }
  2004. editArea->openFile(file, p_mode, p_forceMode);
  2005. }
  2006. }
  2007. void VMainWindow::editOrphanFileInfo(VFile *p_file)
  2008. {
  2009. VOrphanFile *file = dynamic_cast<VOrphanFile *>(p_file);
  2010. Q_ASSERT(file);
  2011. VOrphanFileInfoDialog dialog(file, this);
  2012. if (dialog.exec() == QDialog::Accepted) {
  2013. QString imgFolder = dialog.getImageFolder();
  2014. file->setImageFolder(imgFolder);
  2015. }
  2016. }
  2017. void VMainWindow::checkSharedMemory()
  2018. {
  2019. QStringList files = m_guard->fetchFilesToOpen();
  2020. if (!files.isEmpty()) {
  2021. qDebug() << "shared memory fetch files" << files;
  2022. openFiles(files);
  2023. // Eliminate the signal.
  2024. m_guard->fetchAskedToShow();
  2025. showMainWindow();
  2026. } else if (m_guard->fetchAskedToShow()) {
  2027. qDebug() << "shared memory asked to show up";
  2028. showMainWindow();
  2029. }
  2030. }
  2031. void VMainWindow::initTrayIcon()
  2032. {
  2033. QMenu *menu = new QMenu(this);
  2034. QAction *showMainWindowAct = menu->addAction(tr("Show VNote"));
  2035. connect(showMainWindowAct, &QAction::triggered,
  2036. this, &VMainWindow::showMainWindow);
  2037. QAction *exitAct = menu->addAction(tr("Quit"));
  2038. connect(exitAct, &QAction::triggered,
  2039. this, &VMainWindow::quitApp);
  2040. m_trayIcon = new QSystemTrayIcon(QIcon(":/resources/icons/32x32/vnote.png"), this);
  2041. m_trayIcon->setToolTip(tr("VNote"));
  2042. m_trayIcon->setContextMenu(menu);
  2043. connect(m_trayIcon, &QSystemTrayIcon::activated,
  2044. this, [this](QSystemTrayIcon::ActivationReason p_reason){
  2045. if (p_reason == QSystemTrayIcon::Trigger) {
  2046. this->showMainWindow();
  2047. }
  2048. });
  2049. m_trayIcon->show();
  2050. }
  2051. void VMainWindow::changeEvent(QEvent *p_event)
  2052. {
  2053. if (p_event->type() == QEvent::WindowStateChange) {
  2054. QWindowStateChangeEvent *eve = dynamic_cast<QWindowStateChangeEvent *>(p_event);
  2055. m_windowOldState = eve->oldState();
  2056. }
  2057. QMainWindow::changeEvent(p_event);
  2058. }
  2059. void VMainWindow::showMainWindow()
  2060. {
  2061. if (this->isMinimized()) {
  2062. if (m_windowOldState & Qt::WindowMaximized) {
  2063. this->showMaximized();
  2064. } else if (m_windowOldState & Qt::WindowFullScreen) {
  2065. this->showFullScreen();
  2066. } else {
  2067. this->showNormal();
  2068. }
  2069. } else {
  2070. this->show();
  2071. // Need to call raise() in macOS.
  2072. this->raise();
  2073. }
  2074. this->activateWindow();
  2075. }
  2076. void VMainWindow::openStartupPages()
  2077. {
  2078. StartupPageType type = g_config->getStartupPageType();
  2079. switch (type) {
  2080. case StartupPageType::ContinueLeftOff:
  2081. {
  2082. QVector<VFileSessionInfo> files = g_config->getLastOpenedFiles();
  2083. qDebug() << "open" << files.size() << "last opened files";
  2084. editArea->openFiles(files);
  2085. break;
  2086. }
  2087. case StartupPageType::SpecificPages:
  2088. {
  2089. QStringList pagesToOpen = VUtils::filterFilePathsToOpen(g_config->getStartupPages());
  2090. qDebug() << "open startup pages" << pagesToOpen;
  2091. openFiles(pagesToOpen);
  2092. break;
  2093. }
  2094. default:
  2095. break;
  2096. }
  2097. }
  2098. bool VMainWindow::isHeadingSequenceApplicable() const
  2099. {
  2100. if (!m_curTab) {
  2101. return false;
  2102. }
  2103. Q_ASSERT(m_curFile);
  2104. if (!m_curFile->isModifiable()
  2105. || m_curFile->getDocType() != DocType::Markdown) {
  2106. return false;
  2107. }
  2108. return true;
  2109. }
  2110. // Popup the attachment list if it is enabled.
  2111. bool VMainWindow::showAttachmentListByCaptain(void *p_target, void *p_data)
  2112. {
  2113. Q_UNUSED(p_data);
  2114. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2115. if (obj->m_attachmentBtn->isEnabled()) {
  2116. obj->m_attachmentBtn->showPopupWidget();
  2117. }
  2118. return true;
  2119. }
  2120. bool VMainWindow::locateCurrentFileByCaptain(void *p_target, void *p_data)
  2121. {
  2122. Q_UNUSED(p_data);
  2123. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2124. if (obj->m_curFile) {
  2125. if (obj->locateFile(obj->m_curFile)) {
  2126. return false;
  2127. }
  2128. }
  2129. return true;
  2130. }
  2131. bool VMainWindow::toggleExpandModeByCaptain(void *p_target, void *p_data)
  2132. {
  2133. Q_UNUSED(p_data);
  2134. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2135. obj->expandViewAct->trigger();
  2136. return true;
  2137. }
  2138. bool VMainWindow::toggleOnePanelViewByCaptain(void *p_target, void *p_data)
  2139. {
  2140. Q_UNUSED(p_data);
  2141. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2142. if (obj->m_panelViewState == PanelViewState::TwoPanels) {
  2143. obj->onePanelView();
  2144. } else {
  2145. obj->twoPanelView();
  2146. }
  2147. return true;
  2148. }
  2149. bool VMainWindow::discardAndReadByCaptain(void *p_target, void *p_data)
  2150. {
  2151. Q_UNUSED(p_data);
  2152. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2153. if (obj->m_curTab) {
  2154. obj->discardExitAct->trigger();
  2155. obj->m_curTab->setFocus();
  2156. return false;
  2157. }
  2158. return true;
  2159. }
  2160. bool VMainWindow::toggleToolsDockByCaptain(void *p_target, void *p_data)
  2161. {
  2162. Q_UNUSED(p_data);
  2163. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2164. obj->toolDock->setVisible(!obj->toolDock->isVisible());
  2165. return true;
  2166. }
  2167. bool VMainWindow::closeFileByCaptain(void *p_target, void *p_data)
  2168. {
  2169. Q_UNUSED(p_data);
  2170. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2171. obj->closeCurrentFile();
  2172. QWidget *nextFocus = obj->editArea->getCurrentTab();
  2173. if (nextFocus) {
  2174. nextFocus->setFocus();
  2175. } else {
  2176. obj->m_fileList->setFocus();
  2177. }
  2178. return false;
  2179. }
  2180. bool VMainWindow::shortcutsHelpByCaptain(void *p_target, void *p_data)
  2181. {
  2182. Q_UNUSED(p_data);
  2183. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2184. obj->shortcutsHelp();
  2185. return false;
  2186. }
  2187. bool VMainWindow::flushLogFileByCaptain(void *p_target, void *p_data)
  2188. {
  2189. Q_UNUSED(p_target);
  2190. Q_UNUSED(p_data);
  2191. #if defined(QT_NO_DEBUG)
  2192. // Flush g_logFile.
  2193. g_logFile.flush();
  2194. #endif
  2195. return true;
  2196. }
  2197. void VMainWindow::promptNewNotebookIfEmpty()
  2198. {
  2199. if (vnote->getNotebooks().isEmpty()) {
  2200. notebookSelector->newNotebook();
  2201. }
  2202. }
  2203. void VMainWindow::initShortcuts()
  2204. {
  2205. QString keySeq = g_config->getShortcutKeySequence("CloseNote");
  2206. qDebug() << "set CloseNote shortcut to" << keySeq;
  2207. if (!keySeq.isEmpty()) {
  2208. QShortcut *closeNoteShortcut = new QShortcut(QKeySequence(keySeq), this);
  2209. closeNoteShortcut->setContext(Qt::WidgetWithChildrenShortcut);
  2210. connect(closeNoteShortcut, &QShortcut::activated,
  2211. this, &VMainWindow::closeCurrentFile);
  2212. }
  2213. }
  2214. void VMainWindow::openFlashPage()
  2215. {
  2216. openFiles(QStringList() << g_config->getFlashPage(),
  2217. false,
  2218. OpenFileMode::Edit,
  2219. true);
  2220. }
  2221. void VMainWindow::initHeadingButton(QToolBar *p_tb)
  2222. {
  2223. m_headingBtn = new QPushButton(VIconUtils::toolButtonIcon(":/resources/icons/heading.svg"),
  2224. "",
  2225. this);
  2226. m_headingBtn->setToolTip(tr("Headings"));
  2227. m_headingBtn->setProperty("CornerBtn", true);
  2228. m_headingBtn->setFocusPolicy(Qt::NoFocus);
  2229. m_headingBtn->setEnabled(false);
  2230. QMenu *menu = new QMenu(this);
  2231. QString text(tr("Heading %1"));
  2232. QString tooltip(tr("Heading %1\t%2"));
  2233. QWidgetAction *wact = new QWidgetAction(menu);
  2234. wact->setData(1);
  2235. VButtonMenuItem *w = new VButtonMenuItem(wact, text.arg(1), this);
  2236. w->setToolTip(tooltip.arg(1).arg(VUtils::getShortcutText("Ctrl+1")));
  2237. w->setProperty("Heading1", true);
  2238. wact->setDefaultWidget(w);
  2239. menu->addAction(wact);
  2240. wact = new QWidgetAction(menu);
  2241. wact->setData(2);
  2242. w = new VButtonMenuItem(wact, text.arg(2), this);
  2243. w->setToolTip(tooltip.arg(2).arg(VUtils::getShortcutText("Ctrl+2")));
  2244. w->setProperty("Heading2", true);
  2245. wact->setDefaultWidget(w);
  2246. menu->addAction(wact);
  2247. wact = new QWidgetAction(menu);
  2248. wact->setData(3);
  2249. w = new VButtonMenuItem(wact, text.arg(3), this);
  2250. w->setToolTip(tooltip.arg(3).arg(VUtils::getShortcutText("Ctrl+3")));
  2251. w->setProperty("Heading3", true);
  2252. wact->setDefaultWidget(w);
  2253. menu->addAction(wact);
  2254. wact = new QWidgetAction(menu);
  2255. wact->setData(4);
  2256. w = new VButtonMenuItem(wact, text.arg(4), this);
  2257. w->setToolTip(tooltip.arg(4).arg(VUtils::getShortcutText("Ctrl+4")));
  2258. w->setProperty("Heading4", true);
  2259. wact->setDefaultWidget(w);
  2260. menu->addAction(wact);
  2261. wact = new QWidgetAction(menu);
  2262. wact->setData(5);
  2263. w = new VButtonMenuItem(wact, text.arg(5), this);
  2264. w->setToolTip(tooltip.arg(5).arg(VUtils::getShortcutText("Ctrl+5")));
  2265. w->setProperty("Heading5", true);
  2266. wact->setDefaultWidget(w);
  2267. menu->addAction(wact);
  2268. wact = new QWidgetAction(menu);
  2269. wact->setData(6);
  2270. w = new VButtonMenuItem(wact, text.arg(6), this);
  2271. w->setToolTip(tooltip.arg(6).arg(VUtils::getShortcutText("Ctrl+6")));
  2272. w->setProperty("Heading6", true);
  2273. wact->setDefaultWidget(w);
  2274. menu->addAction(wact);
  2275. wact = new QWidgetAction(menu);
  2276. wact->setData(0);
  2277. w = new VButtonMenuItem(wact, tr("Clear"), this);
  2278. w->setToolTip(tr("Clear\t%1").arg(VUtils::getShortcutText("Ctrl+7")));
  2279. wact->setDefaultWidget(w);
  2280. menu->addAction(wact);
  2281. connect(menu, &QMenu::triggered,
  2282. this, [this, menu](QAction *p_action) {
  2283. if (m_curTab) {
  2284. int level = p_action->data().toInt();
  2285. m_curTab->decorateText(TextDecoration::Heading, level);
  2286. }
  2287. menu->hide();
  2288. });
  2289. m_headingBtn->setMenu(menu);
  2290. p_tb->addWidget(m_headingBtn);
  2291. }
  2292. void VMainWindow::initThemeMenu(QMenu *p_menu)
  2293. {
  2294. QMenu *themeMenu = p_menu->addMenu(tr("Theme"));
  2295. themeMenu->setToolTipsVisible(true);
  2296. QAction *addAct = newAction(VIconUtils::menuIcon(":/resources/icons/add_style.svg"),
  2297. tr("Add Theme"),
  2298. themeMenu);
  2299. addAct->setToolTip(tr("Add custom theme"));
  2300. connect(addAct, &QAction::triggered,
  2301. this, [this]() {
  2302. VTipsDialog dialog(VUtils::getDocFile("tips_add_theme.md"),
  2303. tr("Add Theme"),
  2304. []() {
  2305. QUrl url = QUrl::fromLocalFile(g_config->getThemeConfigFolder());
  2306. QDesktopServices::openUrl(url);
  2307. },
  2308. this);
  2309. dialog.exec();
  2310. });
  2311. themeMenu->addAction(addAct);
  2312. QActionGroup *ag = new QActionGroup(this);
  2313. connect(ag, &QActionGroup::triggered,
  2314. this, [this](QAction *p_action) {
  2315. if (!p_action) {
  2316. return;
  2317. }
  2318. QString data = p_action->data().toString();
  2319. g_config->setTheme(data);
  2320. });
  2321. QList<QString> themes = g_config->getThemes();
  2322. QString theme = g_config->getTheme();
  2323. for (auto const &item : themes) {
  2324. QAction *act = new QAction(item, ag);
  2325. act->setToolTip(tr("Set as the theme of VNote (restart VNote to make it work)"));
  2326. act->setCheckable(true);
  2327. act->setData(item);
  2328. // Add it to the menu.
  2329. themeMenu->addAction(act);
  2330. if (theme == item) {
  2331. act->setChecked(true);
  2332. }
  2333. }
  2334. }
  2335. void VMainWindow::customShortcut()
  2336. {
  2337. VTipsDialog dialog(VUtils::getDocFile("tips_custom_shortcut.md"),
  2338. tr("Custom Shortcuts"),
  2339. []() {
  2340. #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
  2341. // On macOS, it seems that we could not open that ini file directly.
  2342. QUrl url = QUrl::fromLocalFile(g_config->getConfigFolder());
  2343. #else
  2344. QUrl url = QUrl::fromLocalFile(g_config->getConfigFilePath());
  2345. #endif
  2346. QDesktopServices::openUrl(url);
  2347. },
  2348. this);
  2349. dialog.exec();
  2350. }