]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/ruler.hpp
Typo fix: treshold -> threshold
[pulseview.git] / pv / views / trace / ruler.hpp
... / ...
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#ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP
21#define PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP
22
23#include <functional>
24#include <memory>
25
26#include <boost/optional.hpp>
27
28#include "marginwidget.hpp"
29#include <pv/util.hpp>
30
31using std::function;
32using std::pair;
33using std::shared_ptr;
34using std::vector;
35
36namespace RulerTest {
37struct tick_position_test_0;
38struct tick_position_test_1;
39struct tick_position_test_2;
40}
41
42namespace pv {
43namespace views {
44namespace trace {
45
46class TimeItem;
47class ViewItem;
48
49/**
50 * The Ruler class manages and displays the time scale above the trace canvas.
51 * It may also contain @ref TimeItem instances used to identify or highlight
52 * time-related information.
53 */
54class Ruler : public MarginWidget
55{
56 Q_OBJECT
57
58 friend struct RulerTest::tick_position_test_0;
59 friend struct RulerTest::tick_position_test_1;
60 friend struct RulerTest::tick_position_test_2;
61
62private:
63 /// Height of the ruler in multipes of the text height
64 static const float RulerHeight;
65
66 static const int MinorTickSubdivision;
67
68 /// Height of the hover arrow in multiples of the text height
69 static const float HoverArrowSize;
70
71public:
72 Ruler(View &parent);
73
74public:
75 QSize sizeHint() const override;
76
77 /**
78 * The extended area that the header widget would like to be sized to.
79 * @remarks This area is the area specified by sizeHint, extended by
80 * the area to overlap the viewport.
81 */
82 QSize extended_size_hint() const override;
83
84 /**
85 * Formats a timestamp depending on its distance to another timestamp.
86 *
87 * Heuristic function, useful when multiple timestamps should be put side by
88 * side. The function procedes in the following order:
89 * - If 't' is zero, "0" is returned.
90 * - If 'unit' is 'TimeUnit::Samples', 'pv::util::format_time_si_adjusted()'
91 * is used to format 't'.
92 * - If a zoomed out view is detected (determined by 'precision' and
93 * 'distance'), 'pv::util::format_time_minutes() is used.
94 * - For timestamps "near the origin" (determined by 'distance'),
95 * 'pv::util::format_time_si_adjusted()' is used.
96 * - If none of the previous was true, 'pv::util::format_time_minutes()'
97 * is used again.
98 *
99 * @param distance The distance between the timestamp to format and
100 * an adjacent one.
101 * @param t The value to format
102 * @param prefix The SI prefix to use.
103 * @param unit The representation of the timestamp value.
104 * @param precision The number of digits after the decimal separator.
105 * @param sign Whether or not to add a sign also for positive numbers.
106 *
107 * @return The formated value.
108 */
109 static QString format_time_with_distance(
110 const pv::util::Timestamp& distance,
111 const pv::util::Timestamp& t,
112 pv::util::SIPrefix prefix = pv::util::SIPrefix::unspecified,
113 pv::util::TimeUnit unit = pv::util::TimeUnit::Time,
114 unsigned precision = 0,
115 bool sign = true);
116
117private:
118 /**
119 * Gets the time items.
120 */
121 vector< shared_ptr<ViewItem> > items() override;
122
123 /**
124 * Gets the first view item which has a label that contains @c pt .
125 * @param pt the point to search with.
126 * @return the view item that has been found, or and empty
127 * @c shared_ptr if no item was found.
128 */
129 shared_ptr<ViewItem> get_mouse_over_item(const QPoint &pt) override;
130
131 void paintEvent(QPaintEvent *event) override;
132
133 void mouseDoubleClickEvent(QMouseEvent *event) override;
134
135 /**
136 * Draw a hover arrow under the cursor position.
137 * @param p The painter to draw into.
138 * @param text_height The height of a single text ascent.
139 */
140 void draw_hover_mark(QPainter &p, int text_height);
141
142 int calculate_text_height() const;
143
144 struct TickPositions
145 {
146 vector<pair<double, QString>> major;
147 vector<double> minor;
148 };
149
150 /**
151 * Holds the tick positions so that they don't have to be recalculated on
152 * every redraw. Set by 'paintEvent()' when needed.
153 */
154 boost::optional<TickPositions> tick_position_cache_;
155
156 /**
157 * Calculates the major and minor tick positions.
158 *
159 * @param major_period The period between the major ticks.
160 * @param offset The time at the left border of the ruler.
161 * @param scale The scale in seconds per pixel.
162 * @param width the Width of the ruler.
163 * @param format_function A function used to format the major tick times.
164 * @return An object of type 'TickPositions' that contains the major tick
165 * positions together with the labels at that ticks, and the minor
166 * tick positions.
167 */
168 static TickPositions calculate_tick_positions(
169 const pv::util::Timestamp& major_period,
170 const pv::util::Timestamp& offset,
171 const double scale,
172 const int width,
173 function<QString(const pv::util::Timestamp&)> format_function);
174
175protected:
176 void resizeEvent(QResizeEvent*) override;
177
178private Q_SLOTS:
179 void hover_point_changed(const QPoint &hp);
180
181 // Resets the 'tick_position_cache_'.
182 void invalidate_tick_position_cache();
183};
184
185} // namespace trace
186} // namespace views
187} // namespace pv
188
189#endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP