]> sigrok.org Git - pulseview.git/blob - pv/view/ruler.cpp
Ruler: Factor out the calculation of the tick spacing
[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 <extdef.h>
22
23 #include <QApplication>
24 #include <QFontMetrics>
25 #include <QMouseEvent>
26
27 #include "ruler.hpp"
28 #include "view.hpp"
29
30 using namespace Qt;
31
32 using std::shared_ptr;
33 using std::vector;
34
35 namespace pv {
36 namespace view {
37
38 const float Ruler::RulerHeight = 2.5f;  // x Text Height
39 const int Ruler::MinorTickSubdivision = 4;
40
41 const float Ruler::HoverArrowSize = 0.5f;  // x Text Height
42
43 Ruler::Ruler(View &parent) :
44         MarginWidget(parent)
45 {
46         setMouseTracking(true);
47
48         connect(&view_, SIGNAL(hover_point_changed()),
49                 this, SLOT(hover_point_changed()));
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()));
62 }
63
64 QSize Ruler::sizeHint() const
65 {
66         const int text_height = calculate_text_height();
67         return QSize(0, RulerHeight * text_height);
68 }
69
70 QSize Ruler::extended_size_hint() const
71 {
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);
78 }
79
80 vector< 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
87 shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
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
96 void Ruler::paintEvent(QPaintEvent*)
97 {
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         }
115
116         const int ValueMargin = 3;
117
118         const int text_height = calculate_text_height();
119         const int ruler_height = RulerHeight * text_height;
120         const int major_tick_y1 = text_height + ValueMargin * 2;
121         const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
122
123         QPainter p(this);
124         p.setRenderHint(QPainter::Antialiasing);
125
126         // Draw the tick marks
127         p.setPen(palette().color(foregroundRole()));
128
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         }
135
136         for (const auto& tick: tick_position_cache_->minor) {
137                 p.drawLine(QPointF(tick, minor_tick_y1),
138                                 QPointF(tick, ruler_height));
139         }
140
141         // Draw the hover mark
142         draw_hover_mark(p, text_height);
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.
147         const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
148
149         // Draw the items
150         const vector< shared_ptr<TimeItem> > items(view_.time_items());
151         for (auto &i : items) {
152                 const bool highlight = !item_dragging_ &&
153                         i->label_rect(r).contains(mouse_point_);
154                 i->paint_label(p, r, highlight);
155         }
156 }
157
158 Ruler::TickPositions Ruler::calculate_tick_positions(
159         const double major_period,
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
167         const double minor_period = major_period / MinorTickSubdivision;
168         const pv::util::Timestamp first_major_division = floor(offset / major_period);
169         const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
170         const pv::util::Timestamp t0 = first_major_division * major_period;
171
172         int division = (round(first_minor_division -
173                 first_major_division * MinorTickSubdivision)).convert_to<int>() - 1;
174
175         double x;
176
177         do {
178                 const pv::util::Timestamp t = t0 + division * minor_period;
179                 x = ((t - offset) / scale).convert_to<double>();
180
181                 if (division % MinorTickSubdivision == 0) {
182                         tp.major.emplace_back(x, format_function(t));
183                 } else {
184                         tp.minor.emplace_back(x);
185                 }
186
187                 division++;
188         } while (x < width);
189
190         return tp;
191 }
192
193 void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
194 {
195         view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
196 }
197
198 void Ruler::draw_hover_mark(QPainter &p, int text_height)
199 {
200         const int x = view_.hover_point().x();
201
202         if (x == -1)
203                 return;
204
205         p.setPen(QPen(Qt::NoPen));
206         p.setBrush(QBrush(palette().color(foregroundRole())));
207
208         const int b = RulerHeight * text_height;
209         const float hover_arrow_size = HoverArrowSize * text_height;
210         const QPointF points[] = {
211                 QPointF(x, b),
212                 QPointF(x - hover_arrow_size, b - hover_arrow_size),
213                 QPointF(x + hover_arrow_size, b - hover_arrow_size)
214         };
215         p.drawPolygon(points, countof(points));
216 }
217
218 int Ruler::calculate_text_height() const
219 {
220         return QFontMetrics(font()).ascent();
221 }
222
223 void Ruler::hover_point_changed()
224 {
225         update();
226 }
227
228 void Ruler::invalidate_tick_position_cache()
229 {
230         tick_position_cache_ = boost::none;
231 }
232
233 void Ruler::resizeEvent(QResizeEvent*)
234 {
235         // the tick calculation depends on the width of this widget
236         invalidate_tick_position_cache();
237 }
238
239 } // namespace view
240 } // namespace pv