2 * This file is part of the sigrok 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
24 #include "../../extdef.h"
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)
52 const double MinSpacing = 80;
54 const double min_period = _view.scale() * MinSpacing;
56 const int order = (int)floorf(log10f(min_period));
57 const double order_decimal = pow(10, order);
60 double tick_period = 0.0f;
64 tick_period = order_decimal * ScaleUnits[unit++];
65 } while(tick_period < min_period && unit < countof(ScaleUnits));
67 const int prefix = (order - FirstSIPrefixPower) / 3;
69 assert(prefix < countof(SIPrefixes));
71 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
72 Qt::AlignLeft | Qt::AlignTop, "8").height();
74 // Draw the tick marks
77 const double minor_tick_period = tick_period / MinorTickSubdivision;
78 const double first_major_division =
79 floor(_view.offset() / tick_period);
80 const double first_minor_division =
81 ceil(_view.offset() / minor_tick_period);
82 const double t0 = first_major_division * tick_period;
84 int division = (int)round(first_minor_division -
85 first_major_division * MinorTickSubdivision);
88 const double t = t0 + division * minor_tick_period;
89 const double x = (t - _view.offset()) / _view.scale();
94 if(division % MinorTickSubdivision == 0)
99 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
100 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
101 Qt::AlignTop | Qt::TextDontClip, s);
102 p.drawLine(x, text_height, x, height());
107 p.drawLine(x, (text_height + height()) / 2,