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 using namespace boost;
43 const int Ruler::MinorTickSubdivision = 4;
44 const int Ruler::ScaleUnits[3] = {1, 2, 5};
46 const QString Ruler::SIPrefixes[9] =
47 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
48 const int Ruler::FirstSIPrefixPower = -15;
50 const int Ruler::HoverArrowSize = 5;
52 Ruler::Ruler(View &parent) :
56 setMouseTracking(true);
58 connect(&_view, SIGNAL(hover_point_changed()),
59 this, SLOT(hover_point_changed()));
62 void Ruler::clear_selection()
64 CursorPair &cursors = _view.cursors();
65 cursors.first()->select(false);
66 cursors.second()->select(false);
70 QString Ruler::format_time(double t, unsigned int prefix,
71 unsigned int precision)
73 const double multiplier = pow(10.0,
74 - prefix * 3 - FirstSIPrefixPower);
78 ts.setRealNumberPrecision(precision);
79 ts << fixed << forcesign << (t * multiplier) <<
80 SIPrefixes[prefix] << "s";
84 void Ruler::paintEvent(QPaintEvent*)
88 const double SpacingIncrement = 32.0f;
89 const double MinValueSpacing = 32.0f;
90 const int ValueMargin = 3;
93 p.setRenderHint(QPainter::Antialiasing);
95 double min_width = SpacingIncrement, typical_width;
99 // Find tick spacing, and number formatting that does not cause
103 const double min_period = _view.scale() * min_width;
105 const int order = (int)floorf(log10f(min_period));
106 const double order_decimal = pow(10, order);
108 unsigned int unit = 0;
112 tick_period = order_decimal * ScaleUnits[unit++];
113 } while (tick_period < min_period && unit < countof(ScaleUnits));
115 prefix = (order - FirstSIPrefixPower) / 3;
116 assert(prefix < countof(SIPrefixes));
119 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
120 AlignLeft | AlignTop, format_time(_view.offset(),
121 prefix)).width() + MinValueSpacing;
123 min_width += SpacingIncrement;
125 } while(typical_width > tick_period / _view.scale());
127 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
128 AlignLeft | AlignTop, "8").height();
130 // Draw the tick marks
131 p.setPen(palette().color(foregroundRole()));
133 const double minor_tick_period = tick_period / MinorTickSubdivision;
134 const double first_major_division =
135 floor(_view.offset() / tick_period);
136 const double first_minor_division =
137 ceil(_view.offset() / minor_tick_period);
138 const double t0 = first_major_division * tick_period;
140 int division = (int)round(first_minor_division -
141 first_major_division * MinorTickSubdivision) - 1;
143 const int major_tick_y1 = text_height + ValueMargin * 2;
144 const int tick_y2 = height();
145 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
150 const double t = t0 + division * minor_tick_period;
151 x = (t - _view.offset()) / _view.scale();
153 if (division % MinorTickSubdivision == 0)
156 p.drawText(x, ValueMargin, 0, text_height,
157 AlignCenter | AlignTop | TextDontClip,
158 format_time(t, prefix));
159 p.drawLine(QPointF(x, major_tick_y1),
160 QPointF(x, tick_y2));
165 p.drawLine(QPointF(x, minor_tick_y1),
166 QPointF(x, tick_y2));
171 } while (x < width());
174 if (_view.cursors_shown())
175 _view.cursors().draw_markers(p, rect(), prefix);
177 // Draw the hover mark
183 void Ruler::mouseMoveEvent(QMouseEvent *e)
185 if (!(e->buttons() & Qt::LeftButton))
188 if ((e->pos() - _mouse_down_point).manhattanLength() <
189 QApplication::startDragDistance())
194 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
195 m->set_time(_view.offset() +
196 ((double)e->x() + 0.5) * _view.scale());
199 void Ruler::mousePressEvent(QMouseEvent *e)
201 if (e->buttons() & Qt::LeftButton)
203 _mouse_down_point = e->pos();
205 _grabbed_marker.reset();
209 if (_view.cursors_shown()) {
210 CursorPair &cursors = _view.cursors();
211 if (cursors.first()->get_label_rect(
212 rect()).contains(e->pos()))
213 _grabbed_marker = cursors.first();
214 else if (cursors.second()->get_label_rect(
215 rect()).contains(e->pos()))
216 _grabbed_marker = cursors.second();
219 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
226 void Ruler::mouseReleaseEvent(QMouseEvent *)
229 _grabbed_marker.reset();
232 void Ruler::draw_hover_mark(QPainter &p)
234 const int x = _view.hover_point().x();
236 if (x == -1 || _dragging)
239 p.setPen(QPen(Qt::NoPen));
240 p.setBrush(QBrush(palette().color(foregroundRole())));
242 const int b = height() - 1;
243 const QPointF points[] = {
245 QPointF(x - HoverArrowSize, b - HoverArrowSize),
246 QPointF(x + HoverArrowSize, b - HoverArrowSize)
248 p.drawPolygon(points, countof(points));
251 void Ruler::hover_point_changed()