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 #include <QMouseEvent>
35 #include <QTextStream>
37 using namespace boost;
43 const int Ruler::MinorTickSubdivision = 4;
44 const int Ruler::ScaleUnits[3] = {1, 2, 5};
46 const QString Ruler::SIPrefixes[9] =
47 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
48 const int Ruler::FirstSIPrefixPower = -15;
50 const int Ruler::HoverArrowSize = 5;
52 Ruler::Ruler(View &parent) :
55 setMouseTracking(true);
57 connect(&_view, SIGNAL(hover_point_changed()),
58 this, SLOT(hover_point_changed()));
61 void Ruler::clear_selection()
63 CursorPair &cursors = _view.cursors();
64 cursors.first()->select(false);
65 cursors.second()->select(false);
69 QString Ruler::format_time(double t, unsigned int prefix,
70 unsigned int precision)
72 const double multiplier = pow(10.0,
73 - prefix * 3 - FirstSIPrefixPower);
77 ts.setRealNumberPrecision(precision);
78 ts << fixed << forcesign << (t * multiplier) <<
79 SIPrefixes[prefix] << "s";
83 void Ruler::paintEvent(QPaintEvent*)
87 const double SpacingIncrement = 32.0f;
88 const double MinValueSpacing = 32.0f;
89 const int ValueMargin = 3;
92 p.setRenderHint(QPainter::Antialiasing);
94 double min_width = SpacingIncrement, typical_width;
98 // Find tick spacing, and number formatting that does not cause
102 const double min_period = _view.scale() * min_width;
104 const int order = (int)floorf(log10f(min_period));
105 const double order_decimal = pow(10, order);
107 unsigned int unit = 0;
111 tick_period = order_decimal * ScaleUnits[unit++];
112 } while (tick_period < min_period && unit < countof(ScaleUnits));
114 prefix = (order - FirstSIPrefixPower) / 3;
115 assert(prefix < countof(SIPrefixes));
118 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
119 AlignLeft | AlignTop, format_time(_view.offset(),
120 prefix)).width() + MinValueSpacing;
122 min_width += SpacingIncrement;
124 } while(typical_width > tick_period / _view.scale());
126 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
127 AlignLeft | AlignTop, "8").height();
129 // Draw the tick marks
130 p.setPen(palette().color(foregroundRole()));
132 const double minor_tick_period = tick_period / MinorTickSubdivision;
133 const double first_major_division =
134 floor(_view.offset() / tick_period);
135 const double first_minor_division =
136 ceil(_view.offset() / minor_tick_period);
137 const double t0 = first_major_division * tick_period;
139 int division = (int)round(first_minor_division -
140 first_major_division * MinorTickSubdivision) - 1;
142 const int major_tick_y1 = text_height + ValueMargin * 2;
143 const int tick_y2 = height();
144 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
149 const double t = t0 + division * minor_tick_period;
150 x = (t - _view.offset()) / _view.scale();
152 if (division % MinorTickSubdivision == 0)
155 p.drawText(x, ValueMargin, 0, text_height,
156 AlignCenter | AlignTop | TextDontClip,
157 format_time(t, prefix));
158 p.drawLine(QPointF(x, major_tick_y1),
159 QPointF(x, tick_y2));
164 p.drawLine(QPointF(x, minor_tick_y1),
165 QPointF(x, tick_y2));
170 } while (x < width());
173 if (_view.cursors_shown())
174 _view.cursors().draw_markers(p, rect(), prefix);
176 // Draw the hover mark
182 void Ruler::mouseMoveEvent(QMouseEvent *e)
184 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
185 m->set_time(_view.offset() +
186 ((double)e->x() + 0.5) * _view.scale());
189 void Ruler::mousePressEvent(QMouseEvent *e)
191 if (e->buttons() & Qt::LeftButton) {
192 _grabbed_marker.reset();
196 if (_view.cursors_shown()) {
197 CursorPair &cursors = _view.cursors();
198 if (cursors.first()->get_label_rect(
199 rect()).contains(e->pos()))
200 _grabbed_marker = cursors.first();
201 else if (cursors.second()->get_label_rect(
202 rect()).contains(e->pos()))
203 _grabbed_marker = cursors.second();
206 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
213 void Ruler::mouseReleaseEvent(QMouseEvent *)
215 _grabbed_marker.reset();
218 void Ruler::draw_hover_mark(QPainter &p)
220 const int x = _view.hover_point().x();
222 if (x == -1 || !_grabbed_marker.expired())
225 p.setPen(QPen(Qt::NoPen));
226 p.setBrush(QBrush(palette().color(foregroundRole())));
228 const int b = height() - 1;
229 const QPointF points[] = {
231 QPointF(x - HoverArrowSize, b - HoverArrowSize),
232 QPointF(x + HoverArrowSize, b - HoverArrowSize)
234 p.drawPolygon(points, countof(points));
237 void Ruler::hover_point_changed()