QtSpell  0.7.4
Spell checking for Qt text widgets
TextEditChecker_p.hpp
1 /* QtSpell - Spell checking for Qt text widgets.
2  * Copyright (c) 2014 Sandro Mani
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef QTSPELL_TEXTEDITCHECKER_P_HPP
20 #define QTSPELL_TEXTEDITCHECKER_P_HPP
21 
22 #include <QTextCursor>
23 
24 class QMenu;
25 
26 namespace QtSpell {
27 
31 class TextCursor : public QTextCursor
32 {
33 public:
34  TextCursor()
35  : QTextCursor(), m_wordRegEx(QRegExp("^\\w$")) {}
36  TextCursor(QTextDocument* document)
37  : QTextCursor(document), m_wordRegEx(QRegExp("^\\w$")) {}
38  TextCursor(const QTextBlock& block)
39  : QTextCursor(block), m_wordRegEx(QRegExp("^\\w$")) {}
40  TextCursor(const QTextCursor& cursor)
41  : QTextCursor(cursor), m_wordRegEx(QRegExp("^\\w$")) {}
42 
48  QString nextChar(int num = 1) const;
49 
55  QString prevChar(int num = 1) const;
56 
62  void moveWordStart(MoveMode moveMode = MoveAnchor);
63 
69  void moveWordEnd(MoveMode moveMode = MoveAnchor);
70 
75  bool isInsideWord() const{
76  return nextChar().contains(m_wordRegEx) || prevChar().contains(m_wordRegEx);
77  }
78 
83  bool isWordChar(const QString& character) const{
84  return character.contains(m_wordRegEx);
85  }
86 
87 private:
88  QRegExp m_wordRegEx;
89 };
90 
92 
93 class TextEditProxy {
94 public:
95  virtual ~TextEditProxy(){}
96  virtual QTextCursor textCursor() const = 0;
97  virtual QTextDocument* document() const = 0;
98  virtual QPoint mapToGlobal(const QPoint& pos) const = 0;
99  virtual QMenu* createStandardContextMenu() = 0;
100  virtual QTextCursor cursorForPosition(const QPoint& pos) const = 0;
101  virtual void setContextMenuPolicy(Qt::ContextMenuPolicy policy) = 0;
102  virtual void setTextCursor(const QTextCursor& cursor) = 0;
103  virtual Qt::ContextMenuPolicy contextMenuPolicy() const = 0;
104  virtual void installEventFilter(QObject* filterObj) = 0;
105  virtual void removeEventFilter(QObject* filterObj) = 0;
106  virtual void ensureCursorVisible() = 0;
107  virtual QObject* object() = 0;
108 };
109 
110 template<class T>
111 class TextEditProxyT : public TextEditProxy {
112 public:
113  TextEditProxyT(T* textEdit) : m_textEdit(textEdit) {}
114  QTextCursor textCursor() const{ return m_textEdit->textCursor(); }
115  QTextDocument* document() const{ return m_textEdit->document(); }
116  QPoint mapToGlobal(const QPoint& pos) const{ return m_textEdit->mapToGlobal(pos); }
117  QMenu* createStandardContextMenu(){ return m_textEdit->createStandardContextMenu(); }
118  QTextCursor cursorForPosition(const QPoint& pos) const{ return m_textEdit->cursorForPosition(pos); }
119  void setContextMenuPolicy(Qt::ContextMenuPolicy policy){ m_textEdit->setContextMenuPolicy(policy); }
120  void setTextCursor(const QTextCursor& cursor){ m_textEdit->setTextCursor(cursor); }
121  Qt::ContextMenuPolicy contextMenuPolicy() const{ return m_textEdit->contextMenuPolicy(); }
122  void installEventFilter(QObject* filterObj){ m_textEdit->installEventFilter(filterObj); }
123  void removeEventFilter(QObject* filterObj){ m_textEdit->removeEventFilter(filterObj); }
124  void ensureCursorVisible() { m_textEdit->ensureCursorVisible(); }
125  QObject* object(){ return m_textEdit; }
126 
127 private:
128  T* m_textEdit;
129 };
130 
131 } // QtSpell
132 
133 #endif // QTSPELL_TEXTEDITCHECKER_P_HPP
QString prevChar(int num=1) const
Retreive the num-th previous character.
bool isInsideWord() const
Returns whether the cursor is inside a word.
bool isWordChar(const QString &character) const
Returns whether the specified character is a word character.
QString nextChar(int num=1) const
Retreive the num-th next character.
void moveWordEnd(MoveMode moveMode=MoveAnchor)
Move the cursor to the end of the current word. Cursor must be inside a word. This method correctly h...
void moveWordStart(MoveMode moveMode=MoveAnchor)
Move the cursor to the start of the current word. Cursor must be inside a word. This method correctly...
An enhanced QTextCursor.