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