texteditorconfig.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. default:
  82. return QStringLiteral("normal");
  83. }
  84. }
  85. TextEditorConfig::InputMode TextEditorConfig::stringToInputMode(const QString &p_str) const
  86. {
  87. auto inputMode = p_str.toLower();
  88. if (inputMode == QStringLiteral("vi")) {
  89. return InputMode::ViMode;
  90. } else {
  91. return InputMode::NormalMode;
  92. }
  93. }
  94. QString TextEditorConfig::centerCursorToString(CenterCursor p_cursor) const
  95. {
  96. switch (p_cursor) {
  97. case CenterCursor::AlwaysCenter:
  98. return QStringLiteral("always");
  99. case CenterCursor::CenterOnBottom:
  100. return QStringLiteral("bottom");
  101. default:
  102. return QStringLiteral("never");
  103. }
  104. }
  105. TextEditorConfig::CenterCursor TextEditorConfig::stringToCenterCursor(const QString &p_str) const
  106. {
  107. auto centerCursor = p_str.toLower();
  108. if (centerCursor == QStringLiteral("always")) {
  109. return CenterCursor::AlwaysCenter;
  110. } else if (centerCursor == QStringLiteral("bottom")) {
  111. return CenterCursor::CenterOnBottom;
  112. } else {
  113. return CenterCursor::NeverCenter;
  114. }
  115. }
  116. QString TextEditorConfig::wrapModeToString(WrapMode p_mode) const
  117. {
  118. switch (p_mode) {
  119. case WrapMode::NoWrap:
  120. return QStringLiteral("none");
  121. case WrapMode::WrapAnywhere:
  122. return QStringLiteral("anywhere");
  123. case WrapMode::WordWrapOrAnywhere:
  124. return QStringLiteral("word_anywhere");
  125. default:
  126. return QStringLiteral("word");
  127. }
  128. }
  129. TextEditorConfig::WrapMode TextEditorConfig::stringToWrapMode(const QString &p_str) const
  130. {
  131. auto centerCursor = p_str.toLower();
  132. if (centerCursor == QStringLiteral("none")) {
  133. return WrapMode::NoWrap;
  134. } else if (centerCursor == QStringLiteral("anywhere")) {
  135. return WrapMode::WrapAnywhere;
  136. } else if (centerCursor == QStringLiteral("word_anywhere")) {
  137. return WrapMode::WordWrapOrAnywhere;
  138. } else {
  139. return WrapMode::WordWrap;
  140. }
  141. }
  142. TextEditorConfig::LineNumberType TextEditorConfig::getLineNumberType() const
  143. {
  144. return m_lineNumberType;
  145. }
  146. void TextEditorConfig::setLineNumberType(TextEditorConfig::LineNumberType p_type)
  147. {
  148. updateConfig(m_lineNumberType, p_type, this);
  149. }
  150. bool TextEditorConfig::getTextFoldingEnabled() const
  151. {
  152. return m_textFoldingEnabled;
  153. }
  154. void TextEditorConfig::setTextFoldingEnabled(bool p_enabled)
  155. {
  156. updateConfig(m_textFoldingEnabled, p_enabled, this);
  157. }
  158. TextEditorConfig::InputMode TextEditorConfig::getInputMode() const
  159. {
  160. return m_inputMode;
  161. }
  162. void TextEditorConfig::setInputMode(TextEditorConfig::InputMode p_mode)
  163. {
  164. updateConfig(m_inputMode, p_mode, this);
  165. }
  166. TextEditorConfig::CenterCursor TextEditorConfig::getCenterCursor() const
  167. {
  168. return m_centerCursor;
  169. }
  170. void TextEditorConfig::setCenterCursor(TextEditorConfig::CenterCursor p_centerCursor)
  171. {
  172. updateConfig(m_centerCursor, p_centerCursor, this);
  173. }
  174. TextEditorConfig::WrapMode TextEditorConfig::getWrapMode() const
  175. {
  176. return m_wrapMode;
  177. }
  178. void TextEditorConfig::setWrapMode(TextEditorConfig::WrapMode p_mode)
  179. {
  180. updateConfig(m_wrapMode, p_mode, this);
  181. }
  182. bool TextEditorConfig::getExpandTabEnabled() const
  183. {
  184. return m_expandTab;
  185. }
  186. void TextEditorConfig::setExpandTabEnabled(bool p_enabled)
  187. {
  188. updateConfig(m_expandTab, p_enabled, this);
  189. }
  190. bool TextEditorConfig::getHighlightWhitespaceEnabled() const
  191. {
  192. return m_highlightWhitespace;
  193. }
  194. void TextEditorConfig::setHighlightWhitespaceEnabled(bool p_enabled)
  195. {
  196. updateConfig(m_highlightWhitespace, p_enabled, this);
  197. }
  198. int TextEditorConfig::getTabStopWidth() const
  199. {
  200. return m_tabStopWidth;
  201. }
  202. void TextEditorConfig::setTabStopWidth(int p_width)
  203. {
  204. updateConfig(m_tabStopWidth, p_width, this);
  205. }
  206. int TextEditorConfig::getZoomDelta() const
  207. {
  208. return m_zoomDelta;
  209. }
  210. void TextEditorConfig::setZoomDelta(int p_delta)
  211. {
  212. updateConfig(m_zoomDelta, p_delta, this);
  213. }
  214. bool TextEditorConfig::isSpellCheckEnabled() const
  215. {
  216. return m_spellCheckEnabled;
  217. }
  218. void TextEditorConfig::setSpellCheckEnabled(bool p_enabled)
  219. {
  220. updateConfig(m_spellCheckEnabled, p_enabled, this);
  221. }