PulseView  0.3.0
A Qt-based sigrok GUI
cursor.cpp
Go to the documentation of this file.
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 "ruler.hpp"
24 #include "view.hpp"
25 #include "pv/util.hpp"
26 
27 #include <QApplication>
28 #include <QBrush>
29 #include <QPainter>
30 #include <QPointF>
31 #include <QRect>
32 #include <QRectF>
33 
34 #include <cassert>
35 #include <cstdio>
36 #include <limits>
37 
38 using std::abs;
39 using std::shared_ptr;
40 using std::numeric_limits;
41 
42 namespace pv {
43 namespace view {
44 
45 const QColor Cursor::FillColour(52, 101, 164);
46 
47 Cursor::Cursor(View &view, double time) :
48  TimeMarker(view, FillColour, time)
49 {
50 }
51 
52 bool Cursor::enabled() const
53 {
54  return view_.cursors_shown();
55 }
56 
57 QString Cursor::get_text() const
58 {
59  const shared_ptr<Cursor> other = get_other_cursor();
60  const pv::util::Timestamp& diff = abs(time_ - other->time_);
61 
64 }
65 
66 QRectF Cursor::label_rect(const QRectF &rect) const
67 {
68  const shared_ptr<Cursor> other(get_other_cursor());
69  assert(other);
70 
71  const float x = ((time_ - view_.offset())/ view_.scale()).convert_to<float>();
72 
73  QFontMetrics m(QApplication::font());
74  QSize text_size = m.boundingRect(get_text()).size();
75 
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();
82 
83  const pv::util::Timestamp& other_time = other->time();
84 
85  if (time_ > other_time ||
86  (abs(time_ - other_time).is_zero() && this > other.get()))
87  return QRectF(x, top, label_size.width(), height);
88  else
89  return QRectF(x - label_size.width(), top, label_size.width(), height);
90 }
91 
92 shared_ptr<Cursor> Cursor::get_other_cursor() const
93 {
94  const shared_ptr<CursorPair> cursors(view_.cursors());
95  assert(cursors);
96  return (cursors->first().get() == this) ?
97  cursors->second() : cursors->first();
98 }
99 
100 } // namespace view
101 } // namespace pv
static const QSizeF LabelPadding
Definition: viewitem.hpp:49
std::shared_ptr< CursorPair > cursors() const
Definition: view.cpp:487
pv::util::SIPrefix tick_prefix() const
Definition: view.cpp:263
Cursor(View &view, double time)
Definition: cursor.cpp:47
QString get_text() const
Definition: cursor.cpp:57
static const int ArrowSize
Definition: timemarker.hpp:49
static const QColor FillColour
Definition: cursor.hpp:40
unsigned int tick_precision() const
Definition: view.cpp:276
static QString format_time_with_distance(const pv::util::Timestamp &distance, const pv::util::Timestamp &t, pv::util::SIPrefix prefix=pv::util::SIPrefix::unspecified, pv::util::TimeUnit unit=pv::util::TimeUnit::Time, unsigned precision=0, bool sign=true)
Definition: ruler.cpp:80
pv::util::Timestamp time_
Definition: timemarker.hpp:121
util::TimeUnit time_unit() const
Definition: view.cpp:302
bool enabled() const
Definition: cursor.cpp:52
double scale() const
Definition: view.cpp:220
bool cursors_shown() const
Definition: view.cpp:466
std::shared_ptr< Cursor > get_other_cursor() const
Definition: cursor.cpp:92
const pv::util::Timestamp & offset() const
Definition: view.cpp:233
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
Definition: util.hpp:58
QRectF label_rect(const QRectF &rect) const
Definition: cursor.cpp:66