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 <QApplication>
34 #include <QMouseEvent>
36 #include <QTextStream>
38 #include <pv/widgets/popup.h>
40 using namespace boost;
46 const int Ruler::MinorTickSubdivision = 4;
47 const int Ruler::ScaleUnits[3] = {1, 2, 5};
49 const QString Ruler::SIPrefixes[9] =
50 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
51 const int Ruler::FirstSIPrefixPower = -15;
53 const int Ruler::HoverArrowSize = 5;
55 Ruler::Ruler(View &parent) :
59 setMouseTracking(true);
61 connect(&_view, SIGNAL(hover_point_changed()),
62 this, SLOT(hover_point_changed()));
65 void Ruler::clear_selection()
67 CursorPair &cursors = _view.cursors();
68 cursors.first()->select(false);
69 cursors.second()->select(false);
73 QString Ruler::format_time(double t, unsigned int prefix,
74 unsigned int precision)
76 const double multiplier = pow(10.0,
77 (int)- prefix * 3 - FirstSIPrefixPower);
81 ts.setRealNumberPrecision(precision);
82 ts << fixed << forcesign << (t * multiplier) <<
83 SIPrefixes[prefix] << "s";
87 void Ruler::paintEvent(QPaintEvent*)
91 const double SpacingIncrement = 32.0f;
92 const double MinValueSpacing = 32.0f;
93 const int ValueMargin = 3;
96 p.setRenderHint(QPainter::Antialiasing);
98 double min_width = SpacingIncrement, typical_width;
102 // Find tick spacing, and number formatting that does not cause
106 const double min_period = _view.scale() * min_width;
108 const int order = (int)floorf(log10f(min_period));
109 const double order_decimal = pow(10.0, order);
111 unsigned int unit = 0;
115 tick_period = order_decimal * ScaleUnits[unit++];
116 } while (tick_period < min_period && unit < countof(ScaleUnits));
118 prefix = (order - FirstSIPrefixPower) / 3;
119 assert(prefix < countof(SIPrefixes));
122 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
123 AlignLeft | AlignTop, format_time(_view.offset(),
124 prefix)).width() + MinValueSpacing;
126 min_width += SpacingIncrement;
128 } while(typical_width > tick_period / _view.scale());
130 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
131 AlignLeft | AlignTop, "8").height();
133 // Draw the tick marks
134 p.setPen(palette().color(foregroundRole()));
136 const double minor_tick_period = tick_period / MinorTickSubdivision;
137 const double first_major_division =
138 floor(_view.offset() / tick_period);
139 const double first_minor_division =
140 ceil(_view.offset() / minor_tick_period);
141 const double t0 = first_major_division * tick_period;
143 int division = (int)round(first_minor_division -
144 first_major_division * MinorTickSubdivision) - 1;
146 const int major_tick_y1 = text_height + ValueMargin * 2;
147 const int tick_y2 = height();
148 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
153 const double t = t0 + division * minor_tick_period;
154 x = (t - _view.offset()) / _view.scale();
156 if (division % MinorTickSubdivision == 0)
159 p.drawText(x, ValueMargin, 0, text_height,
160 AlignCenter | AlignTop | TextDontClip,
161 format_time(t, prefix));
162 p.drawLine(QPointF(x, major_tick_y1),
163 QPointF(x, tick_y2));
168 p.drawLine(QPointF(x, minor_tick_y1),
169 QPointF(x, tick_y2));
174 } while (x < width());
177 if (_view.cursors_shown())
178 _view.cursors().draw_markers(p, rect(), prefix);
180 // Draw the hover mark
186 void Ruler::mouseMoveEvent(QMouseEvent *e)
188 if (!(e->buttons() & Qt::LeftButton))
191 if ((e->pos() - _mouse_down_point).manhattanLength() <
192 QApplication::startDragDistance())
197 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
198 m->set_time(_view.offset() +
199 ((double)e->x() + 0.5) * _view.scale());
202 void Ruler::mousePressEvent(QMouseEvent *e)
204 if (e->buttons() & Qt::LeftButton)
206 _mouse_down_point = e->pos();
208 _grabbed_marker.reset();
212 if (_view.cursors_shown()) {
213 CursorPair &cursors = _view.cursors();
214 if (cursors.first()->get_label_rect(
215 rect()).contains(e->pos()))
216 _grabbed_marker = cursors.first();
217 else if (cursors.second()->get_label_rect(
218 rect()).contains(e->pos()))
219 _grabbed_marker = cursors.second();
222 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
229 void Ruler::mouseReleaseEvent(QMouseEvent *)
231 using pv::widgets::Popup;
234 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
235 Popup *const p = m->create_popup(&_view);
236 p->set_position(mapToGlobal(QPoint(m->get_x(),
237 height())), Popup::Bottom);
242 _grabbed_marker.reset();
245 void Ruler::draw_hover_mark(QPainter &p)
247 const int x = _view.hover_point().x();
249 if (x == -1 || _dragging)
252 p.setPen(QPen(Qt::NoPen));
253 p.setBrush(QBrush(palette().color(foregroundRole())));
255 const int b = height() - 1;
256 const QPointF points[] = {
258 QPointF(x - HoverArrowSize, b - HoverArrowSize),
259 QPointF(x + HoverArrowSize, b - HoverArrowSize)
261 p.drawPolygon(points, countof(points));
264 void Ruler::hover_point_changed()