fb2code.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef FB2CODE_H
  2. #define FB2CODE_H
  3. #include <QByteArray>
  4. #include <QList>
  5. #include <QPlainTextEdit>
  6. #include <QObject>
  7. #include <QSyntaxHighlighter>
  8. #include <QTextCharFormat>
  9. #include <QColor>
  10. #include <QTextEdit>
  11. #include <QToolBar>
  12. #include "fb2mode.h"
  13. class FbHighlighter : public QSyntaxHighlighter
  14. {
  15. public:
  16. FbHighlighter(QObject* parent);
  17. FbHighlighter(QTextDocument* parent);
  18. FbHighlighter(QTextEdit* parent);
  19. ~FbHighlighter();
  20. enum HighlightType
  21. {
  22. SyntaxChar,
  23. ElementName,
  24. Comment,
  25. AttributeName,
  26. AttributeValue,
  27. Error,
  28. Other
  29. };
  30. void setHighlightColor(HighlightType type, QColor color, bool foreground = true);
  31. void setHighlightFormat(HighlightType type, QTextCharFormat format);
  32. protected:
  33. void highlightBlock(const QString& rstrText);
  34. int processDefaultText(int i, const QString& rstrText);
  35. private:
  36. void init();
  37. QTextCharFormat fmtSyntaxChar;
  38. QTextCharFormat fmtElementName;
  39. QTextCharFormat fmtComment;
  40. QTextCharFormat fmtAttributeName;
  41. QTextCharFormat fmtAttributeValue;
  42. QTextCharFormat fmtError;
  43. QTextCharFormat fmtOther;
  44. enum ParsingState
  45. {
  46. NoState = 0,
  47. ExpectElementNameOrSlash,
  48. ExpectElementName,
  49. ExpectAttributeOrEndOfElement,
  50. ExpectEqual,
  51. ExpectAttributeValue
  52. };
  53. enum BlockState
  54. {
  55. NoBlock = -1,
  56. InComment,
  57. InElement
  58. };
  59. ParsingState state;
  60. };
  61. QT_BEGIN_NAMESPACE
  62. class QPaintEvent;
  63. class QResizeEvent;
  64. class QSize;
  65. class QWidget;
  66. QT_END_NAMESPACE
  67. class FbCodeEdit : public QPlainTextEdit
  68. {
  69. Q_OBJECT
  70. public:
  71. FbCodeEdit(QWidget *parent = 0);
  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 isUndoAvailable() { return false; }
  80. bool isRedoAvailable() { return false; }
  81. void zoomTo ( int ) {}
  82. bool findText(const QString &exp, QTextDocument::FindFlags options = 0);
  83. bool isModified() const { return document()->isModified(); }
  84. public slots:
  85. void find();
  86. void zoomIn();
  87. void zoomOut();
  88. void zoomReset();
  89. protected:
  90. void resizeEvent(QResizeEvent *event);
  91. private slots:
  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