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