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