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