From 58efe2c43e54a3d868784a6968e52674309e2310 Mon Sep 17 00:00:00 2001
From: Vlad Voicu <vladv@rosedu.org>
Date: Wed, 1 Feb 2012 15:09:53 +0200
Subject: Context Menu improvements


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) {
-- 
cgit v0.10.2-6-g49f6