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
32 #include <QMouseEvent>
34 #include <QTextStream>
41 const int Ruler::MinorTickSubdivision = 4;
42 const int Ruler::ScaleUnits[3] = {1, 2, 5};
44 const QString Ruler::SIPrefixes[9] =
45 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
46 const int Ruler::FirstSIPrefixPower = -15;
48 const int Ruler::HoverArrowSize = 5;
50 Ruler::Ruler(View &parent) :
55 setMouseTracking(true);
57 connect(&_view, SIGNAL(hover_point_changed()),
58 this, SLOT(hover_point_changed()));
61 void Ruler::paintEvent(QPaintEvent *event)
64 p.setRenderHint(QPainter::Antialiasing);
66 const double MinSpacing = 80;
68 const double min_period = _view.scale() * MinSpacing;
70 const int order = (int)floorf(log10f(min_period));
71 const double order_decimal = pow(10, order);
73 unsigned int unit = 0;
74 double tick_period = 0.0f;
78 tick_period = order_decimal * ScaleUnits[unit++];
79 } while (tick_period < min_period && unit < countof(ScaleUnits));
81 const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
82 assert(prefix < countof(SIPrefixes));
84 const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
86 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
87 Qt::AlignLeft | Qt::AlignTop, "8").height();
89 // Draw the tick marks
92 const double minor_tick_period = tick_period / MinorTickSubdivision;
93 const double first_major_division =
94 floor(_view.offset() / tick_period);
95 const double first_minor_division =
96 ceil(_view.offset() / minor_tick_period);
97 const double t0 = first_major_division * tick_period;
99 int division = (int)round(first_minor_division -
100 first_major_division * MinorTickSubdivision);
103 const double t = t0 + division * minor_tick_period;
104 const double x = (t - _view.offset()) / _view.scale();
109 if (division % MinorTickSubdivision == 0)
114 ts << (t * multiplier) << SIPrefixes[prefix] << "s";
115 p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
116 Qt::AlignTop | Qt::TextDontClip, s);
117 p.drawLine(QPointF(x, text_height),
118 QPointF(x, height()));
123 p.drawLine(QPointF(x, (text_height + height()) / 2),
124 QPointF(x, height()));
133 // Draw the hover mark
139 void Ruler::mouseMoveEvent(QMouseEvent *e)
141 if (!_grabbed_marker)
144 _grabbed_marker->set_time(_view.offset() +
145 ((double)e->x() + 0.5) * _view.scale());
148 void Ruler::mousePressEvent(QMouseEvent *e)
150 if (e->buttons() & Qt::LeftButton) {
151 _grabbed_marker = NULL;
153 if (_view.cursors_shown()) {
154 std::pair<Cursor, Cursor> &cursors =
156 if (cursors.first.get_label_rect(
157 rect()).contains(e->pos()))
158 _grabbed_marker = &cursors.first;
159 else if (cursors.second.get_label_rect(
160 rect()).contains(e->pos()))
161 _grabbed_marker = &cursors.second;
166 void Ruler::mouseReleaseEvent(QMouseEvent *)
168 _grabbed_marker = NULL;
171 void Ruler::draw_cursors(QPainter &p)
173 if (!_view.cursors_shown())
176 const QRect r = rect();
177 pair<Cursor, Cursor> &cursors = _view.cursors();
178 cursors.first.paint_label(p, r);
179 cursors.second.paint_label(p, r);
182 void Ruler::draw_hover_mark(QPainter &p)
184 const int x = _view.hover_point().x();
186 if (x == -1 || _grabbed_marker)
189 p.setPen(QPen(Qt::NoPen));
190 p.setBrush(QBrush(QColor(Qt::black)));
192 const int b = height() - 1;
193 const QPointF points[] = {
195 QPointF(x - HoverArrowSize, b - HoverArrowSize),
196 QPointF(x + HoverArrowSize, b - HoverArrowSize)
198 p.drawPolygon(points, countof(points));
201 void Ruler::hover_point_changed()