]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
Signal: Save and load signal names as UTF-8 strings.
[pulseview.git] / pv / view / ruler.cpp
CommitLineData
ccdd3ef5 1/*
b3f22de0 2 * This file is part of the PulseView project.
ccdd3ef5
JH
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
2acdb232 21#include "ruler.hpp"
ca4ec3ea 22
2acdb232
JH
23#include "view.hpp"
24#include "pv/util.hpp"
ccdd3ef5 25
cef18fc6 26#include <extdef.h>
ccdd3ef5 27
819f4c25 28using namespace Qt;
f76af637 29
ccdd3ef5
JH
30namespace pv {
31namespace view {
32
a6c1726e 33const int Ruler::RulerHeight = 30;
ccdd3ef5 34const int Ruler::MinorTickSubdivision = 4;
ccdd3ef5 35
b3a7c013
JH
36const int Ruler::HoverArrowSize = 5;
37
ccdd3ef5 38Ruler::Ruler(View &parent) :
84a0d458 39 MarginWidget(parent)
ccdd3ef5 40{
b3a7c013
JH
41 setMouseTracking(true);
42
8dbbc7f0 43 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 44 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
45}
46
a6c1726e
JH
47QSize Ruler::sizeHint() const
48{
49 return QSize(0, RulerHeight);
50}
51
e314eca4 52void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 53{
f260e3bf 54 const int ValueMargin = 3;
3f509c1f 55
ccdd3ef5 56 QPainter p(this);
ce6e73a8 57 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 58
361c560e
JH
59 const double tick_period = view_.tick_period();
60 const unsigned int prefix = view_.tick_prefix();
1b5813fe 61
ccdd3ef5 62 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
3af62a24 63 AlignLeft | AlignTop, "8").height();
ccdd3ef5
JH
64
65 // Draw the tick marks
d2caed8d 66 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
67
68 const double minor_tick_period = tick_period / MinorTickSubdivision;
69 const double first_major_division =
8dbbc7f0 70 floor(view_.offset() / tick_period);
ccdd3ef5 71 const double first_minor_division =
8dbbc7f0 72 ceil(view_.offset() / minor_tick_period);
ccdd3ef5
JH
73 const double t0 = first_major_division * tick_period;
74
75 int division = (int)round(first_minor_division -
64b678ba 76 first_major_division * MinorTickSubdivision) - 1;
f260e3bf
JH
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
64b678ba 82 double x;
ccdd3ef5 83
64b678ba
JH
84 do {
85 const double t = t0 + division * minor_tick_period;
8dbbc7f0 86 x = (t - view_.offset()) / view_.scale();
ccdd3ef5 87
333d5bbc 88 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
89 {
90 // Draw a major tick
f260e3bf
JH
91 p.drawText(x, ValueMargin, 0, text_height,
92 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 93 pv::util::format_time(t, prefix));
f260e3bf
JH
94 p.drawLine(QPointF(x, major_tick_y1),
95 QPointF(x, tick_y2));
ccdd3ef5
JH
96 }
97 else
98 {
99 // Draw a minor tick
f260e3bf
JH
100 p.drawLine(QPointF(x, minor_tick_y1),
101 QPointF(x, tick_y2));
ccdd3ef5
JH
102 }
103
104 division++;
64b678ba
JH
105
106 } while (x < width());
ccdd3ef5 107
b3a7c013
JH
108 // Draw the hover mark
109 draw_hover_mark(p);
ca4ec3ea
JH
110}
111
b3a7c013
JH
112void Ruler::draw_hover_mark(QPainter &p)
113{
8dbbc7f0 114 const int x = view_.hover_point().x();
333d5bbc 115
84a0d458 116 if (x == -1)
b3a7c013
JH
117 return;
118
119 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 120 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013
JH
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
131void Ruler::hover_point_changed()
132{
133 update();
134}
135
ccdd3ef5
JH
136} // namespace view
137} // namespace pv