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
30 #include <QApplication>
31 #include <QMouseEvent>
33 #include <QTextStream>
35 #include <pv/widgets/popup.h>
38 using boost::shared_ptr;
43 const int Ruler::RulerHeight = 30;
44 const int Ruler::MinorTickSubdivision = 4;
45 const int Ruler::ScaleUnits[3] = {1, 2, 5};
47 const int Ruler::HoverArrowSize = 5;
49 Ruler::Ruler(View &parent) :
53 setMouseTracking(true);
55 connect(&_view, SIGNAL(hover_point_changed()),
56 this, SLOT(hover_point_changed()));
59 void Ruler::clear_selection()
61 CursorPair &cursors = _view.cursors();
62 cursors.first()->select(false);
63 cursors.second()->select(false);
68 QSize Ruler::sizeHint() const
70 return QSize(0, RulerHeight);
73 void Ruler::paintEvent(QPaintEvent*)
76 const double SpacingIncrement = 32.0f;
77 const double MinValueSpacing = 32.0f;
78 const int ValueMargin = 3;
81 p.setRenderHint(QPainter::Antialiasing);
83 double min_width = SpacingIncrement, typical_width;
87 // Find tick spacing, and number formatting that does not cause
91 const double min_period = _view.scale() * min_width;
93 const int order = (int)floorf(log10f(min_period));
94 const double order_decimal = pow(10.0, order);
96 unsigned int unit = 0;
100 tick_period = order_decimal * ScaleUnits[unit++];
101 } while (tick_period < min_period && unit < countof(ScaleUnits));
103 prefix = (order - pv::util::FirstSIPrefixPower) / 3;
105 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
106 AlignLeft | AlignTop, pv::util::format_time(_view.offset(),
107 prefix)).width() + MinValueSpacing;
109 min_width += SpacingIncrement;
111 } while(typical_width > tick_period / _view.scale());
113 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
114 AlignLeft | AlignTop, "8").height();
116 // Draw the tick marks
117 p.setPen(palette().color(foregroundRole()));
119 const double minor_tick_period = tick_period / MinorTickSubdivision;
120 const double first_major_division =
121 floor(_view.offset() / tick_period);
122 const double first_minor_division =
123 ceil(_view.offset() / minor_tick_period);
124 const double t0 = first_major_division * tick_period;
126 int division = (int)round(first_minor_division -
127 first_major_division * MinorTickSubdivision) - 1;
129 const int major_tick_y1 = text_height + ValueMargin * 2;
130 const int tick_y2 = height();
131 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
136 const double t = t0 + division * minor_tick_period;
137 x = (t - _view.offset()) / _view.scale();
139 if (division % MinorTickSubdivision == 0)
142 p.drawText(x, ValueMargin, 0, text_height,
143 AlignCenter | AlignTop | TextDontClip,
144 pv::util::format_time(t, prefix));
145 p.drawLine(QPointF(x, major_tick_y1),
146 QPointF(x, tick_y2));
151 p.drawLine(QPointF(x, minor_tick_y1),
152 QPointF(x, tick_y2));
157 } while (x < width());
160 if (_view.cursors_shown())
161 _view.cursors().draw_markers(p, rect(), prefix);
163 // Draw the hover mark
169 void Ruler::mouseMoveEvent(QMouseEvent *e)
171 if (!(e->buttons() & Qt::LeftButton))
174 if ((e->pos() - _mouse_down_point).manhattanLength() <
175 QApplication::startDragDistance())
180 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
181 m->set_time(_view.offset() +
182 ((double)e->x() + 0.5) * _view.scale());
185 void Ruler::mousePressEvent(QMouseEvent *e)
187 if (e->buttons() & Qt::LeftButton)
189 _mouse_down_point = e->pos();
191 _grabbed_marker.reset();
195 if (_view.cursors_shown()) {
196 CursorPair &cursors = _view.cursors();
197 if (cursors.first()->get_label_rect(
198 rect()).contains(e->pos()))
199 _grabbed_marker = cursors.first();
200 else if (cursors.second()->get_label_rect(
201 rect()).contains(e->pos()))
202 _grabbed_marker = cursors.second();
205 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
212 void Ruler::mouseReleaseEvent(QMouseEvent *)
214 using pv::widgets::Popup;
217 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
218 Popup *const p = m->create_popup(&_view);
219 p->set_position(mapToGlobal(QPoint(m->get_x(),
220 height())), Popup::Bottom);
225 _grabbed_marker.reset();
228 void Ruler::draw_hover_mark(QPainter &p)
230 const int x = _view.hover_point().x();
232 if (x == -1 || _dragging)
235 p.setPen(QPen(Qt::NoPen));
236 p.setBrush(QBrush(palette().color(foregroundRole())));
238 const int b = height() - 1;
239 const QPointF points[] = {
241 QPointF(x - HoverArrowSize, b - HoverArrowSize),
242 QPointF(x + HoverArrowSize, b - HoverArrowSize)
244 p.drawPolygon(points, countof(points));
247 void Ruler::hover_point_changed()