summaryrefslogtreecommitdiffstats
blob: e1fdd262969df731c048a15b948ec78de0373262 (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
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * 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();
	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);
		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);
	}
}

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

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);
	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) {
	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;
}

}