vstyleparser.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "vstyleparser.h"
  2. #include <QFont>
  3. #include <QPalette>
  4. #include <QTextEdit>
  5. #include <QColor>
  6. #include <QBrush>
  7. #include <QVector>
  8. #include <QtDebug>
  9. VStyleParser::VStyleParser()
  10. {
  11. markdownStyles = NULL;
  12. }
  13. VStyleParser::~VStyleParser()
  14. {
  15. if (markdownStyles) {
  16. pmh_free_style_collection(markdownStyles);
  17. }
  18. }
  19. QColor VStyleParser::QColorFromPmhAttr(pmh_attr_argb_color *attr) const
  20. {
  21. return QColor(attr->red, attr->green, attr->blue, attr->alpha);
  22. }
  23. QBrush VStyleParser::QBrushFromPmhAttr(pmh_attr_argb_color *attr) const
  24. {
  25. return QBrush(QColorFromPmhAttr(attr));
  26. }
  27. void markdownStyleErrorCB(char *errMsg, int lineNr, void *context)
  28. {
  29. (void)context;
  30. qDebug() << "parser error:" << errMsg << lineNr;
  31. }
  32. QTextCharFormat VStyleParser::QTextCharFormatFromAttrs(pmh_style_attribute *attrs,
  33. const QFont &baseFont) const
  34. {
  35. QTextCharFormat format;
  36. while (attrs) {
  37. switch (attrs->type) {
  38. case pmh_attr_type_foreground_color:
  39. format.setForeground(QBrushFromPmhAttr(attrs->value->argb_color));
  40. break;
  41. case pmh_attr_type_background_color:
  42. format.setBackground(QBrushFromPmhAttr(attrs->value->argb_color));
  43. break;
  44. case pmh_attr_type_font_size_pt:
  45. {
  46. pmh_attr_font_size *fontSize = attrs->value->font_size;
  47. int ptSize = fontSize->size_pt;
  48. if (fontSize->is_relative) {
  49. int basePtSize = baseFont.pointSize();
  50. if (basePtSize == -1) {
  51. // In pixel. Use default font configuration.
  52. basePtSize = 11;
  53. }
  54. ptSize += basePtSize;
  55. }
  56. if (ptSize > 0) {
  57. format.setFontPointSize(ptSize);
  58. }
  59. break;
  60. }
  61. case pmh_attr_type_font_family:
  62. // TODO
  63. break;
  64. case pmh_attr_type_font_style:
  65. {
  66. pmh_attr_font_styles *fontStyle = attrs->value->font_styles;
  67. if (fontStyle->italic) {
  68. format.setFontItalic(true);
  69. }
  70. if (fontStyle->bold) {
  71. format.setFontWeight(QFont::Bold);
  72. }
  73. if (fontStyle->underlined) {
  74. format.setFontUnderline(true);
  75. }
  76. break;
  77. }
  78. default:
  79. qWarning() << "warning: unimplemented format attr type:" << attrs->type;
  80. break;
  81. }
  82. attrs = attrs->next;
  83. }
  84. return format;
  85. }
  86. void VStyleParser::parseMarkdownStyle(const QString &styleStr)
  87. {
  88. if (markdownStyles) {
  89. pmh_free_style_collection(markdownStyles);
  90. }
  91. markdownStyles = pmh_parse_styles(styleStr.toLocal8Bit().data(),
  92. &markdownStyleErrorCB, this);
  93. }
  94. QVector<HighlightingStyle> VStyleParser::fetchMarkdownStyles(const QFont &baseFont) const
  95. {
  96. QVector<HighlightingStyle> styles(pmh_NUM_LANG_TYPES);
  97. for (int i = 0; i < pmh_NUM_LANG_TYPES; ++i) {
  98. pmh_style_attribute *attr = markdownStyles->element_styles[i];
  99. if (!attr) {
  100. continue;
  101. }
  102. styles[i].type = attr->lang_element_type;
  103. styles[i].format = QTextCharFormatFromAttrs(attr, baseFont);
  104. }
  105. return styles;
  106. }
  107. QPalette VStyleParser::fetchMarkdownEditorStyles(const QPalette &basePalette) const
  108. {
  109. QPalette palette(basePalette);
  110. // editor
  111. pmh_style_attribute *editorStyles = markdownStyles->editor_styles;
  112. while (editorStyles) {
  113. switch (editorStyles->type) {
  114. case pmh_attr_type_foreground_color:
  115. palette.setColor(QPalette::Text,
  116. QColorFromPmhAttr(editorStyles->value->argb_color));
  117. break;
  118. case pmh_attr_type_background_color:
  119. palette.setColor(QPalette::Base,
  120. QColorFromPmhAttr(editorStyles->value->argb_color));
  121. break;
  122. default:
  123. qWarning() << "warning: unimplemented editor attr type:" << editorStyles->type;
  124. }
  125. editorStyles = editorStyles->next;
  126. }
  127. // editor-current-line
  128. pmh_style_attribute *curLineStyles = markdownStyles->editor_current_line_styles;
  129. if (curLineStyles) {
  130. qDebug() << "editor-current-line style is not supported";
  131. }
  132. // editor-selection
  133. pmh_style_attribute *selStyles = markdownStyles->editor_selection_styles;
  134. while (selStyles) {
  135. switch (selStyles->type) {
  136. case pmh_attr_type_foreground_color:
  137. palette.setColor(QPalette::HighlightedText,
  138. QColorFromPmhAttr(selStyles->value->argb_color));
  139. break;
  140. case pmh_attr_type_background_color:
  141. palette.setColor(QPalette::Highlight,
  142. QColorFromPmhAttr(selStyles->value->argb_color));
  143. break;
  144. default:
  145. qWarning() << "warning: unimplemented selection attr type:" << selStyles->type;
  146. }
  147. selStyles = selStyles->next;
  148. }
  149. return palette;
  150. }