hgmarkdownhighlighter.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. void run();
  24. char *content;
  25. pmh_element **result;
  26. };
  27. struct HighlightingStyle
  28. {
  29. pmh_element_type type;
  30. QTextCharFormat format;
  31. };
  32. class HGMarkdownHighlighter : public QObject
  33. {
  34. Q_OBJECT
  35. public:
  36. HGMarkdownHighlighter(QTextDocument *parent = 0, int aWaitInterval = 2000);
  37. void setStyles(QVector<HighlightingStyle> &styles);
  38. int waitInterval;
  39. private slots:
  40. void handleContentsChange(int position, int charsRemoved, int charsAdded);
  41. void threadFinished();
  42. void timerTimeout();
  43. private:
  44. QTimer *timer;
  45. QTextDocument *document;
  46. WorkerThread *workerThread;
  47. bool parsePending;
  48. pmh_element **cached_elements;
  49. QVector<HighlightingStyle> *highlightingStyles;
  50. void clearFormatting();
  51. void highlight();
  52. void parse();
  53. void setDefaultStyles();
  54. };
  55. #endif