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