]> git.cworth.org Git - apitrace/blob - gui/graphing/timeaxiswidget.cpp
d3dretrace: Use DirectXTex for d3d10 state too.
[apitrace] / gui / graphing / timeaxiswidget.cpp
1 #include "timeaxiswidget.h"
2 #include "profiling.h"
3
4 #include <qmath.h>
5 #include <QPainter>
6
7 TimeAxisWidget::TimeAxisWidget(QWidget* parent) :
8     GraphAxisWidget(parent)
9 {
10 }
11
12 void TimeAxisWidget::paintEvent(QPaintEvent *)
13 {
14     /* TODO: Support selections? */
15     /* TODO: Vertical scrolling? */
16
17     QPainter painter(this);
18     painter.setPen(Qt::black);
19     painter.setBrush(Qt::lightGray);
20     painter.drawRect(0, 0, width() - 1, height() - 1);
21
22     if (m_orientation == GraphAxisWidget::Vertical) {
23         int axisHeight = height() - 1;
24         int fontHeight = painter.fontMetrics().height();
25         int ticks     = axisHeight / (fontHeight * 2);
26
27         double range  = m_valueMax - m_valueMin;
28         double step   = range / (double)ticks;
29         double step10 = qPow(10.0, qFloor(qLn(step) / qLn(10.0)));
30         step = qFloor((step / step10) * 2) * (step10 / 2);
31
32         if (step <= 0) {
33             return;
34         }
35
36         painter.resetTransform();
37
38         for (double tick = 0; tick <= range; tick += step) {
39             int y = axisHeight - ((tick / range) * axisHeight);
40
41             painter.drawLine(width() - 8, y, width(), y);
42
43             painter.drawText(0,
44                              qBound(0, y - fontHeight / 2, axisHeight - fontHeight),
45                              width() - 10,
46                              fontHeight,
47                              Qt::AlignRight | Qt::AlignVCenter,
48                              Profiling::getTimeString(tick, m_valueMax));
49         }
50     } else {
51         int axisWidth = width() - 1;
52         int fontWidth = painter.fontMetrics().width("0.000 ns");
53         int fontHeight= painter.fontMetrics().height();
54         int ticks     = axisWidth / (fontWidth * 2);
55
56         double range  = m_valueMax - m_valueMin;
57         double step   = range / (double)ticks;
58         double step10 = qPow(10.0, qFloor(qLn(step) / qLn(10.0)));
59         step = qFloor((step / step10) * 2) * (step10 / 2);
60
61         if (step <= 0) {
62             return;
63         }
64
65         painter.resetTransform();
66
67         for (double tick = 0; tick <= range; tick += step) {
68             int x = (tick / range) * axisWidth;
69
70             painter.drawLine(x, 0, x, 8);
71
72             painter.drawText(qBound(0, x - fontWidth / 2, axisWidth - fontWidth),
73                              8,
74                              fontWidth,
75                              fontHeight,
76                              Qt::AlignHCenter | Qt::AlignTop,
77                              Profiling::getTimeString(tick, m_valueMax));
78         }
79     }
80 }