]>
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 | ||
23 | #include <QDebug> | |
24 | #include <QToolTip> | |
25 | ||
2acdb232 | 26 | #include "cursorpair.hpp" |
b42d25c4 | 27 | |
aca9aa83 | 28 | #include "pv/util.hpp" |
3ccf0f7f | 29 | #include "ruler.hpp" |
2acdb232 | 30 | #include "view.hpp" |
b42d25c4 | 31 | |
819f4c25 JH |
32 | using std::max; |
33 | using std::make_pair; | |
34 | using std::min; | |
f9abf97e | 35 | using std::shared_ptr; |
819f4c25 | 36 | using std::pair; |
0ba172cf | 37 | |
b42d25c4 | 38 | namespace pv { |
f4e57597 | 39 | namespace views { |
1573bf16 | 40 | namespace trace { |
b42d25c4 | 41 | |
5139748b | 42 | const int CursorPair::DeltaPadding = 8; |
641574bc | 43 | const QColor CursorPair::ViewportFillColor(220, 231, 243); |
5139748b | 44 | |
8debe10d | 45 | CursorPair::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 | { |
581724de SA |
50 | connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)), |
51 | this, SLOT(on_hover_point_changed(const QWidget*, QPoint))); | |
b42d25c4 JH |
52 | } |
53 | ||
5a0192d4 JH |
54 | bool CursorPair::enabled() const |
55 | { | |
56 | return view_.cursors_shown(); | |
57 | } | |
58 | ||
58864c5c | 59 | shared_ptr<Cursor> CursorPair::first() const |
b42d25c4 | 60 | { |
8dbbc7f0 | 61 | return first_; |
b42d25c4 JH |
62 | } |
63 | ||
58864c5c | 64 | shared_ptr<Cursor> CursorPair::second() const |
b42d25c4 | 65 | { |
8dbbc7f0 | 66 | return second_; |
b42d25c4 JH |
67 | } |
68 | ||
2ad82c2e UH |
69 | void CursorPair::set_time(const pv::util::Timestamp& time) |
70 | { | |
60d9b99a | 71 | const pv::util::Timestamp delta = second_->time() - first_->time(); |
3b9c4a0d JH |
72 | first_->set_time(time); |
73 | second_->set_time(time + delta); | |
74 | } | |
75 | ||
5165bb34 JH |
76 | float CursorPair::get_x() const |
77 | { | |
78 | return (first_->get_x() + second_->get_x()) / 2.0f; | |
79 | } | |
80 | ||
a3d5a7c7 | 81 | QPoint CursorPair::drag_point(const QRect &rect) const |
5a0192d4 | 82 | { |
a3d5a7c7 | 83 | return first_->drag_point(rect); |
5a0192d4 JH |
84 | } |
85 | ||
86 | pv::widgets::Popup* CursorPair::create_popup(QWidget *parent) | |
87 | { | |
88 | (void)parent; | |
89 | return nullptr; | |
90 | } | |
91 | ||
689dea92 | 92 | QRectF CursorPair::label_rect(const QRectF &rect) const |
5139748b | 93 | { |
3fed1f31 | 94 | const QSizeF label_size(text_size_ + LabelPadding * 2); |
5139748b JH |
95 | const pair<float, float> offsets(get_cursor_offsets()); |
96 | const pair<float, float> normal_offsets( | |
97 | (offsets.first < offsets.second) ? offsets : | |
98 | make_pair(offsets.second, offsets.first)); | |
99 | ||
100 | const float height = label_size.height(); | |
101 | const float left = max(normal_offsets.first + DeltaPadding, -height); | |
102 | const float right = min(normal_offsets.second - DeltaPadding, | |
103 | (float)rect.width() + height); | |
104 | ||
105 | return QRectF(left, rect.height() - label_size.height() - | |
6abffa97 | 106 | TimeMarker::ArrowSize - 0.5f, |
5139748b JH |
107 | right - left, height); |
108 | } | |
109 | ||
49028d6c | 110 | void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) |
332afa74 | 111 | { |
8dbbc7f0 JH |
112 | assert(first_); |
113 | assert(second_); | |
58864c5c | 114 | |
9a377493 JH |
115 | if (!enabled()) |
116 | return; | |
117 | ||
581724de | 118 | const QColor text_color = ViewItem::select_text_color(Cursor::FillColor); |
641574bc | 119 | p.setPen(text_color); |
5139748b | 120 | |
581724de SA |
121 | QString text = format_string(); |
122 | text_size_ = p.boundingRect(QRectF(), 0, text).size(); | |
123 | ||
124 | QRectF delta_rect(label_rect(rect)); | |
5139748b | 125 | const int radius = delta_rect.height() / 2; |
581724de SA |
126 | QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0)); |
127 | ||
128 | if (text_rect.width() < text_size_.width()) { | |
129 | text = "..."; | |
130 | text_size_ = p.boundingRect(QRectF(), 0, text).size(); | |
131 | label_incomplete_ = true; | |
132 | } else | |
133 | label_incomplete_ = false; | |
134 | ||
135 | if (selected()) { | |
136 | p.setBrush(Qt::transparent); | |
137 | p.setPen(highlight_pen()); | |
5139748b | 138 | p.drawRoundedRect(delta_rect, radius, radius); |
581724de | 139 | } |
5139748b | 140 | |
581724de SA |
141 | p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor); |
142 | p.setPen(Cursor::FillColor.darker()); | |
143 | p.drawRoundedRect(delta_rect, radius, radius); | |
5139748b | 144 | |
581724de SA |
145 | delta_rect.adjust(1, 1, -1, -1); |
146 | p.setPen(Cursor::FillColor.lighter()); | |
147 | const int highlight_radius = delta_rect.height() / 2 - 2; | |
148 | p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); | |
149 | label_area_ = delta_rect; | |
150 | ||
151 | p.setPen(text_color); | |
152 | p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text); | |
332afa74 JH |
153 | } |
154 | ||
60938e04 | 155 | void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) |
2ad82c2e | 156 | { |
beb897c6 JH |
157 | if (!enabled()) |
158 | return; | |
159 | ||
0ba172cf | 160 | p.setPen(Qt::NoPen); |
641574bc | 161 | p.setBrush(QBrush(ViewportFillColor)); |
0ba172cf | 162 | |
199441e4 | 163 | const pair<float, float> offsets(get_cursor_offsets()); |
581724de SA |
164 | const int l = (int)max(min(offsets.first, offsets.second), 0.0f); |
165 | const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width()); | |
58864c5c | 166 | |
beb897c6 | 167 | p.drawRect(l, pp.top(), r - l, pp.height()); |
0ba172cf JH |
168 | } |
169 | ||
32045253 JH |
170 | QString CursorPair::format_string() |
171 | { | |
d001f416 | 172 | const pv::util::SIPrefix prefix = view_.tick_prefix(); |
3ccf0f7f JS |
173 | const pv::util::Timestamp diff = abs(second_->time() - first_->time()); |
174 | ||
175 | const QString s1 = Ruler::format_time_with_distance( | |
581724de | 176 | diff, diff, prefix, view_.time_unit(), 12, false); /* Always use 12 precision digits */ |
3ccf0f7f JS |
177 | const QString s2 = util::format_time_si( |
178 | 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false); | |
179 | ||
076ce70c | 180 | return QString("%1 / %2").arg(s1, s2); |
32045253 JH |
181 | } |
182 | ||
581724de | 183 | pair<float, float> CursorPair::get_cursor_offsets() const |
5139748b | 184 | { |
8dbbc7f0 JH |
185 | assert(first_); |
186 | assert(second_); | |
58864c5c | 187 | |
581724de | 188 | return pair<float, float>(first_->get_x(), second_->get_x()); |
5139748b JH |
189 | } |
190 | ||
581724de | 191 | void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) |
199441e4 | 192 | { |
581724de SA |
193 | if (widget != view_.ruler()) |
194 | return; | |
58864c5c | 195 | |
581724de SA |
196 | if (!label_incomplete_) |
197 | return; | |
198 | ||
199 | if (label_area_.contains(hp)) | |
200 | QToolTip::showText(view_.mapToGlobal(hp), format_string()); | |
201 | else | |
202 | QToolTip::hideText(); // TODO Will break other tooltips when there can be others | |
199441e4 JH |
203 | } |
204 | ||
1573bf16 | 205 | } // namespace trace |
f4e57597 | 206 | } // namespace views |
b42d25c4 | 207 | } // namespace pv |