summaryrefslogtreecommitdiffstats
blob: 717310bed3d97bf095d275ecd041247a55e1672f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#include <Swift/QtUI/QtWebView.h>

#include <boost/numeric/conversion/cast.hpp>

#include <QFocusEvent>
#include <QKeyEvent>
#include <QMenu>

#include <Swiften/Base/Log.h>

namespace Swift {
QtWebView::QtWebView(QWidget* parent) : QWebView(parent), fontSizeIsMinimal(false) {
    setRenderHint(QPainter::SmoothPixmapTransform);
    filteredActions.push_back(QWebPage::CopyLinkToClipboard);
    filteredActions.push_back(QWebPage::CopyImageToClipboard);
    filteredActions.push_back(QWebPage::Copy);
    if (Log::getLogLevel() == Log::debug) {
        filteredActions.push_back(QWebPage::InspectElement);
    }
}

void QtWebView::keyPressEvent(QKeyEvent* event) {
    Qt::KeyboardModifiers modifiers = event->modifiers();
    int key = event->key();
    if (modifiers == Qt::ShiftModifier && (key == Qt::Key_PageUp || key == Qt::Key_PageDown)) {
        modifiers = Qt::NoModifier;
    }
    QKeyEvent* translatedEvent = new QKeyEvent(QEvent::KeyPress,
                               key,
                               modifiers,
                               event->text(),
                               event->isAutoRepeat(),
                               boost::numeric_cast<unsigned short>(event->count()));
    QWebView::keyPressEvent(translatedEvent);
    delete translatedEvent;
}

void QtWebView::dragEnterEvent(QDragEnterEvent*) {

}

void QtWebView::setFontSizeIsMinimal(bool minimum) {
    fontSizeIsMinimal = minimum;
}

void QtWebView::contextMenuEvent(QContextMenuEvent* ev) {
    // Filter out the relevant actions from the standard actions

    QMenu* menu = page()->createStandardContextMenu();
    QList<QAction*> actions(menu->actions());
    for (int i = 0; i < actions.size(); ++i) {
        QAction* action = actions.at(i);
        bool removeAction = true;
        for(size_t j = 0; j < filteredActions.size(); ++j) {
            if (action == pageAction(filteredActions[j])) {
                removeAction = false;
                break;
            }
        }
        if (removeAction) {
            menu->removeAction(action);
        }
    }

    // Add our own custom actions
    menu->addAction(tr("Clear"), this, SIGNAL(clearRequested()));
    menu->addAction(tr("Increase font size"), this, SIGNAL(fontGrowRequested()));
    QAction* shrink = new QAction(tr("Decrease font size"), this);
    shrink->setEnabled(!fontSizeIsMinimal);
    connect(shrink, SIGNAL(triggered()), this, SIGNAL(fontShrinkRequested()));
    menu->addAction(shrink);

    menu->exec(ev->globalPos());
    delete menu;
}

void QtWebView::focusInEvent(QFocusEvent* event) {
    QWebView::focusInEvent(event);
    emit gotFocus();
}

}