]> git.cworth.org Git - apitrace/blob - gui/profiledialog.cpp
146e26a25d42bea544395089c3504a7fe56b9642
[apitrace] / gui / profiledialog.cpp
1 #include "profiledialog.h"
2 #include "profiletablemodel.h"
3 #include <QSortFilterProxyModel>
4
5 ProfileDialog::ProfileDialog(QWidget *parent)
6     : QDialog(parent),
7       m_profile(0)
8 {
9     setupUi(this);
10
11     connect(m_timeline, SIGNAL(jumpToCall(int)), SIGNAL(jumpToCall(int)));
12     connect(m_timeline, SIGNAL(selectionChanged(int64_t,int64_t)), SLOT(selectionChanged(int64_t,int64_t)));
13
14     connect(m_gpuGraph, SIGNAL(jumpToCall(int)), SIGNAL(jumpToCall(int)));
15     connect(m_cpuGraph, SIGNAL(jumpToCall(int)), SIGNAL(jumpToCall(int)));
16
17     connect(m_gpuGraph, SIGNAL(viewChanged(int,int)), m_cpuGraph, SLOT(changeView(int,int)));
18     connect(m_cpuGraph, SIGNAL(viewChanged(int,int)), m_gpuGraph, SLOT(changeView(int,int)));
19 }
20
21
22 ProfileDialog::~ProfileDialog()
23 {
24     delete m_profile;
25 }
26
27
28 void ProfileDialog::tableDoubleClicked(const QModelIndex& index)
29 {
30     ProfileTableModel* model = (ProfileTableModel*)m_table->model();
31     const trace::Profile::Call* call = model->getJumpCall(index);
32
33     if (call) {
34         emit jumpToCall(call->no);
35     }
36 }
37
38
39 void ProfileDialog::setProfile(trace::Profile* profile)
40 {
41     if (m_profile) {
42         delete m_profile;
43     }
44
45     m_profile = profile;
46     m_timeline->setProfile(m_profile);
47     m_gpuGraph->setProfile(m_profile, GraphGpu);
48     m_cpuGraph->setProfile(m_profile, GraphCpu);
49
50     ProfileTableModel* model = new ProfileTableModel(m_table);
51     model->setProfile(m_profile);
52
53     delete m_table->model();
54     m_table->setModel(model);
55     m_table->resizeColumnsToContents();
56     m_table->sortByColumn(2, Qt::DescendingOrder);
57 }
58
59
60 void ProfileDialog::selectionChanged(int64_t start, int64_t end)
61 {
62     ProfileTableModel* model = (ProfileTableModel*)m_table->model();
63     model->setTimeSelection(start, end);
64     m_table->reset();
65 }
66
67
68 void ProfileDialog::setVerticalScrollMax(int max)
69 {
70     if (max <= 0) {
71         m_verticalScrollBar->hide();
72     } else {
73         m_verticalScrollBar->show();
74         m_verticalScrollBar->setMinimum(0);
75         m_verticalScrollBar->setMaximum(max);
76     }
77 }
78
79
80 void ProfileDialog::setHorizontalScrollMax(int max)
81 {
82     if (max <= 0) {
83         m_horizontalScrollBar->hide();
84     } else {
85         m_horizontalScrollBar->show();
86         m_horizontalScrollBar->setMinimum(0);
87         m_horizontalScrollBar->setMaximum(max);
88     }
89 }
90
91
92 /**
93  * Convert a CPU / GPU time to a textual representation.
94  * This includes automatic unit selection.
95  */
96 QString getTimeString(int64_t time, int64_t unitTime)
97 {
98     QString text;
99     QString unit = " ns";
100     double unitScale = 1;
101
102     if (unitTime == 0) {
103         unitTime = time;
104     }
105
106     if (unitTime >= 60e9) {
107         int64_t mins = time / 60e9;
108         text += QString("%1 m ").arg(mins);
109
110         time -= mins * 60e9;
111         unit = " s";
112         unitScale = 1e9;
113     } else if (unitTime >= 1e9) {
114         unit = " s";
115         unitScale = 1e9;
116     } else if (unitTime >= 1e6) {
117         unit = " ms";
118         unitScale = 1e6;
119     } else if (unitTime >= 1e3) {
120         unit = QString::fromUtf8(" µs");
121         unitScale = 1e3;
122     }
123
124     /* 3 decimal places */
125     text += QString("%1").arg(time / unitScale, 0, 'f', 3);
126
127     /* Remove trailing 0 */
128     while(text.endsWith('0'))
129         text.truncate(text.length() - 1);
130
131     /* Remove trailing decimal point */
132     if (text.endsWith(QLocale::system().decimalPoint()))
133         text.truncate(text.length() - 1);
134
135     return text + unit;
136 }
137
138
139 #include "profiledialog.moc"