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 <QMouseEvent>
32 #include <QTextStream>
39 const int Ruler::MinorTickSubdivision = 4;
40 const int Ruler::ScaleUnits[3] = {1, 2, 5};
42 const QString Ruler::SIPrefixes[9] =
43 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
44 const int Ruler::FirstSIPrefixPower = -15;
46 const int Ruler::HoverArrowSize = 5;
48 Ruler::Ruler(View &parent) :
52 setMouseTracking(true);
54 connect(&_view, SIGNAL(hover_point_changed()),
55 this, SLOT(hover_point_changed()));
58 void Ruler::paintEvent(QPaintEvent *event)
61 p.setRenderHint(QPainter::Antialiasing);
63 const double MinSpacing = 80;
65 const double min_period = _view.scale() * MinSpacing;
67 const int order = (int)floorf(log10f(min_period));
68 const double order_decimal = pow(10, order);
70 unsigned int unit = 0;
71 double tick_period = 0.0f;
75 tick_period = order_decimal * ScaleUnits[unit++];
76 } while(tick_period < min_period && unit < countof(ScaleUnits));
78 const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
80 assert(prefix < countof(SIPrefixes));
82 const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
84 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
85 Qt::AlignLeft | Qt::AlignTop, "8").height();
87 // Draw the tick marks
90 const double minor_tick_period = tick_period / MinorTickSubdivision;
91 const double first_major_division =
92 floor(_view.offset() / tick_period);
93 const double first_minor_division =
94 ceil(_view.offset() / minor_tick_period);
95 const double t0 = first_major_division * tick_period;
97 int division = (int)round(first_minor_division -
98 first_major_division * MinorTickSubdivision);
101 const double t = t0 + division * minor_tick_period;
102 const double x = (t - _view.offset()) / _view.scale();
107 if(division % MinorTickSubdivision == 0)
112 ts << (t * multiplier) << SIPrefixes[prefix] << "s";
113 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
114 Qt::AlignTop | Qt::TextDontClip, s);
115 p.drawLine(QPointF(x, text_height),
116 QPointF(x, height()));
121 p.drawLine(QPointF(x, (text_height + height()) / 2),
122 QPointF(x, height()));
131 // Draw the hover mark
137 void Ruler::draw_cursors(QPainter &p)
139 if(!_view.cursors_shown())
142 const QRect r = rect();
143 pair<Cursor, Cursor> &cursors = _view.cursors();
144 cursors.first.paint_label(p, r);
145 cursors.second.paint_label(p, r);
148 void Ruler::draw_hover_mark(QPainter &p)
150 const int x = _view.hover_point().x();
154 p.setPen(QPen(Qt::NoPen));
155 p.setBrush(QBrush(QColor(Qt::black)));
157 const int b = height() - 1;
158 const QPointF points[] = {
160 QPointF(x - HoverArrowSize, b - HoverArrowSize),
161 QPointF(x + HoverArrowSize, b - HoverArrowSize)
163 p.drawPolygon(points, countof(points));
166 void Ruler::hover_point_changed()