]> sigrok.org Git - pulseview.git/blob - pv/view/cursor.cpp
e4704be497500fd3323ef254ca2dc6c35086d06f
[pulseview.git] / pv / view / 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "cursor.hpp"
22
23 #include "view.hpp"
24 #include "pv/util.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;
38 using std::shared_ptr;
39 using std::numeric_limits;
40
41 namespace pv {
42 namespace view {
43
44 const QColor Cursor::FillColour(52, 101, 164);
45
46 Cursor::Cursor(View &view, double time) :
47         TimeMarker(view, FillColour, 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         return util::format_time(time_, view_.tick_prefix(),
59                 view_.time_unit(), 2);
60 }
61
62 QRectF Cursor::label_rect(const QRectF &rect) const
63 {
64         const shared_ptr<Cursor> other(get_other_cursor());
65         assert(other);
66
67         const float x = ((time_ - view_.offset())/ view_.scale()).convert_to<float>();
68
69         QFontMetrics m(QApplication::font());
70         QSize text_size = m.boundingRect(get_text()).size();
71
72         const QSizeF label_size(
73                 text_size.width() + LabelPadding.width() * 2,
74                 text_size.height() + LabelPadding.height() * 2);
75         const float top = rect.height() - label_size.height() -
76                 TimeMarker::ArrowSize - 0.5f;
77         const float height = label_size.height();
78
79         const pv::util::Timestamp& other_time = other->time();
80
81         if (time_ > other_time ||
82                 (abs(time_ - other_time).is_zero() && this > other.get()))
83                 return QRectF(x, top, label_size.width(), height);
84         else
85                 return QRectF(x - label_size.width(), top, label_size.width(), height);
86 }
87
88 shared_ptr<Cursor> Cursor::get_other_cursor() const
89 {
90         const shared_ptr<CursorPair> cursors(view_.cursors());
91         assert(cursors);
92         return (cursors->first().get() == this) ?
93                 cursors->second() : cursors->first();
94 }
95
96 } // namespace view
97 } // namespace pv