2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
25 #include "pv/util.hpp"
27 #include <QApplication>
39 using std::shared_ptr;
40 using std::numeric_limits;
45 const QColor Cursor::FillColour(52, 101, 164);
47 Cursor::Cursor(View &view, double time) :
48 TimeMarker(view, FillColour, time)
52 bool Cursor::enabled() const
54 return view_.cursors_shown();
57 QString Cursor::get_text() const
59 const shared_ptr<Cursor> other = get_other_cursor();
60 const pv::util::Timestamp& diff = abs(time_ - other->time_);
62 return Ruler::format_time_with_distance(
63 diff, time_, view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
66 QRectF Cursor::label_rect(const QRectF &rect) const
68 const shared_ptr<Cursor> other(get_other_cursor());
71 const float x = ((time_ - view_.offset())/ view_.scale()).convert_to<float>();
73 QFontMetrics m(QApplication::font());
74 QSize text_size = m.boundingRect(get_text()).size();
76 const QSizeF label_size(
77 text_size.width() + LabelPadding.width() * 2,
78 text_size.height() + LabelPadding.height() * 2);
79 const float top = rect.height() - label_size.height() -
80 TimeMarker::ArrowSize - 0.5f;
81 const float height = label_size.height();
83 const pv::util::Timestamp& other_time = other->time();
85 if (time_ > other_time ||
86 (abs(time_ - other_time).is_zero() && this > other.get()))
87 return QRectF(x, top, label_size.width(), height);
89 return QRectF(x - label_size.width(), top, label_size.width(), height);
92 shared_ptr<Cursor> Cursor::get_other_cursor() const
94 const shared_ptr<CursorPair> cursors(view_.cursors());
96 return (cursors->first().get() == this) ?
97 cursors->second() : cursors->first();