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