Example #1
0
static bool
finderNextWord(QTextBoundaryFinder &finder, QString &word, int &bufferStart)
{
    QTextBoundaryFinder::BoundaryReasons boundary = finder.boundaryReasons();
    int start = finder.position(), end = finder.position();
    bool inWord = (boundary & QTextBoundaryFinder::StartWord) != 0;
    while (finder.toNextBoundary() > 0) {
        boundary = finder.boundaryReasons();
        if ((boundary & QTextBoundaryFinder::EndWord) && inWord) {
            end = finder.position();
            QString str = finder.string().mid(start, end - start);
            if (isValidWord(str)) {
                word = str;
                bufferStart = start;
#if 0
                kDebug() << "Word at " << start << " word = '"
                         <<  str << "', len = " << str.length();
#endif
                return true;
            }
            inWord = false;
        }
        if ((boundary & QTextBoundaryFinder::StartWord)) {
            start = finder.position();
            inWord = true;
        }
    }
    return false;
}
Example #2
0
void Speller::populateContextMenu(QMenu* menu, const QWebHitTestResult &hitTest)
{
    m_element = hitTest.element();

    if (!m_enabled || m_element.isNull() ||
            m_element.attribute(QLatin1String("type")) == QLatin1String("password")) {
        return;
    }

    const QString text = m_element.evaluateJavaScript("this.value").toString();
    const int pos = m_element.evaluateJavaScript("this.selectionStart").toInt() + 1;

    QTextBoundaryFinder finder =  QTextBoundaryFinder(QTextBoundaryFinder::Word, text);
    finder.setPosition(pos);
    m_startPos = finder.toPreviousBoundary();
    m_endPos = finder.toNextBoundary();

    const QString &word = text.mid(m_startPos, m_endPos - m_startPos).trimmed();

    if (!isValidWord(word) || !isMisspelled(word)) {
        return;
    }

    const int limit = 6;
    QStringList suggests = suggest(word);
    int count = suggests.count() > limit ? limit : suggests.count();

    QFont boldFont = menu->font();
    boldFont.setBold(true);

    for (int i = 0; i < count; ++i) {
        QAction* act = menu->addAction(suggests.at(i), this, SLOT(replaceWord()));
        act->setData(suggests.at(i));
        act->setFont(boldFont);
    }

    if (count == 0) {
        menu->addAction(tr("No suggestions"))->setEnabled(false);
    }

    menu->addAction(tr("Add to dictionary"), this, SLOT(addToDictionary()))->setData(word);
    menu->addSeparator();
}
Example #3
0
static bool finderWordAt(QTextBoundaryFinder &finder,
                         int at,
                         QString &word, int &bufferStart)
{
    int oldPosition = finder.position();

    finder.setPosition(at);
    if (!finder.isAtBoundary() || (finder.boundaryReasons() & QTextBoundaryFinder::EndWord)) {
        if (finder.toPreviousBoundary() <= 0) {
            /* QTextBoundaryIterator doesn't consider start of the string
             * a boundary so we need to rewind to the beginning to catch
             * the first word */
            if (at > 0 && finder.string().length() > 0) {
                finder.toStart();
            } else
                return false;
        }
    }
    bool ret = finderNextWord(finder, word, bufferStart);
    finder.setPosition(oldPosition);
    return ret;
}
Example #4
0
	void SpellcheckerExt::checkSpellingOfString (const QString& word,
			int *misspellingLocation, int *misspellingLength)
	{
		if (!misspellingLength || !misspellingLocation)
			return;

		*misspellingLocation = 0;
		*misspellingLength = 0;

		QTextBoundaryFinder finder { QTextBoundaryFinder::Word, word };
		bool isInWord = false;
		int start = -1;
		int end = -1;
		do
		{
			const auto reasons = finder.boundaryReasons ();

			if (IsWordStart (reasons, finder.type ()))
			{
				start = finder.position ();
				isInWord = true;
			}

			if (isInWord && IsWordEnd (reasons, finder.type ()))
			{
				end = finder.position ();

				const auto& str = word.mid (start, end - start);
				if (std::none_of (Spellcheckers_.begin (), Spellcheckers_.end (),
						[&str] (const ISpellChecker_ptr& sc) { return sc->IsCorrect (str); }))
				{
					*misspellingLocation = start;
					*misspellingLength = end - start;
					return;
				}

				isInWord = false;
			}
		}
		while (finder.toNextBoundary () > 0);
	}