]> sigrok.org Git - pulseview.git/blob - pv/views/trace/cursor.cpp
Fix regression introduced by 2980ff2da269c9ed
[pulseview.git] / pv / views / trace / cursor.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "cursor.hpp"
21
22 #include "pv/util.hpp"
23 #include "ruler.hpp"
24 #include "view.hpp"
25
26 #include <QApplication>
27 #include <QBrush>
28 #include <QPainter>
29 #include <QPointF>
30 #include <QRect>
31 #include <QRectF>
32
33 #include <cassert>
34 #include <cstdio>
35 #include <limits>
36
37 using std::abs; // NOLINT. Force usage of std::abs() instead of C's abs().
38 using std::shared_ptr;
39
40 namespace pv {
41 namespace views {
42 namespace trace {
43
44 const QColor Cursor::FillColor(52, 101, 164);
45
46 Cursor::Cursor(View &view, double time) :
47         TimeMarker(view, FillColor, time)
48 {
49 }
50
51 bool Cursor::enabled() const
52 {
53         return view_.cursors_shown();
54 }
55
56 QString Cursor::get_text() const
57 {
58         const shared_ptr<Cursor> other = get_other_cursor();
59         const pv::util::Timestamp& diff = abs(time_ - other->time_);
60
61         return Ruler::format_time_with_distance(
62                 diff, time_, view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
63 }
64
65 QRectF Cursor::label_rect(const QRectF &rect) const
66 {
67         const shared_ptr<Cursor> other(get_other_cursor());
68         assert(other);
69
70         const float x = get_x();
71
72         QFontMetrics m(QApplication::font());
73         QSize text_size = m.boundingRect(get_text()).size();
74
75         const QSizeF label_size(
76                 text_size.width() + LabelPadding.width() * 2,
77                 text_size.height() + LabelPadding.height() * 2);
78         const float top = rect.height() - label_size.height() -
79                 TimeMarker::ArrowSize - 0.5f;
80         const float height = label_size.height();
81
82         const pv::util::Timestamp& other_time = other->time();
83
84         if (time_ > other_time ||
85                 (abs(time_ - other_time).is_zero() && this > other.get()))
86                 return QRectF(x, top, label_size.width(), height);
87         else
88                 return QRectF(x - label_size.width(), top, label_size.width(), height);
89 }
90
91 shared_ptr<Cursor> Cursor::get_other_cursor() const
92 {
93         const shared_ptr<CursorPair> cursors(view_.cursors());
94         assert(cursors);
95         return (cursors->first().get() == this) ?
96                 cursors->second() : cursors->first();
97 }
98
99 } // namespace trace
100 } // namespace views
101 } // namespace pv