]> sigrok.org Git - pulseview.git/blame - pv/views/trace/cursorpair.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / cursorpair.cpp
CommitLineData
b42d25c4
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
b42d25c4
JH
18 */
19
581724de
SA
20#include <algorithm>
21#include <cassert>
22
c04f5a29 23#include <QColor>
79a37ed8 24#include <QMenu>
581724de
SA
25#include <QToolTip>
26
2acdb232 27#include "cursorpair.hpp"
b42d25c4 28
c04f5a29 29#include "pv/globalsettings.hpp"
aca9aa83 30#include "pv/util.hpp"
3ccf0f7f 31#include "ruler.hpp"
2acdb232 32#include "view.hpp"
b42d25c4 33
819f4c25
JH
34using std::max;
35using std::make_pair;
36using std::min;
f9abf97e 37using std::shared_ptr;
819f4c25 38using std::pair;
0ba172cf 39
b42d25c4 40namespace pv {
f4e57597 41namespace views {
1573bf16 42namespace trace {
b42d25c4 43
5139748b
JH
44const int CursorPair::DeltaPadding = 8;
45
8debe10d 46CursorPair::CursorPair(View &view) :
5a0192d4 47 TimeItem(view),
8dbbc7f0 48 first_(new Cursor(view, 0.0)),
4ab6d244
UH
49 second_(new Cursor(view, 1.0)),
50 label_incomplete_(true)
b42d25c4 51{
c04f5a29
SA
52 GlobalSettings::add_change_handler(this);
53
54 GlobalSettings settings;
55 fill_color_ = QColor::fromRgba(settings.value(
56 GlobalSettings::Key_View_CursorFillColor).value<uint32_t>());
79a37ed8
MM
57 show_frequency_ = settings.value(
58 GlobalSettings::Key_View_CursorShowFrequency).value<bool>();
59 show_interval_ = settings.value(
60 GlobalSettings::Key_View_CursorShowInterval).value<bool>();
a3c57f44
SA
61 show_samples_ = settings.value(
62 GlobalSettings::Key_View_CursorShowSamples).value<bool>();
c04f5a29 63
581724de
SA
64 connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
65 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
b42d25c4
JH
66}
67
c04f5a29
SA
68CursorPair::~CursorPair()
69{
70 GlobalSettings::remove_change_handler(this);
71}
72
5a0192d4
JH
73bool CursorPair::enabled() const
74{
75 return view_.cursors_shown();
76}
77
58864c5c 78shared_ptr<Cursor> CursorPair::first() const
b42d25c4 79{
8dbbc7f0 80 return first_;
b42d25c4
JH
81}
82
58864c5c 83shared_ptr<Cursor> CursorPair::second() const
b42d25c4 84{
8dbbc7f0 85 return second_;
b42d25c4
JH
86}
87
2ad82c2e
UH
88void CursorPair::set_time(const pv::util::Timestamp& time)
89{
60d9b99a 90 const pv::util::Timestamp delta = second_->time() - first_->time();
3b9c4a0d
JH
91 first_->set_time(time);
92 second_->set_time(time + delta);
93}
94
710c2a18 95const pv::util::Timestamp CursorPair::time() const
96{
e4e951b7 97 return 0;
710c2a18 98}
99
5165bb34
JH
100float CursorPair::get_x() const
101{
102 return (first_->get_x() + second_->get_x()) / 2.0f;
103}
104
710c2a18 105const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const
106{
107 if (other < second_->time())
108 return other - first_->time();
109 else
110 return other - second_->time();
111}
112
a3d5a7c7 113QPoint CursorPair::drag_point(const QRect &rect) const
5a0192d4 114{
a3d5a7c7 115 return first_->drag_point(rect);
5a0192d4
JH
116}
117
118pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
119{
120 (void)parent;
121 return nullptr;
122}
123
79a37ed8
MM
124QMenu *CursorPair::create_header_context_menu(QWidget *parent)
125{
126 QMenu *menu = new QMenu(parent);
127
9a220973 128 QAction *displayIntervalAction = new QAction(tr("Display interval"), this);
79a37ed8
MM
129 displayIntervalAction->setCheckable(true);
130 displayIntervalAction->setChecked(show_interval_);
131 menu->addAction(displayIntervalAction);
132
49fee853
SA
133 connect(displayIntervalAction, &QAction::toggled, displayIntervalAction,
134 [=]{
135 GlobalSettings settings;
136 settings.setValue(GlobalSettings::Key_View_CursorShowInterval,
137 !settings.value(GlobalSettings::Key_View_CursorShowInterval).value<bool>());
79a37ed8
MM
138 });
139
9a220973 140 QAction *displayFrequencyAction = new QAction(tr("Display frequency"), this);
79a37ed8
MM
141 displayFrequencyAction->setCheckable(true);
142 displayFrequencyAction->setChecked(show_frequency_);
143 menu->addAction(displayFrequencyAction);
144
49fee853
SA
145 connect(displayFrequencyAction, &QAction::toggled, displayFrequencyAction,
146 [=]{
147 GlobalSettings settings;
148 settings.setValue(GlobalSettings::Key_View_CursorShowFrequency,
149 !settings.value(GlobalSettings::Key_View_CursorShowFrequency).value<bool>());
79a37ed8
MM
150 });
151
9a220973 152 QAction *displaySamplesAction = new QAction(tr("Display samples"), this);
a3c57f44
SA
153 displaySamplesAction->setCheckable(true);
154 displaySamplesAction->setChecked(show_samples_);
155 menu->addAction(displaySamplesAction);
156
49fee853
SA
157 connect(displaySamplesAction, &QAction::toggled, displaySamplesAction,
158 [=]{
159 GlobalSettings settings;
160 settings.setValue(GlobalSettings::Key_View_CursorShowSamples,
161 !settings.value(GlobalSettings::Key_View_CursorShowSamples).value<bool>());
a3c57f44
SA
162 });
163
79a37ed8
MM
164 return menu;
165}
166
689dea92 167QRectF CursorPair::label_rect(const QRectF &rect) const
5139748b 168{
3fed1f31 169 const QSizeF label_size(text_size_ + LabelPadding * 2);
5139748b
JH
170 const pair<float, float> offsets(get_cursor_offsets());
171 const pair<float, float> normal_offsets(
172 (offsets.first < offsets.second) ? offsets :
173 make_pair(offsets.second, offsets.first));
174
175 const float height = label_size.height();
176 const float left = max(normal_offsets.first + DeltaPadding, -height);
177 const float right = min(normal_offsets.second - DeltaPadding,
178 (float)rect.width() + height);
179
180 return QRectF(left, rect.height() - label_size.height() -
6abffa97 181 TimeMarker::ArrowSize - 0.5f,
5139748b
JH
182 right - left, height);
183}
184
49028d6c 185void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
332afa74 186{
8dbbc7f0
JH
187 assert(first_);
188 assert(second_);
58864c5c 189
9a377493
JH
190 if (!enabled())
191 return;
192
581724de 193 const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
641574bc 194 p.setPen(text_color);
5139748b 195
581724de 196 QRectF delta_rect(label_rect(rect));
5139748b 197 const int radius = delta_rect.height() / 2;
581724de
SA
198 QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
199
66279897
SA
200 QString text = format_string(text_rect.width(),
201 [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
202
ef85cfa4 203 text_size_ = p.boundingRect(QRectF(), 0, text).size();
581724de 204
5f0ea356
SA
205 /* Currently, selecting the middle section between two cursors doesn't do
206 * anything, so don't highlight it when selected
581724de
SA
207 if (selected()) {
208 p.setBrush(Qt::transparent);
209 p.setPen(highlight_pen());
5139748b 210 p.drawRoundedRect(delta_rect, radius, radius);
5f0ea356 211 } */
5139748b 212
581724de
SA
213 p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
214 p.setPen(Cursor::FillColor.darker());
215 p.drawRoundedRect(delta_rect, radius, radius);
5139748b 216
581724de
SA
217 delta_rect.adjust(1, 1, -1, -1);
218 p.setPen(Cursor::FillColor.lighter());
219 const int highlight_radius = delta_rect.height() / 2 - 2;
220 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
221 label_area_ = delta_rect;
222
223 p.setPen(text_color);
224 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
332afa74
JH
225}
226
60938e04 227void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
2ad82c2e 228{
beb897c6
JH
229 if (!enabled())
230 return;
231
0ba172cf 232 p.setPen(Qt::NoPen);
c04f5a29 233 p.setBrush(fill_color_);
0ba172cf 234
199441e4 235 const pair<float, float> offsets(get_cursor_offsets());
581724de
SA
236 const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
237 const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
58864c5c 238
beb897c6 239 p.drawRect(l, pp.top(), r - l, pp.height());
0ba172cf
JH
240}
241
66279897 242QString CursorPair::format_string(int max_width, std::function<double(const QString&)> query_size)
32045253 243{
66279897
SA
244 int time_precision = 12;
245 int freq_precision = 12;
3ccf0f7f 246
66279897 247 QString s = format_string_sub(time_precision, freq_precision);
ef85cfa4 248
66279897
SA
249 // Try full "{time} s / {freq} Hz" format
250 if ((max_width <= 0) || (query_size(s) <= max_width)) {
ef85cfa4 251 label_incomplete_ = false;
66279897 252 return s;
ef85cfa4 253 }
254
255 label_incomplete_ = true;
256
66279897
SA
257 // Gradually reduce time precision to match frequency precision
258 while (time_precision > freq_precision) {
259 time_precision--;
260
261 s = format_string_sub(time_precision, freq_precision);
262 if (query_size(s) <= max_width)
263 return s;
264 }
ef85cfa4 265
66279897
SA
266 // Gradually reduce both precisions down to zero
267 while (time_precision > 0) {
268 time_precision--;
269 freq_precision--;
ef85cfa4 270
66279897
SA
271 s = format_string_sub(time_precision, freq_precision);
272 if (query_size(s) <= max_width)
273 return s;
ef85cfa4 274 }
275
66279897
SA
276 // Try no trailing digits and drop the unit to at least display something
277 s = format_string_sub(0, 0, false);
278
279 if (query_size(s) <= max_width)
280 return s;
3ccf0f7f 281
66279897 282 // Give up
ef85cfa4 283 return "...";
32045253
JH
284}
285
581724de 286pair<float, float> CursorPair::get_cursor_offsets() const
5139748b 287{
8dbbc7f0
JH
288 assert(first_);
289 assert(second_);
58864c5c 290
581724de 291 return pair<float, float>(first_->get_x(), second_->get_x());
5139748b
JH
292}
293
c04f5a29
SA
294void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
295{
296 if (key == GlobalSettings::Key_View_CursorFillColor)
297 fill_color_ = QColor::fromRgba(value.value<uint32_t>());
79a37ed8
MM
298
299 if (key == GlobalSettings::Key_View_CursorShowFrequency)
300 show_frequency_ = value.value<bool>();
301
302 if (key == GlobalSettings::Key_View_CursorShowInterval)
303 show_interval_ = value.value<bool>();
a3c57f44
SA
304
305 if (key == GlobalSettings::Key_View_CursorShowSamples)
306 show_samples_ = value.value<bool>();
c04f5a29
SA
307}
308
581724de 309void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
199441e4 310{
581724de
SA
311 if (widget != view_.ruler())
312 return;
58864c5c 313
581724de
SA
314 if (!label_incomplete_)
315 return;
316
317 if (label_area_.contains(hp))
318 QToolTip::showText(view_.mapToGlobal(hp), format_string());
319 else
320 QToolTip::hideText(); // TODO Will break other tooltips when there can be others
199441e4
JH
321}
322
66279897
SA
323QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit)
324{
79a37ed8
MM
325 QString s = " ";
326
66279897
SA
327 const pv::util::SIPrefix prefix = view_.tick_prefix();
328 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
329
330 const QString time = Ruler::format_time_with_distance(
331 diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None),
332 time_precision, false);
333
334 // We can only show a frequency when there's a time base
335 if (view_.time_unit() == pv::util::TimeUnit::Time) {
79a37ed8
MM
336 int items = 0;
337
338 if (show_frequency_) {
339 const QString freq = util::format_value_si(
340 1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
341 freq_precision, (show_unit ? "Hz" : nullptr), false);
342 s = QString("%1").arg(freq);
343 items++;
344 }
345
346 if (show_interval_) {
347 if (items > 0)
348 s = QString("%1 / %2").arg(s, time);
349 else
350 s = QString("%1").arg(time);
351 items++;
352 }
a3c57f44
SA
353
354 if (show_samples_) {
355 const QString samples = QString::number(
356 (diff * view_.session().get_samplerate()).convert_to<uint64_t>());
357 if (items > 0)
358 s = QString("%1 / %2").arg(s, samples);
359 else
360 s = QString("%1").arg(samples);
361 }
66279897
SA
362 } else
363 // In this case, we return the number of samples, really
79a37ed8
MM
364 s = time;
365
366 return s;
66279897
SA
367}
368
1573bf16 369} // namespace trace
f4e57597 370} // namespace views
b42d25c4 371} // namespace pv