summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVlad Voicu <vladv@rosedu.org>2012-02-01 13:09:53 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-03-09 15:04:01 (GMT)
commit2944711aefec9a9dd66052440bc4f921910dd780 (patch)
tree9c1ec9e2284da3d4c23c98d9456031f3fb8c022b
parent21224565867c695bc41028ce31e567554eb25a0a (diff)
downloadswift-contrib-2944711aefec9a9dd66052440bc4f921910dd780.zip
swift-contrib-2944711aefec9a9dd66052440bc4f921910dd780.tar.bz2
Context Menu improvements
-rw-r--r--SwifTools/SpellChecker.cpp42
-rw-r--r--Swift/QtUI/QtTextEdit.cpp22
2 files changed, 18 insertions, 46 deletions
diff --git a/SwifTools/SpellChecker.cpp b/SwifTools/SpellChecker.cpp
deleted file mode 100644
index 8a27fc5..0000000
--- a/SwifTools/SpellChecker.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (c) 2011 Vlad Voicu
- * Licensed under the Simplified BSD license.
- * See Documentation/Licenses/BSD-simplified.txt for more information.
- */
-
-#include <SwifTools/SpellChecker.h>
-
-#include <algorithm>
-#include <hunspell/hunspell.hxx>
-#include <boost/algorithm/string.hpp>
-
-
-namespace Swift {
-
-SpellChecker::SpellChecker() {
- const char* dictionary_path = "/usr/share/hunspell/en_US.dic";
- const char* affix_path = "/usr/share/hunspell/en_US.aff";
- speller_ = new Hunspell(affix_path, dictionary_path);
-}
-
-SpellChecker::~SpellChecker() {
- delete speller_;
-}
-
-bool SpellChecker::isCorrect(const std::string& word) {
- return speller_->spell(word.c_str());
-}
-
-void SpellChecker::getSuggestions(const std::string& word, std::vector<std::string>& list) {
- char **suggestList;
- int words_returned;
- if (!word.empty()) {
- words_returned = speller_->suggest(&suggestList, word.c_str());
- }
- for (int i = 0; i < words_returned; ++i) {
- list.push_back(suggestList[i]);
- free(suggestList[i]);
- }
-}
-
-}
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index 7e7683c..60f0939 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -12,6 +12,7 @@
#include <Swift/QtUI/QtTextEdit.h>
#include <Swift/QtUI/QtSwiftUtil.h>
+#include <QApplication>
#include <QFontMetrics>
#include <QKeyEvent>
#include <QDebug>
@@ -122,9 +123,14 @@ QSize QtTextEdit::sizeHint() const {
}
void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
+ QMenu *menu = createStandardContextMenu();
QTextCursor cursor = cursorForPosition(event->pos());
boost::tuple<int, int> wordPosition = getWordFromCursor(cursor.position());
if (boost::get<0>(wordPosition) < 0) {
+ // The click was executed outside a spellable word so no
+ // suggestion menu is necessary
+ menu->exec(event->globalPos());
+ delete menu;
return;
}
cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor);
@@ -132,11 +138,19 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
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 *insertPoint = menu->actions().first();
+ if (wordList.size() == 0) {
+ QAction *noSuggestions = new QAction(QApplication::translate("QtTextEdit", "No Suggestions", 0, QApplication::UnicodeUTF8), menu);
+ noSuggestions->setDisabled(true);
+ menu->insertAction(insertPoint, noSuggestions);
+ } else {
+ for (std::vector<std::string>::iterator it = wordList.begin(); it != wordList.end(); ++it) {
+ QAction *wordAction = new QAction(it->c_str(), menu);
+ menu->insertAction(insertPoint, wordAction);
+ replaceWordActions.push_back(wordAction);
+ }
}
-
+ menu->insertAction(insertPoint, menu->addSeparator());
QAction* result = menu->exec(event->globalPos());
for (std::vector<QAction*>::iterator it = replaceWordActions.begin(); it != replaceWordActions.end(); ++it) {
if (*it == result) {