summaryrefslogtreecommitdiffstats
path: root/Swift
diff options
context:
space:
mode:
authorVlad Voicu <vladvoic@gmail.com>2011-11-28 16:37:32 (GMT)
committervlad <vlad@tyrion.(none)>2012-10-13 13:55:44 (GMT)
commit4ef710cfcc4aa4f171426ef55cbece00cd69af62 (patch)
tree243974bc3ae35199d7e4317bc737552e1a2a6f0b /Swift
parent3cf3828562d54db0ddaacfcea755b15c5b8c5e90 (diff)
downloadswift-contrib-4ef710cfcc4aa4f171426ef55cbece00cd69af62.zip
swift-contrib-4ef710cfcc4aa4f171426ef55cbece00cd69af62.tar.bz2
Initial, buggy version of a minimal spell-checker
Diffstat (limited to 'Swift')
-rw-r--r--Swift/QtUI/QtTextEdit.cpp67
-rw-r--r--Swift/QtUI/QtTextEdit.h10
2 files changed, 74 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;
+}
+}
diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h
index 075728b..a940879 100644
--- a/Swift/QtUI/QtTextEdit.h
+++ b/Swift/QtUI/QtTextEdit.h
@@ -5,20 +5,30 @@
*/
#pragma once
+
#include <QTextEdit>
namespace Swift {
+ class SpellChecker;
class QtTextEdit : public QTextEdit {
Q_OBJECT
public:
QtTextEdit(QWidget* parent = 0);
+ virtual ~QtTextEdit();
virtual QSize sizeHint() const;
signals:
+ void wordCorrected(QString& word);
void returnPressed();
void unhandledKeyPressEvent(QKeyEvent* event);
+ public slots:
+ void handleReplaceMisspellWord(const QString& word, const QPoint& position);
protected:
virtual void keyPressEvent(QKeyEvent* event);
+ virtual void contextMenuEvent(QContextMenuEvent* event);
private slots:
void handleTextChanged();
+ private:
+ SpellChecker *checker_;
+ void underlineMisspells();
};
}