]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/ruler.hpp
Fix #1222 by adding a tooltip for when there isn't enough space
[pulseview.git] / pv / views / trace / ruler.hpp
... / ...
CommitLineData
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
31using std::function;
32using std::pair;
33using std::shared_ptr;
34using std::vector;
35
36namespace RulerTest {
37struct tick_position_test_0;
38struct tick_position_test_1;
39struct tick_position_test_2;
40}
41
42namespace pv {
43namespace views {
44namespace trace {
45
46class TimeItem;
47class ViewItem;
48
49struct TickPositions
50{
51 vector<pair<double, QString>> major;
52 vector<double> minor;
53};
54
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 */
60class Ruler : public MarginWidget
61{
62 Q_OBJECT
63
64 friend struct RulerTest::tick_position_test_0;
65 friend struct RulerTest::tick_position_test_1;
66 friend struct RulerTest::tick_position_test_2;
67
68private:
69 /// Height of the ruler in multipes of the text height
70 static const float RulerHeight;
71
72 /// Height of the hover arrow in multiples of the text height
73 static const float HoverArrowSize;
74
75public:
76 Ruler(View &parent);
77
78 QSize sizeHint() const override;
79
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 */
85 QSize extended_size_hint() const override;
86
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,
118 bool sign = true);
119
120 pv::util::Timestamp get_time_from_x_pos(uint32_t x) const;
121
122protected:
123 virtual void contextMenuEvent(QContextMenuEvent *event) override;
124 void resizeEvent(QResizeEvent*) override;
125
126private:
127 /**
128 * Gets the time items.
129 */
130 vector< shared_ptr<ViewItem> > items() override;
131
132 /**
133 * Gets the first view item which has a label that contains @c pt .
134 * @param pt the point to search with.
135 * @return the view item that has been found, or and empty
136 * @c shared_ptr if no item was found.
137 */
138 shared_ptr<ViewItem> get_mouse_over_item(const QPoint &pt) override;
139
140 void mouseDoubleClickEvent(QMouseEvent *event) override;
141
142 void paintEvent(QPaintEvent *event) override;
143
144 /**
145 * Draw a hover arrow under the cursor position.
146 * @param p The painter to draw into.
147 * @param text_height The height of a single text ascent.
148 */
149 void draw_hover_mark(QPainter &p, int text_height);
150
151 int calculate_text_height() const;
152
153 /**
154 * Calculates the major and minor tick positions.
155 *
156 * @param major_period The period between the major ticks.
157 * @param offset The virtual time at the left border of the ruler.
158 * @param scale The scale in seconds per pixel.
159 * @param width the Width of the ruler.
160 * @param format_function A function used to format the major tick times.
161 * @return An object of type 'TickPositions' that contains the major tick
162 * positions together with the labels at that ticks, and the minor
163 * tick positions.
164 */
165 static TickPositions calculate_tick_positions(
166 const pv::util::Timestamp& major_period,
167 const pv::util::Timestamp& offset,
168 const double scale,
169 const int width,
170 const unsigned int minor_tick_count,
171 function<QString(const pv::util::Timestamp&)> format_function);
172
173private Q_SLOTS:
174 void on_hover_point_changed(const QWidget* widget, const QPoint &hp);
175
176 void invalidate_tick_position_cache();
177
178 void on_createMarker();
179 void on_toggleHoverMarker();
180
181private:
182 /**
183 * Holds the tick positions so that they don't have to be recalculated on
184 * every redraw. Set by 'paintEvent()' when needed.
185 */
186 boost::optional<TickPositions> tick_position_cache_;
187
188 uint32_t context_menu_x_pos_;
189};
190
191} // namespace trace
192} // namespace views
193} // namespace pv
194
195#endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_RULER_HPP