]>
Commit | Line | Data |
---|---|---|
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 "view.h" | |
24 | #include "pv/util.h" | |
25 | ||
26 | #include <extdef.h> | |
27 | ||
28 | using namespace Qt; | |
29 | ||
30 | namespace pv { | |
31 | namespace view { | |
32 | ||
33 | const int Ruler::RulerHeight = 30; | |
34 | const int Ruler::MinorTickSubdivision = 4; | |
35 | const int Ruler::ScaleUnits[3] = {1, 2, 5}; | |
36 | ||
37 | const int Ruler::HoverArrowSize = 5; | |
38 | ||
39 | Ruler::Ruler(View &parent) : | |
40 | MarginWidget(parent) | |
41 | { | |
42 | setMouseTracking(true); | |
43 | ||
44 | connect(&_view, SIGNAL(hover_point_changed()), | |
45 | this, SLOT(hover_point_changed())); | |
46 | } | |
47 | ||
48 | QSize Ruler::sizeHint() const | |
49 | { | |
50 | return QSize(0, RulerHeight); | |
51 | } | |
52 | ||
53 | void Ruler::paintEvent(QPaintEvent*) | |
54 | { | |
55 | const double SpacingIncrement = 32.0f; | |
56 | const double MinValueSpacing = 32.0f; | |
57 | const int ValueMargin = 3; | |
58 | ||
59 | QPainter p(this); | |
60 | p.setRenderHint(QPainter::Antialiasing); | |
61 | ||
62 | double min_width = SpacingIncrement, typical_width; | |
63 | double tick_period; | |
64 | unsigned int prefix; | |
65 | ||
66 | // Find tick spacing, and number formatting that does not cause | |
67 | // value to collide. | |
68 | do | |
69 | { | |
70 | const double min_period = _view.scale() * min_width; | |
71 | ||
72 | const int order = (int)floorf(log10f(min_period)); | |
73 | const double order_decimal = pow(10.0, order); | |
74 | ||
75 | unsigned int unit = 0; | |
76 | ||
77 | do | |
78 | { | |
79 | tick_period = order_decimal * ScaleUnits[unit++]; | |
80 | } while (tick_period < min_period && unit < countof(ScaleUnits)); | |
81 | ||
82 | prefix = (order - pv::util::FirstSIPrefixPower) / 3; | |
83 | ||
84 | typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX, | |
85 | AlignLeft | AlignTop, pv::util::format_time(_view.offset(), | |
86 | prefix)).width() + MinValueSpacing; | |
87 | ||
88 | min_width += SpacingIncrement; | |
89 | ||
90 | } while(typical_width > tick_period / _view.scale()); | |
91 | ||
92 | const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, | |
93 | AlignLeft | AlignTop, "8").height(); | |
94 | ||
95 | // Draw the tick marks | |
96 | p.setPen(palette().color(foregroundRole())); | |
97 | ||
98 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
99 | const double first_major_division = | |
100 | floor(_view.offset() / tick_period); | |
101 | const double first_minor_division = | |
102 | ceil(_view.offset() / minor_tick_period); | |
103 | const double t0 = first_major_division * tick_period; | |
104 | ||
105 | int division = (int)round(first_minor_division - | |
106 | first_major_division * MinorTickSubdivision) - 1; | |
107 | ||
108 | const int major_tick_y1 = text_height + ValueMargin * 2; | |
109 | const int tick_y2 = height(); | |
110 | const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2; | |
111 | ||
112 | double x; | |
113 | ||
114 | do { | |
115 | const double t = t0 + division * minor_tick_period; | |
116 | x = (t - _view.offset()) / _view.scale(); | |
117 | ||
118 | if (division % MinorTickSubdivision == 0) | |
119 | { | |
120 | // Draw a major tick | |
121 | p.drawText(x, ValueMargin, 0, text_height, | |
122 | AlignCenter | AlignTop | TextDontClip, | |
123 | pv::util::format_time(t, prefix)); | |
124 | p.drawLine(QPointF(x, major_tick_y1), | |
125 | QPointF(x, tick_y2)); | |
126 | } | |
127 | else | |
128 | { | |
129 | // Draw a minor tick | |
130 | p.drawLine(QPointF(x, minor_tick_y1), | |
131 | QPointF(x, tick_y2)); | |
132 | } | |
133 | ||
134 | division++; | |
135 | ||
136 | } while (x < width()); | |
137 | ||
138 | // Draw the hover mark | |
139 | draw_hover_mark(p); | |
140 | } | |
141 | ||
142 | void Ruler::draw_hover_mark(QPainter &p) | |
143 | { | |
144 | const int x = _view.hover_point().x(); | |
145 | ||
146 | if (x == -1) | |
147 | return; | |
148 | ||
149 | p.setPen(QPen(Qt::NoPen)); | |
150 | p.setBrush(QBrush(palette().color(foregroundRole()))); | |
151 | ||
152 | const int b = height() - 1; | |
153 | const QPointF points[] = { | |
154 | QPointF(x, b), | |
155 | QPointF(x - HoverArrowSize, b - HoverArrowSize), | |
156 | QPointF(x + HoverArrowSize, b - HoverArrowSize) | |
157 | }; | |
158 | p.drawPolygon(points, countof(points)); | |
159 | } | |
160 | ||
161 | void Ruler::hover_point_changed() | |
162 | { | |
163 | update(); | |
164 | } | |
165 | ||
166 | } // namespace view | |
167 | } // namespace pv |