vmainwindow.cpp 107 KB

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