vmainwindow.cpp 113 KB

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