summaryrefslogtreecommitdiffstats
blob: abdc17a85263d685a8b17eb1343de906f7f51d20 (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
90
91
92
93
94
95
96
97
98
99
100
/*
 * 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 <QKeySequence>
#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 (event->matches(QKeySequence::ZoomIn)) {
        event->accept();
        emit fontGrowRequested();
        return;
    }
    if (event->matches(QKeySequence::ZoomOut)) {
        event->accept();
        emit fontShrinkRequested();
        return;
    }
    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 (auto action : actions) {
        bool removeAction = true;
        for(auto& filteredAction : filteredActions) {
            if (action == pageAction(filteredAction)) {
                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()), QKeySequence(QKeySequence::ZoomIn));
    QAction* shrink = new QAction(tr("Decrease font size"), this);
    shrink->setShortcut(QKeySequence(QKeySequence::ZoomOut));
    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();
}

}