]>
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 <QDebug> | |
24 | #include <QToolTip> | |
25 | ||
26 | #include "cursorpair.hpp" | |
27 | ||
28 | #include "pv/util.hpp" | |
29 | #include "ruler.hpp" | |
30 | #include "view.hpp" | |
31 | ||
32 | using std::max; | |
33 | using std::make_pair; | |
34 | using std::min; | |
35 | using std::shared_ptr; | |
36 | using std::pair; | |
37 | ||
38 | namespace pv { | |
39 | namespace views { | |
40 | namespace trace { | |
41 | ||
42 | const int CursorPair::DeltaPadding = 8; | |
43 | const QColor CursorPair::ViewportFillColor(220, 231, 243); | |
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 | connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)), | |
51 | this, SLOT(on_hover_point_changed(const QWidget*, QPoint))); | |
52 | } | |
53 | ||
54 | bool CursorPair::enabled() const | |
55 | { | |
56 | return view_.cursors_shown(); | |
57 | } | |
58 | ||
59 | shared_ptr<Cursor> CursorPair::first() const | |
60 | { | |
61 | return first_; | |
62 | } | |
63 | ||
64 | shared_ptr<Cursor> CursorPair::second() const | |
65 | { | |
66 | return second_; | |
67 | } | |
68 | ||
69 | void CursorPair::set_time(const pv::util::Timestamp& time) | |
70 | { | |
71 | const pv::util::Timestamp delta = second_->time() - first_->time(); | |
72 | first_->set_time(time); | |
73 | second_->set_time(time + delta); | |
74 | } | |
75 | ||
76 | float CursorPair::get_x() const | |
77 | { | |
78 | return (first_->get_x() + second_->get_x()) / 2.0f; | |
79 | } | |
80 | ||
81 | QPoint CursorPair::drag_point(const QRect &rect) const | |
82 | { | |
83 | return first_->drag_point(rect); | |
84 | } | |
85 | ||
86 | pv::widgets::Popup* CursorPair::create_popup(QWidget *parent) | |
87 | { | |
88 | (void)parent; | |
89 | return nullptr; | |
90 | } | |
91 | ||
92 | QRectF CursorPair::label_rect(const QRectF &rect) const | |
93 | { | |
94 | const QSizeF label_size(text_size_ + LabelPadding * 2); | |
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() - | |
106 | TimeMarker::ArrowSize - 0.5f, | |
107 | right - left, height); | |
108 | } | |
109 | ||
110 | void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) | |
111 | { | |
112 | assert(first_); | |
113 | assert(second_); | |
114 | ||
115 | if (!enabled()) | |
116 | return; | |
117 | ||
118 | const QColor text_color = ViewItem::select_text_color(Cursor::FillColor); | |
119 | p.setPen(text_color); | |
120 | ||
121 | QString text = format_string(); | |
122 | text_size_ = p.boundingRect(QRectF(), 0, text).size(); | |
123 | ||
124 | QRectF delta_rect(label_rect(rect)); | |
125 | const int radius = delta_rect.height() / 2; | |
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()); | |
138 | p.drawRoundedRect(delta_rect, radius, radius); | |
139 | } | |
140 | ||
141 | p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor); | |
142 | p.setPen(Cursor::FillColor.darker()); | |
143 | p.drawRoundedRect(delta_rect, radius, radius); | |
144 | ||
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); | |
153 | } | |
154 | ||
155 | void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) | |
156 | { | |
157 | if (!enabled()) | |
158 | return; | |
159 | ||
160 | p.setPen(Qt::NoPen); | |
161 | p.setBrush(QBrush(ViewportFillColor)); | |
162 | ||
163 | const pair<float, float> offsets(get_cursor_offsets()); | |
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()); | |
166 | ||
167 | p.drawRect(l, pp.top(), r - l, pp.height()); | |
168 | } | |
169 | ||
170 | QString CursorPair::format_string() | |
171 | { | |
172 | const pv::util::SIPrefix prefix = view_.tick_prefix(); | |
173 | const pv::util::Timestamp diff = abs(second_->time() - first_->time()); | |
174 | ||
175 | const QString s1 = Ruler::format_time_with_distance( | |
176 | diff, diff, prefix, view_.time_unit(), 12, false); /* Always use 12 precision digits */ | |
177 | const QString s2 = util::format_time_si( | |
178 | 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false); | |
179 | ||
180 | return QString("%1 / %2").arg(s1, s2); | |
181 | } | |
182 | ||
183 | pair<float, float> CursorPair::get_cursor_offsets() const | |
184 | { | |
185 | assert(first_); | |
186 | assert(second_); | |
187 | ||
188 | return pair<float, float>(first_->get_x(), second_->get_x()); | |
189 | } | |
190 | ||
191 | void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) | |
192 | { | |
193 | if (widget != view_.ruler()) | |
194 | return; | |
195 | ||
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 | |
203 | } | |
204 | ||
205 | } // namespace trace | |
206 | } // namespace views | |
207 | } // namespace pv |