]>
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.hpp" | |
22 | ||
23 | #include "view.hpp" | |
24 | #include "pv/util.hpp" | |
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 | ||
36 | const int Ruler::HoverArrowSize = 5; | |
37 | ||
38 | Ruler::Ruler(View &parent) : | |
39 | MarginWidget(parent) | |
40 | { | |
41 | setMouseTracking(true); | |
42 | ||
43 | connect(&view_, SIGNAL(hover_point_changed()), | |
44 | this, SLOT(hover_point_changed())); | |
45 | } | |
46 | ||
47 | QSize Ruler::sizeHint() const | |
48 | { | |
49 | return QSize(0, RulerHeight); | |
50 | } | |
51 | ||
52 | void Ruler::paintEvent(QPaintEvent*) | |
53 | { | |
54 | const int ValueMargin = 3; | |
55 | ||
56 | QPainter p(this); | |
57 | p.setRenderHint(QPainter::Antialiasing); | |
58 | ||
59 | const double tick_period = view_.tick_period(); | |
60 | const unsigned int prefix = view_.tick_prefix(); | |
61 | ||
62 | const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, | |
63 | AlignLeft | AlignTop, "8").height(); | |
64 | ||
65 | // Draw the tick marks | |
66 | p.setPen(palette().color(foregroundRole())); | |
67 | ||
68 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
69 | const double first_major_division = | |
70 | floor(view_.offset() / tick_period); | |
71 | const double first_minor_division = | |
72 | ceil(view_.offset() / minor_tick_period); | |
73 | const double t0 = first_major_division * tick_period; | |
74 | ||
75 | int division = (int)round(first_minor_division - | |
76 | first_major_division * MinorTickSubdivision) - 1; | |
77 | ||
78 | const int major_tick_y1 = text_height + ValueMargin * 2; | |
79 | const int tick_y2 = height(); | |
80 | const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2; | |
81 | ||
82 | double x; | |
83 | ||
84 | do { | |
85 | const double t = t0 + division * minor_tick_period; | |
86 | x = (t - view_.offset()) / view_.scale(); | |
87 | ||
88 | if (division % MinorTickSubdivision == 0) | |
89 | { | |
90 | // Draw a major tick | |
91 | p.drawText(x, ValueMargin, 0, text_height, | |
92 | AlignCenter | AlignTop | TextDontClip, | |
93 | pv::util::format_time(t, prefix)); | |
94 | p.drawLine(QPointF(x, major_tick_y1), | |
95 | QPointF(x, tick_y2)); | |
96 | } | |
97 | else | |
98 | { | |
99 | // Draw a minor tick | |
100 | p.drawLine(QPointF(x, minor_tick_y1), | |
101 | QPointF(x, tick_y2)); | |
102 | } | |
103 | ||
104 | division++; | |
105 | ||
106 | } while (x < width()); | |
107 | ||
108 | // Draw the hover mark | |
109 | draw_hover_mark(p); | |
110 | } | |
111 | ||
112 | void Ruler::draw_hover_mark(QPainter &p) | |
113 | { | |
114 | const int x = view_.hover_point().x(); | |
115 | ||
116 | if (x == -1) | |
117 | return; | |
118 | ||
119 | p.setPen(QPen(Qt::NoPen)); | |
120 | p.setBrush(QBrush(palette().color(foregroundRole()))); | |
121 | ||
122 | const int b = height() - 1; | |
123 | const QPointF points[] = { | |
124 | QPointF(x, b), | |
125 | QPointF(x - HoverArrowSize, b - HoverArrowSize), | |
126 | QPointF(x + HoverArrowSize, b - HoverArrowSize) | |
127 | }; | |
128 | p.drawPolygon(points, countof(points)); | |
129 | } | |
130 | ||
131 | void Ruler::hover_point_changed() | |
132 | { | |
133 | update(); | |
134 | } | |
135 | ||
136 | } // namespace view | |
137 | } // namespace pv |