vmainwindow.cpp 106 KB

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