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