vconfigmanager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "vconfigmanager.h"
  2. #include <QDir>
  3. #include <QFile>
  4. #include <QString>
  5. #include <QJsonArray>
  6. #include <QJsonObject>
  7. #include <QJsonDocument>
  8. #include <QtDebug>
  9. #include <QTextEdit>
  10. #include "utils/vutils.h"
  11. #include "vstyleparser.h"
  12. const QString VConfigManager::orgName = QString("tamlok");
  13. const QString VConfigManager::appName = QString("vnote");
  14. const QString VConfigManager::c_version = QString("1.1");
  15. const QString VConfigManager::dirConfigFileName = QString(".vnote.json");
  16. const QString VConfigManager::defaultConfigFilePath = QString(":/resources/vnote.ini");
  17. VConfigManager::VConfigManager()
  18. : userSettings(NULL), defaultSettings(NULL)
  19. {
  20. }
  21. VConfigManager::~VConfigManager()
  22. {
  23. if (userSettings) {
  24. delete userSettings;
  25. }
  26. if (defaultSettings) {
  27. delete defaultSettings;
  28. }
  29. }
  30. void VConfigManager::initialize()
  31. {
  32. userSettings = new QSettings(QSettings::IniFormat, QSettings::UserScope, orgName, appName);
  33. defaultSettings = new QSettings(defaultConfigFilePath, QSettings::IniFormat);
  34. m_editorFontSize = getConfigFromSettings("global", "editor_font_size").toInt();
  35. if (m_editorFontSize <= 0) {
  36. m_editorFontSize = 12;
  37. }
  38. baseEditFont.setPointSize(m_editorFontSize);
  39. baseEditPalette = QTextEdit().palette();
  40. welcomePagePath = getConfigFromSettings("global", "welcome_page_path").toString();
  41. templateCssUrl = getConfigFromSettings("global", "template_css_url").toString();
  42. curNotebookIndex = getConfigFromSettings("global", "current_notebook").toInt();
  43. markdownExtensions = hoedown_extensions(HOEDOWN_EXT_TABLES | HOEDOWN_EXT_FENCED_CODE |
  44. HOEDOWN_EXT_HIGHLIGHT | HOEDOWN_EXT_AUTOLINK |
  45. HOEDOWN_EXT_QUOTE | HOEDOWN_EXT_MATH | HOEDOWN_EXT_MATH_EXPLICIT);
  46. mdConverterType = (MarkdownConverterType)getConfigFromSettings("global", "markdown_converter").toInt();
  47. tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt();
  48. isExpandTab = getConfigFromSettings("global", "is_expand_tab").toBool();
  49. m_highlightCursorLine = getConfigFromSettings("global", "highlight_cursor_line").toBool();
  50. m_highlightSelectedWord = getConfigFromSettings("global", "highlight_selected_word").toBool();
  51. m_highlightSearchedWord = getConfigFromSettings("global", "highlight_searched_word").toBool();
  52. m_autoIndent = getConfigFromSettings("global", "auto_indent").toBool();
  53. m_autoList = getConfigFromSettings("global", "auto_list").toBool();
  54. readPredefinedColorsFromSettings();
  55. curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString();
  56. updatePaletteColor();
  57. curRenderBackgroundColor = getConfigFromSettings("global",
  58. "current_render_background_color").toString();
  59. m_toolsDockChecked = getConfigFromSettings("session", "tools_dock_checked").toBool();
  60. m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray();
  61. m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray();
  62. m_mainSplitterState = getConfigFromSettings("session", "main_splitter_state").toByteArray();
  63. updateMarkdownEditStyle();
  64. m_findCaseSensitive = getConfigFromSettings("global",
  65. "find_case_sensitive").toBool();
  66. m_findWholeWordOnly = getConfigFromSettings("global",
  67. "find_whole_word_only").toBool();
  68. m_findRegularExpression = getConfigFromSettings("global",
  69. "find_regular_expression").toBool();
  70. m_findIncrementalSearch = getConfigFromSettings("global",
  71. "find_incremental_search").toBool();
  72. m_language = getConfigFromSettings("global", "language").toString();
  73. m_enableMermaid = getConfigFromSettings("global", "enable_mermaid").toBool();
  74. m_enableMathjax = getConfigFromSettings("global", "enable_mathjax").toBool();
  75. m_webZoomFactor = getConfigFromSettings("global", "web_zoom_factor").toReal();
  76. if (!isCustomWebZoomFactor()) {
  77. // Calculate the zoom factor based on DPI.
  78. m_webZoomFactor = VUtils::calculateScaleFactor();
  79. qDebug() << "set WebZoomFactor to" << m_webZoomFactor;
  80. }
  81. }
  82. void VConfigManager::readPredefinedColorsFromSettings()
  83. {
  84. predefinedColors.clear();
  85. int size = defaultSettings->beginReadArray("predefined_colors");
  86. for (int i = 0; i < size; ++i) {
  87. defaultSettings->setArrayIndex(i);
  88. VColor color;
  89. color.name = defaultSettings->value("name").toString();
  90. color.rgb = defaultSettings->value("rgb").toString();
  91. predefinedColors.append(color);
  92. }
  93. defaultSettings->endArray();
  94. qDebug() << "read" << predefinedColors.size()
  95. << "pre-defined colors from [predefined_colors] section";
  96. }
  97. void VConfigManager::readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent)
  98. {
  99. Q_ASSERT(p_notebooks.isEmpty());
  100. int size = userSettings->beginReadArray("notebooks");
  101. for (int i = 0; i < size; ++i) {
  102. userSettings->setArrayIndex(i);
  103. QString name = userSettings->value("name").toString();
  104. QString path = userSettings->value("path").toString();
  105. VNotebook *notebook = new VNotebook(name, path, parent);
  106. p_notebooks.append(notebook);
  107. }
  108. userSettings->endArray();
  109. qDebug() << "read" << p_notebooks.size()
  110. << "notebook items from [notebooks] section";
  111. }
  112. void VConfigManager::writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks)
  113. {
  114. // Clear it first
  115. userSettings->beginGroup("notebooks");
  116. userSettings->remove("");
  117. userSettings->endGroup();
  118. userSettings->beginWriteArray("notebooks");
  119. for (int i = 0; i < p_notebooks.size(); ++i) {
  120. userSettings->setArrayIndex(i);
  121. const VNotebook &notebook = *p_notebooks[i];
  122. userSettings->setValue("name", notebook.getName());
  123. userSettings->setValue("path", notebook.getPath());
  124. }
  125. userSettings->endArray();
  126. qDebug() << "write" << p_notebooks.size()
  127. << "notebook items in [notebooks] section";
  128. }
  129. QVariant VConfigManager::getConfigFromSettings(const QString &section, const QString &key)
  130. {
  131. QString fullKey = section + "/" + key;
  132. // First, look up the user-scoped config file
  133. QVariant value = userSettings->value(fullKey);
  134. if (!value.isNull()) {
  135. qDebug() << "user config:" << fullKey << value.toString();
  136. return value;
  137. }
  138. // Second, look up the default config file
  139. value = defaultSettings->value(fullKey);
  140. qDebug() << "default config:" << fullKey << value.toString();
  141. return value;
  142. }
  143. void VConfigManager::setConfigToSettings(const QString &section, const QString &key, const QVariant &value)
  144. {
  145. // Set the user-scoped config file
  146. QString fullKey = section + "/" + key;
  147. userSettings->setValue(fullKey, value);
  148. qDebug() << "set user config:" << fullKey << value.toString();
  149. }
  150. QJsonObject VConfigManager::readDirectoryConfig(const QString &path)
  151. {
  152. QString configFile = QDir(path).filePath(dirConfigFileName);
  153. qDebug() << "read config file:" << configFile;
  154. QFile config(configFile);
  155. if (!config.open(QIODevice::ReadOnly)) {
  156. qWarning() << "fail to read directory configuration file:"
  157. << configFile;
  158. return QJsonObject();
  159. }
  160. QByteArray configData = config.readAll();
  161. return QJsonDocument::fromJson(configData).object();
  162. }
  163. bool VConfigManager::directoryConfigExist(const QString &path)
  164. {
  165. QString configFile = QDir(path).filePath(dirConfigFileName);
  166. QFile config(configFile);
  167. return config.exists();
  168. }
  169. bool VConfigManager::writeDirectoryConfig(const QString &path, const QJsonObject &configJson)
  170. {
  171. QString configFile = QDir(path).filePath(dirConfigFileName);
  172. qDebug() << "write config file:" << configFile;
  173. QFile config(configFile);
  174. if (!config.open(QIODevice::WriteOnly)) {
  175. qWarning() << "fail to open directory configuration file for write:"
  176. << configFile;
  177. return false;
  178. }
  179. QJsonDocument configDoc(configJson);
  180. config.write(configDoc.toJson());
  181. return true;
  182. }
  183. bool VConfigManager::deleteDirectoryConfig(const QString &path)
  184. {
  185. QString configFile = QDir(path).filePath(dirConfigFileName);
  186. QFile config(configFile);
  187. if (!config.remove()) {
  188. qWarning() << "fail to delete directory configuration file:"
  189. << configFile;
  190. return false;
  191. }
  192. qDebug() << "delete config file:" << configFile;
  193. return true;
  194. }
  195. void VConfigManager::updateMarkdownEditStyle()
  196. {
  197. static const QString defaultCurrentLineBackground = "#C5CAE9";
  198. static const QString defaultCurrentLineVimBackground = "#A5D6A7";
  199. // Read style file .mdhl
  200. QString file(":/resources/styles/default.mdhl");
  201. QString styleStr = VUtils::readFileFromDisk(file);
  202. if (styleStr.isEmpty()) {
  203. return;
  204. }
  205. VStyleParser parser;
  206. parser.parseMarkdownStyle(styleStr);
  207. mdHighlightingStyles = parser.fetchMarkdownStyles(baseEditFont);
  208. mdEditPalette = baseEditPalette;
  209. mdEditFont = baseEditFont;
  210. QMap<QString, QMap<QString, QString>> styles;
  211. parser.fetchMarkdownEditorStyles(mdEditPalette, mdEditFont, styles);
  212. m_editorCurrentLineBackground = defaultCurrentLineBackground;
  213. m_editorCurrentLineVimBackground = defaultCurrentLineVimBackground;
  214. auto editorCurrentLineIt = styles.find("editor-current-line");
  215. if (editorCurrentLineIt != styles.end()) {
  216. auto backgroundIt = editorCurrentLineIt->find("background");
  217. if (backgroundIt != editorCurrentLineIt->end()) {
  218. m_editorCurrentLineBackground = *backgroundIt;
  219. }
  220. auto vimBackgroundIt = editorCurrentLineIt->find("vim-background");
  221. if (vimBackgroundIt != editorCurrentLineIt->end()) {
  222. m_editorCurrentLineVimBackground = "#" + *vimBackgroundIt;
  223. }
  224. }
  225. qDebug() << "editor-current-line:" << m_editorCurrentLineBackground << m_editorCurrentLineVimBackground;
  226. }
  227. void VConfigManager::updatePaletteColor()
  228. {
  229. static const QColor defaultColor = baseEditPalette.color(QPalette::Base);
  230. QColor newColor = defaultColor;
  231. if (curBackgroundColor != "System") {
  232. for (int i = 0; i < predefinedColors.size(); ++i) {
  233. if (predefinedColors[i].name == curBackgroundColor) {
  234. QString rgb = predefinedColors[i].rgb;
  235. if (!rgb.isEmpty()) {
  236. newColor = QColor(VUtils::QRgbFromString(rgb));
  237. }
  238. break;
  239. }
  240. }
  241. }
  242. baseEditPalette.setColor(QPalette::Base, newColor);
  243. // Update markdown editor palette
  244. updateMarkdownEditStyle();
  245. }
  246. void VConfigManager::setWebZoomFactor(qreal p_factor)
  247. {
  248. if (isCustomWebZoomFactor()) {
  249. if (VUtils::realEqual(m_webZoomFactor, p_factor)) {
  250. return;
  251. } else if (VUtils::realEqual(p_factor, -1)) {
  252. m_webZoomFactor = VUtils::calculateScaleFactor();
  253. setConfigToSettings("global", "web_zoom_factor", -1);
  254. return;
  255. }
  256. } else {
  257. if (VUtils::realEqual(p_factor, -1)) {
  258. return;
  259. }
  260. }
  261. m_webZoomFactor = p_factor;
  262. setConfigToSettings("global", "web_zoom_factor", m_webZoomFactor);
  263. }