]> sigrok.org Git - pulseview.git/blob - pv/view/ruler.cpp
Added pv::view::MarginWidget as a common base class of Header and Ruler
[pulseview.git] / pv / view / ruler.cpp
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 #include "ruler.h"
22
23 #include "cursor.h"
24 #include "view.h"
25 #include "viewport.h"
26
27 #include <extdef.h>
28
29 #include <assert.h>
30 #include <math.h>
31 #include <limits.h>
32
33 #include <QMouseEvent>
34 #include <QPainter>
35 #include <QTextStream>
36
37 using namespace std;
38
39 namespace pv {
40 namespace view {
41
42 const int Ruler::MinorTickSubdivision = 4;
43 const int Ruler::ScaleUnits[3] = {1, 2, 5};
44
45 const QString Ruler::SIPrefixes[9] =
46         {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
47 const int Ruler::FirstSIPrefixPower = -15;
48
49 const int Ruler::HoverArrowSize = 5;
50
51 Ruler::Ruler(View &parent) :
52         MarginWidget(parent),
53         _grabbed_marker(NULL)
54 {
55         setMouseTracking(true);
56
57         connect(&_view, SIGNAL(hover_point_changed()),
58                 this, SLOT(hover_point_changed()));
59 }
60
61 QString Ruler::format_time(double t, unsigned int prefix,
62         unsigned int precision)
63 {
64         const double multiplier = pow(10.0,
65                 - prefix * 3 - FirstSIPrefixPower);
66
67         QString s;
68         QTextStream ts(&s);
69         ts.setRealNumberPrecision(precision);
70         ts << fixed << forcesign << (t  * multiplier) <<
71                 SIPrefixes[prefix] << "s";
72         return s;
73 }
74
75 void Ruler::paintEvent(QPaintEvent*)
76 {
77         using namespace Qt;
78
79         const double SpacingIncrement = 32.0f;
80         const double MinValueSpacing = 32.0f;
81         const int ValueMargin = 3;
82
83         QPainter p(this);
84         p.setRenderHint(QPainter::Antialiasing);
85
86         double min_width = SpacingIncrement, typical_width;
87         double tick_period;
88         unsigned int prefix;
89
90         // Find tick spacing, and number formatting that does not cause
91         // value to collide.
92         do
93         {
94                 const double min_period = _view.scale() * min_width;
95
96                 const int order = (int)floorf(log10f(min_period));
97                 const double order_decimal = pow(10, order);
98
99                 unsigned int unit = 0;
100
101                 do
102                 {
103                         tick_period = order_decimal * ScaleUnits[unit++];
104                 } while (tick_period < min_period && unit < countof(ScaleUnits));
105
106                 prefix = (order - FirstSIPrefixPower) / 3;
107                 assert(prefix < countof(SIPrefixes));
108
109
110                 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
111                         AlignLeft | AlignTop, format_time(_view.offset(),
112                         prefix)).width() + MinValueSpacing;
113
114                 min_width += SpacingIncrement;
115
116         } while(typical_width > tick_period / _view.scale());
117
118         const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
119                 AlignLeft | AlignTop, "8").height();
120
121         // Draw the tick marks
122         p.setPen(palette().color(foregroundRole()));
123
124         const double minor_tick_period = tick_period / MinorTickSubdivision;
125         const double first_major_division =
126                 floor(_view.offset() / tick_period);
127         const double first_minor_division =
128                 ceil(_view.offset() / minor_tick_period);
129         const double t0 = first_major_division * tick_period;
130
131         int division = (int)round(first_minor_division -
132                 first_major_division * MinorTickSubdivision) - 1;
133
134         const int major_tick_y1 = text_height + ValueMargin * 2;
135         const int tick_y2 = height();
136         const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
137
138         double x;
139
140         do {
141                 const double t = t0 + division * minor_tick_period;
142                 x = (t - _view.offset()) / _view.scale();
143
144                 if (division % MinorTickSubdivision == 0)
145                 {
146                         // Draw a major tick
147                         p.drawText(x, ValueMargin, 0, text_height,
148                                 AlignCenter | AlignTop | TextDontClip,
149                                 format_time(t, prefix));
150                         p.drawLine(QPointF(x, major_tick_y1),
151                                 QPointF(x, tick_y2));
152                 }
153                 else
154                 {
155                         // Draw a minor tick
156                         p.drawLine(QPointF(x, minor_tick_y1),
157                                 QPointF(x, tick_y2));
158                 }
159
160                 division++;
161
162         } while (x < width());
163
164         // Draw the cursors
165         if (_view.cursors_shown())
166                 _view.cursors().draw_markers(p, rect(), prefix);
167
168         // Draw the hover mark
169         draw_hover_mark(p);
170
171         p.end();
172 }
173
174 void Ruler::mouseMoveEvent(QMouseEvent *e)
175 {
176         if (!_grabbed_marker)
177                 return;
178
179         _grabbed_marker->set_time(_view.offset() +
180                 ((double)e->x() + 0.5) * _view.scale());
181 }
182
183 void Ruler::mousePressEvent(QMouseEvent *e)
184 {
185         if (e->buttons() & Qt::LeftButton) {
186                 _grabbed_marker = NULL;
187
188                 if (_view.cursors_shown()) {
189                         CursorPair &cursors = _view.cursors();
190                         if (cursors.first().get_label_rect(
191                                 rect()).contains(e->pos()))
192                                 _grabbed_marker = &cursors.first();
193                         else if (cursors.second().get_label_rect(
194                                 rect()).contains(e->pos()))
195                                 _grabbed_marker = &cursors.second();
196                 }
197         }
198 }
199
200 void Ruler::mouseReleaseEvent(QMouseEvent *)
201 {
202         _grabbed_marker = NULL;
203 }
204
205 void Ruler::draw_hover_mark(QPainter &p)
206 {
207         const int x = _view.hover_point().x();
208
209         if (x == -1 || _grabbed_marker)
210                 return;
211
212         p.setPen(QPen(Qt::NoPen));
213         p.setBrush(QBrush(palette().color(foregroundRole())));
214
215         const int b = height() - 1;
216         const QPointF points[] = {
217                 QPointF(x, b),
218                 QPointF(x - HoverArrowSize, b - HoverArrowSize),
219                 QPointF(x + HoverArrowSize, b - HoverArrowSize)
220         };
221         p.drawPolygon(points, countof(points));
222 }
223
224 void Ruler::hover_point_changed()
225 {
226         update();
227 }
228
229 } // namespace view
230 } // namespace pv