]> sigrok.org Git - pulseview.git/blob - pv/views/trace/cursorpair.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / cursorpair.cpp
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
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <algorithm>
21 #include <cassert>
22
23 #include <QColor>
24 #include <QMenu>
25 #include <QToolTip>
26
27 #include "cursorpair.hpp"
28
29 #include "pv/globalsettings.hpp"
30 #include "pv/util.hpp"
31 #include "ruler.hpp"
32 #include "view.hpp"
33
34 using std::max;
35 using std::make_pair;
36 using std::min;
37 using std::shared_ptr;
38 using std::pair;
39
40 namespace pv {
41 namespace views {
42 namespace trace {
43
44 const int CursorPair::DeltaPadding = 8;
45
46 CursorPair::CursorPair(View &view) :
47         TimeItem(view),
48         first_(new Cursor(view, 0.0)),
49         second_(new Cursor(view, 1.0)),
50         label_incomplete_(true)
51 {
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>());
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>();
63
64         connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
65                 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
66 }
67
68 CursorPair::~CursorPair()
69 {
70         GlobalSettings::remove_change_handler(this);
71 }
72
73 bool CursorPair::enabled() const
74 {
75         return view_.cursors_shown();
76 }
77
78 shared_ptr<Cursor> CursorPair::first() const
79 {
80         return first_;
81 }
82
83 shared_ptr<Cursor> CursorPair::second() const
84 {
85         return second_;
86 }
87
88 void CursorPair::set_time(const pv::util::Timestamp& time)
89 {
90         const pv::util::Timestamp delta = second_->time() - first_->time();
91         first_->set_time(time);
92         second_->set_time(time + delta);
93 }
94
95 const pv::util::Timestamp CursorPair::time() const
96 {
97         return 0;
98 }
99
100 float CursorPair::get_x() const
101 {
102         return (first_->get_x() + second_->get_x()) / 2.0f;
103 }
104
105 const 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
113 QPoint CursorPair::drag_point(const QRect &rect) const
114 {
115         return first_->drag_point(rect);
116 }
117
118 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
119 {
120         (void)parent;
121         return nullptr;
122 }
123
124 QMenu *CursorPair::create_header_context_menu(QWidget *parent)
125 {
126         QMenu *menu = new QMenu(parent);
127
128         QAction *displayIntervalAction = new QAction(tr("Display interval"), this);
129         displayIntervalAction->setCheckable(true);
130         displayIntervalAction->setChecked(show_interval_);
131         menu->addAction(displayIntervalAction);
132
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>());
138                 });
139
140         QAction *displayFrequencyAction = new QAction(tr("Display frequency"), this);
141         displayFrequencyAction->setCheckable(true);
142         displayFrequencyAction->setChecked(show_frequency_);
143         menu->addAction(displayFrequencyAction);
144
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>());
150                 });
151
152         QAction *displaySamplesAction = new QAction(tr("Display samples"), this);
153         displaySamplesAction->setCheckable(true);
154         displaySamplesAction->setChecked(show_samples_);
155         menu->addAction(displaySamplesAction);
156
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>());
162                 });
163
164         return menu;
165 }
166
167 QRectF CursorPair::label_rect(const QRectF &rect) const
168 {
169         const QSizeF label_size(text_size_ + LabelPadding * 2);
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() -
181                 TimeMarker::ArrowSize - 0.5f,
182                 right - left, height);
183 }
184
185 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
186 {
187         assert(first_);
188         assert(second_);
189
190         if (!enabled())
191                 return;
192
193         const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
194         p.setPen(text_color);
195
196         QRectF delta_rect(label_rect(rect));
197         const int radius = delta_rect.height() / 2;
198         QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
199
200         QString text = format_string(text_rect.width(),
201                 [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
202
203         text_size_ = p.boundingRect(QRectF(), 0, text).size();
204
205         if (selected()) {
206                 p.setBrush(Qt::transparent);
207                 p.setPen(highlight_pen());
208                 p.drawRoundedRect(delta_rect, radius, radius);
209         }
210
211         p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
212         p.setPen(Cursor::FillColor.darker());
213         p.drawRoundedRect(delta_rect, radius, radius);
214
215         delta_rect.adjust(1, 1, -1, -1);
216         p.setPen(Cursor::FillColor.lighter());
217         const int highlight_radius = delta_rect.height() / 2 - 2;
218         p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
219         label_area_ = delta_rect;
220
221         p.setPen(text_color);
222         p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
223 }
224
225 void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
226 {
227         if (!enabled())
228                 return;
229
230         p.setPen(Qt::NoPen);
231         p.setBrush(fill_color_);
232
233         const pair<float, float> offsets(get_cursor_offsets());
234         const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
235         const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
236
237         p.drawRect(l, pp.top(), r - l, pp.height());
238 }
239
240 QString CursorPair::format_string(int max_width, std::function<double(const QString&)> query_size)
241 {
242         int time_precision = 12;
243         int freq_precision = 12;
244
245         QString s = format_string_sub(time_precision, freq_precision);
246
247         // Try full "{time} s / {freq} Hz" format
248         if ((max_width <= 0) || (query_size(s) <= max_width)) {
249                 label_incomplete_ = false;
250                 return s;
251         }
252
253         label_incomplete_ = true;
254
255         // Gradually reduce time precision to match frequency precision
256         while (time_precision > freq_precision) {
257                 time_precision--;
258
259                 s = format_string_sub(time_precision, freq_precision);
260                 if (query_size(s) <= max_width)
261                         return s;
262         }
263
264         // Gradually reduce both precisions down to zero
265         while (time_precision > 0) {
266                 time_precision--;
267                 freq_precision--;
268
269                 s = format_string_sub(time_precision, freq_precision);
270                 if (query_size(s) <= max_width)
271                         return s;
272         }
273
274         // Try no trailing digits and drop the unit to at least display something
275         s = format_string_sub(0, 0, false);
276
277         if (query_size(s) <= max_width)
278                 return s;
279
280         // Give up
281         return "...";
282 }
283
284 pair<float, float> CursorPair::get_cursor_offsets() const
285 {
286         assert(first_);
287         assert(second_);
288
289         return pair<float, float>(first_->get_x(), second_->get_x());
290 }
291
292 void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
293 {
294         if (key == GlobalSettings::Key_View_CursorFillColor)
295                 fill_color_ = QColor::fromRgba(value.value<uint32_t>());
296
297         if (key == GlobalSettings::Key_View_CursorShowFrequency)
298                 show_frequency_ = value.value<bool>();
299
300         if (key == GlobalSettings::Key_View_CursorShowInterval)
301                 show_interval_ = value.value<bool>();
302
303         if (key == GlobalSettings::Key_View_CursorShowSamples)
304                 show_samples_ = value.value<bool>();
305 }
306
307 void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
308 {
309         if (widget != view_.ruler())
310                 return;
311
312         if (!label_incomplete_)
313                 return;
314
315         if (label_area_.contains(hp))
316                 QToolTip::showText(view_.mapToGlobal(hp), format_string());
317         else
318                 QToolTip::hideText();  // TODO Will break other tooltips when there can be others
319 }
320
321 QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit)
322 {
323         QString s = " ";
324
325         const pv::util::SIPrefix prefix = view_.tick_prefix();
326         const pv::util::Timestamp diff = abs(second_->time() - first_->time());
327
328         const QString time = Ruler::format_time_with_distance(
329                 diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None),
330                 time_precision, false);
331
332         // We can only show a frequency when there's a time base
333         if (view_.time_unit() == pv::util::TimeUnit::Time) {
334                 int items = 0;
335
336                 if (show_frequency_) {
337                         const QString freq = util::format_value_si(
338                                 1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
339                                 freq_precision, (show_unit ? "Hz" : nullptr), false);
340                         s = QString("%1").arg(freq);
341                         items++;
342                 }
343
344                 if (show_interval_) {
345                         if (items > 0)
346                                 s = QString("%1 / %2").arg(s, time);
347                         else
348                                 s = QString("%1").arg(time);
349                         items++;
350                 }
351
352                 if (show_samples_) {
353                         const QString samples = QString::number(
354                                 (diff * view_.session().get_samplerate()).convert_to<uint64_t>());
355                         if (items > 0)
356                                 s = QString("%1 / %2").arg(s, samples);
357                         else
358                                 s = QString("%1").arg(samples);
359                 }
360         } else
361                 // In this case, we return the number of samples, really
362                 s = time;
363
364         return s;
365 }
366
367 } // namespace trace
368 } // namespace views
369 } // namespace pv