]>
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 | float CursorPair::get_x() const | |
88 | { | |
89 | return (first_->get_x() + second_->get_x()) / 2.0f; | |
90 | } | |
91 | ||
92 | QPoint CursorPair::drag_point(const QRect &rect) const | |
93 | { | |
94 | return first_->drag_point(rect); | |
95 | } | |
96 | ||
97 | pv::widgets::Popup* CursorPair::create_popup(QWidget *parent) | |
98 | { | |
99 | (void)parent; | |
100 | return nullptr; | |
101 | } | |
102 | ||
103 | QRectF CursorPair::label_rect(const QRectF &rect) const | |
104 | { | |
105 | const QSizeF label_size(text_size_ + LabelPadding * 2); | |
106 | const pair<float, float> offsets(get_cursor_offsets()); | |
107 | const pair<float, float> normal_offsets( | |
108 | (offsets.first < offsets.second) ? offsets : | |
109 | make_pair(offsets.second, offsets.first)); | |
110 | ||
111 | const float height = label_size.height(); | |
112 | const float left = max(normal_offsets.first + DeltaPadding, -height); | |
113 | const float right = min(normal_offsets.second - DeltaPadding, | |
114 | (float)rect.width() + height); | |
115 | ||
116 | return QRectF(left, rect.height() - label_size.height() - | |
117 | TimeMarker::ArrowSize - 0.5f, | |
118 | right - left, height); | |
119 | } | |
120 | ||
121 | void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) | |
122 | { | |
123 | assert(first_); | |
124 | assert(second_); | |
125 | ||
126 | if (!enabled()) | |
127 | return; | |
128 | ||
129 | const QColor text_color = ViewItem::select_text_color(Cursor::FillColor); | |
130 | p.setPen(text_color); | |
131 | ||
132 | QString text = format_string(); | |
133 | text_size_ = p.boundingRect(QRectF(), 0, text).size(); | |
134 | ||
135 | QRectF delta_rect(label_rect(rect)); | |
136 | const int radius = delta_rect.height() / 2; | |
137 | QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0)); | |
138 | ||
139 | if (text_rect.width() < text_size_.width()) { | |
140 | text = "..."; | |
141 | text_size_ = p.boundingRect(QRectF(), 0, text).size(); | |
142 | label_incomplete_ = true; | |
143 | } else | |
144 | label_incomplete_ = false; | |
145 | ||
146 | if (selected()) { | |
147 | p.setBrush(Qt::transparent); | |
148 | p.setPen(highlight_pen()); | |
149 | p.drawRoundedRect(delta_rect, radius, radius); | |
150 | } | |
151 | ||
152 | p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor); | |
153 | p.setPen(Cursor::FillColor.darker()); | |
154 | p.drawRoundedRect(delta_rect, radius, radius); | |
155 | ||
156 | delta_rect.adjust(1, 1, -1, -1); | |
157 | p.setPen(Cursor::FillColor.lighter()); | |
158 | const int highlight_radius = delta_rect.height() / 2 - 2; | |
159 | p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); | |
160 | label_area_ = delta_rect; | |
161 | ||
162 | p.setPen(text_color); | |
163 | p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text); | |
164 | } | |
165 | ||
166 | void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) | |
167 | { | |
168 | if (!enabled()) | |
169 | return; | |
170 | ||
171 | p.setPen(Qt::NoPen); | |
172 | p.setBrush(fill_color_); | |
173 | ||
174 | const pair<float, float> offsets(get_cursor_offsets()); | |
175 | const int l = (int)max(min(offsets.first, offsets.second), 0.0f); | |
176 | const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width()); | |
177 | ||
178 | p.drawRect(l, pp.top(), r - l, pp.height()); | |
179 | } | |
180 | ||
181 | QString CursorPair::format_string() | |
182 | { | |
183 | const pv::util::SIPrefix prefix = view_.tick_prefix(); | |
184 | const pv::util::Timestamp diff = abs(second_->time() - first_->time()); | |
185 | ||
186 | const QString s1 = Ruler::format_time_with_distance( | |
187 | diff, diff, prefix, view_.time_unit(), 12, false); /* Always use 12 precision digits */ | |
188 | const QString s2 = util::format_time_si( | |
189 | 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false); | |
190 | ||
191 | return QString("%1 / %2").arg(s1, s2); | |
192 | } | |
193 | ||
194 | pair<float, float> CursorPair::get_cursor_offsets() const | |
195 | { | |
196 | assert(first_); | |
197 | assert(second_); | |
198 | ||
199 | return pair<float, float>(first_->get_x(), second_->get_x()); | |
200 | } | |
201 | ||
202 | void CursorPair::on_setting_changed(const QString &key, const QVariant &value) | |
203 | { | |
204 | if (key == GlobalSettings::Key_View_CursorFillColor) | |
205 | fill_color_ = QColor::fromRgba(value.value<uint32_t>()); | |
206 | } | |
207 | ||
208 | void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) | |
209 | { | |
210 | if (widget != view_.ruler()) | |
211 | return; | |
212 | ||
213 | if (!label_incomplete_) | |
214 | return; | |
215 | ||
216 | if (label_area_.contains(hp)) | |
217 | QToolTip::showText(view_.mapToGlobal(hp), format_string()); | |
218 | else | |
219 | QToolTip::hideText(); // TODO Will break other tooltips when there can be others | |
220 | } | |
221 | ||
222 | } // namespace trace | |
223 | } // namespace views | |
224 | } // namespace pv |