From 4713387c052d54e0f5ea02efaeaa25931d1cd7ee Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Thu, 8 Oct 2015 23:49:26 +0200 Subject: Add spell checker support Integrate chromium spell checker: * add spellchecker and dependencies to build * underline misspelled words in html text areas * right-click context menu shows up to 4 options to correct the misspelled word * toggle spell check from context menu * add new qml and widget api calls to qwebengineprofile to enable/disable spell check, select spell check language, get list of supported languages/dictionaries * register new qml spell check properties for QtWebEngine 1.3 * CONFIG+="no_spellcheck" to remove spellcheck support Change-Id: Ie61434ab9493597d7759a6f33794f6859c4e3a4c Reviewed-by: Allan Sandfeld Jensen --- src/webenginewidgets/api/qwebenginepage.cpp | 38 +++++++++++++ src/webenginewidgets/api/qwebenginepage.h | 8 ++- src/webenginewidgets/api/qwebengineprofile.cpp | 64 ++++++++++++++++++++++ src/webenginewidgets/api/qwebengineprofile.h | 8 +++ .../doc/src/qwebenginepage_lgpl.qdoc | 5 ++ src/webenginewidgets/webenginewidgets.pro | 7 +++ 6 files changed, 129 insertions(+), 1 deletion(-) (limited to 'src/webenginewidgets') diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index d68f364e4..62c1adee6 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -835,6 +835,11 @@ QAction *QWebEnginePage::action(WebAction action) const case SavePage: text = tr("Save &Page"); break; +#if !defined(QT_NO_SPELLCHECK) + case ToggleSpellcheck: + text = tr("Check Spelling"); + break; +#endif default: break; } @@ -1015,6 +1020,17 @@ void QWebEnginePage::triggerAction(WebAction action, bool) case SavePage: d->adapter->save(); break; +#if !defined(QT_NO_SPELLCHECK) + case ToggleSpellcheck: + d->adapter->toogleSpellCheckEnabled(); + break; + case ReplaceMisspelledWord_1: + case ReplaceMisspelledWord_2: + case ReplaceMisspelledWord_3: + case ReplaceMisspelledWord_4: + d->adapter->replaceMisspelling(d->actions[action]->text()); + break; +#endif default: Q_UNREACHABLE(); } @@ -1194,6 +1210,19 @@ QMenu *QWebEnginePage::createStandardContextMenu() QMenu *menu = new QMenu(d->view); QAction *action = 0; WebEngineContextMenuData contextMenuData(d->m_menuData); + +#if !defined(QT_NO_SPELLCHECK) + if (contextMenuData.isEditable && !contextMenuData.spellCheckerSuggestions.isEmpty()) { + for (int i=0; i < contextMenuData.spellCheckerSuggestions.count() && i < 4; i++) { + int index = ReplaceMisspelledWord_1 + i; + QAction *action(QWebEnginePage::action(static_cast(index))); + action->setText(contextMenuData.spellCheckerSuggestions.at(i)); + menu->addAction(action); + } + menu->addSeparator(); + } +#endif + if (!contextMenuData.linkText.isEmpty() && contextMenuData.linkUrl.isValid()) { action = QWebEnginePage::action(OpenLinkInThisWindow); action->setText(tr("Follow Link")); @@ -1259,6 +1288,15 @@ QMenu *QWebEnginePage::createStandardContextMenu() if (d->isFullScreenMode()) menu->addAction(QWebEnginePage::action(ExitFullScreen)); +#if !defined(QT_NO_SPELLCHECK) + if (contextMenuData.isEditable) { + QAction* spellcheckAction(QWebEnginePage::action(ToggleSpellcheck)); + menu->addAction(spellcheckAction); + spellcheckAction->setCheckable(true); + spellcheckAction->setChecked(contextMenuData.isSpellCheckerEnabled); + } +#endif + return menu; } diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h index d8bdec303..be9f08ad1 100644 --- a/src/webenginewidgets/api/qwebenginepage.h +++ b/src/webenginewidgets/api/qwebenginepage.h @@ -118,7 +118,13 @@ public: RequestClose, Unselect, SavePage, - +#if !defined(QT_NO_SPELLCHECK) + ToggleSpellcheck, + ReplaceMisspelledWord_1, + ReplaceMisspelledWord_2, + ReplaceMisspelledWord_3, + ReplaceMisspelledWord_4, +#endif WebActionCount }; diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index 69d96f925..8a6523a1b 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -530,6 +530,70 @@ QWebEngineProfile *QWebEngineProfile::defaultProfile() return profile; } +#if !defined(QT_NO_SPELLCHECK) +/*! + \since 5.7 + + Returns the subset of \a acceptLanguages supported by the spell checker. + + Checks whether the spell checker dictionary is installed for the specified + language from the \a acceptLanguages list. If the dictionary file is missing + or corrupted, the language is removed from the returned list. + + \sa setSpellCheckLanguage() +*/ +QStringList QWebEngineProfile::spellCheckLanguages(const QStringList &acceptLanguages) +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->spellCheckLanguages(acceptLanguages); +} + +/*! + \since 5.7 + + Sets the current \a language for the spell checker. +*/ +void QWebEngineProfile::setSpellCheckLanguage(const QString &language) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setSpellCheckLanguage(language); +} + +/*! + \since 5.7 + + Returns the language used by the spell checker. +*/ +QString QWebEngineProfile::spellCheckLanguage() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->spellCheckLanguage(); +} + +/*! + \since 5.7 + + Enables spell checker if \a enable is \c true, otherwise disables it. + \sa isSpellCheckEnabled() + */ +void QWebEngineProfile::setSpellCheckEnabled(bool enable) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setSpellCheckEnabled(enable); +} +/*! + \since 5.7 + + Returns \c true if the spell checker is enabled; otherwise returns \c false. + \sa setSpellCheckEnabled() + */ +bool QWebEngineProfile::isSpellCheckEnabled() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->isSpellCheckEnabled(); +} +#endif + /*! Returns the default settings for all pages in this profile. */ diff --git a/src/webenginewidgets/api/qwebengineprofile.h b/src/webenginewidgets/api/qwebengineprofile.h index 22a913fb2..1a9bfe43a 100644 --- a/src/webenginewidgets/api/qwebengineprofile.h +++ b/src/webenginewidgets/api/qwebengineprofile.h @@ -121,6 +121,14 @@ public: void clearHttpCache(); +#if !defined(QT_NO_SPELLCHECK) + QStringList spellCheckLanguages(const QStringList &acceptLanguages); + void setSpellCheckLanguage(const QString &language); + QString spellCheckLanguage() const; + void setSpellCheckEnabled(bool enabled); + bool isSpellCheckEnabled() const; +#endif + static QWebEngineProfile *defaultProfile(); Q_SIGNALS: diff --git a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc index d3c540016..3b25bac76 100644 --- a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc +++ b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc @@ -125,6 +125,11 @@ \value Unselect Clear the current selection. (Added in Qt 5.7) \value SavePage Save the current page to disk. MHTML is the default format that is used to store the web page on disk. (Added in Qt 5.7) + \value ToggleSpellcheck Enable or disable the spell checker. (Added in Qt 5.7) + \value ReplaceMisspelledWord_1 Replace a misspelled word. (Added in Qt 5.7) + \value ReplaceMisspelledWord_2 Replace a misspelled word. (Added in Qt 5.7) + \value ReplaceMisspelledWord_3 Replace a misspelled word. (Added in Qt 5.7) + \value ReplaceMisspelledWord_4 Replace a misspelled word. (Added in Qt 5.7) \omitvalue WebActionCount diff --git a/src/webenginewidgets/webenginewidgets.pro b/src/webenginewidgets/webenginewidgets.pro index 5687bffaf..3b6b843b6 100644 --- a/src/webenginewidgets/webenginewidgets.pro +++ b/src/webenginewidgets/webenginewidgets.pro @@ -46,4 +46,11 @@ HEADERS = \ DEFINES += QT_UI_DELEGATES } +no_spellcheck { + DEFINES += QT_NO_SPELLCHECK + MODULE_DEFINES += QT_NO_SPELLCHECK +} else { + DEFINES += ENABLE_SPELLCHECK +} + load(qt_module) -- cgit v1.2.3