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
24 #include "pv/util.hpp"
33 const int Ruler::RulerHeight = 30;
34 const int Ruler::MinorTickSubdivision = 4;
36 const int Ruler::HoverArrowSize = 5;
38 Ruler::Ruler(View &parent) :
41 setMouseTracking(true);
43 connect(&view_, SIGNAL(hover_point_changed()),
44 this, SLOT(hover_point_changed()));
47 QSize Ruler::sizeHint() const
49 return QSize(0, RulerHeight);
52 void Ruler::paintEvent(QPaintEvent*)
54 const int ValueMargin = 3;
57 p.setRenderHint(QPainter::Antialiasing);
59 const double tick_period = view_.tick_period();
60 const unsigned int prefix = view_.tick_prefix();
62 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
63 AlignLeft | AlignTop, "8").height();
65 // Draw the tick marks
66 p.setPen(palette().color(foregroundRole()));
68 const double minor_tick_period = tick_period / MinorTickSubdivision;
69 const double first_major_division =
70 floor(view_.offset() / tick_period);
71 const double first_minor_division =
72 ceil(view_.offset() / minor_tick_period);
73 const double t0 = first_major_division * tick_period;
75 int division = (int)round(first_minor_division -
76 first_major_division * MinorTickSubdivision) - 1;
78 const int major_tick_y1 = text_height + ValueMargin * 2;
79 const int tick_y2 = height();
80 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
85 const double t = t0 + division * minor_tick_period;
86 x = (t - view_.offset()) / view_.scale();
88 if (division % MinorTickSubdivision == 0)
91 p.drawText(x, ValueMargin, 0, text_height,
92 AlignCenter | AlignTop | TextDontClip,
93 pv::util::format_time(t, prefix));
94 p.drawLine(QPointF(x, major_tick_y1),
100 p.drawLine(QPointF(x, minor_tick_y1),
101 QPointF(x, tick_y2));
106 } while (x < width());
108 // Draw the hover mark
112 void Ruler::draw_hover_mark(QPainter &p)
114 const int x = view_.hover_point().x();
119 p.setPen(QPen(Qt::NoPen));
120 p.setBrush(QBrush(palette().color(foregroundRole())));
122 const int b = height() - 1;
123 const QPointF points[] = {
125 QPointF(x - HoverArrowSize, b - HoverArrowSize),
126 QPointF(x + HoverArrowSize, b - HoverArrowSize)
128 p.drawPolygon(points, countof(points));
131 void Ruler::hover_point_changed()