summaryrefslogtreecommitdiffstats
path: root/src/webenginequick/api/qquickwebengineaction.cpp
blob: 006715c701f5b8915211cb703e8314039ea1a2c0 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only


#include "qquickwebengineaction_p.h"
#include "qquickwebengineaction_p_p.h"
#include "qquickwebengineview_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype WebEngineAction
    \instantiates QQuickWebEngineAction
    \inqmlmodule QtWebEngine
    \since QtWebEngine 1.8

    \brief An action that represents a \l WebEngineView::WebAction.

    A WebEngineAction is returned by the \l WebEngineView::action()
    method. It provides information about the action, such as
    whether it is \l enabled.

    The following code uses the \l WebEngineView::action() method to check if
    the copy action is enabled:

    \code
    var copyAction = webEngineView.action(WebEngineView.Copy);
    if (copyAction.enabled)
        console.log("Copy is enabled.");
    else
        console.log("Copy is disabled.");
    \endcode

    A \l ToolButton can be connected to a WebEngineAction as follows:

    \snippet qtwebengine_webengineaction.qml 0

    A context menu could be implemented like this:

    \snippet qtwebengine_webengineaction.qml 1
*/

QQuickWebEngineActionPrivate::QQuickWebEngineActionPrivate(const QVariant &data, const QString &text, const QString &iconName, bool enabled)
    : m_data(data)
    , m_text(text)
    , m_iconName(iconName)
    , m_enabled(enabled)
{
}

QQuickWebEngineActionPrivate::~QQuickWebEngineActionPrivate()
{
}

void QQuickWebEngineActionPrivate::setEnabled(bool enabled)
{
    Q_Q(QQuickWebEngineAction);
    if (m_enabled == enabled)
        return;
    m_enabled = enabled;
    emit q->enabledChanged();
}

QVariant QQuickWebEngineActionPrivate::data() const
{
    return m_data;
}

void QQuickWebEngineActionPrivate::trigger()
{
    Q_Q(QQuickWebEngineAction);
    if (QQuickWebEngineView *view = static_cast<QQuickWebEngineView*>(q->parent())) {
        view->triggerWebAction(static_cast<QQuickWebEngineView::WebAction>(data().toInt()));
    }
}

QQuickWebEngineAction::QQuickWebEngineAction(const QVariant &data, const QString &text, const QString &iconName, bool enabled, QObject *parent)
    : QObject(parent)
    , d_ptr(new QQuickWebEngineActionPrivate(data, text, iconName, enabled))
{
    d_ptr->q_ptr = this;
}

QQuickWebEngineAction::QQuickWebEngineAction(QObject *parent)
    : QObject(parent)
    , d_ptr(new QQuickWebEngineActionPrivate(-1, QStringLiteral(""), QStringLiteral(""), false))
{
    d_ptr->q_ptr = this;
}

QQuickWebEngineAction::~QQuickWebEngineAction()
{
}

/*!
    \qmlproperty int WebEngineAction::text

    This property holds a textual description of the action.
*/
QString QQuickWebEngineAction::text() const
{
    Q_D(const QQuickWebEngineAction);
    return d->m_text;
}

/*!
    \qmlproperty string WebEngineAction::iconName

    This property holds the name of the icon for the action. This name
    can be used to pick the icon from a theme.
*/
QString QQuickWebEngineAction::iconName() const
{
    Q_D(const QQuickWebEngineAction);
    return d->m_iconName;
}

/*!
    \qmlproperty bool WebEngineAction::enabled

    This property holds whether the action is enabled.
*/
bool QQuickWebEngineAction::isEnabled() const
{
    Q_D(const QQuickWebEngineAction);
    return d->m_enabled;
}

/*!
    \qmlmethod void WebEngineAction::trigger()

    Triggers the action.
*/
void QQuickWebEngineAction::trigger()
{
    Q_D(QQuickWebEngineAction);
    if (!isEnabled())
        return;

    d->trigger();
    emit triggered();
}

QT_END_NAMESPACE

#include "moc_qquickwebengineaction_p.cpp"