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