]>
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include "cursorpair.hpp" | |
22 | ||
23 | #include "ruler.hpp" | |
24 | #include "view.hpp" | |
25 | #include "pv/util.hpp" | |
26 | ||
27 | #include <cassert> | |
28 | #include <algorithm> | |
29 | ||
30 | using std::max; | |
31 | using std::make_pair; | |
32 | using std::min; | |
33 | using std::shared_ptr; | |
34 | using std::pair; | |
35 | ||
36 | namespace pv { | |
37 | namespace views { | |
38 | namespace TraceView { | |
39 | ||
40 | const int CursorPair::DeltaPadding = 8; | |
41 | const QColor CursorPair::ViewportFillColour(220, 231, 243); | |
42 | ||
43 | CursorPair::CursorPair(View &view) : | |
44 | TimeItem(view), | |
45 | first_(new Cursor(view, 0.0)), | |
46 | second_(new Cursor(view, 1.0)) | |
47 | { | |
48 | } | |
49 | ||
50 | bool CursorPair::enabled() const | |
51 | { | |
52 | return view_.cursors_shown(); | |
53 | } | |
54 | ||
55 | shared_ptr<Cursor> CursorPair::first() const | |
56 | { | |
57 | return first_; | |
58 | } | |
59 | ||
60 | shared_ptr<Cursor> CursorPair::second() const | |
61 | { | |
62 | return second_; | |
63 | } | |
64 | ||
65 | void CursorPair::set_time(const pv::util::Timestamp& time) | |
66 | { | |
67 | const pv::util::Timestamp delta = second_->time() - first_->time(); | |
68 | first_->set_time(time); | |
69 | second_->set_time(time + delta); | |
70 | } | |
71 | ||
72 | float CursorPair::get_x() const | |
73 | { | |
74 | return (first_->get_x() + second_->get_x()) / 2.0f; | |
75 | } | |
76 | ||
77 | QPoint CursorPair::point(const QRect &rect) const | |
78 | { | |
79 | return first_->point(rect); | |
80 | } | |
81 | ||
82 | pv::widgets::Popup* CursorPair::create_popup(QWidget *parent) | |
83 | { | |
84 | (void)parent; | |
85 | return nullptr; | |
86 | } | |
87 | ||
88 | QRectF CursorPair::label_rect(const QRectF &rect) const | |
89 | { | |
90 | const QSizeF label_size(text_size_ + LabelPadding * 2); | |
91 | const pair<float, float> offsets(get_cursor_offsets()); | |
92 | const pair<float, float> normal_offsets( | |
93 | (offsets.first < offsets.second) ? offsets : | |
94 | make_pair(offsets.second, offsets.first)); | |
95 | ||
96 | const float height = label_size.height(); | |
97 | const float left = max(normal_offsets.first + DeltaPadding, -height); | |
98 | const float right = min(normal_offsets.second - DeltaPadding, | |
99 | (float)rect.width() + height); | |
100 | ||
101 | return QRectF(left, rect.height() - label_size.height() - | |
102 | TimeMarker::ArrowSize - 0.5f, | |
103 | right - left, height); | |
104 | } | |
105 | ||
106 | void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) | |
107 | { | |
108 | assert(first_); | |
109 | assert(second_); | |
110 | ||
111 | if (!enabled()) | |
112 | return; | |
113 | ||
114 | const QColor text_colour = | |
115 | ViewItem::select_text_colour(Cursor::FillColour); | |
116 | ||
117 | p.setPen(text_colour); | |
118 | compute_text_size(p); | |
119 | QRectF delta_rect(label_rect(rect)); | |
120 | ||
121 | const int radius = delta_rect.height() / 2; | |
122 | const QRectF text_rect(delta_rect.intersected( | |
123 | rect).adjusted(radius, 0, -radius, 0)); | |
124 | if (text_rect.width() >= text_size_.width()) { | |
125 | const int highlight_radius = delta_rect.height() / 2 - 2; | |
126 | ||
127 | if (selected()) { | |
128 | p.setBrush(Qt::transparent); | |
129 | p.setPen(highlight_pen()); | |
130 | p.drawRoundedRect(delta_rect, radius, radius); | |
131 | } | |
132 | ||
133 | p.setBrush(hover ? Cursor::FillColour.lighter() : | |
134 | Cursor::FillColour); | |
135 | p.setPen(Cursor::FillColour.darker()); | |
136 | p.drawRoundedRect(delta_rect, radius, radius); | |
137 | ||
138 | delta_rect.adjust(1, 1, -1, -1); | |
139 | p.setPen(Cursor::FillColour.lighter()); | |
140 | p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); | |
141 | ||
142 | p.setPen(text_colour); | |
143 | p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, | |
144 | format_string()); | |
145 | } | |
146 | } | |
147 | ||
148 | void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) | |
149 | { | |
150 | if (!enabled()) | |
151 | return; | |
152 | ||
153 | p.setPen(Qt::NoPen); | |
154 | p.setBrush(QBrush(ViewportFillColour)); | |
155 | ||
156 | const pair<float, float> offsets(get_cursor_offsets()); | |
157 | const int l = (int)max(min( | |
158 | offsets.first, offsets.second), 0.0f); | |
159 | const int r = (int)min(max( | |
160 | offsets.first, offsets.second), (float)pp.width()); | |
161 | ||
162 | p.drawRect(l, pp.top(), r - l, pp.height()); | |
163 | } | |
164 | ||
165 | QString CursorPair::format_string() | |
166 | { | |
167 | const pv::util::SIPrefix prefix = view_.tick_prefix(); | |
168 | const pv::util::Timestamp diff = abs(second_->time() - first_->time()); | |
169 | ||
170 | const QString s1 = Ruler::format_time_with_distance( | |
171 | diff, diff, prefix, view_.time_unit(), view_.tick_precision(), false); | |
172 | const QString s2 = util::format_time_si( | |
173 | 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false); | |
174 | ||
175 | return QString("%1 / %2").arg(s1).arg(s2); | |
176 | } | |
177 | ||
178 | void CursorPair::compute_text_size(QPainter &p) | |
179 | { | |
180 | assert(first_); | |
181 | assert(second_); | |
182 | ||
183 | text_size_ = p.boundingRect(QRectF(), 0, format_string()).size(); | |
184 | } | |
185 | ||
186 | pair<float, float> CursorPair::get_cursor_offsets() const | |
187 | { | |
188 | assert(first_); | |
189 | assert(second_); | |
190 | ||
191 | return pair<float, float>( | |
192 | ((first_->time() - view_.offset()) / view_.scale()).convert_to<float>(), | |
193 | ((second_->time() - view_.offset()) / view_.scale()).convert_to<float>()); | |
194 | } | |
195 | ||
196 | } // namespace TraceView | |
197 | } // namespace views | |
198 | } // namespace pv |