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