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