/* PEG Markdown Highlight * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info). * * highlighter.h * * Qt 4.7 example for highlighting a rich text widget. */ #ifndef HGMARKDOWNHIGHLIGHTER_H #define HGMARKDOWNHIGHLIGHTER_H #include #include extern "C" { #include "utils/peg-highlight/pmh_parser.h" } QT_BEGIN_NAMESPACE class QTextDocument; QT_END_NAMESPACE class WorkerThread : public QThread { public: WorkerThread(); ~WorkerThread(); void prepareAndStart(const char *data); pmh_element** retriveResult(); protected: void run(); private: void resizeBuffer(unsigned int newCap); char *content; unsigned int capacity; pmh_element **result; static const unsigned int initCapacity; }; struct HighlightingStyle { pmh_element_type type; QTextCharFormat format; }; class HGMarkdownHighlighter : public QObject { Q_OBJECT public: HGMarkdownHighlighter(const QVector &styles, QTextDocument *parent = 0, int aWaitInterval = 2000); ~HGMarkdownHighlighter(); void setStyles(const QVector &styles); int waitInterval; private slots: void handleContentsChange(int position, int charsRemoved, int charsAdded); void threadFinished(); void timerTimeout(); private: QTimer *timer; QTextDocument *document; WorkerThread *workerThread; bool parsePending; pmh_element **cached_elements; QVector highlightingStyles; void clearFormatting(); void highlight(); void highlightOneRegion(const HighlightingStyle &style, unsigned long pos, unsigned long end, bool clearBeforeHighlight = false); void highlightCodeBlock(); void parse(); }; #endif