texteditorconfig.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "texteditorconfig.h"
  2. using namespace vnotex;
  3. #define READSTR(key) readString(appObj, userObj, (key))
  4. #define READBOOL(key) readBool(appObj, userObj, (key))
  5. #define READINT(key) readInt(appObj, userObj, (key))
  6. TextEditorConfig::TextEditorConfig(ConfigMgr *p_mgr, IConfig *p_topConfig)
  7. : IConfig(p_mgr, p_topConfig)
  8. {
  9. m_sessionName = QStringLiteral("text_editor");
  10. }
  11. void TextEditorConfig::init(const QJsonObject &p_app,
  12. const QJsonObject &p_user)
  13. {
  14. const auto appObj = p_app.value(m_sessionName).toObject();
  15. const auto userObj = p_user.value(m_sessionName).toObject();
  16. {
  17. auto lineNumber = READSTR(QStringLiteral("line_number"));
  18. m_lineNumberType = stringToLineNumberType(lineNumber);
  19. }
  20. m_textFoldingEnabled = READBOOL(QStringLiteral("text_folding"));
  21. {
  22. auto inputMode = READSTR(QStringLiteral("input_mode"));
  23. m_inputMode = stringToInputMode(inputMode);
  24. }
  25. {
  26. auto centerCursor = READSTR(QStringLiteral("center_cursor"));
  27. m_centerCursor = stringToCenterCursor(centerCursor);
  28. }
  29. {
  30. auto wrapMode = READSTR(QStringLiteral("wrap_mode"));
  31. m_wrapMode = stringToWrapMode(wrapMode);
  32. }
  33. m_expandTab = READBOOL(QStringLiteral("expand_tab"));
  34. m_tabStopWidth = READINT(QStringLiteral("tab_stop_width"));
  35. m_highlightWhitespace = READBOOL(QStringLiteral("highlight_whitespace"));
  36. m_zoomDelta = READINT(QStringLiteral("zoom_delta"));
  37. m_spellCheckEnabled = READBOOL(QStringLiteral("spell_check"));
  38. }
  39. QJsonObject TextEditorConfig::toJson() const
  40. {
  41. QJsonObject obj;
  42. obj[QStringLiteral("line_number")] = lineNumberTypeToString(m_lineNumberType);
  43. obj[QStringLiteral("text_folding")] = m_textFoldingEnabled;
  44. obj[QStringLiteral("input_mode")] = inputModeToString(m_inputMode);
  45. obj[QStringLiteral("center_cursor")] = centerCursorToString(m_centerCursor);
  46. obj[QStringLiteral("wrap_mode")] = wrapModeToString(m_wrapMode);
  47. obj[QStringLiteral("expand_tab")] = m_expandTab;
  48. obj[QStringLiteral("tab_stop_width")] = m_tabStopWidth;
  49. obj[QStringLiteral("highlight_whitespace")] = m_highlightWhitespace;
  50. obj[QStringLiteral("zoom_delta")] = m_zoomDelta;
  51. obj[QStringLiteral("spell_check")] = m_spellCheckEnabled;
  52. return obj;
  53. }
  54. QString TextEditorConfig::lineNumberTypeToString(LineNumberType p_type) const
  55. {
  56. switch (p_type) {
  57. case LineNumberType::None:
  58. return QStringLiteral("none");
  59. case LineNumberType::Relative:
  60. return QStringLiteral("relative");
  61. default:
  62. return QStringLiteral("absolute");
  63. }
  64. }
  65. TextEditorConfig::LineNumberType TextEditorConfig::stringToLineNumberType(const QString &p_str) const
  66. {
  67. auto lineNumber = p_str.toLower();
  68. if (lineNumber == QStringLiteral("none")) {
  69. return LineNumberType::None;
  70. } else if (lineNumber == QStringLiteral("relative")) {
  71. return LineNumberType::Relative;
  72. } else {
  73. return LineNumberType::Absolute;
  74. }
  75. }
  76. QString TextEditorConfig::inputModeToString(InputMode p_mode) const
  77. {
  78. switch (p_mode) {
  79. case InputMode::ViMode:
  80. return QStringLiteral("vi");
  81. case InputMode::VscodeMode:
  82. return QStringLiteral("vscode");
  83. default:
  84. return QStringLiteral("normal");
  85. }
  86. }
  87. TextEditorConfig::InputMode TextEditorConfig::stringToInputMode(const QString &p_str) const
  88. {
  89. auto inputMode = p_str.toLower();
  90. if (inputMode == QStringLiteral("vi")) {
  91. return InputMode::ViMode;
  92. } else if (inputMode == QStringLiteral("vscode")) {
  93. return InputMode::VscodeMode;
  94. } else {
  95. return InputMode::NormalMode;
  96. }
  97. }
  98. QString TextEditorConfig::centerCursorToString(CenterCursor p_cursor) const
  99. {
  100. switch (p_cursor) {
  101. case CenterCursor::AlwaysCenter:
  102. return QStringLiteral("always");
  103. case CenterCursor::CenterOnBottom:
  104. return QStringLiteral("bottom");
  105. default:
  106. return QStringLiteral("never");
  107. }
  108. }
  109. TextEditorConfig::CenterCursor TextEditorConfig::stringToCenterCursor(const QString &p_str) const
  110. {
  111. auto centerCursor = p_str.toLower();
  112. if (centerCursor == QStringLiteral("always")) {
  113. return CenterCursor::AlwaysCenter;
  114. } else if (centerCursor == QStringLiteral("bottom")) {
  115. return CenterCursor::CenterOnBottom;
  116. } else {
  117. return CenterCursor::NeverCenter;
  118. }
  119. }
  120. QString TextEditorConfig::wrapModeToString(WrapMode p_mode) const
  121. {
  122. switch (p_mode) {
  123. case WrapMode::NoWrap:
  124. return QStringLiteral("none");
  125. case WrapMode::WrapAnywhere:
  126. return QStringLiteral("anywhere");
  127. case WrapMode::WordWrapOrAnywhere:
  128. return QStringLiteral("word_anywhere");
  129. default:
  130. return QStringLiteral("word");
  131. }
  132. }
  133. TextEditorConfig::WrapMode TextEditorConfig::stringToWrapMode(const QString &p_str) const
  134. {
  135. auto centerCursor = p_str.toLower();
  136. if (centerCursor == QStringLiteral("none")) {
  137. return WrapMode::NoWrap;
  138. } else if (centerCursor == QStringLiteral("anywhere")) {
  139. return WrapMode::WrapAnywhere;
  140. } else if (centerCursor == QStringLiteral("word_anywhere")) {
  141. return WrapMode::WordWrapOrAnywhere;
  142. } else {
  143. return WrapMode::WordWrap;
  144. }
  145. }
  146. TextEditorConfig::LineNumberType TextEditorConfig::getLineNumberType() const
  147. {
  148. return m_lineNumberType;
  149. }
  150. void TextEditorConfig::setLineNumberType(TextEditorConfig::LineNumberType p_type)
  151. {
  152. updateConfig(m_lineNumberType, p_type, this);
  153. }
  154. bool TextEditorConfig::getTextFoldingEnabled() const
  155. {
  156. return m_textFoldingEnabled;
  157. }
  158. void TextEditorConfig::setTextFoldingEnabled(bool p_enabled)
  159. {
  160. updateConfig(m_textFoldingEnabled, p_enabled, this);
  161. }
  162. TextEditorConfig::InputMode TextEditorConfig::getInputMode() const
  163. {
  164. return m_inputMode;
  165. }
  166. void TextEditorConfig::setInputMode(TextEditorConfig::InputMode p_mode)
  167. {
  168. updateConfig(m_inputMode, p_mode, this);
  169. }
  170. TextEditorConfig::CenterCursor TextEditorConfig::getCenterCursor() const
  171. {
  172. return m_centerCursor;
  173. }
  174. void TextEditorConfig::setCenterCursor(TextEditorConfig::CenterCursor p_centerCursor)
  175. {
  176. updateConfig(m_centerCursor, p_centerCursor, this);
  177. }
  178. TextEditorConfig::WrapMode TextEditorConfig::getWrapMode() const
  179. {
  180. return m_wrapMode;
  181. }
  182. void TextEditorConfig::setWrapMode(TextEditorConfig::WrapMode p_mode)
  183. {
  184. updateConfig(m_wrapMode, p_mode, this);
  185. }
  186. bool TextEditorConfig::getExpandTabEnabled() const
  187. {
  188. return m_expandTab;
  189. }
  190. void TextEditorConfig::setExpandTabEnabled(bool p_enabled)
  191. {
  192. updateConfig(m_expandTab, p_enabled, this);
  193. }
  194. bool TextEditorConfig::getHighlightWhitespaceEnabled() const
  195. {
  196. return m_highlightWhitespace;
  197. }
  198. void TextEditorConfig::setHighlightWhitespaceEnabled(bool p_enabled)
  199. {
  200. updateConfig(m_highlightWhitespace, p_enabled, this);
  201. }
  202. int TextEditorConfig::getTabStopWidth() const
  203. {
  204. return m_tabStopWidth;
  205. }
  206. void TextEditorConfig::setTabStopWidth(int p_width)
  207. {
  208. updateConfig(m_tabStopWidth, p_width, this);
  209. }
  210. int TextEditorConfig::getZoomDelta() const
  211. {
  212. return m_zoomDelta;
  213. }
  214. void TextEditorConfig::setZoomDelta(int p_delta)
  215. {
  216. updateConfig(m_zoomDelta, p_delta, this);
  217. }
  218. bool TextEditorConfig::isSpellCheckEnabled() const
  219. {
  220. return m_spellCheckEnabled;
  221. }
  222. void TextEditorConfig::setSpellCheckEnabled(bool p_enabled)
  223. {
  224. updateConfig(m_spellCheckEnabled, p_enabled, this);
  225. }