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
33 const int Ruler::RulerHeight = 30;
34 const int Ruler::MinorTickSubdivision = 4;
35 const int Ruler::ScaleUnits[3] = {1, 2, 5};
37 const int Ruler::HoverArrowSize = 5;
39 Ruler::Ruler(View &parent) :
42 setMouseTracking(true);
44 connect(&_view, SIGNAL(hover_point_changed()),
45 this, SLOT(hover_point_changed()));
48 QSize Ruler::sizeHint() const
50 return QSize(0, RulerHeight);
53 void Ruler::paintEvent(QPaintEvent*)
55 const int ValueMargin = 3;
58 p.setRenderHint(QPainter::Antialiasing);
60 std::pair<double, unsigned int> spacing =
61 calculate_tick_spacing(p, _view.scale(), _view.offset());
63 double tick_period = spacing.first;
64 unsigned int prefix = spacing.second;
66 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
67 AlignLeft | AlignTop, "8").height();
69 // Draw the tick marks
70 p.setPen(palette().color(foregroundRole()));
72 const double minor_tick_period = tick_period / MinorTickSubdivision;
73 const double first_major_division =
74 floor(_view.offset() / tick_period);
75 const double first_minor_division =
76 ceil(_view.offset() / minor_tick_period);
77 const double t0 = first_major_division * tick_period;
79 int division = (int)round(first_minor_division -
80 first_major_division * MinorTickSubdivision) - 1;
82 const int major_tick_y1 = text_height + ValueMargin * 2;
83 const int tick_y2 = height();
84 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
89 const double t = t0 + division * minor_tick_period;
90 x = (t - _view.offset()) / _view.scale();
92 if (division % MinorTickSubdivision == 0)
95 p.drawText(x, ValueMargin, 0, text_height,
96 AlignCenter | AlignTop | TextDontClip,
97 pv::util::format_time(t, prefix));
98 p.drawLine(QPointF(x, major_tick_y1),
104 p.drawLine(QPointF(x, minor_tick_y1),
105 QPointF(x, tick_y2));
110 } while (x < width());
112 // Draw the hover mark
116 void Ruler::draw_hover_mark(QPainter &p)
118 const int x = _view.hover_point().x();
123 p.setPen(QPen(Qt::NoPen));
124 p.setBrush(QBrush(palette().color(foregroundRole())));
126 const int b = height() - 1;
127 const QPointF points[] = {
129 QPointF(x - HoverArrowSize, b - HoverArrowSize),
130 QPointF(x + HoverArrowSize, b - HoverArrowSize)
132 p.drawPolygon(points, countof(points));
135 void Ruler::hover_point_changed()
140 std::pair<double, unsigned int> Ruler::calculate_tick_spacing(
141 QPainter& p, double scale, double offset)
143 const double SpacingIncrement = 32.0f;
144 const double MinValueSpacing = 32.0f;
146 double min_width = SpacingIncrement, typical_width;
152 const double min_period = scale * min_width;
154 const int order = (int)floorf(log10f(min_period));
155 const double order_decimal = pow(10.0, order);
157 unsigned int unit = 0;
160 tick_period = order_decimal * ScaleUnits[unit++];
161 } while (tick_period < min_period && unit < countof(ScaleUnits));
163 prefix = (order - pv::util::FirstSIPrefixPower) / 3;
165 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
166 AlignLeft | AlignTop, pv::util::format_time(offset,
167 prefix)).width() + MinValueSpacing;
169 min_width += SpacingIncrement;
171 } while(typical_width > tick_period / scale);
173 return std::make_pair(tick_period, prefix);