hgmarkdownhighlighter.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* PEG Markdown Highlight
  2. * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
  3. * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
  4. *
  5. * highlighter.h
  6. *
  7. * Qt 4.7 example for highlighting a rich text widget.
  8. */
  9. #ifndef HGMARKDOWNHIGHLIGHTER_H
  10. #define HGMARKDOWNHIGHLIGHTER_H
  11. #include <QTextCharFormat>
  12. #include <QThread>
  13. extern "C" {
  14. #include "utils/peg-highlight/pmh_parser.h"
  15. }
  16. QT_BEGIN_NAMESPACE
  17. class QTextDocument;
  18. QT_END_NAMESPACE
  19. class WorkerThread : public QThread
  20. {
  21. public:
  22. WorkerThread();
  23. ~WorkerThread();
  24. void prepareAndStart(const char *data);
  25. pmh_element** retriveResult();
  26. protected:
  27. void run();
  28. private:
  29. void resizeBuffer(unsigned int newCap);
  30. char *content;
  31. unsigned int capacity;
  32. pmh_element **result;
  33. static const unsigned int initCapacity;
  34. };
  35. struct HighlightingStyle
  36. {
  37. pmh_element_type type;
  38. QTextCharFormat format;
  39. };
  40. class HGMarkdownHighlighter : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. HGMarkdownHighlighter(const QVector<HighlightingStyle> &styles,
  45. QTextDocument *parent = 0, int aWaitInterval = 2000);
  46. ~HGMarkdownHighlighter();
  47. void setStyles(const QVector<HighlightingStyle> &styles);
  48. int waitInterval;
  49. private slots:
  50. void handleContentsChange(int position, int charsRemoved, int charsAdded);
  51. void threadFinished();
  52. void timerTimeout();
  53. private:
  54. QTimer *timer;
  55. QTextDocument *document;
  56. WorkerThread *workerThread;
  57. bool parsePending;
  58. pmh_element **cached_elements;
  59. QVector<HighlightingStyle> highlightingStyles;
  60. void clearFormatting();
  61. void highlight();
  62. void highlightOneRegion(const HighlightingStyle &style, unsigned long pos,
  63. unsigned long end, bool clearBeforeHighlight = false);
  64. void highlightCodeBlock();
  65. void parse();
  66. };
  67. #endif