]>
Commit | Line | Data |
---|---|---|
ccdd3ef5 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
ccdd3ef5 JH |
3 | * |
4 | * Copyright (C) 2012 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/>. |
ccdd3ef5 JH |
18 | */ |
19 | ||
819e2e95 | 20 | #include <extdef.h> |
ca4ec3ea | 21 | |
819e2e95 JH |
22 | #include <QApplication> |
23 | #include <QFontMetrics> | |
24 | #include <QMouseEvent> | |
25 | ||
26 | #include "ruler.hpp" | |
2acdb232 | 27 | #include "view.hpp" |
ccdd3ef5 | 28 | |
819f4c25 | 29 | using namespace Qt; |
f76af637 | 30 | |
6f925ba9 | 31 | using std::function; |
819e2e95 JH |
32 | using std::shared_ptr; |
33 | using std::vector; | |
34 | ||
ccdd3ef5 | 35 | namespace pv { |
f4e57597 SA |
36 | namespace views { |
37 | namespace TraceView { | |
ccdd3ef5 | 38 | |
3d79f521 | 39 | const float Ruler::RulerHeight = 2.5f; // x Text Height |
ccdd3ef5 | 40 | const int Ruler::MinorTickSubdivision = 4; |
ccdd3ef5 | 41 | |
3d79f521 | 42 | const float Ruler::HoverArrowSize = 0.5f; // x Text Height |
b3a7c013 | 43 | |
ccdd3ef5 | 44 | Ruler::Ruler(View &parent) : |
e456e8e1 | 45 | MarginWidget(parent) |
ccdd3ef5 | 46 | { |
b3a7c013 JH |
47 | setMouseTracking(true); |
48 | ||
8dbbc7f0 | 49 | connect(&view_, SIGNAL(hover_point_changed()), |
b3a7c013 | 50 | this, SLOT(hover_point_changed())); |
4b0af0b6 JS |
51 | connect(&view_, SIGNAL(offset_changed()), |
52 | this, SLOT(invalidate_tick_position_cache())); | |
53 | connect(&view_, SIGNAL(scale_changed()), | |
54 | this, SLOT(invalidate_tick_position_cache())); | |
55 | connect(&view_, SIGNAL(tick_prefix_changed()), | |
56 | this, SLOT(invalidate_tick_position_cache())); | |
57 | connect(&view_, SIGNAL(tick_precision_changed()), | |
58 | this, SLOT(invalidate_tick_position_cache())); | |
59 | connect(&view_, SIGNAL(tick_period_changed()), | |
60 | this, SLOT(invalidate_tick_position_cache())); | |
61 | connect(&view_, SIGNAL(time_unit_changed()), | |
62 | this, SLOT(invalidate_tick_position_cache())); | |
ccdd3ef5 JH |
63 | } |
64 | ||
a6c1726e JH |
65 | QSize Ruler::sizeHint() const |
66 | { | |
249229ec JH |
67 | const int text_height = calculate_text_height(); |
68 | return QSize(0, RulerHeight * text_height); | |
a6c1726e JH |
69 | } |
70 | ||
819e2e95 JH |
71 | QSize Ruler::extended_size_hint() const |
72 | { | |
f5b833c6 | 73 | QRectF max_rect; |
6f925ba9 | 74 | vector< shared_ptr<TimeItem> > items(view_.time_items()); |
f5b833c6 JH |
75 | for (auto &i : items) |
76 | max_rect = max_rect.united(i->label_rect(QRect())); | |
77 | return QSize(0, sizeHint().height() - max_rect.top() / 2 + | |
78 | ViewItem::HighlightRadius); | |
819e2e95 JH |
79 | } |
80 | ||
3ccf0f7f JS |
81 | QString Ruler::format_time_with_distance( |
82 | const pv::util::Timestamp& distance, | |
83 | const pv::util::Timestamp& t, | |
84 | pv::util::SIPrefix prefix, | |
85 | pv::util::TimeUnit unit, | |
86 | unsigned precision, | |
87 | bool sign) | |
88 | { | |
89 | const unsigned limit = 60; | |
90 | ||
91 | if (t.is_zero()) | |
92 | return "0"; | |
93 | ||
94 | // If we have to use samples then we have no alternative formats | |
95 | if (unit == pv::util::TimeUnit::Samples) | |
96 | return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign); | |
97 | ||
98 | // View zoomed way out -> low precision (0), big distance (>=60s) | |
99 | // -> DD:HH:MM | |
100 | if ((precision == 0) && (distance >= limit)) | |
101 | return pv::util::format_time_minutes(t, 0, sign); | |
102 | ||
103 | // View in "normal" range -> medium precision, medium step size | |
104 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
105 | // View zoomed way in -> high precision (>3), low step size (<1s) | |
106 | // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds | |
107 | if (abs(t) < limit) | |
108 | return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign); | |
109 | else | |
110 | return pv::util::format_time_minutes(t, precision, sign); | |
111 | } | |
112 | ||
3e124bee JH |
113 | vector< shared_ptr<ViewItem> > Ruler::items() |
114 | { | |
115 | const vector< shared_ptr<TimeItem> > time_items(view_.time_items()); | |
116 | return vector< shared_ptr<ViewItem> >( | |
117 | time_items.begin(), time_items.end()); | |
118 | } | |
119 | ||
6871ee9f | 120 | shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt) |
25a0d47c JH |
121 | { |
122 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
123 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
124 | if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt)) | |
125 | return *i; | |
126 | return nullptr; | |
127 | } | |
128 | ||
e314eca4 | 129 | void Ruler::paintEvent(QPaintEvent*) |
ccdd3ef5 | 130 | { |
4b0af0b6 | 131 | if (!tick_position_cache_) { |
2ad82c2e | 132 | auto ffunc = [this](const pv::util::Timestamp& t) { |
3ccf0f7f JS |
133 | return format_time_with_distance( |
134 | this->view_.tick_period(), | |
4b0af0b6 JS |
135 | t, |
136 | this->view_.tick_prefix(), | |
137 | this->view_.time_unit(), | |
138 | this->view_.tick_precision()); | |
139 | }; | |
140 | ||
7c6cbdda | 141 | tick_position_cache_ = calculate_tick_positions( |
4b0af0b6 JS |
142 | view_.tick_period(), |
143 | view_.offset(), | |
144 | view_.scale(), | |
145 | width(), | |
7c6cbdda | 146 | ffunc); |
4b0af0b6 | 147 | } |
ccdd3ef5 | 148 | |
4b0af0b6 | 149 | const int ValueMargin = 3; |
f260e3bf | 150 | |
e456e8e1 | 151 | const int text_height = calculate_text_height(); |
249229ec | 152 | const int ruler_height = RulerHeight * text_height; |
e456e8e1 | 153 | const int major_tick_y1 = text_height + ValueMargin * 2; |
249229ec | 154 | const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; |
f260e3bf | 155 | |
4b0af0b6 | 156 | QPainter p(this); |
ccdd3ef5 | 157 | |
4b0af0b6 JS |
158 | // Draw the tick marks |
159 | p.setPen(palette().color(foregroundRole())); | |
ccdd3ef5 | 160 | |
4b0af0b6 JS |
161 | for (const auto& tick: tick_position_cache_->major) { |
162 | p.drawText(tick.first, ValueMargin, 0, text_height, | |
163 | AlignCenter | AlignTop | TextDontClip, tick.second); | |
164 | p.drawLine(QPointF(tick.first, major_tick_y1), | |
165 | QPointF(tick.first, ruler_height)); | |
166 | } | |
ccdd3ef5 | 167 | |
4b0af0b6 JS |
168 | for (const auto& tick: tick_position_cache_->minor) { |
169 | p.drawLine(QPointF(tick, minor_tick_y1), | |
170 | QPointF(tick, ruler_height)); | |
171 | } | |
ccdd3ef5 | 172 | |
b3a7c013 | 173 | // Draw the hover mark |
249229ec | 174 | draw_hover_mark(p, text_height); |
819e2e95 | 175 | |
97f71253 JS |
176 | p.setRenderHint(QPainter::Antialiasing); |
177 | ||
819e2e95 JH |
178 | // The cursor labels are not drawn with the arrows exactly on the |
179 | // bottom line of the widget, because then the selection shadow | |
180 | // would be clipped away. | |
09d5df11 | 181 | const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius); |
819e2e95 JH |
182 | |
183 | // Draw the items | |
184 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
49028d6c | 185 | for (auto &i : items) { |
803cdac4 | 186 | const bool highlight = !item_dragging_ && |
49028d6c JH |
187 | i->label_rect(r).contains(mouse_point_); |
188 | i->paint_label(p, r, highlight); | |
189 | } | |
819e2e95 JH |
190 | } |
191 | ||
4b0af0b6 | 192 | Ruler::TickPositions Ruler::calculate_tick_positions( |
c677193d | 193 | const pv::util::Timestamp& major_period, |
4b0af0b6 JS |
194 | const pv::util::Timestamp& offset, |
195 | const double scale, | |
196 | const int width, | |
6f925ba9 | 197 | function<QString(const pv::util::Timestamp&)> format_function) |
4b0af0b6 JS |
198 | { |
199 | TickPositions tp; | |
200 | ||
2d141937 | 201 | const pv::util::Timestamp minor_period = major_period / MinorTickSubdivision; |
4b0af0b6 JS |
202 | const pv::util::Timestamp first_major_division = floor(offset / major_period); |
203 | const pv::util::Timestamp first_minor_division = ceil(offset / minor_period); | |
204 | const pv::util::Timestamp t0 = first_major_division * major_period; | |
205 | ||
206 | int division = (round(first_minor_division - | |
207 | first_major_division * MinorTickSubdivision)).convert_to<int>() - 1; | |
208 | ||
209 | double x; | |
210 | ||
211 | do { | |
c677193d | 212 | pv::util::Timestamp t = t0 + division * minor_period; |
4b0af0b6 JS |
213 | x = ((t - offset) / scale).convert_to<double>(); |
214 | ||
215 | if (division % MinorTickSubdivision == 0) { | |
2d141937 | 216 | // Recalculate 't' without using 'minor_period' which is a fraction |
c677193d | 217 | t = t0 + division / MinorTickSubdivision * major_period; |
4b0af0b6 JS |
218 | tp.major.emplace_back(x, format_function(t)); |
219 | } else { | |
220 | tp.minor.emplace_back(x); | |
221 | } | |
222 | ||
223 | division++; | |
224 | } while (x < width); | |
225 | ||
226 | return tp; | |
227 | } | |
228 | ||
d9ea9628 | 229 | void Ruler::mouseDoubleClickEvent(QMouseEvent *event) |
819e2e95 | 230 | { |
d9ea9628 | 231 | view_.add_flag(view_.offset() + ((double)event->x() + 0.5) * view_.scale()); |
819e2e95 JH |
232 | } |
233 | ||
249229ec | 234 | void Ruler::draw_hover_mark(QPainter &p, int text_height) |
b3a7c013 | 235 | { |
8dbbc7f0 | 236 | const int x = view_.hover_point().x(); |
333d5bbc | 237 | |
84a0d458 | 238 | if (x == -1) |
b3a7c013 JH |
239 | return; |
240 | ||
241 | p.setPen(QPen(Qt::NoPen)); | |
ad1d8e2b | 242 | p.setBrush(QBrush(palette().color(foregroundRole()))); |
b3a7c013 | 243 | |
249229ec | 244 | const int b = RulerHeight * text_height; |
415341a1 | 245 | const float hover_arrow_size = HoverArrowSize * text_height; |
b3a7c013 JH |
246 | const QPointF points[] = { |
247 | QPointF(x, b), | |
415341a1 JH |
248 | QPointF(x - hover_arrow_size, b - hover_arrow_size), |
249 | QPointF(x + hover_arrow_size, b - hover_arrow_size) | |
b3a7c013 JH |
250 | }; |
251 | p.drawPolygon(points, countof(points)); | |
252 | } | |
253 | ||
e456e8e1 | 254 | int Ruler::calculate_text_height() const |
819e2e95 | 255 | { |
c0096500 | 256 | return QFontMetrics(font()).ascent(); |
819e2e95 JH |
257 | } |
258 | ||
b3a7c013 JH |
259 | void Ruler::hover_point_changed() |
260 | { | |
261 | update(); | |
262 | } | |
263 | ||
4b0af0b6 JS |
264 | void Ruler::invalidate_tick_position_cache() |
265 | { | |
266 | tick_position_cache_ = boost::none; | |
267 | } | |
268 | ||
269 | void Ruler::resizeEvent(QResizeEvent*) | |
270 | { | |
271 | // the tick calculation depends on the width of this widget | |
272 | invalidate_tick_position_cache(); | |
273 | } | |
274 | ||
f4e57597 SA |
275 | } // namespace TraceView |
276 | } // namespace views | |
ccdd3ef5 | 277 | } // namespace pv |