]> sigrok.org Git - pulseview.git/blob - pv/view/ruler.cpp
Made TimeMarker and Cursor QObjects
[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 #include "view.h"
23 #include "viewport.h"
24
25 #include <extdef.h>
26
27 #include <assert.h>
28 #include <math.h>
29
30 #include <QMouseEvent>
31 #include <QPainter>
32 #include <QTextStream>
33
34 using namespace std;
35
36 namespace pv {
37 namespace view {
38
39 const int Ruler::MinorTickSubdivision = 4;
40 const int Ruler::ScaleUnits[3] = {1, 2, 5};
41
42 const QString Ruler::SIPrefixes[9] =
43         {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
44 const int Ruler::FirstSIPrefixPower = -15;
45
46 const int Ruler::HoverArrowSize = 5;
47
48 Ruler::Ruler(View &parent) :
49         QWidget(&parent),
50         _view(parent)
51 {
52         setMouseTracking(true);
53
54         connect(&_view, SIGNAL(hover_point_changed()),
55                 this, SLOT(hover_point_changed()));
56 }
57
58 void Ruler::paintEvent(QPaintEvent *event)
59 {
60         QPainter p(this);
61         p.setRenderHint(QPainter::Antialiasing);
62
63         const double MinSpacing = 80;
64
65         const double min_period = _view.scale() * MinSpacing;
66
67         const int order = (int)floorf(log10f(min_period));
68         const double order_decimal = pow(10, order);
69
70         unsigned int unit = 0;
71         double tick_period = 0.0f;
72
73         do
74         {
75                 tick_period = order_decimal * ScaleUnits[unit++];
76         } while(tick_period < min_period && unit < countof(ScaleUnits));
77
78         const unsigned int prefix = (order - FirstSIPrefixPower) / 3;
79         assert(prefix >= 0);
80         assert(prefix < countof(SIPrefixes));
81
82         const double multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower);
83
84         const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
85                 Qt::AlignLeft | Qt::AlignTop, "8").height();
86
87         // Draw the tick marks
88         p.setPen(Qt::black);
89
90         const double minor_tick_period = tick_period / MinorTickSubdivision;
91         const double first_major_division =
92                 floor(_view.offset() / tick_period);
93         const double first_minor_division =
94                 ceil(_view.offset() / minor_tick_period);
95         const double t0 = first_major_division * tick_period;
96
97         int division = (int)round(first_minor_division -
98                 first_major_division * MinorTickSubdivision);
99         while(1)
100         {
101                 const double t = t0 + division * minor_tick_period;
102                 const double x = (t - _view.offset()) / _view.scale();
103
104                 if(x >= width())
105                         break;
106
107                 if(division % MinorTickSubdivision == 0)
108                 {
109                         // Draw a major tick
110                         QString s;
111                         QTextStream ts(&s);
112                         ts << (t * multiplier) << SIPrefixes[prefix] << "s";
113                         p.drawText(x, 0, 0, text_height, Qt::AlignCenter |
114                                 Qt::AlignTop | Qt::TextDontClip, s);
115                         p.drawLine(QPointF(x, text_height),
116                                 QPointF(x, height()));
117                 }
118                 else
119                 {
120                         // Draw a minor tick
121                         p.drawLine(QPointF(x, (text_height + height()) / 2),
122                                 QPointF(x, height()));
123                 }
124
125                 division++;
126         }
127
128         // Draw the cursors
129         draw_cursors(p);
130
131         // Draw the hover mark
132         draw_hover_mark(p);
133
134         p.end();
135 }
136
137 void Ruler::draw_cursors(QPainter &p)
138 {
139         if(!_view.cursors_shown())
140                 return;
141
142         const QRect r = rect();
143         pair<Cursor, Cursor> &cursors = _view.cursors();
144         cursors.first.paint_label(p, r);
145         cursors.second.paint_label(p, r);
146 }
147
148 void Ruler::draw_hover_mark(QPainter &p)
149 {
150         const int x = _view.hover_point().x();
151         if(x == -1)
152                 return;
153
154         p.setPen(QPen(Qt::NoPen));
155         p.setBrush(QBrush(QColor(Qt::black)));
156
157         const int b = height() - 1;
158         const QPointF points[] = {
159                 QPointF(x, b),
160                 QPointF(x - HoverArrowSize, b - HoverArrowSize),
161                 QPointF(x + HoverArrowSize, b - HoverArrowSize)
162         };
163         p.drawPolygon(points, countof(points));
164 }
165
166 void Ruler::hover_point_changed()
167 {
168         update();
169 }
170
171 } // namespace view
172 } // namespace pv