]> sigrok.org Git - pulseview.git/blame - pv/views/trace/cursorpair.cpp
Make cursor pair drop precision when too small
[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>
581724de
SA
24#include <QToolTip>
25
2acdb232 26#include "cursorpair.hpp"
b42d25c4 27
c04f5a29 28#include "pv/globalsettings.hpp"
aca9aa83 29#include "pv/util.hpp"
3ccf0f7f 30#include "ruler.hpp"
2acdb232 31#include "view.hpp"
b42d25c4 32
819f4c25
JH
33using std::max;
34using std::make_pair;
35using std::min;
f9abf97e 36using std::shared_ptr;
819f4c25 37using std::pair;
0ba172cf 38
b42d25c4 39namespace pv {
f4e57597 40namespace views {
1573bf16 41namespace trace {
b42d25c4 42
5139748b
JH
43const int CursorPair::DeltaPadding = 8;
44
8debe10d 45CursorPair::CursorPair(View &view) :
5a0192d4 46 TimeItem(view),
8dbbc7f0 47 first_(new Cursor(view, 0.0)),
5a0192d4 48 second_(new Cursor(view, 1.0))
b42d25c4 49{
c04f5a29
SA
50 GlobalSettings::add_change_handler(this);
51
52 GlobalSettings settings;
53 fill_color_ = QColor::fromRgba(settings.value(
54 GlobalSettings::Key_View_CursorFillColor).value<uint32_t>());
55
581724de
SA
56 connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
57 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
b42d25c4
JH
58}
59
c04f5a29
SA
60CursorPair::~CursorPair()
61{
62 GlobalSettings::remove_change_handler(this);
63}
64
5a0192d4
JH
65bool CursorPair::enabled() const
66{
67 return view_.cursors_shown();
68}
69
58864c5c 70shared_ptr<Cursor> CursorPair::first() const
b42d25c4 71{
8dbbc7f0 72 return first_;
b42d25c4
JH
73}
74
58864c5c 75shared_ptr<Cursor> CursorPair::second() const
b42d25c4 76{
8dbbc7f0 77 return second_;
b42d25c4
JH
78}
79
2ad82c2e
UH
80void CursorPair::set_time(const pv::util::Timestamp& time)
81{
60d9b99a 82 const pv::util::Timestamp delta = second_->time() - first_->time();
3b9c4a0d
JH
83 first_->set_time(time);
84 second_->set_time(time + delta);
85}
86
5165bb34
JH
87float CursorPair::get_x() const
88{
89 return (first_->get_x() + second_->get_x()) / 2.0f;
90}
91
a3d5a7c7 92QPoint CursorPair::drag_point(const QRect &rect) const
5a0192d4 93{
a3d5a7c7 94 return first_->drag_point(rect);
5a0192d4
JH
95}
96
97pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
98{
99 (void)parent;
100 return nullptr;
101}
102
689dea92 103QRectF CursorPair::label_rect(const QRectF &rect) const
5139748b 104{
3fed1f31 105 const QSizeF label_size(text_size_ + LabelPadding * 2);
5139748b
JH
106 const pair<float, float> offsets(get_cursor_offsets());
107 const pair<float, float> normal_offsets(
108 (offsets.first < offsets.second) ? offsets :
109 make_pair(offsets.second, offsets.first));
110
111 const float height = label_size.height();
112 const float left = max(normal_offsets.first + DeltaPadding, -height);
113 const float right = min(normal_offsets.second - DeltaPadding,
114 (float)rect.width() + height);
115
116 return QRectF(left, rect.height() - label_size.height() -
6abffa97 117 TimeMarker::ArrowSize - 0.5f,
5139748b
JH
118 right - left, height);
119}
120
49028d6c 121void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
332afa74 122{
8dbbc7f0
JH
123 assert(first_);
124 assert(second_);
58864c5c 125
9a377493
JH
126 if (!enabled())
127 return;
128
581724de 129 const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
641574bc 130 p.setPen(text_color);
5139748b 131
581724de 132 QRectF delta_rect(label_rect(rect));
5139748b 133 const int radius = delta_rect.height() / 2;
581724de
SA
134 QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
135
ef85cfa4 136 QString text = format_string(text_rect.width(), [&p](const QString& s) -> qreal {
137 return p.boundingRect(QRectF(), 0, s).width();
138 });
139 text_size_ = p.boundingRect(QRectF(), 0, text).size();
581724de
SA
140
141 if (selected()) {
142 p.setBrush(Qt::transparent);
143 p.setPen(highlight_pen());
5139748b 144 p.drawRoundedRect(delta_rect, radius, radius);
581724de 145 }
5139748b 146
581724de
SA
147 p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
148 p.setPen(Cursor::FillColor.darker());
149 p.drawRoundedRect(delta_rect, radius, radius);
5139748b 150
581724de
SA
151 delta_rect.adjust(1, 1, -1, -1);
152 p.setPen(Cursor::FillColor.lighter());
153 const int highlight_radius = delta_rect.height() / 2 - 2;
154 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
155 label_area_ = delta_rect;
156
157 p.setPen(text_color);
158 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
332afa74
JH
159}
160
60938e04 161void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
2ad82c2e 162{
beb897c6
JH
163 if (!enabled())
164 return;
165
0ba172cf 166 p.setPen(Qt::NoPen);
c04f5a29 167 p.setBrush(fill_color_);
0ba172cf 168
199441e4 169 const pair<float, float> offsets(get_cursor_offsets());
581724de
SA
170 const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
171 const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
58864c5c 172
beb897c6 173 p.drawRect(l, pp.top(), r - l, pp.height());
0ba172cf
JH
174}
175
ef85cfa4 176QString CursorPair::format_string(qreal max_width, std::function<qreal(const QString&)> query_size)
32045253 177{
ef85cfa4 178 constexpr int time_precision = 12;
179 constexpr int freq_precision = 4;
180
d001f416 181 const pv::util::SIPrefix prefix = view_.tick_prefix();
3ccf0f7f
JS
182 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
183
ef85cfa4 184 const QString time = Ruler::format_time_with_distance(
185 diff, diff, prefix, view_.time_unit(), time_precision, false);
186 const QString freq = util::format_time_si(
187 1 / diff, pv::util::SIPrefix::unspecified, freq_precision, "Hz", false);
188 const QString out = QString("%1 / %2").arg(time, freq);
189
190 // Try full "{time} ms / {freq} Hz" format
191 if (max_width <= 0 || query_size(out) <= max_width) {
192 label_incomplete_ = false;
193 return out;
194 }
195
196 label_incomplete_ = true;
197
198 // Try just "{time}ms" format and gradually reduce time precision down to zero
199 for (int shrinkage=0; shrinkage <= time_precision; shrinkage++) {
200 int prec = time_precision - shrinkage ;
201
202 const QString time = Ruler::format_time_with_distance(
203 diff, diff, prefix, view_.time_unit(),
204 prec, false);
205
206 if (query_size(time) <= max_width)
207 return time;
208 }
209
210 // Try no trailing digits and drop the unit to at least display something. The unit should be obvious from the ruler
211 // anyway.
212 const QString bare_number = Ruler::format_time_with_distance(
213 diff, diff, prefix, view_.time_unit(),
214 0, false, false);
215 if (query_size(bare_number) <= max_width)
216 return bare_number;
3ccf0f7f 217
ef85cfa4 218 // Give up.
219 return "...";
32045253
JH
220}
221
581724de 222pair<float, float> CursorPair::get_cursor_offsets() const
5139748b 223{
8dbbc7f0
JH
224 assert(first_);
225 assert(second_);
58864c5c 226
581724de 227 return pair<float, float>(first_->get_x(), second_->get_x());
5139748b
JH
228}
229
c04f5a29
SA
230void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
231{
232 if (key == GlobalSettings::Key_View_CursorFillColor)
233 fill_color_ = QColor::fromRgba(value.value<uint32_t>());
234}
235
581724de 236void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
199441e4 237{
581724de
SA
238 if (widget != view_.ruler())
239 return;
58864c5c 240
581724de
SA
241 if (!label_incomplete_)
242 return;
243
244 if (label_area_.contains(hp))
245 QToolTip::showText(view_.mapToGlobal(hp), format_string());
246 else
247 QToolTip::hideText(); // TODO Will break other tooltips when there can be others
199441e4
JH
248}
249
1573bf16 250} // namespace trace
f4e57597 251} // namespace views
b42d25c4 252} // namespace pv