fb2code.hpp 3.3 KB

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