]> sigrok.org Git - pulseview.git/blob - pv/view/cursor.cpp
d5b0a641f075a5b9c5d7f4ddd0cb511102aee08a
[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
36 using std::shared_ptr;
37
38 namespace pv {
39 namespace view {
40
41 const QColor Cursor::FillColour(52, 101, 164);
42
43 Cursor::Cursor(View &view, double time) :
44         TimeMarker(view, FillColour, time)
45 {
46 }
47
48 QRectF Cursor::get_label_rect(const QRect &rect) const
49 {
50         const shared_ptr<Cursor> other(get_other_cursor());
51         assert(other);
52
53         const float x = (time_ - view_.offset()) / view_.scale();
54
55         QFontMetrics m(QApplication::font());
56         QSize text_size = m.boundingRect(
57                 pv::util::format_time(time_, view_.tick_prefix(), 2)).size();
58
59         const QSizeF label_size(
60                 text_size.width() + View::LabelPadding.width() * 2,
61                 text_size.height() + View::LabelPadding.height() * 2);
62         const float top = rect.height() - label_size.height() -
63                 TimeMarker::Offset - TimeMarker::ArrowSize - 0.5f;
64         const float height = label_size.height();
65
66         if (time_ > other->time())
67                 return QRectF(x, top, label_size.width(), height);
68         else
69                 return QRectF(x - label_size.width(), top,
70                         label_size.width(), height);
71 }
72
73 shared_ptr<Cursor> Cursor::get_other_cursor() const
74 {
75         const CursorPair &cursors = view_.cursors();
76         return (cursors.first().get() == this) ?
77                 cursors.second() : cursors.first();
78 }
79
80 } // namespace view
81 } // namespace pv