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