]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/ruler.cpp
Replaced boost::shared_ptr with std::shared_ptr
[pulseview.git] / pv / view / ruler.cpp
... / ...
CommitLineData
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 "cursor.h"
24#include "view.h"
25#include "viewport.h"
26#include "pv/util.h"
27
28#include <extdef.h>
29
30#include <QApplication>
31#include <QMouseEvent>
32#include <QPainter>
33#include <QTextStream>
34
35#include <pv/widgets/popup.h>
36
37using namespace Qt;
38using std::shared_ptr;
39
40namespace pv {
41namespace view {
42
43const int Ruler::RulerHeight = 30;
44const int Ruler::MinorTickSubdivision = 4;
45const int Ruler::ScaleUnits[3] = {1, 2, 5};
46
47const int Ruler::HoverArrowSize = 5;
48
49Ruler::Ruler(View &parent) :
50 MarginWidget(parent),
51 _dragging(false)
52{
53 setMouseTracking(true);
54
55 connect(&_view, SIGNAL(hover_point_changed()),
56 this, SLOT(hover_point_changed()));
57}
58
59void Ruler::clear_selection()
60{
61 CursorPair &cursors = _view.cursors();
62 cursors.first()->select(false);
63 cursors.second()->select(false);
64 update();
65}
66
67
68QSize Ruler::sizeHint() const
69{
70 return QSize(0, RulerHeight);
71}
72
73void Ruler::paintEvent(QPaintEvent*)
74{
75
76 const double SpacingIncrement = 32.0f;
77 const double MinValueSpacing = 32.0f;
78 const int ValueMargin = 3;
79
80 QPainter p(this);
81 p.setRenderHint(QPainter::Antialiasing);
82
83 double min_width = SpacingIncrement, typical_width;
84 double tick_period;
85 unsigned int prefix;
86
87 // Find tick spacing, and number formatting that does not cause
88 // value to collide.
89 do
90 {
91 const double min_period = _view.scale() * min_width;
92
93 const int order = (int)floorf(log10f(min_period));
94 const double order_decimal = pow(10.0, order);
95
96 unsigned int unit = 0;
97
98 do
99 {
100 tick_period = order_decimal * ScaleUnits[unit++];
101 } while (tick_period < min_period && unit < countof(ScaleUnits));
102
103 prefix = (order - pv::util::FirstSIPrefixPower) / 3;
104
105 typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
106 AlignLeft | AlignTop, pv::util::format_time(_view.offset(),
107 prefix)).width() + MinValueSpacing;
108
109 min_width += SpacingIncrement;
110
111 } while(typical_width > tick_period / _view.scale());
112
113 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
114 AlignLeft | AlignTop, "8").height();
115
116 // Draw the tick marks
117 p.setPen(palette().color(foregroundRole()));
118
119 const double minor_tick_period = tick_period / MinorTickSubdivision;
120 const double first_major_division =
121 floor(_view.offset() / tick_period);
122 const double first_minor_division =
123 ceil(_view.offset() / minor_tick_period);
124 const double t0 = first_major_division * tick_period;
125
126 int division = (int)round(first_minor_division -
127 first_major_division * MinorTickSubdivision) - 1;
128
129 const int major_tick_y1 = text_height + ValueMargin * 2;
130 const int tick_y2 = height();
131 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
132
133 double x;
134
135 do {
136 const double t = t0 + division * minor_tick_period;
137 x = (t - _view.offset()) / _view.scale();
138
139 if (division % MinorTickSubdivision == 0)
140 {
141 // Draw a major tick
142 p.drawText(x, ValueMargin, 0, text_height,
143 AlignCenter | AlignTop | TextDontClip,
144 pv::util::format_time(t, prefix));
145 p.drawLine(QPointF(x, major_tick_y1),
146 QPointF(x, tick_y2));
147 }
148 else
149 {
150 // Draw a minor tick
151 p.drawLine(QPointF(x, minor_tick_y1),
152 QPointF(x, tick_y2));
153 }
154
155 division++;
156
157 } while (x < width());
158
159 // Draw the cursors
160 if (_view.cursors_shown())
161 _view.cursors().draw_markers(p, rect(), prefix);
162
163 // Draw the hover mark
164 draw_hover_mark(p);
165
166 p.end();
167}
168
169void Ruler::mouseMoveEvent(QMouseEvent *e)
170{
171 if (!(e->buttons() & Qt::LeftButton))
172 return;
173
174 if ((e->pos() - _mouse_down_point).manhattanLength() <
175 QApplication::startDragDistance())
176 return;
177
178 _dragging = true;
179
180 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
181 m->set_time(_view.offset() +
182 ((double)e->x() + 0.5) * _view.scale());
183}
184
185void Ruler::mousePressEvent(QMouseEvent *e)
186{
187 if (e->buttons() & Qt::LeftButton)
188 {
189 _mouse_down_point = e->pos();
190
191 _grabbed_marker.reset();
192
193 clear_selection();
194
195 if (_view.cursors_shown()) {
196 CursorPair &cursors = _view.cursors();
197 if (cursors.first()->get_label_rect(
198 rect()).contains(e->pos()))
199 _grabbed_marker = cursors.first();
200 else if (cursors.second()->get_label_rect(
201 rect()).contains(e->pos()))
202 _grabbed_marker = cursors.second();
203 }
204
205 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
206 m->select();
207
208 selection_changed();
209 }
210}
211
212void Ruler::mouseReleaseEvent(QMouseEvent *)
213{
214 using pv::widgets::Popup;
215
216 if (!_dragging)
217 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
218 Popup *const p = m->create_popup(&_view);
219 p->set_position(mapToGlobal(QPoint(m->get_x(),
220 height())), Popup::Bottom);
221 p->show();
222 }
223
224 _dragging = false;
225 _grabbed_marker.reset();
226}
227
228void Ruler::draw_hover_mark(QPainter &p)
229{
230 const int x = _view.hover_point().x();
231
232 if (x == -1 || _dragging)
233 return;
234
235 p.setPen(QPen(Qt::NoPen));
236 p.setBrush(QBrush(palette().color(foregroundRole())));
237
238 const int b = height() - 1;
239 const QPointF points[] = {
240 QPointF(x, b),
241 QPointF(x - HoverArrowSize, b - HoverArrowSize),
242 QPointF(x + HoverArrowSize, b - HoverArrowSize)
243 };
244 p.drawPolygon(points, countof(points));
245}
246
247void Ruler::hover_point_changed()
248{
249 update();
250}
251
252} // namespace view
253} // namespace pv