]> sigrok.org Git - pulseview.git/blame - pv/views/trace/cursorpair.cpp
TraceView: Add context menu to the cursor
[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>();
c04f5a29 61
581724de
SA
62 connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
63 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
b42d25c4
JH
64}
65
c04f5a29
SA
66CursorPair::~CursorPair()
67{
68 GlobalSettings::remove_change_handler(this);
69}
70
5a0192d4
JH
71bool CursorPair::enabled() const
72{
73 return view_.cursors_shown();
74}
75
58864c5c 76shared_ptr<Cursor> CursorPair::first() const
b42d25c4 77{
8dbbc7f0 78 return first_;
b42d25c4
JH
79}
80
58864c5c 81shared_ptr<Cursor> CursorPair::second() const
b42d25c4 82{
8dbbc7f0 83 return second_;
b42d25c4
JH
84}
85
2ad82c2e
UH
86void CursorPair::set_time(const pv::util::Timestamp& time)
87{
60d9b99a 88 const pv::util::Timestamp delta = second_->time() - first_->time();
3b9c4a0d
JH
89 first_->set_time(time);
90 second_->set_time(time + delta);
91}
92
710c2a18 93const pv::util::Timestamp CursorPair::time() const
94{
e4e951b7 95 return 0;
710c2a18 96}
97
5165bb34
JH
98float CursorPair::get_x() const
99{
100 return (first_->get_x() + second_->get_x()) / 2.0f;
101}
102
710c2a18 103const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const
104{
105 if (other < second_->time())
106 return other - first_->time();
107 else
108 return other - second_->time();
109}
110
a3d5a7c7 111QPoint CursorPair::drag_point(const QRect &rect) const
5a0192d4 112{
a3d5a7c7 113 return first_->drag_point(rect);
5a0192d4
JH
114}
115
116pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
117{
118 (void)parent;
119 return nullptr;
120}
121
79a37ed8
MM
122QMenu *CursorPair::create_header_context_menu(QWidget *parent)
123{
124 QMenu *menu = new QMenu(parent);
125
126 QAction *displayIntervalAction = new QAction(tr("Display interval"));
127 displayIntervalAction->setCheckable(true);
128 displayIntervalAction->setChecked(show_interval_);
129 menu->addAction(displayIntervalAction);
130
131 connect(displayIntervalAction, &QAction::toggled, [=]{
132 GlobalSettings settings;
133 settings.setValue(GlobalSettings::Key_View_CursorShowInterval,
134 !settings.value(GlobalSettings::Key_View_CursorShowInterval).value<bool>());
135 });
136
137 QAction *displayFrequencyAction = new QAction(tr("Display frequency"));
138 displayFrequencyAction->setCheckable(true);
139 displayFrequencyAction->setChecked(show_frequency_);
140 menu->addAction(displayFrequencyAction);
141
142 connect(displayFrequencyAction, &QAction::toggled, [=]{
143 GlobalSettings settings;
144 settings.setValue(GlobalSettings::Key_View_CursorShowFrequency,
145 !settings.value(GlobalSettings::Key_View_CursorShowFrequency).value<bool>());
146 });
147
148 return menu;
149}
150
689dea92 151QRectF CursorPair::label_rect(const QRectF &rect) const
5139748b 152{
3fed1f31 153 const QSizeF label_size(text_size_ + LabelPadding * 2);
5139748b
JH
154 const pair<float, float> offsets(get_cursor_offsets());
155 const pair<float, float> normal_offsets(
156 (offsets.first < offsets.second) ? offsets :
157 make_pair(offsets.second, offsets.first));
158
159 const float height = label_size.height();
160 const float left = max(normal_offsets.first + DeltaPadding, -height);
161 const float right = min(normal_offsets.second - DeltaPadding,
162 (float)rect.width() + height);
163
164 return QRectF(left, rect.height() - label_size.height() -
6abffa97 165 TimeMarker::ArrowSize - 0.5f,
5139748b
JH
166 right - left, height);
167}
168
49028d6c 169void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
332afa74 170{
8dbbc7f0
JH
171 assert(first_);
172 assert(second_);
58864c5c 173
9a377493
JH
174 if (!enabled())
175 return;
176
581724de 177 const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
641574bc 178 p.setPen(text_color);
5139748b 179
581724de 180 QRectF delta_rect(label_rect(rect));
5139748b 181 const int radius = delta_rect.height() / 2;
581724de
SA
182 QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
183
66279897
SA
184 QString text = format_string(text_rect.width(),
185 [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
186
ef85cfa4 187 text_size_ = p.boundingRect(QRectF(), 0, text).size();
581724de
SA
188
189 if (selected()) {
190 p.setBrush(Qt::transparent);
191 p.setPen(highlight_pen());
5139748b 192 p.drawRoundedRect(delta_rect, radius, radius);
581724de 193 }
5139748b 194
581724de
SA
195 p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
196 p.setPen(Cursor::FillColor.darker());
197 p.drawRoundedRect(delta_rect, radius, radius);
5139748b 198
581724de
SA
199 delta_rect.adjust(1, 1, -1, -1);
200 p.setPen(Cursor::FillColor.lighter());
201 const int highlight_radius = delta_rect.height() / 2 - 2;
202 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
203 label_area_ = delta_rect;
204
205 p.setPen(text_color);
206 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
332afa74
JH
207}
208
60938e04 209void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
2ad82c2e 210{
beb897c6
JH
211 if (!enabled())
212 return;
213
0ba172cf 214 p.setPen(Qt::NoPen);
c04f5a29 215 p.setBrush(fill_color_);
0ba172cf 216
199441e4 217 const pair<float, float> offsets(get_cursor_offsets());
581724de
SA
218 const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
219 const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
58864c5c 220
beb897c6 221 p.drawRect(l, pp.top(), r - l, pp.height());
0ba172cf
JH
222}
223
66279897 224QString CursorPair::format_string(int max_width, std::function<double(const QString&)> query_size)
32045253 225{
66279897
SA
226 int time_precision = 12;
227 int freq_precision = 12;
3ccf0f7f 228
66279897 229 QString s = format_string_sub(time_precision, freq_precision);
ef85cfa4 230
66279897
SA
231 // Try full "{time} s / {freq} Hz" format
232 if ((max_width <= 0) || (query_size(s) <= max_width)) {
ef85cfa4 233 label_incomplete_ = false;
66279897 234 return s;
ef85cfa4 235 }
236
237 label_incomplete_ = true;
238
66279897
SA
239 // Gradually reduce time precision to match frequency precision
240 while (time_precision > freq_precision) {
241 time_precision--;
242
243 s = format_string_sub(time_precision, freq_precision);
244 if (query_size(s) <= max_width)
245 return s;
246 }
ef85cfa4 247
66279897
SA
248 // Gradually reduce both precisions down to zero
249 while (time_precision > 0) {
250 time_precision--;
251 freq_precision--;
ef85cfa4 252
66279897
SA
253 s = format_string_sub(time_precision, freq_precision);
254 if (query_size(s) <= max_width)
255 return s;
ef85cfa4 256 }
257
66279897
SA
258 // Try no trailing digits and drop the unit to at least display something
259 s = format_string_sub(0, 0, false);
260
261 if (query_size(s) <= max_width)
262 return s;
3ccf0f7f 263
66279897 264 // Give up
ef85cfa4 265 return "...";
32045253
JH
266}
267
581724de 268pair<float, float> CursorPair::get_cursor_offsets() const
5139748b 269{
8dbbc7f0
JH
270 assert(first_);
271 assert(second_);
58864c5c 272
581724de 273 return pair<float, float>(first_->get_x(), second_->get_x());
5139748b
JH
274}
275
c04f5a29
SA
276void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
277{
278 if (key == GlobalSettings::Key_View_CursorFillColor)
279 fill_color_ = QColor::fromRgba(value.value<uint32_t>());
79a37ed8
MM
280
281 if (key == GlobalSettings::Key_View_CursorShowFrequency)
282 show_frequency_ = value.value<bool>();
283
284 if (key == GlobalSettings::Key_View_CursorShowInterval)
285 show_interval_ = value.value<bool>();
c04f5a29
SA
286}
287
581724de 288void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
199441e4 289{
581724de
SA
290 if (widget != view_.ruler())
291 return;
58864c5c 292
581724de
SA
293 if (!label_incomplete_)
294 return;
295
296 if (label_area_.contains(hp))
297 QToolTip::showText(view_.mapToGlobal(hp), format_string());
298 else
299 QToolTip::hideText(); // TODO Will break other tooltips when there can be others
199441e4
JH
300}
301
66279897
SA
302QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit)
303{
79a37ed8
MM
304 QString s = " ";
305
66279897
SA
306 const pv::util::SIPrefix prefix = view_.tick_prefix();
307 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
308
309 const QString time = Ruler::format_time_with_distance(
310 diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None),
311 time_precision, false);
312
313 // We can only show a frequency when there's a time base
314 if (view_.time_unit() == pv::util::TimeUnit::Time) {
79a37ed8
MM
315 int items = 0;
316
317 if (show_frequency_) {
318 const QString freq = util::format_value_si(
319 1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
320 freq_precision, (show_unit ? "Hz" : nullptr), false);
321 s = QString("%1").arg(freq);
322 items++;
323 }
324
325 if (show_interval_) {
326 if (items > 0)
327 s = QString("%1 / %2").arg(s, time);
328 else
329 s = QString("%1").arg(time);
330 items++;
331 }
66279897
SA
332 } else
333 // In this case, we return the number of samples, really
79a37ed8
MM
334 s = time;
335
336 return s;
66279897
SA
337}
338
1573bf16 339} // namespace trace
f4e57597 340} // namespace views
b42d25c4 341} // namespace pv