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