]> sigrok.org Git - pulseview.git/blob - pv/view/ruler.hpp
Change namespace for the trace view and implement ViewBase
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP
22 #define PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP
23
24 #include <functional>
25 #include <memory>
26
27 #include <boost/optional.hpp>
28
29 #include "marginwidget.hpp"
30 #include <pv/util.hpp>
31
32 namespace RulerTest {
33 struct tick_position_test_0;
34 struct tick_position_test_1;
35 struct tick_position_test_2;
36 }
37
38 namespace pv {
39 namespace views {
40 namespace TraceView {
41
42 class TimeItem;
43 class ViewItem;
44
45 class Ruler : public MarginWidget
46 {
47         Q_OBJECT
48
49         friend struct RulerTest::tick_position_test_0;
50         friend struct RulerTest::tick_position_test_1;
51         friend struct RulerTest::tick_position_test_2;
52
53 private:
54
55         /// Height of the ruler in multipes of the text height
56         static const float RulerHeight;
57
58         static const int MinorTickSubdivision;
59
60         /// Height of the hover arrow in multiples of the text height
61         static const float HoverArrowSize;
62
63 public:
64         Ruler(View &parent);
65
66 public:
67         QSize sizeHint() const override;
68
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          */
74         QSize extended_size_hint() const override;
75
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
109 private:
110         /**
111          * Gets the time items.
112          */
113         std::vector< std::shared_ptr<ViewItem> > items() override;
114
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          */
121         std::shared_ptr<ViewItem> get_mouse_over_item(
122                 const QPoint &pt) override;
123
124         void paintEvent(QPaintEvent *event) override;
125
126         void mouseDoubleClickEvent(QMouseEvent *event) override;
127
128         /**
129          * Draw a hover arrow under the cursor position.
130          * @param p The painter to draw into.
131          * @param text_height The height of a single text ascent.
132          */
133         void draw_hover_mark(QPainter &p, int text_height);
134
135         int calculate_text_height() const;
136
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(
162                 const pv::util::Timestamp& major_period,
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
168 protected:
169         void resizeEvent(QResizeEvent*) override;
170
171 private Q_SLOTS:
172         void hover_point_changed();
173
174         // Resets the 'tick_position_cache_'.
175         void invalidate_tick_position_cache();
176 };
177
178 } // namespace TraceView
179 } // namespace views
180 } // namespace pv
181
182 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP