1
0

fb2code.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef FB2CODE_H
  2. #define FB2CODE_H
  3. #ifdef FB2_USE_SCINTILLA
  4. #include <Qsci/qsciscintilla.h>
  5. #include <QList>
  6. class Fb2CodeEdit : public QsciScintilla
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit Fb2CodeEdit(QWidget *parent = 0);
  11. void load(const QByteArray &array, const QList<int> &folds);
  12. signals:
  13. public slots:
  14. void zoomReset();
  15. private slots:
  16. void linesChanged();
  17. };
  18. #else // FB2_USE_SCINTILLA
  19. #ifndef FB2_USE_PLAINTEXT
  20. #define FB2_USE_PLAINTEXT
  21. #endif // FB2_USE_PLAINTEXT
  22. #endif // FB2_USE_SCINTILLA
  23. #ifdef FB2_USE_PLAINTEXT
  24. #include <QByteArray>
  25. #include <QList>
  26. #include <QPlainTextEdit>
  27. #include <QObject>
  28. #include <QSyntaxHighlighter>
  29. #include <QTextCharFormat>
  30. #include <QColor>
  31. #include <QTextEdit>
  32. class Fb2Highlighter : public QSyntaxHighlighter
  33. {
  34. public:
  35. Fb2Highlighter(QObject* parent);
  36. Fb2Highlighter(QTextDocument* parent);
  37. Fb2Highlighter(QTextEdit* parent);
  38. ~Fb2Highlighter();
  39. enum HighlightType
  40. {
  41. SyntaxChar,
  42. ElementName,
  43. Comment,
  44. AttributeName,
  45. AttributeValue,
  46. Error,
  47. Other
  48. };
  49. void setHighlightColor(HighlightType type, QColor color, bool foreground = true);
  50. void setHighlightFormat(HighlightType type, QTextCharFormat format);
  51. protected:
  52. void highlightBlock(const QString& rstrText);
  53. int processDefaultText(int i, const QString& rstrText);
  54. private:
  55. void init();
  56. QTextCharFormat fmtSyntaxChar;
  57. QTextCharFormat fmtElementName;
  58. QTextCharFormat fmtComment;
  59. QTextCharFormat fmtAttributeName;
  60. QTextCharFormat fmtAttributeValue;
  61. QTextCharFormat fmtError;
  62. QTextCharFormat fmtOther;
  63. enum ParsingState
  64. {
  65. NoState = 0,
  66. ExpectElementNameOrSlash,
  67. ExpectElementName,
  68. ExpectAttributeOrEndOfElement,
  69. ExpectEqual,
  70. ExpectAttributeValue
  71. };
  72. enum BlockState
  73. {
  74. NoBlock = -1,
  75. InComment,
  76. InElement
  77. };
  78. ParsingState state;
  79. };
  80. QT_BEGIN_NAMESPACE
  81. class QPaintEvent;
  82. class QResizeEvent;
  83. class QSize;
  84. class QWidget;
  85. QT_END_NAMESPACE
  86. class Fb2CodeEdit : public QPlainTextEdit
  87. {
  88. Q_OBJECT
  89. public:
  90. Fb2CodeEdit(QWidget *parent = 0);
  91. QString text() const { return toPlainText(); }
  92. bool read(QIODevice *device);
  93. void load(const QByteArray data, const QList<int> folds)
  94. { setPlainText(QString::fromUtf8(data.data())); }
  95. bool isUndoAvailable() { return false; }
  96. bool isRedoAvailable() { return false; }
  97. void zoomTo ( int size ) {}
  98. bool findText(const QString &exp, QTextDocument::FindFlags options = 0);
  99. public slots:
  100. void find();
  101. void zoomIn();
  102. void zoomOut();
  103. void zoomReset();
  104. protected:
  105. void resizeEvent(QResizeEvent *event);
  106. private slots:
  107. void updateLineNumberAreaWidth(int newBlockCount);
  108. void highlightCurrentLine();
  109. void updateLineNumberArea(const QRect &, int);
  110. private:
  111. class LineNumberArea : public QWidget
  112. {
  113. public:
  114. LineNumberArea(Fb2CodeEdit *parent) : QWidget(parent) { editor = parent; }
  115. QSize sizeHint() const { return QSize(editor->lineNumberAreaWidth(), 0); }
  116. protected:
  117. void paintEvent(QPaintEvent *event) { editor->lineNumberAreaPaintEvent(event); }
  118. private:
  119. Fb2CodeEdit *editor;
  120. };
  121. private:
  122. void lineNumberAreaPaintEvent(QPaintEvent *event);
  123. int lineNumberAreaWidth();
  124. void setZoomRatio(qreal ratio);
  125. private:
  126. Fb2Highlighter * highlighter;
  127. QWidget *lineNumberArea;
  128. qreal zoomRatio;
  129. static qreal baseFontSize;
  130. static qreal zoomRatioMin;
  131. static qreal zoomRatioMax;
  132. friend class Fb2CodeEdit::LineNumberArea;
  133. };
  134. #endif // FB2_USE_PLAINTEXT
  135. #endif // FB2CODE_H