aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/perfprofiler/perfprofilerstatisticsview.cpp
blob: 4b43557d7527061bdf576a6dde534f874f6a635b (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "perfprofilerstatisticsmodel.h"
#include "perfprofilerstatisticsview.h"
#include "perfprofilertool.h"

#include <coreplugin/minisplitter.h>

#include <QApplication>
#include <QClipboard>
#include <QStyledItemDelegate>
#include <QVBoxLayout>

namespace PerfProfiler {
namespace Internal {

class StatisticsView : public Utils::BaseTreeView
{
    Q_OBJECT
public:
    StatisticsView(QWidget *parent);
    void clear();
    QString rowToString(int row) const;

    void copyTableToClipboard() const;
    void copySelectionToClipboard() const;
};

class HexNumberDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    HexNumberDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    QString displayText(const QVariant &value, const QLocale &locale) const final
    {
        Q_UNUSED(locale)
        return QString::fromLatin1("0x%1").arg(value.toULongLong(), 16, 16, QLatin1Char('0'));
    }
};

PerfProfilerStatisticsView::PerfProfilerStatisticsView(QWidget *parent, PerfProfilerTool *tool) :
    QWidget(parent)
{
    setObjectName(QLatin1String("PerfProfilerStatisticsView"));

    m_mainView = new StatisticsView(this);
    m_parentsView = new StatisticsView(this);
    m_childrenView = new StatisticsView(this);

    // widget arrangement
    QVBoxLayout *groupLayout = new QVBoxLayout;
    groupLayout->setContentsMargins(0,0,0,0);
    groupLayout->setSpacing(0);

    Core::MiniSplitter *splitterVertical = new Core::MiniSplitter;
    splitterVertical->addWidget(m_mainView);
    Core::MiniSplitter *splitterHorizontal = new Core::MiniSplitter;
    splitterHorizontal->addWidget(m_parentsView);
    splitterHorizontal->addWidget(m_childrenView);
    splitterHorizontal->setOrientation(Qt::Horizontal);
    splitterVertical->addWidget(splitterHorizontal);
    splitterVertical->setOrientation(Qt::Vertical);
    splitterVertical->setStretchFactor(0,5);
    splitterVertical->setStretchFactor(1,2);
    groupLayout->addWidget(splitterVertical);
    setLayout(groupLayout);

    PerfProfilerTraceManager *manager = tool->traceManager();
    PerfProfilerStatisticsMainModel *mainModel = new PerfProfilerStatisticsMainModel(manager);

    PerfProfilerStatisticsRelativesModel *children = mainModel->children();
    PerfProfilerStatisticsRelativesModel *parents = mainModel->parents();

    m_mainView->setModel(mainModel);
    m_childrenView->setModel(children);
    m_parentsView->setModel(parents);

    auto propagateSelection = [this, manager, children, parents](int locationId){
        children->selectByTypeId(locationId);
        parents->selectByTypeId(locationId);
        const PerfEventType::Location &location = manager->location(locationId);
        const QByteArray &file = manager->string(location.file);
        if (!file.isEmpty())
            emit gotoSourceLocation(QString::fromUtf8(file), location.line, location.column);
        emit typeSelected(locationId);
    };

    connect(m_mainView, &QAbstractItemView::activated, this,
            [propagateSelection, mainModel](QModelIndex index) {
        propagateSelection(mainModel->typeId(index.row()));
    });

    connect(m_childrenView, &QAbstractItemView::activated, this,
            [this, propagateSelection, mainModel, children](QModelIndex index) {
        int typeId = children->typeId(index.row());
        m_mainView->setCurrentIndex(mainModel->index(mainModel->rowForTypeId(typeId), 0));
        propagateSelection(typeId);
    });

    connect(m_parentsView, &QAbstractItemView::activated, this,
            [this, propagateSelection, mainModel, parents](QModelIndex index) {
        int typeId = parents->typeId(index.row());
        m_mainView->setCurrentIndex(mainModel->index(mainModel->rowForTypeId(typeId), 0));
        propagateSelection(typeId);
    });
}

bool PerfProfilerStatisticsView::focusedTableHasValidSelection() const
{
    if (m_mainView->hasFocus())
        return m_mainView->currentIndex().isValid();
    else if (m_childrenView->hasFocus())
        return m_childrenView->currentIndex().isValid();
    else if (m_parentsView->hasFocus())
        return m_parentsView->currentIndex().isValid();
    return false;
}

void PerfProfilerStatisticsView::selectByTypeId(int typeId)
{
    const PerfProfilerStatisticsMainModel *mainModel =
            static_cast<const PerfProfilerStatisticsMainModel *>(m_mainView->model());
    if (mainModel) {
        if (m_mainView->currentIndex().isValid() &&
                mainModel->typeId(m_mainView->currentIndex().row()) == typeId)
            return;

        m_mainView->setCurrentIndex(mainModel->index(mainModel->rowForTypeId(typeId), 0));
        mainModel->children()->selectByTypeId(typeId);
        mainModel->parents()->selectByTypeId(typeId);
    }
}

void PerfProfilerStatisticsView::copyFocusedTableToClipboard() const
{
    if (m_mainView->hasFocus())
        m_mainView->copyTableToClipboard();
    else if (m_childrenView->hasFocus())
        m_childrenView->copyTableToClipboard();
    else if (m_parentsView->hasFocus())
        m_parentsView->copyTableToClipboard();
}

void PerfProfilerStatisticsView::copyFocusedSelectionToClipboard() const
{
    if (m_mainView->hasFocus())
        m_mainView->copySelectionToClipboard();
    else if (m_childrenView->hasFocus())
        m_childrenView->copySelectionToClipboard();
    else if (m_parentsView->hasFocus())
        m_parentsView->copySelectionToClipboard();
}

StatisticsView::StatisticsView(QWidget *parent) :
    Utils::BaseTreeView(parent)
{
    setObjectName(QLatin1String("StatisticsView"));
    setSortingEnabled(true);
    setAlternatingRowColors(true);
    setItemDelegateForColumn(0, new HexNumberDelegate(this));
    setSelectionMode(QAbstractItemView::SingleSelection);
}

void StatisticsView::clear()
{
    model()->removeRows(0, model()->rowCount());
}

QString StatisticsView::rowToString(int row) const
{
    QString str;
    const QAbstractItemModel *m = model();
    str += QString::fromLatin1("0x%1").arg(m->data(m->index(row, 0)).toULongLong(), 16, 16,
                                             QLatin1Char('0'));
    for (int column = 1; column < m->columnCount(); ++column)
        str += QLatin1Char('\t') + m->data(m->index(row, column)).toString();
    return str + QLatin1Char('\n');
}

static void sendToClipboard(const QString &str)
{
    QClipboard *clipboard = QApplication::clipboard();
    if (clipboard->supportsSelection())
        clipboard->setText(str, QClipboard::Selection);
    clipboard->setText(str, QClipboard::Clipboard);
}

void StatisticsView::copyTableToClipboard() const
{
    const QAbstractItemModel *m = model();
    QString str;
    // headers
    int columnCount = m->columnCount();
    for (int column = 0; column < columnCount; ++column) {
        str += m->headerData(column, Qt::Horizontal).toString();
        if (column < columnCount - 1)
            str += QLatin1Char('\t');
        else
            str += QLatin1Char('\n');
    }
    // data
    int rowCount = m->rowCount();
    for (int row = 0; row < rowCount; ++row)
        str += rowToString(row);

    sendToClipboard(str);
}

void StatisticsView::copySelectionToClipboard() const
{
    if (currentIndex().isValid())
        sendToClipboard(rowToString(currentIndex().row()));
}

} // namespace Internal
} // namespace PerfProfiler

#include "perfprofilerstatisticsview.moc"