2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 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/>.
27 #include "cursorpair.hpp"
29 #include "pv/globalsettings.hpp"
30 #include "pv/util.hpp"
37 using std::shared_ptr;
44 const int CursorPair::DeltaPadding = 8;
46 CursorPair::CursorPair(View &view) :
48 first_(new Cursor(view, 0.0)),
49 second_(new Cursor(view, 1.0)),
50 label_incomplete_(true)
52 GlobalSettings::add_change_handler(this);
54 GlobalSettings settings;
55 fill_color_ = QColor::fromRgba(settings.value(
56 GlobalSettings::Key_View_CursorFillColor).value<uint32_t>());
57 show_frequency_ = settings.value(
58 GlobalSettings::Key_View_CursorShowFrequency).value<bool>();
59 show_interval_ = settings.value(
60 GlobalSettings::Key_View_CursorShowInterval).value<bool>();
61 show_samples_ = settings.value(
62 GlobalSettings::Key_View_CursorShowSamples).value<bool>();
64 connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
65 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
68 CursorPair::~CursorPair()
70 GlobalSettings::remove_change_handler(this);
73 bool CursorPair::enabled() const
75 return view_.cursors_shown();
78 shared_ptr<Cursor> CursorPair::first() const
83 shared_ptr<Cursor> CursorPair::second() const
88 void CursorPair::set_time(const pv::util::Timestamp& time)
90 const pv::util::Timestamp delta = second_->time() - first_->time();
91 first_->set_time(time);
92 second_->set_time(time + delta);
95 const pv::util::Timestamp CursorPair::time() const
100 float CursorPair::get_x() const
102 return (first_->get_x() + second_->get_x()) / 2.0f;
105 const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const
107 if (other < second_->time())
108 return other - first_->time();
110 return other - second_->time();
113 QPoint CursorPair::drag_point(const QRect &rect) const
115 return first_->drag_point(rect);
118 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
124 QMenu *CursorPair::create_header_context_menu(QWidget *parent)
126 QMenu *menu = new QMenu(parent);
128 QAction *displayIntervalAction = new QAction(tr("Display interval"), this);
129 displayIntervalAction->setCheckable(true);
130 displayIntervalAction->setChecked(show_interval_);
131 menu->addAction(displayIntervalAction);
133 connect(displayIntervalAction, &QAction::toggled, [=]{
134 GlobalSettings settings;
135 settings.setValue(GlobalSettings::Key_View_CursorShowInterval,
136 !settings.value(GlobalSettings::Key_View_CursorShowInterval).value<bool>());
139 QAction *displayFrequencyAction = new QAction(tr("Display frequency"), this);
140 displayFrequencyAction->setCheckable(true);
141 displayFrequencyAction->setChecked(show_frequency_);
142 menu->addAction(displayFrequencyAction);
144 connect(displayFrequencyAction, &QAction::toggled, [=]{
145 GlobalSettings settings;
146 settings.setValue(GlobalSettings::Key_View_CursorShowFrequency,
147 !settings.value(GlobalSettings::Key_View_CursorShowFrequency).value<bool>());
150 QAction *displaySamplesAction = new QAction(tr("Display samples"), this);
151 displaySamplesAction->setCheckable(true);
152 displaySamplesAction->setChecked(show_samples_);
153 menu->addAction(displaySamplesAction);
155 connect(displaySamplesAction, &QAction::toggled, [=]{
156 GlobalSettings settings;
157 settings.setValue(GlobalSettings::Key_View_CursorShowSamples,
158 !settings.value(GlobalSettings::Key_View_CursorShowSamples).value<bool>());
164 QRectF CursorPair::label_rect(const QRectF &rect) const
166 const QSizeF label_size(text_size_ + LabelPadding * 2);
167 const pair<float, float> offsets(get_cursor_offsets());
168 const pair<float, float> normal_offsets(
169 (offsets.first < offsets.second) ? offsets :
170 make_pair(offsets.second, offsets.first));
172 const float height = label_size.height();
173 const float left = max(normal_offsets.first + DeltaPadding, -height);
174 const float right = min(normal_offsets.second - DeltaPadding,
175 (float)rect.width() + height);
177 return QRectF(left, rect.height() - label_size.height() -
178 TimeMarker::ArrowSize - 0.5f,
179 right - left, height);
182 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
190 const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
191 p.setPen(text_color);
193 QRectF delta_rect(label_rect(rect));
194 const int radius = delta_rect.height() / 2;
195 QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
197 QString text = format_string(text_rect.width(),
198 [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
200 text_size_ = p.boundingRect(QRectF(), 0, text).size();
203 p.setBrush(Qt::transparent);
204 p.setPen(highlight_pen());
205 p.drawRoundedRect(delta_rect, radius, radius);
208 p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
209 p.setPen(Cursor::FillColor.darker());
210 p.drawRoundedRect(delta_rect, radius, radius);
212 delta_rect.adjust(1, 1, -1, -1);
213 p.setPen(Cursor::FillColor.lighter());
214 const int highlight_radius = delta_rect.height() / 2 - 2;
215 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
216 label_area_ = delta_rect;
218 p.setPen(text_color);
219 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
222 void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
228 p.setBrush(fill_color_);
230 const pair<float, float> offsets(get_cursor_offsets());
231 const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
232 const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
234 p.drawRect(l, pp.top(), r - l, pp.height());
237 QString CursorPair::format_string(int max_width, std::function<double(const QString&)> query_size)
239 int time_precision = 12;
240 int freq_precision = 12;
242 QString s = format_string_sub(time_precision, freq_precision);
244 // Try full "{time} s / {freq} Hz" format
245 if ((max_width <= 0) || (query_size(s) <= max_width)) {
246 label_incomplete_ = false;
250 label_incomplete_ = true;
252 // Gradually reduce time precision to match frequency precision
253 while (time_precision > freq_precision) {
256 s = format_string_sub(time_precision, freq_precision);
257 if (query_size(s) <= max_width)
261 // Gradually reduce both precisions down to zero
262 while (time_precision > 0) {
266 s = format_string_sub(time_precision, freq_precision);
267 if (query_size(s) <= max_width)
271 // Try no trailing digits and drop the unit to at least display something
272 s = format_string_sub(0, 0, false);
274 if (query_size(s) <= max_width)
281 pair<float, float> CursorPair::get_cursor_offsets() const
286 return pair<float, float>(first_->get_x(), second_->get_x());
289 void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
291 if (key == GlobalSettings::Key_View_CursorFillColor)
292 fill_color_ = QColor::fromRgba(value.value<uint32_t>());
294 if (key == GlobalSettings::Key_View_CursorShowFrequency)
295 show_frequency_ = value.value<bool>();
297 if (key == GlobalSettings::Key_View_CursorShowInterval)
298 show_interval_ = value.value<bool>();
300 if (key == GlobalSettings::Key_View_CursorShowSamples)
301 show_samples_ = value.value<bool>();
304 void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
306 if (widget != view_.ruler())
309 if (!label_incomplete_)
312 if (label_area_.contains(hp))
313 QToolTip::showText(view_.mapToGlobal(hp), format_string());
315 QToolTip::hideText(); // TODO Will break other tooltips when there can be others
318 QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit)
322 const pv::util::SIPrefix prefix = view_.tick_prefix();
323 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
325 const QString time = Ruler::format_time_with_distance(
326 diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None),
327 time_precision, false);
329 // We can only show a frequency when there's a time base
330 if (view_.time_unit() == pv::util::TimeUnit::Time) {
333 if (show_frequency_) {
334 const QString freq = util::format_value_si(
335 1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
336 freq_precision, (show_unit ? "Hz" : nullptr), false);
337 s = QString("%1").arg(freq);
341 if (show_interval_) {
343 s = QString("%1 / %2").arg(s, time);
345 s = QString("%1").arg(time);
350 const QString samples = QString::number(
351 (diff * view_.session().get_samplerate()).convert_to<uint64_t>());
353 s = QString("%1 / %2").arg(s, samples);
355 s = QString("%1").arg(samples);
358 // In this case, we return the number of samples, really