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