diff options
author | Michal Klocek <michal.klocek@qt.io> | 2016-10-18 19:01:16 +0200 |
---|---|---|
committer | Michal Klocek <michal.klocek@qt.io> | 2016-10-29 12:27:42 +0000 |
commit | 2fe08ddf9388598003cb3a30f30a2d18ab044969 (patch) | |
tree | 287851c5a4e699fe7fd4fd6277c585a50de190b1 | |
parent | 05df6e5f4af76f10a6dc5884c1aa1599a2d7a486 (diff) |
Update spellcheck api to support multiple languages
Task-number: QTBUG-56074
Change-Id: I2a66e91dd4ed1026e5ab2539cfd3f9094263b48c
Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
-rw-r--r-- | examples/webenginewidgets/spellchecker/webview.cpp | 6 | ||||
-rw-r--r-- | src/core/browser_context_adapter.cpp | 10 | ||||
-rw-r--r-- | src/core/browser_context_adapter.h | 5 | ||||
-rw-r--r-- | src/core/browser_context_qt.cpp | 25 | ||||
-rw-r--r-- | src/core/browser_context_qt.h | 4 | ||||
-rw-r--r-- | src/webengine/api/qquickwebengineprofile.cpp | 32 | ||||
-rw-r--r-- | src/webengine/api/qquickwebengineprofile.h | 8 | ||||
-rw-r--r-- | src/webengine/plugin/plugins.qmltypes | 4 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebengineprofile.cpp | 20 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebengineprofile.h | 4 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginespellcheck/dict/de-DE.aff | 5 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginespellcheck/dict/de-DE.dic | 14 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginespellcheck/qwebenginespellcheck.pro | 6 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginespellcheck/tst_qwebenginespellcheck.cpp | 35 |
14 files changed, 114 insertions, 64 deletions
diff --git a/examples/webenginewidgets/spellchecker/webview.cpp b/examples/webenginewidgets/spellchecker/webview.cpp index 6c73d88fa..86e21ab13 100644 --- a/examples/webenginewidgets/spellchecker/webview.cpp +++ b/examples/webenginewidgets/spellchecker/webview.cpp @@ -51,7 +51,7 @@ WebView::WebView(QWidget *parent) m_spellCheckLanguages["German"] = "de-DE"; QWebEngineProfile *profile = page()->profile(); profile->setSpellCheckEnabled(true); - profile->setSpellCheckLanguage("en-US"); + profile->setSpellCheckLanguages({"en-US"}); } void WebView::contextMenuEvent(QContextMenuEvent *event) @@ -65,7 +65,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) } QWebEngineProfile *profile = page()->profile(); - const QString &language = profile->spellCheckLanguage(); + const QString &language = profile->spellCheckLanguages().first(); QMenu *menu = page()->createStandardContextMenu(); menu->addSeparator(); @@ -85,7 +85,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) QString lang = m_spellCheckLanguages[str]; action->setChecked(language == lang); connect(action, &QAction::triggered, this, [profile, lang](){ - profile->setSpellCheckLanguage(lang); + profile->setSpellCheckLanguages(QStringList()<<lang); }); } } diff --git a/src/core/browser_context_adapter.cpp b/src/core/browser_context_adapter.cpp index 86808e177..3396f73ef 100644 --- a/src/core/browser_context_adapter.cpp +++ b/src/core/browser_context_adapter.cpp @@ -491,19 +491,19 @@ void BrowserContextAdapter::clearHttpCache() m_browserContext->url_request_getter_->clearHttpCache(); } -void BrowserContextAdapter::setSpellCheckLanguage(const QString &language) +void BrowserContextAdapter::setSpellCheckLanguages(const QStringList &languages) { #if defined(ENABLE_SPELLCHECK) - m_browserContext->setSpellCheckLanguage(language); + m_browserContext->setSpellCheckLanguages(languages); #endif } -QString BrowserContextAdapter::spellCheckLanguage() const +QStringList BrowserContextAdapter::spellCheckLanguages() const { #if defined(ENABLE_SPELLCHECK) - return m_browserContext->spellCheckLanguage(); + return m_browserContext->spellCheckLanguages(); #else - return QString(); + return QStringList(); #endif } diff --git a/src/core/browser_context_adapter.h b/src/core/browser_context_adapter.h index a6e5a2a3e..39b9db61c 100644 --- a/src/core/browser_context_adapter.h +++ b/src/core/browser_context_adapter.h @@ -108,9 +108,8 @@ public: QString httpUserAgent() const; void setHttpUserAgent(const QString &userAgent); - QStringList spellCheckLanguages(const QStringList &acceptLanguages); - void setSpellCheckLanguage(const QString &language); - QString spellCheckLanguage() const; + void setSpellCheckLanguages(const QStringList &language); + QStringList spellCheckLanguages() const; void setSpellCheckEnabled(bool enabled); bool isSpellCheckEnabled() const; diff --git a/src/core/browser_context_qt.cpp b/src/core/browser_context_qt.cpp index 5f544ab86..a6801e1d7 100644 --- a/src/core/browser_context_qt.cpp +++ b/src/core/browser_context_qt.cpp @@ -213,18 +213,27 @@ void BrowserContextQt::failedToLoadDictionary(const std::string &language) << "Make sure that correct bdic file is in:" << toQt(WebEngineLibraryInfo::getPath(base::DIR_APP_DICTIONARIES).value()); } -void BrowserContextQt::setSpellCheckLanguage(const QString &language) +void BrowserContextQt::setSpellCheckLanguages(const QStringList &languages) { - base::ListValue dictionaries; - dictionaries.AppendString(language.toStdString()); - m_prefService->Set(prefs::kSpellCheckDictionaries, dictionaries); + StringListPrefMember dictionaries_pref; + dictionaries_pref.Init(prefs::kSpellCheckDictionaries, m_prefService.get()); + std::vector<std::string> dictionaries; + dictionaries.reserve(languages.size()); + for (const auto &language : languages) + dictionaries.push_back(language.toStdString()); + dictionaries_pref.SetValue(dictionaries); } -QString BrowserContextQt::spellCheckLanguage() const +QStringList BrowserContextQt::spellCheckLanguages() const { - std::string dictionary; - m_prefService->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); - return QString::fromStdString(dictionary); + QStringList spellcheck_dictionaries; + for (const auto &value : *m_prefService->GetList(prefs::kSpellCheckDictionaries)) { + std::string dictionary; + if (value->GetAsString(&dictionary)) + spellcheck_dictionaries.append(QString::fromStdString(dictionary)); + } + + return spellcheck_dictionaries; } void BrowserContextQt::setSpellCheckEnabled(bool enabled) diff --git a/src/core/browser_context_qt.h b/src/core/browser_context_qt.h index c3528871f..ca65552be 100644 --- a/src/core/browser_context_qt.h +++ b/src/core/browser_context_qt.h @@ -102,8 +102,8 @@ public: #if defined(ENABLE_SPELLCHECK) void failedToLoadDictionary(const std::string& language) override; - void setSpellCheckLanguage(const QString &language); - QString spellCheckLanguage() const; + void setSpellCheckLanguages(const QStringList &languages); + QStringList spellCheckLanguages() const; void setSpellCheckEnabled(bool enabled); bool isSpellCheckEnabled() const; #endif diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index 6a4846ddf..871428f33 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -87,8 +87,8 @@ ASSERT_ENUMS_MATCH(QQuickWebEngineDownloadItem::MimeHtmlSaveFormat, QtWebEngineC register custom URL schemes (QQuickWebEngineProfile::installUrlSchemeHandler). Spellchecking HTML form fields can be enabled per profile by setting the \l spellCheckEnabled - property and the current language used for spellchecking can be set by using the - \l spellCheckLanguage property. + property and the current languages used for spellchecking can be set by using the + \l spellCheckLanguages property. */ /*! @@ -618,18 +618,18 @@ QQuickWebEngineProfile *QQuickWebEngineProfile::defaultProfile() } /*! - \property QQuickWebEngineProfile::spellCheckLanguage - \brief the language used by the spell checker. + \property QQuickWebEngineProfile::spellCheckLanguages + \brief The languages used by the spell checker. \since QtWebEngine 1.4 */ /*! - \qmlproperty QString WebEngineProfile::spellCheckLanguage + \qmlproperty list<string> WebEngineProfile::spellCheckLanguages - This property holds the language used by the spell checker. - The language should match the name of the \c .bdic dictionary. - For example, the \a language \c en-US will load the \c en-US.bdic + This property holds the list of languages used by the spell checker. + Each language should match the name of the \c .bdic dictionary. + For example, the language \c en-US will load the \c en-US.bdic dictionary file. Qt WebEngine checks for the \c qtwebengine_dictionaries subdirectory @@ -666,24 +666,24 @@ QQuickWebEngineProfile *QQuickWebEngineProfile::defaultProfile() \since QtWebEngine 1.4 */ -void QQuickWebEngineProfile::setSpellCheckLanguage(const QString &language) +void QQuickWebEngineProfile::setSpellCheckLanguages(const QStringList &languages) { Q_D(QQuickWebEngineProfile); - if (language != d->browserContext()->spellCheckLanguage()) { - d->browserContext()->setSpellCheckLanguage(language); - emit spellCheckLanguageChanged(); + if (languages != d->browserContext()->spellCheckLanguages()) { + d->browserContext()->setSpellCheckLanguages(languages); + emit spellCheckLanguagesChanged(); } } /*! \since 5.8 - Returns the language used by the spell checker. + Returns the list of languages used by the spell checker. */ -QString QQuickWebEngineProfile::spellCheckLanguage() const +QStringList QQuickWebEngineProfile::spellCheckLanguages() const { const Q_D(QQuickWebEngineProfile); - return d->browserContext()->spellCheckLanguage(); + return d->browserContext()->spellCheckLanguages(); } /*! @@ -694,7 +694,7 @@ QString QQuickWebEngineProfile::spellCheckLanguage() const */ /*! - \qmlproperty QString WebEngineProfile::spellCheckEnabled + \qmlproperty bool WebEngineProfile::spellCheckEnabled This property holds whether the web engine spell checker is enabled. diff --git a/src/webengine/api/qquickwebengineprofile.h b/src/webengine/api/qquickwebengineprofile.h index 577cc7f6a..d0d925930 100644 --- a/src/webengine/api/qquickwebengineprofile.h +++ b/src/webengine/api/qquickwebengineprofile.h @@ -71,7 +71,7 @@ class Q_WEBENGINE_EXPORT QQuickWebEngineProfile : public QObject { Q_PROPERTY(QString httpAcceptLanguage READ httpAcceptLanguage WRITE setHttpAcceptLanguage NOTIFY httpAcceptLanguageChanged FINAL REVISION 1) Q_PROPERTY(PersistentCookiesPolicy persistentCookiesPolicy READ persistentCookiesPolicy WRITE setPersistentCookiesPolicy NOTIFY persistentCookiesPolicyChanged FINAL) Q_PROPERTY(int httpCacheMaximumSize READ httpCacheMaximumSize WRITE setHttpCacheMaximumSize NOTIFY httpCacheMaximumSizeChanged FINAL) - Q_PROPERTY(QString spellCheckLanguage READ spellCheckLanguage WRITE setSpellCheckLanguage NOTIFY spellCheckLanguageChanged FINAL REVISION 3) + Q_PROPERTY(QStringList spellCheckLanguages READ spellCheckLanguages WRITE setSpellCheckLanguages NOTIFY spellCheckLanguagesChanged FINAL REVISION 3) Q_PROPERTY(bool spellCheckEnabled READ isSpellCheckEnabled WRITE setSpellCheckEnabled NOTIFY spellCheckEnabledChanged FINAL REVISION 3) public: @@ -131,8 +131,8 @@ public: Q_REVISION(2) Q_INVOKABLE void clearHttpCache(); - void setSpellCheckLanguage(const QString &language); - QString spellCheckLanguage() const; + void setSpellCheckLanguages(const QStringList &languages); + QStringList spellCheckLanguages() const; void setSpellCheckEnabled(bool enabled); bool isSpellCheckEnabled() const; @@ -148,7 +148,7 @@ Q_SIGNALS: void persistentCookiesPolicyChanged(); void httpCacheMaximumSizeChanged(); Q_REVISION(1) void httpAcceptLanguageChanged(); - Q_REVISION(3) void spellCheckLanguageChanged(); + Q_REVISION(3) void spellCheckLanguagesChanged(); Q_REVISION(3) void spellCheckEnabledChanged(); void downloadRequested(QQuickWebEngineDownloadItem *download); diff --git a/src/webengine/plugin/plugins.qmltypes b/src/webengine/plugin/plugins.qmltypes index c8aaaf420..304bfd6b7 100644 --- a/src/webengine/plugin/plugins.qmltypes +++ b/src/webengine/plugin/plugins.qmltypes @@ -359,10 +359,10 @@ Module { Property { name: "httpAcceptLanguage"; revision: 1; type: "string" } Property { name: "persistentCookiesPolicy"; type: "PersistentCookiesPolicy" } Property { name: "httpCacheMaximumSize"; type: "int" } - Property { name: "spellCheckLanguage"; revision: 3; type: "string" } + Property { name: "spellCheckLanguages"; revision: 3; type: "QStringList" } Property { name: "spellCheckEnabled"; revision: 3; type: "bool" } Signal { name: "httpAcceptLanguageChanged"; revision: 1 } - Signal { name: "spellCheckLanguageChanged"; revision: 3 } + Signal { name: "spellCheckLanguagesChanged"; revision: 3 } Signal { name: "spellCheckEnabledChanged"; revision: 3 } Signal { name: "downloadRequested" diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index bd41a5126..886a7207e 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -98,8 +98,8 @@ using QtWebEngineCore::BrowserContextAdapter; QWebEngineUrlSchemeHandler::requestStarted() as QWebEngineUrlRequestJob objects. Spellchecking HTML form fields can be enabled per profile by using the setSpellCheckEnabled() - method and the current language used for spellchecking can be set by using the - setSpellCheckLanguage() method. + method and the current languages used for spellchecking can be set by using the + setSpellCheckLanguages() method. */ @@ -565,9 +565,9 @@ QWebEngineProfile *QWebEngineProfile::defaultProfile() /*! \since 5.8 - Sets the current \a language for the spell checker. - The language should match the name of the \c .bdic dictionary. - For example, the \a language \c en-US will load the \c en-US.bdic + Sets the current list of \a languages for the spell checker. + Each language should match the name of the \c .bdic dictionary. + For example, the language \c en-US will load the \c en-US.bdic dictionary file. Qt WebEngine checks for the \c qtwebengine_dictionaries subdirectory @@ -603,21 +603,21 @@ QWebEngineProfile *QWebEngineProfile::defaultProfile() \l{WebEngine Widgets Spellchecker Example}{Spellchecker Example}. */ -void QWebEngineProfile::setSpellCheckLanguage(const QString &language) +void QWebEngineProfile::setSpellCheckLanguages(const QStringList &languages) { Q_D(QWebEngineProfile); - d->browserContext()->setSpellCheckLanguage(language); + d->browserContext()->setSpellCheckLanguages(languages); } /*! \since 5.8 - Returns the language used by the spell checker. + Returns the list of languages used by the spell checker. */ -QString QWebEngineProfile::spellCheckLanguage() const +QStringList QWebEngineProfile::spellCheckLanguages() const { const Q_D(QWebEngineProfile); - return d->browserContext()->spellCheckLanguage(); + return d->browserContext()->spellCheckLanguages(); } /*! diff --git a/src/webenginewidgets/api/qwebengineprofile.h b/src/webenginewidgets/api/qwebengineprofile.h index 704414bcf..1ce4bfe17 100644 --- a/src/webenginewidgets/api/qwebengineprofile.h +++ b/src/webenginewidgets/api/qwebengineprofile.h @@ -121,8 +121,8 @@ public: void clearHttpCache(); - void setSpellCheckLanguage(const QString &language); - QString spellCheckLanguage() const; + void setSpellCheckLanguages(const QStringList &languages); + QStringList spellCheckLanguages() const; void setSpellCheckEnabled(bool enabled); bool isSpellCheckEnabled() const; diff --git a/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.aff b/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.aff new file mode 100644 index 000000000..ff8185771 --- /dev/null +++ b/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.aff @@ -0,0 +1,5 @@ +SET UTF-8 +TRY esianrtolcdugmphbyfvkwzqESIANRTOLCDUGMPHBYFVKWZQ + +PFX Q Y 1 +PFX Q 0 q . diff --git a/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.dic b/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.dic new file mode 100644 index 000000000..d10ae2600 --- /dev/null +++ b/tests/auto/widgets/qwebenginespellcheck/dict/de-DE.dic @@ -0,0 +1,14 @@ +15 +du/Q +er/Q +es/Q +ich/Q +ihr/Q +liebe/Q +lieben/Q +liebst/Q +liebt/Q +qt/Q +sie/Q +Sie/Q +wir/Q diff --git a/tests/auto/widgets/qwebenginespellcheck/qwebenginespellcheck.pro b/tests/auto/widgets/qwebenginespellcheck/qwebenginespellcheck.pro index 437aad937..a36c82e20 100644 --- a/tests/auto/widgets/qwebenginespellcheck/qwebenginespellcheck.pro +++ b/tests/auto/widgets/qwebenginespellcheck/qwebenginespellcheck.pro @@ -2,7 +2,9 @@ include(../tests.pri) DISTFILES += \ dict/en-US.dic \ - dict/en-US.aff + dict/en-US.aff \ + dict/de-DE.dic \ + dict/de-DE.aff \ qtPrepareTool(CONVERT_TOOL, qwebengine_convert_dict) @@ -13,7 +15,7 @@ debug_and_release { DICTIONARIES_DIR = qtwebengine_dictionaries } -dict.files = $$PWD/dict/en-US.dic +dict.files = $$PWD/dict/en-US.dic $$PWD/dict/de-DE.dic dictoolbuild.input = dict.files dictoolbuild.output = $${DICTIONARIES_DIR}/${QMAKE_FILE_BASE}.bdic dictoolbuild.commands = $${CONVERT_TOOL} ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT} diff --git a/tests/auto/widgets/qwebenginespellcheck/tst_qwebenginespellcheck.cpp b/tests/auto/widgets/qwebenginespellcheck/tst_qwebenginespellcheck.cpp index 534d40f45..4db5b9477 100644 --- a/tests/auto/widgets/qwebenginespellcheck/tst_qwebenginespellcheck.cpp +++ b/tests/auto/widgets/qwebenginespellcheck/tst_qwebenginespellcheck.cpp @@ -73,8 +73,10 @@ private Q_SLOTS: void cleanup(); void initTestCase(); void spellCheckLanguage(); + void spellCheckLanguages(); void spellCheckEnabled(); void spellcheck(); + void spellcheck_data(); private: void load(); @@ -86,14 +88,14 @@ void tst_QWebEngineSpellcheck::initTestCase() QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); QVERIFY(profile); QVERIFY(!profile->isSpellCheckEnabled()); - QVERIFY(profile->spellCheckLanguage().isEmpty()); + QVERIFY(profile->spellCheckLanguages().isEmpty()); } void tst_QWebEngineSpellcheck::init() { QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); profile->setSpellCheckEnabled(false); - profile->setSpellCheckLanguage(QString::null); + profile->setSpellCheckLanguages(QStringList()); m_view = new WebView(); } @@ -113,10 +115,19 @@ void tst_QWebEngineSpellcheck::spellCheckLanguage() { QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); QVERIFY(profile); - profile->setSpellCheckLanguage("en-US"); - QVERIFY(profile->spellCheckLanguage() == "en-US"); + profile->setSpellCheckLanguages({"en-US"}); + QVERIFY(profile->spellCheckLanguages() == QStringList({"en-US"})); } +void tst_QWebEngineSpellcheck::spellCheckLanguages() +{ + QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); + QVERIFY(profile); + profile->setSpellCheckLanguages({"en-US","de-DE"}); + QVERIFY(profile->spellCheckLanguages() == QStringList({"en-US","de-DE"})); +} + + void tst_QWebEngineSpellcheck::spellCheckEnabled() { QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); @@ -127,9 +138,12 @@ void tst_QWebEngineSpellcheck::spellCheckEnabled() void tst_QWebEngineSpellcheck::spellcheck() { + QFETCH(QStringList, languages); + QFETCH(QStringList, suggestions); + QWebEngineProfile *profile = QWebEngineProfile::defaultProfile(); QVERIFY(profile); - profile->setSpellCheckLanguage("en-US"); + profile->setSpellCheckLanguages(languages); profile->setSpellCheckEnabled(true); load(); @@ -166,8 +180,7 @@ void tst_QWebEngineSpellcheck::spellcheck() QVERIFY(m_view->data().misspelledWord() == "lovee"); // check suggestions - QStringList expected {"love", "loves"}; - QVERIFY(m_view->data().spellCheckerSuggestions() == expected); + QVERIFY(m_view->data().spellCheckerSuggestions() == suggestions); // check replace word m_view->page()->replaceMisspelledWord("love"); @@ -175,5 +188,13 @@ void tst_QWebEngineSpellcheck::spellcheck() QTRY_VERIFY(evaluateJavaScriptSync(m_view->page(), "text();").toString() == text); } +void tst_QWebEngineSpellcheck::spellcheck_data() +{ + QTest::addColumn<QStringList>("languages"); + QTest::addColumn<QStringList>("suggestions"); + QTest::newRow("en-US") << QStringList({"en-US"}) << QStringList({"love", "loves"}); + QTest::newRow("en-US,de-DE") << QStringList({"en-US","de-DE"}) << QStringList({"love", "liebe", "loves"}); +} + QTEST_MAIN(tst_QWebEngineSpellcheck) #include "tst_qwebenginespellcheck.moc" |