コード例 #1
0
ファイル: dictionarycombobox.cpp プロジェクト: vasi/kdelibs
void DictionaryComboBox::reloadCombo()
{
    clear();
    Sonnet::Speller* speller = new Sonnet::Speller();
    QMap<QString, QString> dictionaries = speller->availableDictionaries();
    QMapIterator<QString, QString> i( dictionaries );
    while ( i.hasNext() ) {
        i.next();
        kDebug() << "Populate combo:" << i.key() << ":" << i.value();
        addItem( i.key(), i.value() );
    }
    delete speller;
}
コード例 #2
0
void TextEdit::setupSpeller()
{
    BehaviorSettings::self()->readConfig();
    d->curLang = BehaviorSettings::spellerLanguage();
    Sonnet::Speller s;
    if(d->curLang.isEmpty()){
        d->curLang = s.defaultLanguage();
    }
    kDebug()<<"Current LANG: "<<d->curLang;
    QMap<QString, QString> list = s.availableDictionaries();
    QMap<QString, QString>::const_iterator it = list.constBegin(), endIt = list.constEnd();
    for(; it!=endIt; ++it){
        QAction *act = new QAction(it.key(), d->langActions);
        act->setData(it.value());
        act->setCheckable(true);
        if(d->curLang == it.value())
            act->setChecked(true);
        connect(act, SIGNAL(triggered(bool)), SLOT(slotChangeSpellerLanguage()));
        d->langActions->addAction(act);
        d->langActionMap.insert(it.value(), act);
    }
}
コード例 #3
0
bool WebView::popupSpellMenu(QContextMenuEvent *event)
{
    // return false if not handled
    if (!ReKonfig::automaticSpellChecking())
        return false;

    QWebElement element(m_contextMenuHitResult.element());
    if (element.isNull())
        return false;

    int selStart = element.evaluateJavaScript("this.selectionStart").toInt();
    int selEnd = element.evaluateJavaScript("this.selectionEnd").toInt();
    if (selEnd != selStart)
        return false; // selection, handle normally

    // No selection - Spell Checking only
    // Get word
    QString text = element.evaluateJavaScript("this.value").toString();
    QRegExp ws("\\b");
    int s1 = text.lastIndexOf(ws, selStart);
    int s2 = text.indexOf(ws, selStart);
    QString word = text.mid(s1, s2 - s1).trimmed();

    // sanity check
    if (word.isEmpty())
        return false;

    kDebug() << s1 << ":" << s2 << ":" << word << ":";
    Sonnet::Speller spellor;
    if (spellor.isCorrect(word))
        return false; // no need to popup spell menu

    // find alternates
    QStringList words = spellor.suggest(word);

    // Construct popup menu
    QMenu mnu(this);

    // Add alternates
    if (words.isEmpty())
    {
        QAction *a = mnu.addAction(i18n("No suggestions for %1", word));
        a->setEnabled(false);
    }
    else
    {
        QStringListIterator it(words);
        while (it.hasNext())
        {
            QString w = it.next();
            QAction *aWord = mnu.addAction(w);
            aWord->setData(w);
        }
    }

    // Add dictionary options
    mnu.addSeparator();
    QAction *aIgnore = mnu.addAction(i18n("Ignore"));
    QAction *aAddToDict = mnu.addAction(i18n("Add to Dictionary"));

    QAction *aSpellChoice = mnu.exec(event->globalPos());
    if (aSpellChoice)
    {
        if (aSpellChoice == aAddToDict)
            spellor.addToPersonal(word);
        else if (aSpellChoice == aIgnore)
        {
            // Ignore :)
        }
        else
        {
            // Choose a replacement word
            QString w = aSpellChoice->data().toString();
            if (!w.isEmpty())
            {
                // replace word
                QString script(QL1S("this.value=this.value.substring(0,"));
                script += QString::number(s1);
                script += QL1S(") + \'");
                script +=  w.replace('\'', "\\\'"); // Escape any Quote marks in replacement word
                script += QL1C('\'') + QL1S("+this.value.substring(");
                script += QString::number(s2);
                script += QL1C(')');
                
                element.evaluateJavaScript(script);
                // reposition cursor
                element.evaluateJavaScript("this.selectionEnd=this.selectionStart=" + QString::number(selStart) + QL1C(';'));
            }
        }
    }

    return true;
}