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, see <http://www.gnu.org/licenses/>.
22 #include <QApplication>
23 #include <QFontMetrics>
25 #include <QMouseEvent>
27 #include <pv/globalsettings.hpp>
37 using std::shared_ptr;
44 const float Ruler::RulerHeight = 2.5f; // x Text Height
46 const float Ruler::HoverArrowSize = 0.5f; // x Text Height
48 Ruler::Ruler(View &parent) :
51 setMouseTracking(true);
53 connect(&view_, SIGNAL(hover_point_changed(QPoint)),
54 this, SLOT(hover_point_changed(QPoint)));
55 connect(&view_, SIGNAL(offset_changed()),
56 this, SLOT(invalidate_tick_position_cache()));
57 connect(&view_, SIGNAL(scale_changed()),
58 this, SLOT(invalidate_tick_position_cache()));
59 connect(&view_, SIGNAL(tick_prefix_changed()),
60 this, SLOT(invalidate_tick_position_cache()));
61 connect(&view_, SIGNAL(tick_precision_changed()),
62 this, SLOT(invalidate_tick_position_cache()));
63 connect(&view_, SIGNAL(tick_period_changed()),
64 this, SLOT(invalidate_tick_position_cache()));
65 connect(&view_, SIGNAL(time_unit_changed()),
66 this, SLOT(invalidate_tick_position_cache()));
69 QSize Ruler::sizeHint() const
71 const int text_height = calculate_text_height();
72 return QSize(0, RulerHeight * text_height);
75 QSize Ruler::extended_size_hint() const
78 vector< shared_ptr<TimeItem> > items(view_.time_items());
80 max_rect = max_rect.united(i->label_rect(QRect()));
81 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
82 ViewItem::HighlightRadius);
85 QString Ruler::format_time_with_distance(
86 const pv::util::Timestamp& distance,
87 const pv::util::Timestamp& t,
88 pv::util::SIPrefix prefix,
89 pv::util::TimeUnit unit,
93 const unsigned limit = 60;
98 // If we have to use samples then we have no alternative formats
99 if (unit == pv::util::TimeUnit::Samples)
100 return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign);
102 // View zoomed way out -> low precision (0), big distance (>=60s)
104 if ((precision == 0) && (distance >= limit))
105 return pv::util::format_time_minutes(t, 0, sign);
107 // View in "normal" range -> medium precision, medium step size
108 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
109 // View zoomed way in -> high precision (>3), low step size (<1s)
110 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
112 return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign);
114 return pv::util::format_time_minutes(t, precision, sign);
117 pv::util::Timestamp Ruler::get_time_from_x_pos(uint32_t x)
119 return view_.ruler_offset() + ((double)x + 0.5) * view_.scale();
122 void Ruler::contextMenuEvent(QContextMenuEvent *event)
124 context_menu_x_pos_ = event->pos().x();
126 QMenu *const menu = new QMenu(this);
128 QAction *const create_marker = new QAction(tr("Create marker here"), this);
129 connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker()));
130 menu->addAction(create_marker);
132 QAction *const toggle_hover_marker = new QAction(this);
133 connect(toggle_hover_marker, SIGNAL(triggered()), this, SLOT(on_toggleHoverMarker()));
134 menu->addAction(toggle_hover_marker);
136 GlobalSettings settings;
137 const bool hover_marker_shown =
138 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
139 toggle_hover_marker->setText(hover_marker_shown ?
140 tr("Disable mouse hover marker") : tr("Enable mouse hover marker"));
142 menu->popup(event->globalPos());
145 void Ruler::resizeEvent(QResizeEvent*)
147 // the tick calculation depends on the width of this widget
148 invalidate_tick_position_cache();
151 vector< shared_ptr<ViewItem> > Ruler::items()
153 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
154 return vector< shared_ptr<ViewItem> >(
155 time_items.begin(), time_items.end());
158 shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
160 const vector< shared_ptr<TimeItem> > items(view_.time_items());
161 for (auto i = items.rbegin(); i != items.rend(); i++)
162 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
167 void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
169 view_.add_flag(get_time_from_x_pos(event->x()));
172 void Ruler::paintEvent(QPaintEvent*)
174 if (!tick_position_cache_) {
175 auto ffunc = [this](const pv::util::Timestamp& t) {
176 return format_time_with_distance(
177 this->view_.tick_period(),
179 this->view_.tick_prefix(),
180 this->view_.time_unit(),
181 this->view_.tick_precision());
184 tick_position_cache_ = calculate_tick_positions(
186 view_.ruler_offset(),
189 view_.minor_tick_count(),
193 const int ValueMargin = 3;
195 const int text_height = calculate_text_height();
196 const int ruler_height = RulerHeight * text_height;
197 const int major_tick_y1 = text_height + ValueMargin * 2;
198 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
202 // Draw the tick marks
203 p.setPen(palette().color(foregroundRole()));
205 for (const auto& tick: tick_position_cache_->major) {
206 const int leftedge = 0;
207 const int rightedge = width();
208 const int x_tick = tick.first;
209 if ((x_tick > leftedge) && (x_tick < rightedge)) {
210 const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2;
211 const int x_right_bound = rightedge - x_left_bound;
212 const int x_legend = min(max(x_tick, x_left_bound), x_right_bound);
213 p.drawText(x_legend, ValueMargin, 0, text_height,
214 AlignCenter | AlignTop | TextDontClip, tick.second);
215 p.drawLine(QPointF(x_tick, major_tick_y1),
216 QPointF(tick.first, ruler_height));
220 for (const auto& tick: tick_position_cache_->minor) {
221 p.drawLine(QPointF(tick, minor_tick_y1),
222 QPointF(tick, ruler_height));
225 // Draw the hover mark
226 draw_hover_mark(p, text_height);
228 p.setRenderHint(QPainter::Antialiasing);
230 // The cursor labels are not drawn with the arrows exactly on the
231 // bottom line of the widget, because then the selection shadow
232 // would be clipped away.
233 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
236 const vector< shared_ptr<TimeItem> > items(view_.time_items());
237 for (auto &i : items) {
238 const bool highlight = !item_dragging_ &&
239 i->label_rect(r).contains(mouse_point_);
240 i->paint_label(p, r, highlight);
244 void Ruler::draw_hover_mark(QPainter &p, int text_height)
246 const int x = view_.hover_point().x();
251 p.setPen(QPen(Qt::NoPen));
252 p.setBrush(QBrush(palette().color(foregroundRole())));
254 const int b = RulerHeight * text_height;
255 const float hover_arrow_size = HoverArrowSize * text_height;
256 const QPointF points[] = {
258 QPointF(x - hover_arrow_size, b - hover_arrow_size),
259 QPointF(x + hover_arrow_size, b - hover_arrow_size)
261 p.drawPolygon(points, countof(points));
264 int Ruler::calculate_text_height() const
266 return QFontMetrics(font()).ascent();
269 TickPositions Ruler::calculate_tick_positions(
270 const pv::util::Timestamp& major_period,
271 const pv::util::Timestamp& offset,
274 const unsigned int minor_tick_count,
275 function<QString(const pv::util::Timestamp&)> format_function)
279 const pv::util::Timestamp minor_period = major_period / minor_tick_count;
280 const pv::util::Timestamp first_major_division = floor(offset / major_period);
281 const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
282 const pv::util::Timestamp t0 = first_major_division * major_period;
284 int division = (round(first_minor_division -
285 first_major_division * minor_tick_count)).convert_to<int>() - 1;
290 pv::util::Timestamp t = t0 + division * minor_period;
291 x = ((t - offset) / scale).convert_to<double>();
293 if (division % minor_tick_count == 0) {
294 // Recalculate 't' without using 'minor_period' which is a fraction
295 t = t0 + division / minor_tick_count * major_period;
296 tp.major.emplace_back(x, format_function(t));
298 tp.minor.emplace_back(x);
307 void Ruler::hover_point_changed(const QPoint &hp)
314 void Ruler::invalidate_tick_position_cache()
316 tick_position_cache_ = boost::none;
319 void Ruler::on_createMarker()
321 view_.add_flag(get_time_from_x_pos(context_menu_x_pos_));
324 void Ruler::on_toggleHoverMarker()
326 GlobalSettings settings;
327 const bool state = settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
328 settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, !state);