]> sigrok.org Git - pulseview.git/blame - pv/view/cursor.cpp
Fix an isnan() issue on (at least) MinGW and Mac OS X.
[pulseview.git] / pv / view / cursor.cpp
CommitLineData
cabce982
JH
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
2acdb232 21#include "cursor.hpp"
cabce982 22
2acdb232
JH
23#include "view.hpp"
24#include "pv/util.hpp"
cabce982 25
4fabd61a 26#include <QApplication>
cabce982
JH
27#include <QBrush>
28#include <QPainter>
34c1ef02 29#include <QPointF>
cabce982
JH
30#include <QRect>
31#include <QRectF>
32
f9abf97e
JH
33#include <cassert>
34#include <cstdio>
e899d8bd 35#include <limits>
34c1ef02 36
e899d8bd 37using std::abs;
f9abf97e 38using std::shared_ptr;
e899d8bd 39using std::numeric_limits;
58864c5c 40
cabce982
JH
41namespace pv {
42namespace view {
43
cabce982 44const QColor Cursor::FillColour(52, 101, 164);
34c1ef02 45
8debe10d 46Cursor::Cursor(View &view, double time) :
4fabd61a 47 TimeMarker(view, FillColour, time)
cabce982
JH
48{
49}
50
096fb584
JH
51bool Cursor::enabled() const
52{
53 return view_.cursors_shown();
54}
55
3b84fd6d
JH
56QString Cursor::get_text() const
57{
58 return pv::util::format_time(time_, view_.tick_prefix(), 2);
59}
60
689dea92 61QRectF Cursor::label_rect(const QRectF &rect) const
cabce982 62{
58864c5c
JH
63 const shared_ptr<Cursor> other(get_other_cursor());
64 assert(other);
65
8dbbc7f0 66 const float x = (time_ - view_.offset()) / view_.scale();
34c1ef02 67
4fabd61a 68 QFontMetrics m(QApplication::font());
3b84fd6d 69 QSize text_size = m.boundingRect(get_text()).size();
4fabd61a 70
34c1ef02 71 const QSizeF label_size(
ec39632d
JH
72 text_size.width() + LabelPadding.width() * 2,
73 text_size.height() + LabelPadding.height() * 2);
199441e4 74 const float top = rect.height() - label_size.height() -
6abffa97 75 TimeMarker::ArrowSize - 0.5f;
5139748b 76 const float height = label_size.height();
199441e4 77
e899d8bd
JH
78 const double other_time = other->time();
79 if (time_ > other_time ||
80 (abs(time_ - other_time) < numeric_limits<double>::epsilon() &&
81 this > other.get()))
199441e4
JH
82 return QRectF(x, top, label_size.width(), height);
83 else
84 return QRectF(x - label_size.width(), top,
85 label_size.width(), height);
ca4ec3ea
JH
86}
87
58864c5c
JH
88shared_ptr<Cursor> Cursor::get_other_cursor() const
89{
5c5ce757
JH
90 const shared_ptr<CursorPair> cursors(view_.cursors());
91 assert(cursors);
92 return (cursors->first().get() == this) ?
93 cursors->second() : cursors->first();
58864c5c
JH
94}
95
cabce982
JH
96} // namespace view
97} // namespace pv