PulseView  unreleased development snapshot
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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "cursor.hpp"
21 
22 #include "pv/util.hpp"
23 #include "ruler.hpp"
24 #include "view.hpp"
25 
26 #include <QApplication>
27 #include <QBrush>
28 #include <QMenu>
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; // NOLINT. Force usage of std::abs() instead of C's abs().
39 using std::shared_ptr;
40 
41 namespace pv {
42 namespace views {
43 namespace trace {
44 
45 const QColor Cursor::FillColor(52, 101, 164);
46 
47 Cursor::Cursor(View &view, double time) :
48  TimeMarker(view, FillColor, 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 
65 }
66 
67 QRectF Cursor::label_rect(const QRectF &rect) const
68 {
69  const shared_ptr<Cursor> other(get_other_cursor());
70  assert(other);
71 
72  const float x = get_x();
73 
74  QFontMetrics m(QApplication::font());
75  QSize text_size = m.boundingRect(get_display_text()).size();
76 
77  const QSizeF label_size(
78  text_size.width() + LabelPadding.width() * 2,
79  text_size.height() + LabelPadding.height() * 2);
80  const float top = rect.height() - label_size.height() -
81  TimeMarker::ArrowSize - 0.5f;
82  const float height = label_size.height();
83 
84  const pv::util::Timestamp& other_time = other->time();
85 
86  if (time_ > other_time ||
87  (abs(time_ - other_time).is_zero() && this > other.get()))
88  return QRectF(x, top, label_size.width(), height);
89  else
90  return QRectF(x - label_size.width(), top, label_size.width(), height);
91 }
92 
93 QMenu *Cursor::create_header_context_menu(QWidget *parent)
94 {
95  QMenu *const menu = new QMenu(parent);
96 
97  QAction *const snap_disable = new QAction(tr("Disable snapping"), this);
98  snap_disable->setCheckable(true);
99  snap_disable->setChecked(snapping_disabled_);
100  connect(snap_disable, &QAction::toggled, this, [=](bool checked){snapping_disabled_ = checked;});
101  menu->addAction(snap_disable);
102 
103  return menu;
104 }
105 
106 shared_ptr<Cursor> Cursor::get_other_cursor() const
107 {
108  const shared_ptr<CursorPair> cursors(view_.cursors());
109  assert(cursors);
110  return (cursors->first().get() == this) ?
111  cursors->second() : cursors->first();
112 }
113 
114 } // namespace trace
115 } // namespace views
116 } // namespace pv
const Ruler * ruler() const
Definition: view.cpp:491
util::TimeUnit time_unit() const
Definition: view.cpp:730
pv::util::Timestamp time_
Definition: timemarker.hpp:138
shared_ptr< CursorPair > cursors() const
Definition: view.cpp:1007
virtual QMenu * create_header_context_menu(QWidget *parent) override
Definition: cursor.cpp:93
pv::util::SIPrefix tick_prefix() const
Definition: view.cpp:686
float get_x() const override
Definition: timemarker.cpp:74
pv::util::Timestamp get_ruler_time_from_absolute_time(const pv::util::Timestamp &abs_time) const
Definition: ruler.cpp:131
m
Definition: CMakeCache.txt:621
static const QSizeF LabelPadding
Definition: viewitem.hpp:51
virtual QString get_text() const override
Definition: cursor.cpp:57
Cursor(View &view, double time)
Definition: cursor.cpp:47
shared_ptr< Cursor > get_other_cursor() const
Definition: cursor.cpp:106
bool cursors_shown() const
Definition: view.cpp:968
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:84
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
Definition: util.hpp:67
virtual QString get_display_text() const
Definition: timemarker.cpp:108
virtual QRectF label_rect(const QRectF &rect) const override
Definition: cursor.cpp:67
static const QColor FillColor
Definition: cursor.hpp:42
static const int ArrowSize
Definition: timemarker.hpp:53
unsigned int tick_precision() const
Definition: view.cpp:699
virtual bool enabled() const override
Definition: cursor.cpp:52