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::RulerHeight = 30;
47 const int Ruler::MinorTickSubdivision = 4;
48 const int Ruler::ScaleUnits[3] = {1, 2, 5};
50 const QString Ruler::SIPrefixes[9] =
51 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
52 const int Ruler::FirstSIPrefixPower = -15;
54 const int Ruler::HoverArrowSize = 5;
56 Ruler::Ruler(View &parent) :
60 setMouseTracking(true);
62 connect(&_view, SIGNAL(hover_point_changed()),
63 this, SLOT(hover_point_changed()));
66 void Ruler::clear_selection()
68 CursorPair &cursors = _view.cursors();
69 cursors.first()->select(false);
70 cursors.second()->select(false);
74 QString Ruler::format_time(double t, unsigned int prefix,
75 unsigned int precision)
77 const double multiplier = pow(10.0,
78 (int)- prefix * 3 - FirstSIPrefixPower);
82 ts.setRealNumberPrecision(precision);
83 ts << fixed << forcesign << (t * multiplier) <<
84 SIPrefixes[prefix] << "s";
88 QSize Ruler::sizeHint() const
90 return QSize(0, RulerHeight);
93 void Ruler::paintEvent(QPaintEvent*)
97 const double SpacingIncrement = 32.0f;
98 const double MinValueSpacing = 32.0f;
99 const int ValueMargin = 3;
102 p.setRenderHint(QPainter::Antialiasing);
104 double min_width = SpacingIncrement, typical_width;
108 // Find tick spacing, and number formatting that does not cause
112 const double min_period = _view.scale() * min_width;
114 const int order = (int)floorf(log10f(min_period));
115 const double order_decimal = pow(10.0, order);
117 unsigned int unit = 0;
121 tick_period = order_decimal * ScaleUnits[unit++];
122 } while (tick_period < min_period && unit < countof(ScaleUnits));
124 prefix = (order - FirstSIPrefixPower) / 3;
125 assert(prefix < countof(SIPrefixes));
128 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
129 AlignLeft | AlignTop, format_time(_view.offset(),
130 prefix)).width() + MinValueSpacing;
132 min_width += SpacingIncrement;
134 } while(typical_width > tick_period / _view.scale());
136 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
137 AlignLeft | AlignTop, "8").height();
139 // Draw the tick marks
140 p.setPen(palette().color(foregroundRole()));
142 const double minor_tick_period = tick_period / MinorTickSubdivision;
143 const double first_major_division =
144 floor(_view.offset() / tick_period);
145 const double first_minor_division =
146 ceil(_view.offset() / minor_tick_period);
147 const double t0 = first_major_division * tick_period;
149 int division = (int)round(first_minor_division -
150 first_major_division * MinorTickSubdivision) - 1;
152 const int major_tick_y1 = text_height + ValueMargin * 2;
153 const int tick_y2 = height();
154 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
159 const double t = t0 + division * minor_tick_period;
160 x = (t - _view.offset()) / _view.scale();
162 if (division % MinorTickSubdivision == 0)
165 p.drawText(x, ValueMargin, 0, text_height,
166 AlignCenter | AlignTop | TextDontClip,
167 format_time(t, prefix));
168 p.drawLine(QPointF(x, major_tick_y1),
169 QPointF(x, tick_y2));
174 p.drawLine(QPointF(x, minor_tick_y1),
175 QPointF(x, tick_y2));
180 } while (x < width());
183 if (_view.cursors_shown())
184 _view.cursors().draw_markers(p, rect(), prefix);
186 // Draw the hover mark
192 void Ruler::mouseMoveEvent(QMouseEvent *e)
194 if (!(e->buttons() & Qt::LeftButton))
197 if ((e->pos() - _mouse_down_point).manhattanLength() <
198 QApplication::startDragDistance())
203 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
204 m->set_time(_view.offset() +
205 ((double)e->x() + 0.5) * _view.scale());
208 void Ruler::mousePressEvent(QMouseEvent *e)
210 if (e->buttons() & Qt::LeftButton)
212 _mouse_down_point = e->pos();
214 _grabbed_marker.reset();
218 if (_view.cursors_shown()) {
219 CursorPair &cursors = _view.cursors();
220 if (cursors.first()->get_label_rect(
221 rect()).contains(e->pos()))
222 _grabbed_marker = cursors.first();
223 else if (cursors.second()->get_label_rect(
224 rect()).contains(e->pos()))
225 _grabbed_marker = cursors.second();
228 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
235 void Ruler::mouseReleaseEvent(QMouseEvent *)
237 using pv::widgets::Popup;
240 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
241 Popup *const p = m->create_popup(&_view);
242 p->set_position(mapToGlobal(QPoint(m->get_x(),
243 height())), Popup::Bottom);
248 _grabbed_marker.reset();
251 void Ruler::draw_hover_mark(QPainter &p)
253 const int x = _view.hover_point().x();
255 if (x == -1 || _dragging)
258 p.setPen(QPen(Qt::NoPen));
259 p.setBrush(QBrush(palette().color(foregroundRole())));
261 const int b = height() - 1;
262 const QPointF points[] = {
264 QPointF(x - HoverArrowSize, b - HoverArrowSize),
265 QPointF(x + HoverArrowSize, b - HoverArrowSize)
267 p.drawPolygon(points, countof(points));
270 void Ruler::hover_point_changed()