]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
View: Do not emit superfluous signals in a loop
[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
819e2e95 21#include <extdef.h>
ca4ec3ea 22
819e2e95
JH
23#include <QApplication>
24#include <QFontMetrics>
25#include <QMouseEvent>
26
27#include "ruler.hpp"
2acdb232 28#include "view.hpp"
ccdd3ef5 29
819f4c25 30using namespace Qt;
f76af637 31
819e2e95
JH
32using std::shared_ptr;
33using std::vector;
34
ccdd3ef5
JH
35namespace pv {
36namespace view {
37
249229ec 38const float Ruler::RulerHeight = 2.5f; // x Text Height
ccdd3ef5 39const int Ruler::MinorTickSubdivision = 4;
ccdd3ef5 40
415341a1 41const float Ruler::HoverArrowSize = 0.5f; // x Text Height
b3a7c013 42
ccdd3ef5 43Ruler::Ruler(View &parent) :
e456e8e1 44 MarginWidget(parent)
ccdd3ef5 45{
b3a7c013
JH
46 setMouseTracking(true);
47
8dbbc7f0 48 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 49 this, SLOT(hover_point_changed()));
4b0af0b6
JS
50 connect(&view_, SIGNAL(offset_changed()),
51 this, SLOT(invalidate_tick_position_cache()));
52 connect(&view_, SIGNAL(scale_changed()),
53 this, SLOT(invalidate_tick_position_cache()));
54 connect(&view_, SIGNAL(tick_prefix_changed()),
55 this, SLOT(invalidate_tick_position_cache()));
56 connect(&view_, SIGNAL(tick_precision_changed()),
57 this, SLOT(invalidate_tick_position_cache()));
58 connect(&view_, SIGNAL(tick_period_changed()),
59 this, SLOT(invalidate_tick_position_cache()));
60 connect(&view_, SIGNAL(time_unit_changed()),
61 this, SLOT(invalidate_tick_position_cache()));
ccdd3ef5
JH
62}
63
a6c1726e
JH
64QSize Ruler::sizeHint() const
65{
249229ec
JH
66 const int text_height = calculate_text_height();
67 return QSize(0, RulerHeight * text_height);
a6c1726e
JH
68}
69
819e2e95
JH
70QSize Ruler::extended_size_hint() const
71{
f5b833c6
JH
72 QRectF max_rect;
73 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
74 for (auto &i : items)
75 max_rect = max_rect.united(i->label_rect(QRect()));
76 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
77 ViewItem::HighlightRadius);
819e2e95
JH
78}
79
3e124bee
JH
80vector< shared_ptr<ViewItem> > Ruler::items()
81{
82 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
83 return vector< shared_ptr<ViewItem> >(
84 time_items.begin(), time_items.end());
85}
86
6871ee9f 87shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
25a0d47c
JH
88{
89 const vector< shared_ptr<TimeItem> > items(view_.time_items());
90 for (auto i = items.rbegin(); i != items.rend(); i++)
91 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
92 return *i;
93 return nullptr;
94}
95
e314eca4 96void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 97{
4b0af0b6
JS
98 if (!tick_position_cache_) {
99 auto ffunc = [this](const pv::util::Timestamp& t)
100 {
101 return util::format_time(
102 t,
103 this->view_.tick_prefix(),
104 this->view_.time_unit(),
105 this->view_.tick_precision());
106 };
107
108 tick_position_cache_.emplace(calculate_tick_positions(
109 view_.tick_period(),
110 view_.offset(),
111 view_.scale(),
112 width(),
113 ffunc));
114 }
ccdd3ef5 115
4b0af0b6 116 const int ValueMargin = 3;
f260e3bf 117
e456e8e1 118 const int text_height = calculate_text_height();
249229ec 119 const int ruler_height = RulerHeight * text_height;
e456e8e1 120 const int major_tick_y1 = text_height + ValueMargin * 2;
249229ec 121 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
f260e3bf 122
4b0af0b6
JS
123 QPainter p(this);
124 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 125
4b0af0b6
JS
126 // Draw the tick marks
127 p.setPen(palette().color(foregroundRole()));
ccdd3ef5 128
4b0af0b6
JS
129 for (const auto& tick: tick_position_cache_->major) {
130 p.drawText(tick.first, ValueMargin, 0, text_height,
131 AlignCenter | AlignTop | TextDontClip, tick.second);
132 p.drawLine(QPointF(tick.first, major_tick_y1),
133 QPointF(tick.first, ruler_height));
134 }
ccdd3ef5 135
4b0af0b6
JS
136 for (const auto& tick: tick_position_cache_->minor) {
137 p.drawLine(QPointF(tick, minor_tick_y1),
138 QPointF(tick, ruler_height));
139 }
ccdd3ef5 140
b3a7c013 141 // Draw the hover mark
249229ec 142 draw_hover_mark(p, text_height);
819e2e95
JH
143
144 // The cursor labels are not drawn with the arrows exactly on the
145 // bottom line of the widget, because then the selection shadow
146 // would be clipped away.
09d5df11 147 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
819e2e95
JH
148
149 // Draw the items
150 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c 151 for (auto &i : items) {
803cdac4 152 const bool highlight = !item_dragging_ &&
49028d6c
JH
153 i->label_rect(r).contains(mouse_point_);
154 i->paint_label(p, r, highlight);
155 }
819e2e95
JH
156}
157
4b0af0b6 158Ruler::TickPositions Ruler::calculate_tick_positions(
c677193d 159 const pv::util::Timestamp& major_period,
4b0af0b6
JS
160 const pv::util::Timestamp& offset,
161 const double scale,
162 const int width,
163 std::function<QString(const pv::util::Timestamp&)> format_function)
164{
165 TickPositions tp;
166
c677193d
JS
167 const double minor_period =
168 (major_period / MinorTickSubdivision).convert_to<double>();
4b0af0b6
JS
169 const pv::util::Timestamp first_major_division = floor(offset / major_period);
170 const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
171 const pv::util::Timestamp t0 = first_major_division * major_period;
172
173 int division = (round(first_minor_division -
174 first_major_division * MinorTickSubdivision)).convert_to<int>() - 1;
175
176 double x;
177
178 do {
c677193d 179 pv::util::Timestamp t = t0 + division * minor_period;
4b0af0b6
JS
180 x = ((t - offset) / scale).convert_to<double>();
181
182 if (division % MinorTickSubdivision == 0) {
c677193d
JS
183 // Recalculate 't' without using 'minor_period' which is of type double.
184 t = t0 + division / MinorTickSubdivision * major_period;
4b0af0b6
JS
185 tp.major.emplace_back(x, format_function(t));
186 } else {
187 tp.minor.emplace_back(x);
188 }
189
190 division++;
191 } while (x < width);
192
193 return tp;
194}
195
819e2e95
JH
196void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
197{
198 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
199}
200
249229ec 201void Ruler::draw_hover_mark(QPainter &p, int text_height)
b3a7c013 202{
8dbbc7f0 203 const int x = view_.hover_point().x();
333d5bbc 204
84a0d458 205 if (x == -1)
b3a7c013
JH
206 return;
207
208 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 209 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013 210
249229ec 211 const int b = RulerHeight * text_height;
415341a1 212 const float hover_arrow_size = HoverArrowSize * text_height;
b3a7c013
JH
213 const QPointF points[] = {
214 QPointF(x, b),
415341a1
JH
215 QPointF(x - hover_arrow_size, b - hover_arrow_size),
216 QPointF(x + hover_arrow_size, b - hover_arrow_size)
b3a7c013
JH
217 };
218 p.drawPolygon(points, countof(points));
219}
220
e456e8e1 221int Ruler::calculate_text_height() const
819e2e95 222{
c0096500 223 return QFontMetrics(font()).ascent();
819e2e95
JH
224}
225
b3a7c013
JH
226void Ruler::hover_point_changed()
227{
228 update();
229}
230
4b0af0b6
JS
231void Ruler::invalidate_tick_position_cache()
232{
233 tick_position_cache_ = boost::none;
234}
235
236void Ruler::resizeEvent(QResizeEvent*)
237{
238 // the tick calculation depends on the width of this widget
239 invalidate_tick_position_cache();
240}
241
ccdd3ef5
JH
242} // namespace view
243} // namespace pv