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*)
67 p.setRenderHint(QPainter::Antialiasing);
69 const double MinSpacing = 80;
71 const double min_period = _view.scale() * MinSpacing;
73 const int order = (int)floorf(log10f(min_period));
74 const double order_decimal = pow(10, order);
76 unsigned int unit = 0;
77 double tick_period = 0.0f;
81 tick_period = order_decimal * ScaleUnits[unit++];
82 } while (tick_period < min_period && unit < countof(ScaleUnits));
84 const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
85 assert(prefix < countof(SIPrefixes));
87 const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
89 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
90 AlignLeft | AlignTop, "8").height();
92 // Draw the tick marks
93 p.setPen(palette().color(foregroundRole()));
95 const double minor_tick_period = tick_period / MinorTickSubdivision;
96 const double first_major_division =
97 floor(_view.offset() / tick_period);
98 const double first_minor_division =
99 ceil(_view.offset() / minor_tick_period);
100 const double t0 = first_major_division * tick_period;
102 int division = (int)round(first_minor_division -
103 first_major_division * MinorTickSubdivision);
106 const double t = t0 + division * minor_tick_period;
107 const double x = (t - _view.offset()) / _view.scale();
112 if (division % MinorTickSubdivision == 0)
117 ts << (t * multiplier) << SIPrefixes[prefix] << "s";
118 p.drawText(x, 0, 0, text_height, AlignCenter |
119 AlignTop | TextDontClip, s);
120 p.drawLine(QPointF(x, text_height),
121 QPointF(x, height()));
126 p.drawLine(QPointF(x, (text_height + height()) / 2),
127 QPointF(x, height()));
136 // Draw the hover mark
142 void Ruler::mouseMoveEvent(QMouseEvent *e)
144 if (!_grabbed_marker)
147 _grabbed_marker->set_time(_view.offset() +
148 ((double)e->x() + 0.5) * _view.scale());
151 void Ruler::mousePressEvent(QMouseEvent *e)
153 if (e->buttons() & Qt::LeftButton) {
154 _grabbed_marker = NULL;
156 if (_view.cursors_shown()) {
157 std::pair<Cursor, Cursor> &cursors =
159 if (cursors.first.get_label_rect(
160 rect()).contains(e->pos()))
161 _grabbed_marker = &cursors.first;
162 else if (cursors.second.get_label_rect(
163 rect()).contains(e->pos()))
164 _grabbed_marker = &cursors.second;
169 void Ruler::mouseReleaseEvent(QMouseEvent *)
171 _grabbed_marker = NULL;
174 void Ruler::draw_cursors(QPainter &p)
176 if (!_view.cursors_shown())
179 const QRect r = rect();
180 pair<Cursor, Cursor> &cursors = _view.cursors();
181 cursors.first.paint_label(p, r);
182 cursors.second.paint_label(p, r);
185 void Ruler::draw_hover_mark(QPainter &p)
187 const int x = _view.hover_point().x();
189 if (x == -1 || _grabbed_marker)
192 p.setPen(QPen(Qt::NoPen));
193 p.setBrush(QBrush(QColor(Qt::black)));
195 const int b = height() - 1;
196 const QPointF points[] = {
198 QPointF(x - HoverArrowSize, b - HoverArrowSize),
199 QPointF(x + HoverArrowSize, b - HoverArrowSize)
201 p.drawPolygon(points, countof(points));
204 void Ruler::hover_point_changed()