2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <QTextStream>
35 const int Ruler::MinorTickSubdivision = 4;
36 const int Ruler::ScaleUnits[3] = {1, 2, 5};
38 const QString Ruler::SIPrefixes[9] =
39 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
40 const int Ruler::FirstSIPrefixPower = -15;
42 Ruler::Ruler(View &parent) :
48 void Ruler::paintEvent(QPaintEvent *event)
51 p.setRenderHint(QPainter::Antialiasing);
53 const double MinSpacing = 80;
55 const double min_period = _view.scale() * MinSpacing;
57 const int order = (int)floorf(log10f(min_period));
58 const double order_decimal = pow(10, order);
60 unsigned int unit = 0;
61 double tick_period = 0.0f;
65 tick_period = order_decimal * ScaleUnits[unit++];
66 } while(tick_period < min_period && unit < countof(ScaleUnits));
68 const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
70 assert(prefix < countof(SIPrefixes));
72 const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
74 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
75 Qt::AlignLeft | Qt::AlignTop, "8").height();
77 // Draw the tick marks
80 const double minor_tick_period = tick_period / MinorTickSubdivision;
81 const double first_major_division =
82 floor(_view.offset() / tick_period);
83 const double first_minor_division =
84 ceil(_view.offset() / minor_tick_period);
85 const double t0 = first_major_division * tick_period;
87 int division = (int)round(first_minor_division -
88 first_major_division * MinorTickSubdivision);
91 const double t = t0 + division * minor_tick_period;
92 const double x = (t - _view.offset()) / _view.scale();
97 if(division % MinorTickSubdivision == 0)
102 ts << (t * multiplier) << SIPrefixes[prefix] << "s";
103 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
104 Qt::AlignTop | Qt::TextDontClip, s);
105 p.drawLine(QPointF(x, text_height),
106 QPointF(x, height()));
111 p.drawLine(QPointF(x, (text_height + height()) / 2),
112 QPointF(x, height()));