Commit 64e7096f authored by David Faure's avatar David Faure

WebEngine: port from execJScript to runJavaScript+lambda. Not tested.

parent 94f7685b
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#define QL1S(x) QLatin1String(x) #define QL1S(x) QLatin1String(x)
#define QL1C(x) QLatin1Char(x) #define QL1C(x) QLatin1Char(x)
// A functor that calls a member function
template<typename Arg, typename R, typename C> template<typename Arg, typename R, typename C>
struct InvokeWrapper { struct InvokeWrapper {
R *receiver; R *receiver;
...@@ -67,8 +68,7 @@ struct InvokeWrapper { ...@@ -67,8 +68,7 @@ struct InvokeWrapper {
template<typename Arg, typename R, typename C> template<typename Arg, typename R, typename C>
InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFun)(Arg)) InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFun)(Arg))
{ {
InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFun}; return InvokeWrapper<Arg, R, C>{receiver, memberFun};
return wrapper;
} }
WebEngineBrowserExtension::WebEngineBrowserExtension(WebEnginePart *parent, const QByteArray& cachedHistoryData) WebEngineBrowserExtension::WebEngineBrowserExtension(WebEnginePart *parent, const QByteArray& cachedHistoryData)
...@@ -165,7 +165,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream) ...@@ -165,7 +165,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream)
QDataStream stream (&buffer); QDataStream stream (&buffer);
view()->page()->setProperty("HistoryNavigationLocked", true); view()->page()->setProperty("HistoryNavigationLocked", true);
stream >> *history; stream >> *history;
QWebEngineHistoryItem currentItem (history->currentItem()); QWebEngineHistoryItem currentItem(history->currentItem());
if (currentItem.isValid()) { if (currentItem.isValid()) {
if (currentItem.isValid() && (xOfs != -1 || yOfs != -1)) { if (currentItem.isValid() && (xOfs != -1 || yOfs != -1)) {
const QPoint scrollPos (xOfs, yOfs); const QPoint scrollPos (xOfs, yOfs);
...@@ -174,7 +174,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream) ...@@ -174,7 +174,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream)
// NOTE 1: The following Konqueror specific workaround is necessary // NOTE 1: The following Konqueror specific workaround is necessary
// because Konqueror only preserves information for the last visited // because Konqueror only preserves information for the last visited
// page. However, we save the entire history content in saveState and // page. However, we save the entire history content in saveState and
// and hence need to elimiate all but the current item here. // and hence need to eliminate all but the current item here.
// NOTE 2: This condition only applies when Konqueror is restored from // NOTE 2: This condition only applies when Konqueror is restored from
// abnormal termination ; a crash and/or a session restoration. // abnormal termination ; a crash and/or a session restoration.
if (QCoreApplication::applicationName() == QLatin1String("konqueror")) { if (QCoreApplication::applicationName() == QLatin1String("konqueror")) {
...@@ -211,7 +211,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream) ...@@ -211,7 +211,7 @@ void WebEngineBrowserExtension::restoreState(QDataStream &stream)
// As a last resort, in case the history restoration logic above fails, // As a last resort, in case the history restoration logic above fails,
// attempt to open the requested URL directly. // attempt to open the requested URL directly.
qDebug() << "Normal history navgation logic failed! Falling back to opening url directly."; qDebug() << "Normal history navigation logic failed! Falling back to opening url directly.";
m_part->openUrl(u); m_part->openUrl(u);
} }
...@@ -687,61 +687,52 @@ void WebEngineBrowserExtension::slotTextDirectionChanged() ...@@ -687,61 +687,52 @@ void WebEngineBrowserExtension::slotTextDirectionChanged()
} }
} }
static QVariant execJScript(WebEngineView* view, const QString& script)
{
#if 0
QWebElement element (view->contextMenuResult().element());
if (element.isNull())
return QVariant();
return element.evaluateJavaScript(script);
#endif
return QVariant();
}
void WebEngineBrowserExtension::slotCheckSpelling() void WebEngineBrowserExtension::slotCheckSpelling()
{ {
const QString text (execJScript(view(), QL1S("this.value")).toString()); view()->page()->runJavaScript(QL1S("this.value"), [this](const QVariant &value) {
const QString text = value.toString();
if ( text.isEmpty() ) { if (!text.isEmpty()) {
return; m_spellTextSelectionStart = 0;
} m_spellTextSelectionEnd = 0;
m_spellTextSelectionStart = 0; Sonnet::BackgroundChecker *backgroundSpellCheck = new Sonnet::BackgroundChecker;
m_spellTextSelectionEnd = 0; Sonnet::Dialog* spellDialog = new Sonnet::Dialog(backgroundSpellCheck, view());
backgroundSpellCheck->setParent(spellDialog);
Sonnet::BackgroundChecker *backgroundSpellCheck = new Sonnet::BackgroundChecker; spellDialog->setAttribute(Qt::WA_DeleteOnClose, true);
Sonnet::Dialog* spellDialog = new Sonnet::Dialog(backgroundSpellCheck, view()); spellDialog->showSpellCheckCompletionMessage(true);
backgroundSpellCheck->setParent(spellDialog); connect(spellDialog, SIGNAL(replace(QString,int,QString)), this, SLOT(spellCheckerCorrected(QString,int,QString)));
spellDialog->setAttribute(Qt::WA_DeleteOnClose, true); connect(spellDialog, SIGNAL(misspelling(QString,int)), this, SLOT(spellCheckerMisspelling(QString,int)));
spellDialog->showSpellCheckCompletionMessage(true); spellDialog->setBuffer(text);
connect(spellDialog, SIGNAL(replace(QString,int,QString)), this, SLOT(spellCheckerCorrected(QString,int,QString))); spellDialog->show();
connect(spellDialog, SIGNAL(misspelling(QString,int)), this, SLOT(spellCheckerMisspelling(QString,int))); }
spellDialog->setBuffer(text); });
spellDialog->show();
} }
void WebEngineBrowserExtension::slotSpellCheckSelection() void WebEngineBrowserExtension::slotSpellCheckSelection()
{ {
QString text (execJScript(view(), QL1S("this.value")).toString()); view()->page()->runJavaScript(QL1S("this.value"), [this](const QVariant &value) {
const QString text = value.toString();
if ( text.isEmpty() ) { if (!text.isEmpty()) {
return; view()->page()->runJavaScript(QL1S("this.selectionStart + ' ' + this.selectionEnd"), [this, text](const QVariant &value) {
} const QString values = value.toString();
const int pos = values.indexOf(' ');
m_spellTextSelectionStart = qMax(0, execJScript(view(), QL1S("this.selectionStart")).toInt()); m_spellTextSelectionStart = qMax(0, values.left(pos).toInt());
m_spellTextSelectionEnd = qMax(0, execJScript(view(), QL1S("this.selectionEnd")).toInt()); m_spellTextSelectionEnd = qMax(0, values.mid(pos + 1).toInt());
// kDebug() << "selection start:" << m_spellTextSelectionStart << "end:" << m_spellTextSelectionEnd; // kDebug() << "selection start:" << m_spellTextSelectionStart << "end:" << m_spellTextSelectionEnd;
Sonnet::BackgroundChecker *backgroundSpellCheck = new Sonnet::BackgroundChecker; Sonnet::BackgroundChecker *backgroundSpellCheck = new Sonnet::BackgroundChecker;
Sonnet::Dialog* spellDialog = new Sonnet::Dialog(backgroundSpellCheck, view()); Sonnet::Dialog* spellDialog = new Sonnet::Dialog(backgroundSpellCheck, view());
backgroundSpellCheck->setParent(spellDialog); backgroundSpellCheck->setParent(spellDialog);
spellDialog->setAttribute(Qt::WA_DeleteOnClose, true); spellDialog->setAttribute(Qt::WA_DeleteOnClose, true);
spellDialog->showSpellCheckCompletionMessage(true); spellDialog->showSpellCheckCompletionMessage(true);
connect(spellDialog, SIGNAL(replace(QString,int,QString)), this, SLOT(spellCheckerCorrected(QString,int,QString))); connect(spellDialog, SIGNAL(replace(QString,int,QString)), this, SLOT(spellCheckerCorrected(QString,int,QString)));
connect(spellDialog, SIGNAL(misspelling(QString,int)), this, SLOT(spellCheckerMisspelling(QString,int))); connect(spellDialog, SIGNAL(misspelling(QString,int)), this, SLOT(spellCheckerMisspelling(QString,int)));
connect(spellDialog, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString))); connect(spellDialog, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString)));
spellDialog->setBuffer(text.mid(m_spellTextSelectionStart, (m_spellTextSelectionEnd - m_spellTextSelectionStart))); spellDialog->setBuffer(text.mid(m_spellTextSelectionStart, (m_spellTextSelectionEnd - m_spellTextSelectionStart)));
spellDialog->show(); spellDialog->show();
});
}
});
} }
void WebEngineBrowserExtension::spellCheckerCorrected(const QString& original, int pos, const QString& replacement) void WebEngineBrowserExtension::spellCheckerCorrected(const QString& original, int pos, const QString& replacement)
...@@ -761,7 +752,7 @@ void WebEngineBrowserExtension::spellCheckerCorrected(const QString& original, i ...@@ -761,7 +752,7 @@ void WebEngineBrowserExtension::spellCheckerCorrected(const QString& original, i
script += QL1S(")"); script += QL1S(")");
//kDebug() << "**** script:" << script; //kDebug() << "**** script:" << script;
execJScript(view(), script); view()->page()->runJavaScript(script);
} }
void WebEngineBrowserExtension::spellCheckerMisspelling(const QString& text, int pos) void WebEngineBrowserExtension::spellCheckerMisspelling(const QString& text, int pos)
...@@ -772,7 +763,7 @@ void WebEngineBrowserExtension::spellCheckerMisspelling(const QString& text, int ...@@ -772,7 +763,7 @@ void WebEngineBrowserExtension::spellCheckerMisspelling(const QString& text, int
selectionScript += QL1C(','); selectionScript += QL1C(',');
selectionScript += QString::number(pos + text.length() + m_spellTextSelectionStart); selectionScript += QString::number(pos + text.length() + m_spellTextSelectionStart);
selectionScript += QL1C(')'); selectionScript += QL1C(')');
execJScript(view(), selectionScript); view()->page()->runJavaScript(selectionScript);
} }
void WebEngineBrowserExtension::slotSpellCheckDone(const QString&) void WebEngineBrowserExtension::slotSpellCheckDone(const QString&)
...@@ -785,11 +776,10 @@ void WebEngineBrowserExtension::slotSpellCheckDone(const QString&) ...@@ -785,11 +776,10 @@ void WebEngineBrowserExtension::slotSpellCheckDone(const QString&)
script += QL1C(','); script += QL1C(',');
script += QString::number(m_spellTextSelectionEnd); script += QString::number(m_spellTextSelectionEnd);
script += QL1C(')'); script += QL1C(')');
execJScript(view(), script); view()->page()->runJavaScript(script);
} }
} }
void WebEngineBrowserExtension::saveHistory() void WebEngineBrowserExtension::saveHistory()
{ {
QWebEngineHistory* history = (view() ? view()->history() : 0); QWebEngineHistory* history = (view() ? view()->history() : 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment