Dekko
MarkdownDocument.h
1 #ifndef MARKDOWNDOCUMENT_H
2 #define MARKDOWNDOCUMENT_H
3 
4 #include <QChar>
5 #include <QHash>
6 #include <QObject>
7 #include <QRegExp>
8 #include <QTextCursor>
9 #include <QQuickItem>
10 #include <QQuickTextDocument>
11 #include <QQmlAutoPropertyHelpers.h>
12 #include "MarkdownHighlighter.h"
13 
14 class MarkdownDocument : public QQuickItem
15 {
16  Q_OBJECT
17  QML_WRITABLE_AUTO_PROPERTY(FormattingOptions*, options)
18  QML_WRITABLE_AUTO_PROPERTY(QQuickTextDocument*, textDocument)
19  QML_WRITABLE_AUTO_PROPERTY(bool, enabled)
20  QML_WRITABLE_AUTO_PROPERTY(bool, hasSelection)
21  QML_WRITABLE_AUTO_PROPERTY(int, selectionStart)
22  QML_WRITABLE_AUTO_PROPERTY(int, selectionEnd)
23  Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
24  Q_ENUMS(BulletListMarkerType)
25  Q_ENUMS(NumberedListMarkerType)
26 public:
27  explicit MarkdownDocument(QQuickItem *parent = 0);
28  ~MarkdownDocument() {
29  if (m_highlighter != Q_NULLPTR) {
30  delete m_highlighter;
31  m_highlighter = 0;
32  }
33  }
34 
35  enum BulletListMarkerType {
36  Asterisk,
37  Minus,
38  Plus
39  };
40 
41  enum NumberedListMarkerType {
42  Period,
43  Parenthesis
44  };
45 
46  int cursorPosition() const;
47 
48 signals:
49  void cursorPositionChanged(int cursorPosition);
50 
51 public slots:
52  void indentText();
53  void unindentText();
54  void setCursorPosition(int cursorPosition);
55 
56 protected:
57  QTextDocument *document() const;
58  void keyPressEvent(QKeyEvent *event);
59  void handleCRLF();
60  bool handleBackspace();
61  QString getBlockStart(QRegExp &regexp);
62  QString getPreviousIndentation();
63 
64 private slots:
65  void onDocumentChanged();
66  void onContentsChange(const int &pos, const int &rm, const int &add);
67 private:
68  QTextCursor textCursor();
69 
70  MarkdownHighlighter *m_highlighter;
71  bool insertPair(const QChar &c);
72  bool endPairHandled(const QChar &c);
73  QHash<QChar, QChar> m_pairs;
74  QHash<QChar, bool> m_matches;
75  QRegExp m_blockQuote;
76  QRegExp m_numList;
77  QRegExp m_bulletList;
78  QRegExp m_taskList;
79  QTextCursor m_cursor;
80  int m_cursorPosition;
81 };
82 
83 #endif // MARKDOWNDOCUMENT_H
FormattingOptions
Definition: FormattingOptions.h:9
MarkdownDocument
Definition: MarkdownDocument.h:14
MarkdownHighlighter
Definition: MarkdownHighlighter.h:13