inputeventsmodel.cpp 5.5 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of Qt Creator.
7
**
8 9 10
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
11
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26
**
****************************************************************************/

#include "inputeventsmodel.h"
27
#include "qmlprofilermodelmanager.h"
28
#include "qmlprofilereventtypes.h"
29

30 31 32 33
#include <QKeyEvent>
#include <QMouseEvent>
#include <QMetaEnum>

34
namespace QmlProfiler {
35 36
namespace Internal {

37
InputEventsModel::InputEventsModel(QmlProfilerModelManager *manager, QObject *parent) :
38
    QmlProfilerTimelineModel(manager, Event, MaximumRangeType, ProfileInputEvents, parent),
39
    m_keyTypeId(-1), m_mouseTypeId(-1)
40 41 42
{
}

43
int InputEventsModel::typeId(int index) const
Ulf Hermann's avatar
Ulf Hermann committed
44
{
45
    return selectionId(index) == Mouse ? m_mouseTypeId : m_keyTypeId;
Ulf Hermann's avatar
Ulf Hermann committed
46 47 48 49 50 51 52 53 54 55 56
}

QColor InputEventsModel::color(int index) const
{
    return colorBySelectionId(index);
}

QVariantList InputEventsModel::labels() const
{
    QVariantList result;

57 58
    QVariantMap element;
    element.insert(QLatin1String("description"), QVariant(tr("Mouse Events")));
59
    element.insert(QLatin1String("id"), QVariant(Mouse));
60
    result << element;
Ulf Hermann's avatar
Ulf Hermann committed
61

62 63
    element.clear();
    element.insert(QLatin1String("description"), QVariant(tr("Keyboard Events")));
64
    element.insert(QLatin1String("id"), QVariant(Key));
65
    result << element;
Ulf Hermann's avatar
Ulf Hermann committed
66 67 68 69

    return result;
}

70 71 72 73 74
QMetaEnum InputEventsModel::metaEnum(const char *name)
{
    return staticQtMetaObject.enumerator(staticQtMetaObject.indexOfEnumerator(name));
}

Ulf Hermann's avatar
Ulf Hermann committed
75 76 77
QVariantMap InputEventsModel::details(int index) const
{
    QVariantMap result;
78
    result.insert(tr("Timestamp"), QmlProfilerDataModel::formatTime(startTime(index)));
79 80 81
    QString type;
    const InputEvent &event = m_data[index];
    switch (event.type) {
82
    case InputKeyPress:
83
        type = tr("Key Press");
84
    case InputKeyRelease:
85
        if (type.isEmpty())
86
            type = tr("Key Release");
87
        if (event.a != 0) {
88
            result.insert(tr("Key"), QLatin1String(metaEnum("Key").valueToKey(event.a)));
89 90
        }
        if (event.b != 0) {
91 92
            result.insert(tr("Modifiers"),
                          QLatin1String(metaEnum("KeyboardModifiers").valueToKeys(event.b)));
93 94
        }
        break;
95
    case InputMouseDoubleClick:
96
        type = tr("Double Click");
97
    case InputMousePress:
98
        if (type.isEmpty())
99
            type = tr("Mouse Press");
100
    case InputMouseRelease:
101
        if (type.isEmpty())
102
            type = tr("Mouse Release");
103 104
        result.insert(tr("Button"), QLatin1String(metaEnum("MouseButtons").valueToKey(event.a)));
        result.insert(tr("Result"), QLatin1String(metaEnum("MouseButtons").valueToKeys(event.b)));
105
        break;
106
    case InputMouseMove:
107 108 109
        type = tr("Mouse Move");
        result.insert(tr("X"), QString::number(event.a));
        result.insert(tr("Y"), QString::number(event.b));
110
        break;
111
    case InputMouseWheel:
112 113 114
        type = tr("Mouse Wheel");
        result.insert(tr("Angle X"), QString::number(event.a));
        result.insert(tr("Angle Y"), QString::number(event.b));
115
        break;
116
    case InputKeyUnknown:
117
        type = tr("Keyboard Event");
118
        break;
119
    case InputMouseUnknown:
120
        type = tr("Mouse Event");
121 122 123 124 125 126 127 128
        break;
    default:
        Q_UNREACHABLE();
        break;
    }

    result.insert(QLatin1String("displayName"), type);

Ulf Hermann's avatar
Ulf Hermann committed
129 130 131
    return result;
}

132
int InputEventsModel::expandedRow(int index) const
Ulf Hermann's avatar
Ulf Hermann committed
133
{
134
    return selectionId(index) == Mouse ? 1 : 2;
Ulf Hermann's avatar
Ulf Hermann committed
135 136
}

137 138 139 140 141 142
int InputEventsModel::collapsedRow(int index) const
{
    Q_UNUSED(index)
    return 1;
}

143
void InputEventsModel::loadEvent(const QmlEvent &event, const QmlEventType &type)
Ulf Hermann's avatar
Ulf Hermann committed
144
{
145
    m_data.insert(insert(event.timestamp(), 0, type.detailType()),
146 147 148
                  InputEvent(static_cast<InputEventType>(event.number<qint32>(0)),
                             event.number<qint32>(1), event.number<qint32>(2)));

149
    if (type.detailType() == Mouse) {
150 151 152 153
        if (m_mouseTypeId == -1)
            m_mouseTypeId = event.typeIndex();
    } else if (m_keyTypeId == -1) {
        m_keyTypeId = event.typeIndex();
Ulf Hermann's avatar
Ulf Hermann committed
154
    }
155 156 157 158
}

void InputEventsModel::finalize()
{
159 160
    setCollapsedRowCount(2);
    setExpandedRowCount(3);
Ulf Hermann's avatar
Ulf Hermann committed
161 162
}

163 164 165
void InputEventsModel::clear()
{
    m_keyTypeId = m_mouseTypeId = -1;
166
    m_data.clear();
167
    QmlProfilerTimelineModel::clear();
168 169
}

170
bool InputEventsModel::accepted(const QmlEventType &type) const
Ulf Hermann's avatar
Ulf Hermann committed
171
{
172
    return QmlProfilerTimelineModel::accepted(type) &&
173
            (type.detailType() == Mouse || type.detailType() == Key);
Ulf Hermann's avatar
Ulf Hermann committed
174 175
}

176
InputEventsModel::InputEvent::InputEvent(InputEventType type, int a, int b) :
177 178 179 180
    type(type), a(a), b(b)
{
}

181 182
} // namespace Internal
} // namespace QmlProfiler