#include "fb2code.hpp" #include #include #include #include #include "fb2dlgs.hpp" //--------------------------------------------------------------------------- // FbHighlighter //--------------------------------------------------------------------------- class FbSchemaHandler : public QAbstractMessageHandler { public: FbSchemaHandler() : QAbstractMessageHandler(0) { } QString statusMessage() const { return m_description; } int line() const { return m_sourceLocation.line(); } int column() const { return m_sourceLocation.column(); } protected: virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation) { Q_UNUSED(type); Q_UNUSED(identifier); m_messageType = type; m_description = description; m_sourceLocation = sourceLocation; } private: QtMsgType m_messageType; QString m_description; QSourceLocation m_sourceLocation; }; //--------------------------------------------------------------------------- // FbHighlighter //--------------------------------------------------------------------------- #include #include #include class FbHighlighter : public QSyntaxHighlighter { public: FbHighlighter(QObject* parent); FbHighlighter(QTextDocument* parent); FbHighlighter(QTextEdit* parent); ~FbHighlighter(); enum HighlightType { SyntaxChar, ElementName, Comment, AttributeName, AttributeValue, Error, Other }; void setHighlightColor(HighlightType type, QColor color, bool foreground = true); void setHighlightFormat(HighlightType type, QTextCharFormat format); protected: void highlightBlock(const QString& rstrText); int processDefaultText(int i, const QString& rstrText); private: void init(); QTextCharFormat fmtSyntaxChar; QTextCharFormat fmtElementName; QTextCharFormat fmtComment; QTextCharFormat fmtAttributeName; QTextCharFormat fmtAttributeValue; QTextCharFormat fmtError; QTextCharFormat fmtOther; enum ParsingState { NoState = 0, ExpectElementNameOrSlash, ExpectElementName, ExpectAttributeOrEndOfElement, ExpectEqual, ExpectAttributeValue }; enum BlockState { NoBlock = -1, InComment, InElement }; ParsingState state; }; static const QColor DEFAULT_SYNTAX_CHAR = Qt::blue; static const QColor DEFAULT_ELEMENT_NAME = Qt::darkRed; static const QColor DEFAULT_COMMENT = Qt::darkGray; static const QColor DEFAULT_ATTRIBUTE_NAME = Qt::red; static const QColor DEFAULT_ATTRIBUTE_VALUE = Qt::darkGreen; static const QColor DEFAULT_ERROR = Qt::darkMagenta; static const QColor DEFAULT_OTHER = Qt::black; // Regular expressions for parsing XML borrowed from: // http://www.cs.sfu.ca/~cameron/REX.html static const QString EXPR_COMMENT = "