summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/QtUI/QtTextEdit.cpp')
-rw-r--r--Swift/QtUI/QtTextEdit.cpp67
1 files changed, 64 insertions, 3 deletions
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index 3a62325..e1fdd26 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -4,18 +4,28 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
+#include <SwifTools/SpellChecker.h>
+
#include <Swift/QtUI/QtTextEdit.h>
+#include <Swift/QtUI/QtSwiftUtil.h>
#include <QFontMetrics>
#include <QKeyEvent>
+#include <QDebug>
+#include <QMenu>
namespace Swift {
QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent){
connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
handleTextChanged();
+ checker_ = new SpellChecker();
};
+QtTextEdit::~QtTextEdit() {
+ delete checker_;
+}
+
void QtTextEdit::keyPressEvent(QKeyEvent* event) {
int key = event->key();
Qt::KeyboardModifiers modifiers = event->modifiers();
@@ -35,13 +45,37 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) {
emit unhandledKeyPressEvent(event);
}
else if ((key == Qt::Key_Up)
- || (key == Qt::Key_Down)
- ){
+ || (key == Qt::Key_Down)) {
emit unhandledKeyPressEvent(event);
QTextEdit::keyPressEvent(event);
}
else {
QTextEdit::keyPressEvent(event);
+ underlineMisspells();
+ }
+}
+
+void QtTextEdit::underlineMisspells() {
+ QTextCharFormat spellingErrorFormat;
+ QTextCharFormat normalFormat;
+ spellingErrorFormat.setUnderlineColor(QColor(Qt::red));
+ spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
+ QTextCursor cursor = textCursor();
+ QString word;
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1);
+ word = cursor.selectedText();
+ // Qt handles punctuation as words. Workaround that.
+ while (!word.isEmpty() && !word.at(0).isLetter()) {
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 2);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1);
+ word = cursor.selectedText();
+ }
+ if ((!word.isEmpty()) && !checker_->isCorrect(word.toStdString())) {
+ cursor.mergeCharFormat(spellingErrorFormat);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor, 1);
+ cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, 1);
+ cursor.setCharFormat(normalFormat);
}
}
@@ -53,6 +87,14 @@ void QtTextEdit::handleTextChanged() {
}
}
+void QtTextEdit::handleReplaceMisspellWord(const QString& word, const QPoint& position) {
+ QTextCursor cursor = cursorForPosition(position);
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1);
+ QTextCharFormat normalFormat;
+ cursor.insertText(word, normalFormat);
+}
+
QSize QtTextEdit::sizeHint() const {
QFontMetrics inputMetrics(currentFont());
QRect horizontalBounds = contentsRect().adjusted(0,0,0,9999);
@@ -66,7 +108,26 @@ QSize QtTextEdit::sizeHint() const {
//return QSize(QTextEdit::sizeHint().width(), lineHeight * numberOfLines);
}
-}
+void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
+ QTextCursor cursor = cursorForPosition(event->pos());
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1);
+ std::vector<std::string> wordList;
+ checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList);
+ std::vector<QAction*> replaceWordActions;
+ QMenu *menu = createStandardContextMenu();
+ for (std::vector<std::string>::iterator it = wordList.begin(); it != wordList.end(); ++it) {
+ replaceWordActions.push_back(menu->addAction(tr(it->c_str())));
+ }
+ QAction* result = menu->exec(event->globalPos());
+ for (std::vector<QAction*>::iterator it = replaceWordActions.begin(); it != replaceWordActions.end(); ++it) {
+ if (*it == result) {
+ handleReplaceMisspellWord((*it)->text(), event->pos());
+ }
+ }
+ delete menu;
+}
+}