summaryrefslogtreecommitdiffstats
blob: e523dca16a18d4ba604fa34aaa5639bc3a1b514c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <boost/tuple/tuple.hpp>
#include <boost/algorithm/string.hpp>

#include <SwifTools/SpellCheckerFactory.h>
#include <SwifTools/SpellChecker.h>

#include <Swift/QtUI/QtTextEdit.h>
#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtSpellCheckerWindow.h>

#include <QApplication>
#include <QFontMetrics>
#include <QKeyEvent>
#include <QDebug>
#include <QMenu>

namespace Swift {

QtTextEdit::QtTextEdit(SettingsProvider* settings, QWidget* parent) : QTextEdit(parent) {
	connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
	settings_ = settings;
	setUpSpellChecker();
	handleTextChanged();
};

QtTextEdit::~QtTextEdit() {
	delete checker_;
}

void QtTextEdit::keyPressEvent(QKeyEvent* event) {
	int key = event->key();
	Qt::KeyboardModifiers modifiers = event->modifiers();
	if ((key == Qt::Key_Enter || key == Qt::Key_Return)
		&& (modifiers == Qt::NoModifier || modifiers == Qt::KeypadModifier)) {
		emit returnPressed();
	}
	else if (((key == Qt::Key_PageUp || key == Qt::Key_PageDown) && modifiers == Qt::ShiftModifier)
			   || (key == Qt::Key_C && modifiers == Qt::ControlModifier && textCursor().selectedText().isEmpty())
			   || (key == Qt::Key_W && modifiers == Qt::ControlModifier)
			   || (key == Qt::Key_PageUp && modifiers == Qt::ControlModifier)
			   || (key == Qt::Key_PageDown && modifiers == Qt::ControlModifier)
			   || (key == Qt::Key_Tab && modifiers == Qt::ControlModifier)
			   || (key == Qt::Key_A && modifiers == Qt::AltModifier)
			   || (key == Qt::Key_Tab)
	) {
		emit unhandledKeyPressEvent(event);
	}
	else if ((key == Qt::Key_Up)
			   || (key == Qt::Key_Down)) {
		emit unhandledKeyPressEvent(event);
		QTextEdit::keyPressEvent(event);
	}
	else {
		QTextEdit::keyPressEvent(event);
		if(settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
			underlineMisspells();
		}
	}
}

void QtTextEdit::underlineMisspells() {
	if (checker_ == NULL) {
		// Try to setUp the checker again in case that
		// settings changed.
		setUpSpellChecker();
		if (checker_ == NULL) {
			return;
		}
	}
	misspelledPositions_.clear();
	QTextCharFormat spellingErrorFormat;
	QTextCharFormat normalFormat;
	spellingErrorFormat.setUnderlineColor(QColor(Qt::red));
	spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
	QTextCursor cursor = textCursor();
	cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
	cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor, 1);
	cursor.setCharFormat(normalFormat);
	std::string fragment = Q2PSTRING(cursor.selectedText());
	checker_->checkFragment(fragment, misspelledPositions_);
	for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) {
		if (textCursor().position() > boost::get<1>(*it)) {
			cursor.setPosition(boost::get<0>(*it), QTextCursor::MoveAnchor);
			cursor.setPosition(boost::get<1>(*it), QTextCursor::KeepAnchor);
			cursor.setCharFormat(spellingErrorFormat);
			cursor.clearSelection();
		}
		cursor.setCharFormat(normalFormat);
	}
}

void QtTextEdit::handleTextChanged() {
	QSize previous(maximumSize());
	setMaximumSize(QSize(maximumWidth(), sizeHint().height()));
	if (previous != maximumSize()) {
		updateGeometry();
	}
}

void QtTextEdit::handleReplaceMisspellWord(const QString& word, const boost::tuple<int, int>& wordPosition) {
	QTextCursor cursor = textCursor();
	cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor);
	cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor);
	QTextCharFormat normalFormat;
	cursor.insertText(word, normalFormat);
}

boost::tuple<int, int> QtTextEdit::getWordFromCursor(int cursorPosition) {
	for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) {
		if (cursorPosition >= boost::get<0>(*it) && cursorPosition <= boost::get<1>(*it)) {
			return *it;
		}
	}
	return boost::make_tuple(-1,-1);
}

QSize QtTextEdit::sizeHint() const {
	QFontMetrics inputMetrics(currentFont());
	QRect horizontalBounds = contentsRect().adjusted(0,0,0,9999);
	QRect boundingRect = inputMetrics.boundingRect(horizontalBounds, Qt::TextWordWrap, toPlainText() + "A");
	int left, top, right, bottom;
	getContentsMargins(&left, &top, &right, &bottom);
	int height = boundingRect.height() + top + bottom + inputMetrics.height();
	return QSize(width(), height);
	//int numberOfLines = 1;
	//int lineHeight = inputMetrics.lineSpacing();
	//return QSize(QTextEdit::sizeHint().width(), lineHeight * numberOfLines);
}

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);
	cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor);
	std::vector<std::string> wordList;
	checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList);
	std::vector<QAction*> replaceWordActions;
	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) {
			handleReplaceMisspellWord((*it)->text(), wordPosition);
		}
	}
	delete menu;
}

void QtTextEdit::setUpSpellChecker()
{
	SpellCheckerFactory *checkerFactory = new SpellCheckerFactory();
	std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE);
	if (dictFile.empty()) {
		// Disable the dictionary to force the user to select a dictionary
		settings_->storeSetting(SettingConstants::SPELL_CHECKER, false);
	}
	if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
		std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH);
		std::string affixFile(dictFile);
		boost::replace_all(affixFile, ".dic", ".aff");
		if (checker_ != NULL) {
			delete checker_;
		}
		checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str());
		delete checkerFactory;
	} else {
		checker_ = NULL;
	}
}

}