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