markdowneditorpage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "markdowneditorpage.h"
  2. #include <QFormLayout>
  3. #include <QVBoxLayout>
  4. #include <QCheckBox>
  5. #include <QGroupBox>
  6. #include <QDoubleSpinBox>
  7. #include <QComboBox>
  8. #include <QSpinBox>
  9. #include <QHBoxLayout>
  10. #include <widgets/widgetsfactory.h>
  11. #include <core/editorconfig.h>
  12. #include <core/markdowneditorconfig.h>
  13. #include <core/configmgr.h>
  14. #include <utils/widgetutils.h>
  15. #include "editorpage.h"
  16. using namespace vnotex;
  17. MarkdownEditorPage::MarkdownEditorPage(QWidget *p_parent)
  18. : SettingsPage(p_parent)
  19. {
  20. setupUI();
  21. }
  22. void MarkdownEditorPage::setupUI()
  23. {
  24. auto mainLayout = new QVBoxLayout(this);
  25. auto generalBox = setupGeneralGroup();
  26. mainLayout->addWidget(generalBox);
  27. auto readBox = setupReadGroup();
  28. mainLayout->addWidget(readBox);
  29. auto editBox = setupEditGroup();
  30. mainLayout->addWidget(editBox);
  31. }
  32. void MarkdownEditorPage::loadInternal()
  33. {
  34. const auto &markdownConfig = ConfigMgr::getInst().getEditorConfig().getMarkdownEditorConfig();
  35. m_insertFileNameAsTitleCheckBox->setChecked(markdownConfig.getInsertFileNameAsTitle());
  36. {
  37. int idx = m_sectionNumberComboBox->findData(static_cast<int>(markdownConfig.getSectionNumberMode()));
  38. Q_ASSERT(idx != -1);
  39. m_sectionNumberComboBox->setCurrentIndex(idx);
  40. m_sectionNumberBaseLevelSpinBox->setValue(markdownConfig.getSectionNumberBaseLevel());
  41. idx = m_sectionNumberStyleComboBox->findData(static_cast<int>(markdownConfig.getSectionNumberStyle()));
  42. Q_ASSERT(idx != -1);
  43. m_sectionNumberStyleComboBox->setCurrentIndex(idx);
  44. }
  45. m_constrainImageWidthCheckBox->setChecked(markdownConfig.getConstrainImageWidthEnabled());
  46. m_zoomFactorSpinBox->setValue(markdownConfig.getZoomFactorInReadMode());
  47. m_constrainInPlacePreviewWidthCheckBox->setChecked(markdownConfig.getConstrainInPlacePreviewWidthEnabled());
  48. m_fetchImagesToLocalCheckBox->setChecked(markdownConfig.getFetchImagesInParseAndPaste());
  49. m_htmlTagCheckBox->setChecked(markdownConfig.getHtmlTagEnabled());
  50. m_autoBreakCheckBox->setChecked(markdownConfig.getAutoBreakEnabled());
  51. m_linkifyCheckBox->setChecked(markdownConfig.getLinkifyEnabled());
  52. m_indentFirstLineCheckBox->setChecked(markdownConfig.getIndentFirstLineEnabled());
  53. m_smartTableCheckBox->setChecked(markdownConfig.getSmartTableEnabled());
  54. m_spellCheckCheckBox->setChecked(markdownConfig.isSpellCheckEnabled());
  55. }
  56. void MarkdownEditorPage::saveInternal()
  57. {
  58. auto &markdownConfig = ConfigMgr::getInst().getEditorConfig().getMarkdownEditorConfig();
  59. markdownConfig.setInsertFileNameAsTitle(m_insertFileNameAsTitleCheckBox->isChecked());
  60. {
  61. auto mode = m_sectionNumberComboBox->currentData().toInt();
  62. markdownConfig.setSectionNumberMode(static_cast<MarkdownEditorConfig::SectionNumberMode>(mode));
  63. if (m_sectionNumberBaseLevelSpinBox->isEnabled()) {
  64. markdownConfig.setSectionNumberBaseLevel(m_sectionNumberBaseLevelSpinBox->value());
  65. }
  66. if (m_sectionNumberStyleComboBox->isEnabled()) {
  67. auto style = m_sectionNumberStyleComboBox->currentData().toInt();
  68. markdownConfig.setSectionNumberStyle(static_cast<MarkdownEditorConfig::SectionNumberStyle>(style));
  69. }
  70. }
  71. markdownConfig.setConstrainImageWidthEnabled(m_constrainImageWidthCheckBox->isChecked());
  72. markdownConfig.setZoomFactorInReadMode(m_zoomFactorSpinBox->value());
  73. markdownConfig.setConstrainInPlacePreviewWidthEnabled(m_constrainInPlacePreviewWidthCheckBox->isChecked());
  74. markdownConfig.setFetchImagesInParseAndPaste(m_fetchImagesToLocalCheckBox->isChecked());
  75. markdownConfig.setHtmlTagEnabled(m_htmlTagCheckBox->isChecked());
  76. markdownConfig.setAutoBreakEnabled(m_autoBreakCheckBox->isChecked());
  77. markdownConfig.setLinkifyEnabled(m_linkifyCheckBox->isChecked());
  78. markdownConfig.setIndentFirstLineEnabled(m_indentFirstLineCheckBox->isChecked());
  79. markdownConfig.setSmartTableEnabled(m_smartTableCheckBox->isChecked());
  80. markdownConfig.setSpellCheckEnabled(m_spellCheckCheckBox->isChecked());
  81. EditorPage::notifyEditorConfigChange();
  82. }
  83. QString MarkdownEditorPage::title() const
  84. {
  85. return tr("Markdown Editor");
  86. }
  87. QGroupBox *MarkdownEditorPage::setupReadGroup()
  88. {
  89. auto box = new QGroupBox(tr("Read"), this);
  90. auto layout = WidgetsFactory::createFormLayout(box);
  91. {
  92. const QString label(tr("Constrain image width"));
  93. m_constrainImageWidthCheckBox = WidgetsFactory::createCheckBox(label, box);
  94. m_constrainImageWidthCheckBox->setToolTip(tr("Constrain image width to the window"));
  95. layout->addRow(m_constrainImageWidthCheckBox);
  96. addSearchItem(label, m_constrainImageWidthCheckBox->toolTip(), m_constrainImageWidthCheckBox);
  97. connect(m_constrainImageWidthCheckBox, &QCheckBox::stateChanged,
  98. this, &MarkdownEditorPage::pageIsChanged);
  99. }
  100. {
  101. m_zoomFactorSpinBox = WidgetsFactory::createDoubleSpinBox(box);
  102. m_zoomFactorSpinBox->setToolTip(tr("Zoom factor in read mode"));
  103. m_zoomFactorSpinBox->setRange(0.1, 10);
  104. m_zoomFactorSpinBox->setSingleStep(0.1);
  105. const QString label(tr("Zoom factor:"));
  106. layout->addRow(label, m_zoomFactorSpinBox);
  107. addSearchItem(label, m_zoomFactorSpinBox->toolTip(), m_zoomFactorSpinBox);
  108. connect(m_zoomFactorSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
  109. this, &MarkdownEditorPage::pageIsChanged);
  110. }
  111. {
  112. const QString label(tr("HTML tag"));
  113. m_htmlTagCheckBox = WidgetsFactory::createCheckBox(label, box);
  114. m_htmlTagCheckBox->setToolTip(tr("Allow HTML tags in source"));
  115. layout->addRow(m_htmlTagCheckBox);
  116. addSearchItem(label, m_htmlTagCheckBox->toolTip(), m_htmlTagCheckBox);
  117. connect(m_htmlTagCheckBox, &QCheckBox::stateChanged,
  118. this, &MarkdownEditorPage::pageIsChanged);
  119. }
  120. {
  121. const QString label(tr("Auto break"));
  122. m_autoBreakCheckBox = WidgetsFactory::createCheckBox(label, box);
  123. m_autoBreakCheckBox->setToolTip(tr("Automatically break a line with '\\n'"));
  124. layout->addRow(m_autoBreakCheckBox);
  125. addSearchItem(label, m_autoBreakCheckBox->toolTip(), m_autoBreakCheckBox);
  126. connect(m_autoBreakCheckBox, &QCheckBox::stateChanged,
  127. this, &MarkdownEditorPage::pageIsChanged);
  128. }
  129. {
  130. const QString label(tr("Linkify"));
  131. m_linkifyCheckBox = WidgetsFactory::createCheckBox(label, box);
  132. m_linkifyCheckBox->setToolTip(tr("Convert URL-like text to links"));
  133. layout->addRow(m_linkifyCheckBox);
  134. addSearchItem(label, m_linkifyCheckBox->toolTip(), m_linkifyCheckBox);
  135. connect(m_linkifyCheckBox, &QCheckBox::stateChanged,
  136. this, &MarkdownEditorPage::pageIsChanged);
  137. }
  138. {
  139. const QString label(tr("Indent first line"));
  140. m_indentFirstLineCheckBox = WidgetsFactory::createCheckBox(label, box);
  141. m_indentFirstLineCheckBox->setToolTip(tr("Indent the first line of each paragraph"));
  142. layout->addRow(m_indentFirstLineCheckBox);
  143. addSearchItem(label, m_indentFirstLineCheckBox->toolTip(), m_indentFirstLineCheckBox);
  144. connect(m_indentFirstLineCheckBox, &QCheckBox::stateChanged,
  145. this, &MarkdownEditorPage::pageIsChanged);
  146. }
  147. return box;
  148. }
  149. QGroupBox *MarkdownEditorPage::setupEditGroup()
  150. {
  151. auto box = new QGroupBox(tr("Edit"), this);
  152. auto layout = WidgetsFactory::createFormLayout(box);
  153. {
  154. const QString label(tr("Insert file name as title"));
  155. m_insertFileNameAsTitleCheckBox = WidgetsFactory::createCheckBox(label, box);
  156. m_insertFileNameAsTitleCheckBox->setToolTip(tr("Insert file name as title when creating note"));
  157. layout->addRow(m_insertFileNameAsTitleCheckBox);
  158. addSearchItem(label, m_insertFileNameAsTitleCheckBox->toolTip(), m_insertFileNameAsTitleCheckBox);
  159. connect(m_insertFileNameAsTitleCheckBox, &QCheckBox::stateChanged,
  160. this, &MarkdownEditorPage::pageIsChanged);
  161. }
  162. {
  163. const QString label(tr("Constrain in-place preview width"));
  164. m_constrainInPlacePreviewWidthCheckBox = WidgetsFactory::createCheckBox(label, box);
  165. m_constrainInPlacePreviewWidthCheckBox->setToolTip(tr("Constrain in-place preview width to the window"));
  166. layout->addRow(m_constrainInPlacePreviewWidthCheckBox);
  167. addSearchItem(label, m_constrainInPlacePreviewWidthCheckBox->toolTip(), m_constrainInPlacePreviewWidthCheckBox);
  168. connect(m_constrainInPlacePreviewWidthCheckBox, &QCheckBox::stateChanged,
  169. this, &MarkdownEditorPage::pageIsChanged);
  170. }
  171. {
  172. const QString label(tr("Fetch images to local in Parse And Paste"));
  173. m_fetchImagesToLocalCheckBox = WidgetsFactory::createCheckBox(label, box);
  174. m_fetchImagesToLocalCheckBox->setToolTip(tr("Fetch images to local in Parse To Markdown And Paste"));
  175. layout->addRow(m_fetchImagesToLocalCheckBox);
  176. addSearchItem(label, m_fetchImagesToLocalCheckBox->toolTip(), m_fetchImagesToLocalCheckBox);
  177. connect(m_fetchImagesToLocalCheckBox, &QCheckBox::stateChanged,
  178. this, &MarkdownEditorPage::pageIsChanged);
  179. }
  180. {
  181. const QString label(tr("Smart table"));
  182. m_smartTableCheckBox = WidgetsFactory::createCheckBox(label, box);
  183. m_smartTableCheckBox->setToolTip(tr("Smart table formation"));
  184. layout->addRow(m_smartTableCheckBox);
  185. addSearchItem(label, m_smartTableCheckBox->toolTip(), m_smartTableCheckBox);
  186. connect(m_smartTableCheckBox, &QCheckBox::stateChanged,
  187. this, &MarkdownEditorPage::pageIsChanged);
  188. }
  189. {
  190. const QString label(tr("Spell check"));
  191. m_spellCheckCheckBox = WidgetsFactory::createCheckBox(label, box);
  192. m_spellCheckCheckBox->setToolTip(tr("Spell check"));
  193. layout->addRow(m_spellCheckCheckBox);
  194. addSearchItem(label, m_spellCheckCheckBox->toolTip(), m_spellCheckCheckBox);
  195. connect(m_spellCheckCheckBox, &QCheckBox::stateChanged,
  196. this, &MarkdownEditorPage::pageIsChanged);
  197. }
  198. return box;
  199. }
  200. QGroupBox *MarkdownEditorPage::setupGeneralGroup()
  201. {
  202. auto box = new QGroupBox(tr("General"), this);
  203. auto layout = WidgetsFactory::createFormLayout(box);
  204. {
  205. auto sectionLayout = new QHBoxLayout();
  206. m_sectionNumberComboBox = WidgetsFactory::createComboBox(this);
  207. m_sectionNumberComboBox->setToolTip(tr("Section number mode"));
  208. m_sectionNumberComboBox->addItem(tr("None"), (int)MarkdownEditorConfig::SectionNumberMode::None);
  209. m_sectionNumberComboBox->addItem(tr("Read"), (int)MarkdownEditorConfig::SectionNumberMode::Read);
  210. m_sectionNumberComboBox->addItem(tr("Edit"), (int)MarkdownEditorConfig::SectionNumberMode::Edit);
  211. sectionLayout->addWidget(m_sectionNumberComboBox);
  212. connect(m_sectionNumberComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  213. this, &MarkdownEditorPage::pageIsChanged);
  214. m_sectionNumberBaseLevelSpinBox = WidgetsFactory::createSpinBox(this);
  215. m_sectionNumberBaseLevelSpinBox->setToolTip(tr("Base level to start section numbering in edit mode"));
  216. m_sectionNumberBaseLevelSpinBox->setRange(1, 6);
  217. m_sectionNumberBaseLevelSpinBox->setSingleStep(1);
  218. sectionLayout->addWidget(m_sectionNumberBaseLevelSpinBox);
  219. connect(m_sectionNumberBaseLevelSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
  220. this, &MarkdownEditorPage::pageIsChanged);
  221. m_sectionNumberStyleComboBox = WidgetsFactory::createComboBox(this);
  222. m_sectionNumberStyleComboBox->setToolTip(tr("Section number style"));
  223. m_sectionNumberStyleComboBox->addItem(tr("1.1."), (int)MarkdownEditorConfig::SectionNumberStyle::DigDotDigDot);
  224. m_sectionNumberStyleComboBox->addItem(tr("1.1"), (int)MarkdownEditorConfig::SectionNumberStyle::DigDotDig);
  225. sectionLayout->addWidget(m_sectionNumberStyleComboBox);
  226. connect(m_sectionNumberStyleComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  227. this, &MarkdownEditorPage::pageIsChanged);
  228. connect(m_sectionNumberComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  229. this, [this](int p_index) {
  230. m_sectionNumberBaseLevelSpinBox->setEnabled(p_index != MarkdownEditorConfig::SectionNumberMode::None);
  231. m_sectionNumberStyleComboBox->setEnabled(p_index == MarkdownEditorConfig::SectionNumberMode::Edit);
  232. });
  233. const QString label(tr("Section number:"));
  234. layout->addRow(label, sectionLayout);
  235. addSearchItem(label, m_sectionNumberComboBox->toolTip(), m_sectionNumberComboBox);
  236. }
  237. return box;
  238. }