testwebenginescrolladdattachment.cpp 3.03 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
  Copyright (c) 2016 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "testwebenginescrolladdattachment.h"
19
#include "webengine/webenginescript.h"
Laurent Montel's avatar
Laurent Montel committed
20

21
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QApplication>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <QPushButton>
#include <QVBoxLayout>
#include <QWebEngineSettings>

#include <viewer/webengine/mailwebengineview.h>

template<typename Arg, typename R, typename C>
struct InvokeWrapper {
    R *receiver;
    void (C::*memberFunction)(Arg);
    void operator()(Arg result)
    {
        (receiver->*memberFunction)(result);
    }
};

template<typename Arg, typename R, typename C>

InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFunction)(Arg))
{
    InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFunction};
    return wrapper;
}
Laurent Montel's avatar
Laurent Montel committed
46
47
48
49

TestWebEngineScrollAddAttachment::TestWebEngineScrollAddAttachment(QWidget *parent)
    : QWidget(parent)
{
50
51
52
53
54
55
56
57
58
    QVBoxLayout *vboxLayout = new QVBoxLayout(this);

    mTestWebEngine = new MessageViewer::MailWebEngineView(new KActionCollection(this), this);
    mTestWebEngine->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
    vboxLayout->addWidget(mTestWebEngine);
    mTestWebEngine->load(QUrl(QStringLiteral("http://www.kde.org")));
    QPushButton *scrollToButton = new QPushButton(QStringLiteral("Scroll to Attachment"), this);
    vboxLayout->addWidget(scrollToButton);
    connect(scrollToButton, &QPushButton::clicked, this, &TestWebEngineScrollAddAttachment::slotScrollToAttachment);
Laurent Montel's avatar
Laurent Montel committed
59
60
61

}

62
63
64
void TestWebEngineScrollAddAttachment::handleScrollToAnchor(const QVariant &result)
{
    qDebug() << " result "<<result;
65
66
67
68
69
70
71
    if (result.isValid()) {
        const QList<QVariant> lst = result.toList();
        if (lst.count() == 2) {
            const QPoint pos(lst.at(0).toInt(), lst.at(1).toInt());
            mTestWebEngine->page()->runJavaScript(MessageViewer::WebEngineScript::scrollToPosition(pos));
        }
    }
72
73
74
75
}

void TestWebEngineScrollAddAttachment::slotScrollToAttachment()
{
Laurent Montel's avatar
Laurent Montel committed
76
    mTestWebEngine->page()->runJavaScript(MessageViewer::WebEngineScript::searchElementPosition(QStringLiteral("module")), invoke(this, &TestWebEngineScrollAddAttachment::handleScrollToAnchor));
77
78
}

Laurent Montel's avatar
Laurent Montel committed
79
80
81
82
83
84
85
86
87
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
    TestWebEngineScrollAddAttachment *testWebEngine = new TestWebEngineScrollAddAttachment;
    testWebEngine->show();
    const int ret = app.exec();
    return ret;
}