vmainwindow.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374
  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("Custom Shortcuts"), this);
  889. customShortcutAct->setToolTip(tr("Custom 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 (m_panelViewState == PanelViewState::CompactMode) {
  1982. compactModeView();
  1983. } else {
  1984. twoPanelView();
  1985. }
  1986. return ret;
  1987. }
  1988. bool VMainWindow::locateDirectory(VDirectory *p_directory)
  1989. {
  1990. bool ret = false;
  1991. if (!p_directory) {
  1992. return ret;
  1993. }
  1994. VNotebook *notebook = p_directory->getNotebook();
  1995. if (m_notebookSelector->locateNotebook(notebook)) {
  1996. while (directoryTree->currentNotebook() != notebook) {
  1997. QCoreApplication::sendPostedEvents();
  1998. }
  1999. ret = directoryTree->locateDirectory(p_directory);
  2000. }
  2001. // Open the directory and file panels after location.
  2002. if (m_panelViewState == PanelViewState::CompactMode) {
  2003. compactModeView();
  2004. } else {
  2005. twoPanelView();
  2006. }
  2007. return ret;
  2008. }
  2009. void VMainWindow::handleFindDialogTextChanged(const QString &p_text, uint /* p_options */)
  2010. {
  2011. bool enabled = true;
  2012. if (p_text.isEmpty()) {
  2013. enabled = false;
  2014. }
  2015. m_findNextAct->setEnabled(enabled);
  2016. m_findPreviousAct->setEnabled(enabled);
  2017. m_replaceAct->setEnabled(enabled);
  2018. m_replaceFindAct->setEnabled(enabled);
  2019. m_replaceAllAct->setEnabled(enabled);
  2020. }
  2021. void VMainWindow::openFindDialog()
  2022. {
  2023. m_findReplaceDialog->openDialog(m_editArea->getSelectedText());
  2024. }
  2025. void VMainWindow::viewSettings()
  2026. {
  2027. VSettingsDialog settingsDialog(this);
  2028. settingsDialog.exec();
  2029. }
  2030. void VMainWindow::closeCurrentFile()
  2031. {
  2032. if (m_curFile) {
  2033. m_editArea->closeFile(m_curFile, false);
  2034. }
  2035. }
  2036. void VMainWindow::changeAutoIndent(bool p_checked)
  2037. {
  2038. g_config->setAutoIndent(p_checked);
  2039. }
  2040. void VMainWindow::changeAutoList(bool p_checked)
  2041. {
  2042. g_config->setAutoList(p_checked);
  2043. if (p_checked) {
  2044. if (!m_autoIndentAct->isChecked()) {
  2045. m_autoIndentAct->trigger();
  2046. }
  2047. m_autoIndentAct->setEnabled(false);
  2048. } else {
  2049. m_autoIndentAct->setEnabled(true);
  2050. }
  2051. }
  2052. void VMainWindow::changeVimMode(bool p_checked)
  2053. {
  2054. g_config->setEnableVimMode(p_checked);
  2055. }
  2056. void VMainWindow::enableCodeBlockHighlight(bool p_checked)
  2057. {
  2058. g_config->setEnableCodeBlockHighlight(p_checked);
  2059. }
  2060. void VMainWindow::enableImagePreview(bool p_checked)
  2061. {
  2062. g_config->setEnablePreviewImages(p_checked);
  2063. emit editorConfigUpdated();
  2064. }
  2065. void VMainWindow::enableImagePreviewConstraint(bool p_checked)
  2066. {
  2067. g_config->setEnablePreviewImageConstraint(p_checked);
  2068. emit editorConfigUpdated();
  2069. }
  2070. void VMainWindow::enableImageConstraint(bool p_checked)
  2071. {
  2072. g_config->setEnableImageConstraint(p_checked);
  2073. vnote->updateTemplate();
  2074. }
  2075. void VMainWindow::enableImageCaption(bool p_checked)
  2076. {
  2077. g_config->setEnableImageCaption(p_checked);
  2078. }
  2079. void VMainWindow::shortcutsHelp()
  2080. {
  2081. QString docFile = VUtils::getDocFile(VNote::c_shortcutsDocFile);
  2082. VFile *file = vnote->getOrphanFile(docFile, false, true);
  2083. m_editArea->openFile(file, OpenFileMode::Read);
  2084. }
  2085. void VMainWindow::printNote()
  2086. {
  2087. if (m_printer
  2088. || !m_curFile
  2089. || m_curFile->getDocType() != DocType::Markdown) {
  2090. return;
  2091. }
  2092. m_printer = new QPrinter();
  2093. QPrintDialog dialog(m_printer, this);
  2094. dialog.setWindowTitle(tr("Print Note"));
  2095. V_ASSERT(m_curTab);
  2096. VMdTab *mdTab = dynamic_cast<VMdTab *>((VEditTab *)m_curTab);
  2097. VWebView *webView = mdTab->getWebViewer();
  2098. V_ASSERT(webView);
  2099. if (webView->hasSelection()) {
  2100. dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
  2101. }
  2102. if (dialog.exec() == QDialog::Accepted) {
  2103. webView->page()->print(m_printer, [this](bool p_succ) {
  2104. qDebug() << "print web page callback" << p_succ;
  2105. delete m_printer;
  2106. m_printer = NULL;
  2107. });
  2108. } else {
  2109. delete m_printer;
  2110. m_printer = NULL;
  2111. }
  2112. }
  2113. QAction *VMainWindow::newAction(const QIcon &p_icon,
  2114. const QString &p_text,
  2115. QObject *p_parent)
  2116. {
  2117. #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
  2118. Q_UNUSED(p_icon);
  2119. return new QAction(p_text, p_parent);
  2120. #else
  2121. return new QAction(p_icon, p_text, p_parent);
  2122. #endif
  2123. }
  2124. void VMainWindow::showStatusMessage(const QString &p_msg)
  2125. {
  2126. const int timeout = 5000;
  2127. statusBar()->showMessage(p_msg, timeout);
  2128. }
  2129. void VMainWindow::updateStatusInfo(const VEditTabInfo &p_info)
  2130. {
  2131. if (m_curTab) {
  2132. m_tabIndicator->update(p_info);
  2133. m_tabIndicator->show();
  2134. if (m_curTab->isEditMode()) {
  2135. if (p_info.m_type == VEditTabInfo::InfoType::All) {
  2136. m_curTab->requestUpdateVimStatus();
  2137. }
  2138. } else {
  2139. m_vimIndicator->hide();
  2140. }
  2141. } else {
  2142. m_tabIndicator->hide();
  2143. m_vimIndicator->hide();
  2144. }
  2145. }
  2146. void VMainWindow::handleVimStatusUpdated(const VVim *p_vim)
  2147. {
  2148. m_vimIndicator->update(p_vim);
  2149. if (!p_vim || !m_curTab || !m_curTab->isEditMode()) {
  2150. m_vimIndicator->hide();
  2151. } else {
  2152. m_vimIndicator->show();
  2153. }
  2154. }
  2155. bool VMainWindow::tryOpenInternalFile(const QString &p_filePath)
  2156. {
  2157. if (p_filePath.isEmpty()) {
  2158. return false;
  2159. }
  2160. if (QFileInfo::exists(p_filePath)) {
  2161. VFile *file = vnote->getInternalFile(p_filePath);
  2162. if (file) {
  2163. m_editArea->openFile(file, OpenFileMode::Read);
  2164. return true;
  2165. }
  2166. }
  2167. return false;
  2168. }
  2169. void VMainWindow::openFiles(const QStringList &p_files,
  2170. bool p_forceOrphan,
  2171. OpenFileMode p_mode,
  2172. bool p_forceMode)
  2173. {
  2174. for (int i = 0; i < p_files.size(); ++i) {
  2175. VFile *file = NULL;
  2176. if (!p_forceOrphan) {
  2177. file = vnote->getInternalFile(p_files[i]);
  2178. }
  2179. if (!file) {
  2180. file = vnote->getOrphanFile(p_files[i], true);
  2181. }
  2182. m_editArea->openFile(file, p_mode, p_forceMode);
  2183. }
  2184. }
  2185. void VMainWindow::editOrphanFileInfo(VFile *p_file)
  2186. {
  2187. VOrphanFile *file = dynamic_cast<VOrphanFile *>(p_file);
  2188. Q_ASSERT(file);
  2189. VOrphanFileInfoDialog dialog(file, this);
  2190. if (dialog.exec() == QDialog::Accepted) {
  2191. QString imgFolder = dialog.getImageFolder();
  2192. file->setImageFolder(imgFolder);
  2193. }
  2194. }
  2195. void VMainWindow::checkSharedMemory()
  2196. {
  2197. QStringList files = m_guard->fetchFilesToOpen();
  2198. if (!files.isEmpty()) {
  2199. qDebug() << "shared memory fetch files" << files;
  2200. openFiles(files);
  2201. // Eliminate the signal.
  2202. m_guard->fetchAskedToShow();
  2203. showMainWindow();
  2204. } else if (m_guard->fetchAskedToShow()) {
  2205. qDebug() << "shared memory asked to show up";
  2206. showMainWindow();
  2207. }
  2208. }
  2209. void VMainWindow::initTrayIcon()
  2210. {
  2211. QMenu *menu = new QMenu(this);
  2212. QAction *showMainWindowAct = menu->addAction(tr("Show VNote"));
  2213. connect(showMainWindowAct, &QAction::triggered,
  2214. this, &VMainWindow::showMainWindow);
  2215. QAction *exitAct = menu->addAction(tr("Quit"));
  2216. connect(exitAct, &QAction::triggered,
  2217. this, &VMainWindow::quitApp);
  2218. m_trayIcon = new QSystemTrayIcon(QIcon(":/resources/icons/32x32/vnote.png"), this);
  2219. m_trayIcon->setToolTip(tr("VNote"));
  2220. m_trayIcon->setContextMenu(menu);
  2221. connect(m_trayIcon, &QSystemTrayIcon::activated,
  2222. this, [this](QSystemTrayIcon::ActivationReason p_reason){
  2223. if (p_reason == QSystemTrayIcon::Trigger) {
  2224. this->showMainWindow();
  2225. }
  2226. });
  2227. m_trayIcon->show();
  2228. }
  2229. void VMainWindow::changeEvent(QEvent *p_event)
  2230. {
  2231. if (p_event->type() == QEvent::WindowStateChange) {
  2232. QWindowStateChangeEvent *eve = dynamic_cast<QWindowStateChangeEvent *>(p_event);
  2233. m_windowOldState = eve->oldState();
  2234. }
  2235. QMainWindow::changeEvent(p_event);
  2236. }
  2237. void VMainWindow::showMainWindow()
  2238. {
  2239. if (this->isMinimized()) {
  2240. if (m_windowOldState & Qt::WindowMaximized) {
  2241. this->showMaximized();
  2242. } else if (m_windowOldState & Qt::WindowFullScreen) {
  2243. this->showFullScreen();
  2244. } else {
  2245. this->showNormal();
  2246. }
  2247. } else {
  2248. this->show();
  2249. // Need to call raise() in macOS.
  2250. this->raise();
  2251. }
  2252. this->activateWindow();
  2253. }
  2254. void VMainWindow::openStartupPages()
  2255. {
  2256. StartupPageType type = g_config->getStartupPageType();
  2257. switch (type) {
  2258. case StartupPageType::ContinueLeftOff:
  2259. {
  2260. QVector<VFileSessionInfo> files = g_config->getLastOpenedFiles();
  2261. qDebug() << "open" << files.size() << "last opened files";
  2262. m_editArea->openFiles(files);
  2263. break;
  2264. }
  2265. case StartupPageType::SpecificPages:
  2266. {
  2267. QStringList pagesToOpen = VUtils::filterFilePathsToOpen(g_config->getStartupPages());
  2268. qDebug() << "open startup pages" << pagesToOpen;
  2269. openFiles(pagesToOpen);
  2270. break;
  2271. }
  2272. default:
  2273. break;
  2274. }
  2275. }
  2276. bool VMainWindow::isHeadingSequenceApplicable() const
  2277. {
  2278. if (!m_curTab) {
  2279. return false;
  2280. }
  2281. Q_ASSERT(m_curFile);
  2282. if (!m_curFile->isModifiable()
  2283. || m_curFile->getDocType() != DocType::Markdown) {
  2284. return false;
  2285. }
  2286. return true;
  2287. }
  2288. // Popup the attachment list if it is enabled.
  2289. bool VMainWindow::showAttachmentListByCaptain(void *p_target, void *p_data)
  2290. {
  2291. Q_UNUSED(p_data);
  2292. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2293. if (obj->m_attachmentBtn->isEnabled()) {
  2294. obj->m_attachmentBtn->showPopupWidget();
  2295. }
  2296. return true;
  2297. }
  2298. bool VMainWindow::locateCurrentFileByCaptain(void *p_target, void *p_data)
  2299. {
  2300. Q_UNUSED(p_data);
  2301. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2302. if (obj->m_curFile) {
  2303. if (obj->locateFile(obj->m_curFile)) {
  2304. return false;
  2305. }
  2306. }
  2307. return true;
  2308. }
  2309. bool VMainWindow::toggleExpandModeByCaptain(void *p_target, void *p_data)
  2310. {
  2311. Q_UNUSED(p_data);
  2312. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2313. obj->expandViewAct->trigger();
  2314. return true;
  2315. }
  2316. bool VMainWindow::toggleOnePanelViewByCaptain(void *p_target, void *p_data)
  2317. {
  2318. Q_UNUSED(p_data);
  2319. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2320. if (obj->m_panelViewState == PanelViewState::TwoPanels) {
  2321. obj->onePanelView();
  2322. } else {
  2323. obj->twoPanelView();
  2324. }
  2325. return true;
  2326. }
  2327. bool VMainWindow::discardAndReadByCaptain(void *p_target, void *p_data)
  2328. {
  2329. Q_UNUSED(p_data);
  2330. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2331. if (obj->m_curTab) {
  2332. obj->m_discardExitAct->trigger();
  2333. obj->m_curTab->setFocus();
  2334. return false;
  2335. }
  2336. return true;
  2337. }
  2338. bool VMainWindow::toggleToolsDockByCaptain(void *p_target, void *p_data)
  2339. {
  2340. Q_UNUSED(p_data);
  2341. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2342. obj->m_toolDock->setVisible(!obj->m_toolDock->isVisible());
  2343. return true;
  2344. }
  2345. bool VMainWindow::toggleSearchDockByCaptain(void *p_target, void *p_data)
  2346. {
  2347. Q_UNUSED(p_data);
  2348. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2349. bool visible = obj->m_searchDock->isVisible();
  2350. obj->m_searchDock->setVisible(!visible);
  2351. if (!visible) {
  2352. obj->m_searcher->focusToSearch();
  2353. return false;
  2354. }
  2355. return true;
  2356. }
  2357. bool VMainWindow::closeFileByCaptain(void *p_target, void *p_data)
  2358. {
  2359. Q_UNUSED(p_data);
  2360. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2361. obj->closeCurrentFile();
  2362. QWidget *nextFocus = obj->m_editArea->getCurrentTab();
  2363. if (nextFocus) {
  2364. nextFocus->setFocus();
  2365. } else {
  2366. obj->m_fileList->setFocus();
  2367. }
  2368. return false;
  2369. }
  2370. bool VMainWindow::shortcutsHelpByCaptain(void *p_target, void *p_data)
  2371. {
  2372. Q_UNUSED(p_data);
  2373. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2374. obj->shortcutsHelp();
  2375. return false;
  2376. }
  2377. bool VMainWindow::flushLogFileByCaptain(void *p_target, void *p_data)
  2378. {
  2379. Q_UNUSED(p_target);
  2380. Q_UNUSED(p_data);
  2381. #if defined(QT_NO_DEBUG)
  2382. // Flush g_logFile.
  2383. g_logFile.flush();
  2384. #endif
  2385. return true;
  2386. }
  2387. bool VMainWindow::exportByCaptain(void *p_target, void *p_data)
  2388. {
  2389. Q_UNUSED(p_data);
  2390. VMainWindow *obj = static_cast<VMainWindow *>(p_target);
  2391. QTimer::singleShot(50, obj, SLOT(handleExportAct()));
  2392. return true;
  2393. }
  2394. void VMainWindow::promptNewNotebookIfEmpty()
  2395. {
  2396. if (vnote->getNotebooks().isEmpty()) {
  2397. m_notebookSelector->newNotebook();
  2398. }
  2399. }
  2400. void VMainWindow::initShortcuts()
  2401. {
  2402. QString keySeq = g_config->getShortcutKeySequence("CloseNote");
  2403. qDebug() << "set CloseNote shortcut to" << keySeq;
  2404. if (!keySeq.isEmpty()) {
  2405. QShortcut *closeNoteShortcut = new QShortcut(QKeySequence(keySeq), this);
  2406. closeNoteShortcut->setContext(Qt::WidgetWithChildrenShortcut);
  2407. connect(closeNoteShortcut, &QShortcut::activated,
  2408. this, &VMainWindow::closeCurrentFile);
  2409. }
  2410. }
  2411. void VMainWindow::openFlashPage()
  2412. {
  2413. openFiles(QStringList() << g_config->getFlashPage(),
  2414. false,
  2415. OpenFileMode::Edit,
  2416. true);
  2417. }
  2418. void VMainWindow::initHeadingButton(QToolBar *p_tb)
  2419. {
  2420. m_headingBtn = new QPushButton(VIconUtils::toolButtonIcon(":/resources/icons/heading.svg"),
  2421. "",
  2422. this);
  2423. m_headingBtn->setToolTip(tr("Headings"));
  2424. m_headingBtn->setProperty("CornerBtn", true);
  2425. m_headingBtn->setFocusPolicy(Qt::NoFocus);
  2426. m_headingBtn->setEnabled(false);
  2427. QMenu *menu = new QMenu(this);
  2428. QString text(tr("Heading %1"));
  2429. QString tooltip(tr("Heading %1\t%2"));
  2430. QWidgetAction *wact = new QWidgetAction(menu);
  2431. wact->setData(1);
  2432. VButtonMenuItem *w = new VButtonMenuItem(wact, text.arg(1), this);
  2433. w->setToolTip(tooltip.arg(1).arg(VUtils::getShortcutText("Ctrl+1")));
  2434. w->setProperty("Heading1", true);
  2435. wact->setDefaultWidget(w);
  2436. menu->addAction(wact);
  2437. wact = new QWidgetAction(menu);
  2438. wact->setData(2);
  2439. w = new VButtonMenuItem(wact, text.arg(2), this);
  2440. w->setToolTip(tooltip.arg(2).arg(VUtils::getShortcutText("Ctrl+2")));
  2441. w->setProperty("Heading2", true);
  2442. wact->setDefaultWidget(w);
  2443. menu->addAction(wact);
  2444. wact = new QWidgetAction(menu);
  2445. wact->setData(3);
  2446. w = new VButtonMenuItem(wact, text.arg(3), this);
  2447. w->setToolTip(tooltip.arg(3).arg(VUtils::getShortcutText("Ctrl+3")));
  2448. w->setProperty("Heading3", true);
  2449. wact->setDefaultWidget(w);
  2450. menu->addAction(wact);
  2451. wact = new QWidgetAction(menu);
  2452. wact->setData(4);
  2453. w = new VButtonMenuItem(wact, text.arg(4), this);
  2454. w->setToolTip(tooltip.arg(4).arg(VUtils::getShortcutText("Ctrl+4")));
  2455. w->setProperty("Heading4", true);
  2456. wact->setDefaultWidget(w);
  2457. menu->addAction(wact);
  2458. wact = new QWidgetAction(menu);
  2459. wact->setData(5);
  2460. w = new VButtonMenuItem(wact, text.arg(5), this);
  2461. w->setToolTip(tooltip.arg(5).arg(VUtils::getShortcutText("Ctrl+5")));
  2462. w->setProperty("Heading5", true);
  2463. wact->setDefaultWidget(w);
  2464. menu->addAction(wact);
  2465. wact = new QWidgetAction(menu);
  2466. wact->setData(6);
  2467. w = new VButtonMenuItem(wact, text.arg(6), this);
  2468. w->setToolTip(tooltip.arg(6).arg(VUtils::getShortcutText("Ctrl+6")));
  2469. w->setProperty("Heading6", true);
  2470. wact->setDefaultWidget(w);
  2471. menu->addAction(wact);
  2472. wact = new QWidgetAction(menu);
  2473. wact->setData(0);
  2474. w = new VButtonMenuItem(wact, tr("Clear"), this);
  2475. w->setToolTip(tr("Clear\t%1").arg(VUtils::getShortcutText("Ctrl+7")));
  2476. wact->setDefaultWidget(w);
  2477. menu->addAction(wact);
  2478. connect(menu, &QMenu::triggered,
  2479. this, [this, menu](QAction *p_action) {
  2480. if (m_curTab) {
  2481. int level = p_action->data().toInt();
  2482. m_curTab->decorateText(TextDecoration::Heading, level);
  2483. }
  2484. menu->hide();
  2485. });
  2486. m_headingBtn->setMenu(menu);
  2487. p_tb->addWidget(m_headingBtn);
  2488. }
  2489. void VMainWindow::initThemeMenu(QMenu *p_menu)
  2490. {
  2491. QMenu *themeMenu = p_menu->addMenu(tr("Theme"));
  2492. themeMenu->setToolTipsVisible(true);
  2493. QAction *addAct = newAction(VIconUtils::menuIcon(":/resources/icons/add_style.svg"),
  2494. tr("Add Theme"),
  2495. themeMenu);
  2496. addAct->setToolTip(tr("Add custom theme"));
  2497. connect(addAct, &QAction::triggered,
  2498. this, [this]() {
  2499. VTipsDialog dialog(VUtils::getDocFile("tips_add_theme.md"),
  2500. tr("Add Theme"),
  2501. []() {
  2502. QUrl url = QUrl::fromLocalFile(g_config->getThemeConfigFolder());
  2503. QDesktopServices::openUrl(url);
  2504. },
  2505. this);
  2506. dialog.exec();
  2507. });
  2508. themeMenu->addAction(addAct);
  2509. QActionGroup *ag = new QActionGroup(this);
  2510. connect(ag, &QActionGroup::triggered,
  2511. this, [this](QAction *p_action) {
  2512. if (!p_action) {
  2513. return;
  2514. }
  2515. QString data = p_action->data().toString();
  2516. g_config->setTheme(data);
  2517. });
  2518. QList<QString> themes = g_config->getThemes();
  2519. QString theme = g_config->getTheme();
  2520. for (auto const &item : themes) {
  2521. QAction *act = new QAction(item, ag);
  2522. act->setToolTip(tr("Set as the theme of VNote (restart VNote to make it work)"));
  2523. act->setCheckable(true);
  2524. act->setData(item);
  2525. // Add it to the menu.
  2526. themeMenu->addAction(act);
  2527. if (theme == item) {
  2528. act->setChecked(true);
  2529. }
  2530. }
  2531. }
  2532. void VMainWindow::customShortcut()
  2533. {
  2534. VTipsDialog dialog(VUtils::getDocFile("tips_custom_shortcut.md"),
  2535. tr("Custom Shortcuts"),
  2536. []() {
  2537. #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
  2538. // On macOS, it seems that we could not open that ini file directly.
  2539. QUrl url = QUrl::fromLocalFile(g_config->getConfigFolder());
  2540. #else
  2541. QUrl url = QUrl::fromLocalFile(g_config->getConfigFilePath());
  2542. #endif
  2543. QDesktopServices::openUrl(url);
  2544. },
  2545. this);
  2546. dialog.exec();
  2547. }
  2548. void VMainWindow::initVimCmd()
  2549. {
  2550. m_vimCmd = new VVimCmdLineEdit(this);
  2551. m_vimCmd->setProperty("VimCommandLine", true);
  2552. connect(m_vimCmd, &VVimCmdLineEdit::commandCancelled,
  2553. this, [this]() {
  2554. if (m_curTab) {
  2555. m_curTab->focusTab();
  2556. }
  2557. // NOTICE: should not hide before setting focus to edit tab.
  2558. m_vimCmd->hide();
  2559. if (m_curTab) {
  2560. m_curTab->handleVimCmdCommandCancelled();
  2561. }
  2562. });
  2563. connect(m_vimCmd, &VVimCmdLineEdit::commandFinished,
  2564. this, [this](VVim::CommandLineType p_type, const QString &p_cmd) {
  2565. if (m_curTab) {
  2566. m_curTab->focusTab();
  2567. }
  2568. m_vimCmd->hide();
  2569. // Hide the cmd line edit before execute the command.
  2570. // If we execute the command first, we will get Chinese input
  2571. // method enabled after returning to read mode.
  2572. if (m_curTab) {
  2573. m_curTab->handleVimCmdCommandFinished(p_type, p_cmd);
  2574. }
  2575. });
  2576. connect(m_vimCmd, &VVimCmdLineEdit::commandChanged,
  2577. this, [this](VVim::CommandLineType p_type, const QString &p_cmd) {
  2578. if (m_curTab) {
  2579. m_curTab->handleVimCmdCommandChanged(p_type, p_cmd);
  2580. }
  2581. });
  2582. connect(m_vimCmd, &VVimCmdLineEdit::requestNextCommand,
  2583. this, [this](VVim::CommandLineType p_type, const QString &p_cmd) {
  2584. if (m_curTab) {
  2585. QString cmd = m_curTab->handleVimCmdRequestNextCommand(p_type, p_cmd);
  2586. if (!cmd.isNull()) {
  2587. m_vimCmd->setCommand(cmd);
  2588. } else {
  2589. m_vimCmd->restoreUserLastInput();
  2590. }
  2591. }
  2592. });
  2593. connect(m_vimCmd, &VVimCmdLineEdit::requestPreviousCommand,
  2594. this, [this](VVim::CommandLineType p_type, const QString &p_cmd) {
  2595. if (m_curTab) {
  2596. QString cmd = m_curTab->handleVimCmdRequestPreviousCommand(p_type, p_cmd);
  2597. if (!cmd.isNull()) {
  2598. m_vimCmd->setCommand(cmd);
  2599. }
  2600. }
  2601. });
  2602. connect(m_vimCmd, &VVimCmdLineEdit::requestRegister,
  2603. this, [this](int p_key, int p_modifiers){
  2604. if (m_curTab) {
  2605. QString val = m_curTab->handleVimCmdRequestRegister(p_key, p_modifiers);
  2606. if (!val.isEmpty()) {
  2607. m_vimCmd->setText(m_vimCmd->text() + val);
  2608. }
  2609. }
  2610. });
  2611. m_vimCmd->hide();
  2612. }
  2613. void VMainWindow::toggleEditReadMode()
  2614. {
  2615. if (!m_curTab) {
  2616. return;
  2617. }
  2618. if (m_curTab->isEditMode()) {
  2619. // Save changes and read.
  2620. m_editArea->saveAndReadFile();
  2621. } else {
  2622. // Edit.
  2623. m_editArea->editFile();
  2624. }
  2625. }
  2626. void VMainWindow::updateEditReadAct(const VEditTab *p_tab)
  2627. {
  2628. static QIcon editIcon = VIconUtils::toolButtonIcon(":/resources/icons/edit_note.svg");
  2629. static QString editText;
  2630. static QIcon readIcon = VIconUtils::toolButtonIcon(":/resources/icons/save_exit.svg");
  2631. static QString readText;
  2632. if (editText.isEmpty()) {
  2633. QString keySeq = g_config->getShortcutKeySequence("EditReadNote");
  2634. QKeySequence seq(keySeq);
  2635. if (!seq.isEmpty()) {
  2636. QString shortcutText = VUtils::getShortcutText(keySeq);
  2637. editText = tr("Edit\t%1").arg(shortcutText);
  2638. readText = tr("Save Changes And Read\t%1").arg(shortcutText);
  2639. m_editReadAct->setShortcut(seq);
  2640. } else {
  2641. editText = tr("Edit");
  2642. readText = tr("Save Changes And Read");
  2643. }
  2644. }
  2645. if (!p_tab || !p_tab->isEditMode()) {
  2646. // Edit.
  2647. m_editReadAct->setIcon(editIcon);
  2648. m_editReadAct->setText(editText);
  2649. m_editReadAct->setStatusTip(tr("Edit current note"));
  2650. m_discardExitAct->setEnabled(false);
  2651. } else {
  2652. // Read.
  2653. m_editReadAct->setIcon(readIcon);
  2654. m_editReadAct->setText(readText);
  2655. m_editReadAct->setStatusTip(tr("Save changes and exit edit mode"));
  2656. m_discardExitAct->setEnabled(true);
  2657. }
  2658. m_editReadAct->setEnabled(p_tab);
  2659. }
  2660. void VMainWindow::handleExportAct()
  2661. {
  2662. VExportDialog dialog(m_notebookSelector->currentNotebook(),
  2663. directoryTree->currentDirectory(),
  2664. m_curFile,
  2665. m_cart,
  2666. g_config->getMdConverterType(),
  2667. this);
  2668. dialog.exec();
  2669. }
  2670. VNotebook *VMainWindow::getCurrentNotebook() const
  2671. {
  2672. return m_notebookSelector->currentNotebook();
  2673. }
  2674. void VMainWindow::activateUniversalEntry()
  2675. {
  2676. if (!m_ue) {
  2677. initUniversalEntry();
  2678. }
  2679. m_captain->setCaptainModeEnabled(false);
  2680. // Move it to the top left corner of edit area.
  2681. QPoint topLeft = m_editArea->mapToGlobal(QPoint(0, 0));
  2682. QRect eaRect = m_editArea->editAreaRect();
  2683. topLeft += eaRect.topLeft();
  2684. // Use global position.
  2685. m_ue->move(topLeft);
  2686. eaRect.moveTop(0);
  2687. m_ue->setAvailableRect(eaRect);
  2688. m_ue->show();
  2689. m_ue->raise();
  2690. }
  2691. void VMainWindow::initUniversalEntry()
  2692. {
  2693. m_ue = new VUniversalEntry(this);
  2694. m_ue->hide();
  2695. m_ue->setWindowFlags(Qt::Popup
  2696. | Qt::FramelessWindowHint
  2697. | Qt::NoDropShadowWindowHint);
  2698. connect(m_ue, &VUniversalEntry::exited,
  2699. this, [this]() {
  2700. m_captain->setCaptainModeEnabled(true);
  2701. });
  2702. // Register entries.
  2703. VSearchUE *searchUE = new VSearchUE(this);
  2704. m_ue->registerEntry('q', searchUE, VSearchUE::Name_FolderNote_AllNotebook);
  2705. m_ue->registerEntry('a', searchUE, VSearchUE::Content_Note_AllNotebook);
  2706. m_ue->registerEntry('w', searchUE, VSearchUE::Name_Notebook_AllNotebook);
  2707. m_ue->registerEntry('e', searchUE, VSearchUE::Name_FolderNote_CurrentNotebook);
  2708. m_ue->registerEntry('d', searchUE, VSearchUE::Content_Note_CurrentNotebook);
  2709. m_ue->registerEntry('r', searchUE, VSearchUE::Name_FolderNote_CurrentFolder);
  2710. m_ue->registerEntry('f', searchUE, VSearchUE::Content_Note_CurrentFolder);
  2711. m_ue->registerEntry('t', searchUE, VSearchUE::Name_Note_Buffer);
  2712. m_ue->registerEntry('g', searchUE, VSearchUE::Content_Note_Buffer);
  2713. m_ue->registerEntry('b', searchUE, VSearchUE::Outline_Note_Buffer);
  2714. m_ue->registerEntry('y', new VOutlineUE(this), 0);
  2715. m_ue->registerEntry('h', searchUE, VSearchUE::Path_FolderNote_AllNotebook);
  2716. m_ue->registerEntry('n', searchUE, VSearchUE::Path_FolderNote_CurrentNotebook);
  2717. m_ue->registerEntry('m', new VListFolderUE(this), 0);
  2718. m_ue->registerEntry('?', new VHelpUE(this), 0);
  2719. }
  2720. void VMainWindow::checkNotebooks()
  2721. {
  2722. bool updated = false;
  2723. QVector<VNotebook *> &notebooks = g_vnote->getNotebooks();
  2724. for (int i = 0; i < notebooks.size(); ++i) {
  2725. VNotebook *nb = notebooks[i];
  2726. if (nb->isValid()) {
  2727. continue;
  2728. }
  2729. VFixNotebookDialog dialog(nb, notebooks, this);
  2730. if (dialog.exec()) {
  2731. qDebug() << "fix path of notebook" << nb->getName() << "to" << dialog.getPathInput();
  2732. nb->updatePath(dialog.getPathInput());
  2733. } else {
  2734. notebooks.removeOne(nb);
  2735. --i;
  2736. }
  2737. updated = true;
  2738. }
  2739. if (updated) {
  2740. g_config->setNotebooks(notebooks);
  2741. m_notebookSelector->update();
  2742. }
  2743. m_notebookSelector->restoreCurrentNotebook();
  2744. }
  2745. void VMainWindow::setMenuBarVisible(bool p_visible)
  2746. {
  2747. // Hiding the menubar will disable the shortcut of QActions.
  2748. if (p_visible) {
  2749. menuBar()->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
  2750. } else {
  2751. menuBar()->setFixedHeight(0);
  2752. }
  2753. }
  2754. void VMainWindow::postChangePanelView()
  2755. {
  2756. const int minVal = 10;
  2757. bool needUpdate = false;
  2758. QList<int> sizes = m_mainSplitter->sizes();
  2759. switch (m_panelViewState) {
  2760. case PanelViewState::SinglePanel:
  2761. if (sizes[1] == 0) {
  2762. sizes[1] = minVal;
  2763. needUpdate = true;
  2764. }
  2765. if (sizes[2] == 0) {
  2766. sizes[2] = minVal;
  2767. needUpdate = true;
  2768. }
  2769. break;
  2770. case PanelViewState::TwoPanels:
  2771. if (sizes[0] == 0) {
  2772. sizes[0] = minVal;
  2773. needUpdate = true;
  2774. }
  2775. if (sizes[1] == 0) {
  2776. sizes[1] = minVal;
  2777. needUpdate = true;
  2778. }
  2779. if (sizes[2] == 0) {
  2780. sizes[2] = minVal;
  2781. needUpdate = true;
  2782. }
  2783. break;
  2784. case PanelViewState::CompactMode:
  2785. {
  2786. if (sizes[0] == 0) {
  2787. sizes[0] = minVal;
  2788. needUpdate = true;
  2789. }
  2790. if (sizes[2] == 0) {
  2791. sizes[2] = minVal;
  2792. needUpdate = true;
  2793. }
  2794. bool naviUpdate = false;
  2795. QList<int> naviSizes = m_naviSplitter->sizes();
  2796. if (naviSizes[0] == 0) {
  2797. naviSizes[0] = minVal;
  2798. naviUpdate = true;
  2799. }
  2800. if (naviSizes[1] == 0) {
  2801. naviSizes[1] = minVal;
  2802. naviUpdate = true;
  2803. }
  2804. if (naviUpdate) {
  2805. m_naviSplitter->setSizes(naviSizes);
  2806. }
  2807. break;
  2808. }
  2809. default:
  2810. break;
  2811. }
  2812. if (needUpdate) {
  2813. m_mainSplitter->setSizes(sizes);
  2814. }
  2815. }