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 #include <pv/widgets/popup.h>
39 using namespace boost;
45 const int Ruler::MinorTickSubdivision = 4;
46 const int Ruler::ScaleUnits[3] = {1, 2, 5};
48 const QString Ruler::SIPrefixes[9] =
49 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
50 const int Ruler::FirstSIPrefixPower = -15;
52 const int Ruler::HoverArrowSize = 5;
54 Ruler::Ruler(View &parent) :
58 setMouseTracking(true);
60 connect(&_view, SIGNAL(hover_point_changed()),
61 this, SLOT(hover_point_changed()));
64 void Ruler::clear_selection()
66 CursorPair &cursors = _view.cursors();
67 cursors.first()->select(false);
68 cursors.second()->select(false);
72 QString Ruler::format_time(double t, unsigned int prefix,
73 unsigned int precision)
75 const double multiplier = pow(10.0,
76 - prefix * 3 - FirstSIPrefixPower);
80 ts.setRealNumberPrecision(precision);
81 ts << fixed << forcesign << (t * multiplier) <<
82 SIPrefixes[prefix] << "s";
86 void Ruler::paintEvent(QPaintEvent*)
90 const double SpacingIncrement = 32.0f;
91 const double MinValueSpacing = 32.0f;
92 const int ValueMargin = 3;
95 p.setRenderHint(QPainter::Antialiasing);
97 double min_width = SpacingIncrement, typical_width;
101 // Find tick spacing, and number formatting that does not cause
105 const double min_period = _view.scale() * min_width;
107 const int order = (int)floorf(log10f(min_period));
108 const double order_decimal = pow(10, order);
110 unsigned int unit = 0;
114 tick_period = order_decimal * ScaleUnits[unit++];
115 } while (tick_period < min_period && unit < countof(ScaleUnits));
117 prefix = (order - FirstSIPrefixPower) / 3;
118 assert(prefix < countof(SIPrefixes));
121 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
122 AlignLeft | AlignTop, format_time(_view.offset(),
123 prefix)).width() + MinValueSpacing;
125 min_width += SpacingIncrement;
127 } while(typical_width > tick_period / _view.scale());
129 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
130 AlignLeft | AlignTop, "8").height();
132 // Draw the tick marks
133 p.setPen(palette().color(foregroundRole()));
135 const double minor_tick_period = tick_period / MinorTickSubdivision;
136 const double first_major_division =
137 floor(_view.offset() / tick_period);
138 const double first_minor_division =
139 ceil(_view.offset() / minor_tick_period);
140 const double t0 = first_major_division * tick_period;
142 int division = (int)round(first_minor_division -
143 first_major_division * MinorTickSubdivision) - 1;
145 const int major_tick_y1 = text_height + ValueMargin * 2;
146 const int tick_y2 = height();
147 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
152 const double t = t0 + division * minor_tick_period;
153 x = (t - _view.offset()) / _view.scale();
155 if (division % MinorTickSubdivision == 0)
158 p.drawText(x, ValueMargin, 0, text_height,
159 AlignCenter | AlignTop | TextDontClip,
160 format_time(t, prefix));
161 p.drawLine(QPointF(x, major_tick_y1),
162 QPointF(x, tick_y2));
167 p.drawLine(QPointF(x, minor_tick_y1),
168 QPointF(x, tick_y2));
173 } while (x < width());
176 if (_view.cursors_shown())
177 _view.cursors().draw_markers(p, rect(), prefix);
179 // Draw the hover mark
185 void Ruler::mouseMoveEvent(QMouseEvent *e)
187 if (!(e->buttons() & Qt::LeftButton))
190 if ((e->pos() - _mouse_down_point).manhattanLength() <
191 QApplication::startDragDistance())
196 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
197 m->set_time(_view.offset() +
198 ((double)e->x() + 0.5) * _view.scale());
201 void Ruler::mousePressEvent(QMouseEvent *e)
203 if (e->buttons() & Qt::LeftButton)
205 _mouse_down_point = e->pos();
207 _grabbed_marker.reset();
211 if (_view.cursors_shown()) {
212 CursorPair &cursors = _view.cursors();
213 if (cursors.first()->get_label_rect(
214 rect()).contains(e->pos()))
215 _grabbed_marker = cursors.first();
216 else if (cursors.second()->get_label_rect(
217 rect()).contains(e->pos()))
218 _grabbed_marker = cursors.second();
221 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
228 void Ruler::mouseReleaseEvent(QMouseEvent *)
230 using pv::widgets::Popup;
233 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
234 Popup *const p = m->create_popup(&_view);
235 p->set_position(mapToGlobal(QPoint(m->get_x(),
236 height())), Popup::Bottom);
241 _grabbed_marker.reset();
244 void Ruler::draw_hover_mark(QPainter &p)
246 const int x = _view.hover_point().x();
248 if (x == -1 || _dragging)
251 p.setPen(QPen(Qt::NoPen));
252 p.setBrush(QBrush(palette().color(foregroundRole())));
254 const int b = height() - 1;
255 const QPointF points[] = {
257 QPointF(x - HoverArrowSize, b - HoverArrowSize),
258 QPointF(x + HoverArrowSize, b - HoverArrowSize)
260 p.drawPolygon(points, countof(points));
263 void Ruler::hover_point_changed()