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>
42 const int Ruler::MinorTickSubdivision = 4;
43 const int Ruler::ScaleUnits[3] = {1, 2, 5};
45 const QString Ruler::SIPrefixes[9] =
46 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
47 const int Ruler::FirstSIPrefixPower = -15;
49 const int Ruler::HoverArrowSize = 5;
51 Ruler::Ruler(View &parent) :
56 setMouseTracking(true);
58 connect(&_view, SIGNAL(hover_point_changed()),
59 this, SLOT(hover_point_changed()));
62 void Ruler::paintEvent(QPaintEvent*)
66 const double SpacingIncrement = 32.0f;
67 const double MinValueSpacing = 32.0f;
68 const int ValueMargin = 3;
71 p.setRenderHint(QPainter::Antialiasing);
73 double min_width = SpacingIncrement, typical_width;
78 // Find tick spacing, and number formatting that does not cause
82 const double min_period = _view.scale() * min_width;
84 const int order = (int)floorf(log10f(min_period));
85 const double order_decimal = pow(10, order);
87 unsigned int unit = 0;
91 tick_period = order_decimal * ScaleUnits[unit++];
92 } while (tick_period < min_period && unit < countof(ScaleUnits));
94 prefix = (order - FirstSIPrefixPower) / 3;
95 assert(prefix < countof(SIPrefixes));
97 multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
99 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
100 AlignLeft | AlignTop, format_time(_view.offset(),
101 multiplier, prefix)).width() + MinValueSpacing;
103 min_width += SpacingIncrement;
105 } while(typical_width > tick_period / _view.scale());
107 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
108 AlignLeft | AlignTop, "8").height();
110 // Draw the tick marks
111 p.setPen(palette().color(foregroundRole()));
113 const double minor_tick_period = tick_period / MinorTickSubdivision;
114 const double first_major_division =
115 floor(_view.offset() / tick_period);
116 const double first_minor_division =
117 ceil(_view.offset() / minor_tick_period);
118 const double t0 = first_major_division * tick_period;
120 int division = (int)round(first_minor_division -
121 first_major_division * MinorTickSubdivision);
123 const int major_tick_y1 = text_height + ValueMargin * 2;
124 const int tick_y2 = height();
125 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
129 const double t = t0 + division * minor_tick_period;
130 const double x = (t - _view.offset()) / _view.scale();
135 if (division % MinorTickSubdivision == 0)
138 p.drawText(x, ValueMargin, 0, text_height,
139 AlignCenter | AlignTop | TextDontClip,
140 format_time(t, multiplier, prefix));
141 p.drawLine(QPointF(x, major_tick_y1),
142 QPointF(x, tick_y2));
147 p.drawLine(QPointF(x, minor_tick_y1),
148 QPointF(x, tick_y2));
157 // Draw the hover mark
163 void Ruler::mouseMoveEvent(QMouseEvent *e)
165 if (!_grabbed_marker)
168 _grabbed_marker->set_time(_view.offset() +
169 ((double)e->x() + 0.5) * _view.scale());
172 void Ruler::mousePressEvent(QMouseEvent *e)
174 if (e->buttons() & Qt::LeftButton) {
175 _grabbed_marker = NULL;
177 if (_view.cursors_shown()) {
178 std::pair<Cursor, Cursor> &cursors =
180 if (cursors.first.get_label_rect(
181 rect()).contains(e->pos()))
182 _grabbed_marker = &cursors.first;
183 else if (cursors.second.get_label_rect(
184 rect()).contains(e->pos()))
185 _grabbed_marker = &cursors.second;
190 void Ruler::mouseReleaseEvent(QMouseEvent *)
192 _grabbed_marker = NULL;
195 QString Ruler::format_time(double t, double multiplier,
200 ts.setRealNumberPrecision(0);
201 ts << fixed << forcesign << (t * multiplier) <<
202 SIPrefixes[prefix] << "s";
206 void Ruler::draw_cursors(QPainter &p)
208 if (!_view.cursors_shown())
211 const QRect r = rect();
212 pair<Cursor, Cursor> &cursors = _view.cursors();
213 cursors.first.paint_label(p, r);
214 cursors.second.paint_label(p, r);
217 void Ruler::draw_hover_mark(QPainter &p)
219 const int x = _view.hover_point().x();
221 if (x == -1 || _grabbed_marker)
224 p.setPen(QPen(Qt::NoPen));
225 p.setBrush(QBrush(QColor(Qt::black)));
227 const int b = height() - 1;
228 const QPointF points[] = {
230 QPointF(x - HoverArrowSize, b - HoverArrowSize),
231 QPointF(x + HoverArrowSize, b - HoverArrowSize)
233 p.drawPolygon(points, countof(points));
236 void Ruler::hover_point_changed()