]> sigrok.org Git - pulseview.git/blame - pv/view/cursorpair.cpp
MainWindow: Fix session error slot declaration
[pulseview.git] / pv / view / cursorpair.cpp
CommitLineData
b42d25c4
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 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 "cursorpair.hpp"
b42d25c4 22
3ccf0f7f 23#include "ruler.hpp"
2acdb232
JH
24#include "view.hpp"
25#include "pv/util.hpp"
b42d25c4 26
f9abf97e 27#include <cassert>
0ba172cf
JH
28#include <algorithm>
29
819f4c25
JH
30using std::max;
31using std::make_pair;
32using std::min;
f9abf97e 33using std::shared_ptr;
819f4c25 34using std::pair;
0ba172cf 35
b42d25c4 36namespace pv {
f4e57597
SA
37namespace views {
38namespace TraceView {
b42d25c4 39
5139748b 40const int CursorPair::DeltaPadding = 8;
ccee9230 41const QColor CursorPair::ViewportFillColour(220, 231, 243);
5139748b 42
8debe10d 43CursorPair::CursorPair(View &view) :
5a0192d4 44 TimeItem(view),
8dbbc7f0 45 first_(new Cursor(view, 0.0)),
5a0192d4 46 second_(new Cursor(view, 1.0))
b42d25c4
JH
47{
48}
49
5a0192d4
JH
50bool CursorPair::enabled() const
51{
52 return view_.cursors_shown();
53}
54
58864c5c 55shared_ptr<Cursor> CursorPair::first() const
b42d25c4 56{
8dbbc7f0 57 return first_;
b42d25c4
JH
58}
59
58864c5c 60shared_ptr<Cursor> CursorPair::second() const
b42d25c4 61{
8dbbc7f0 62 return second_;
b42d25c4
JH
63}
64
2ad82c2e
UH
65void CursorPair::set_time(const pv::util::Timestamp& time)
66{
60d9b99a 67 const pv::util::Timestamp delta = second_->time() - first_->time();
3b9c4a0d
JH
68 first_->set_time(time);
69 second_->set_time(time + delta);
70}
71
5165bb34
JH
72float CursorPair::get_x() const
73{
74 return (first_->get_x() + second_->get_x()) / 2.0f;
75}
76
be717066 77QPoint CursorPair::point(const QRect &rect) const
5a0192d4 78{
be717066 79 return first_->point(rect);
5a0192d4
JH
80}
81
82pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
83{
84 (void)parent;
85 return nullptr;
86}
87
689dea92 88QRectF CursorPair::label_rect(const QRectF &rect) const
5139748b 89{
3fed1f31 90 const QSizeF label_size(text_size_ + LabelPadding * 2);
5139748b
JH
91 const pair<float, float> offsets(get_cursor_offsets());
92 const pair<float, float> normal_offsets(
93 (offsets.first < offsets.second) ? offsets :
94 make_pair(offsets.second, offsets.first));
95
96 const float height = label_size.height();
97 const float left = max(normal_offsets.first + DeltaPadding, -height);
98 const float right = min(normal_offsets.second - DeltaPadding,
99 (float)rect.width() + height);
100
101 return QRectF(left, rect.height() - label_size.height() -
6abffa97 102 TimeMarker::ArrowSize - 0.5f,
5139748b
JH
103 right - left, height);
104}
105
49028d6c 106void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
332afa74 107{
8dbbc7f0
JH
108 assert(first_);
109 assert(second_);
58864c5c 110
9a377493
JH
111 if (!enabled())
112 return;
113
f106fc3a
JH
114 const QColor text_colour =
115 ViewItem::select_text_colour(Cursor::FillColour);
361c560e 116
f106fc3a 117 p.setPen(text_colour);
32045253 118 compute_text_size(p);
689dea92 119 QRectF delta_rect(label_rect(rect));
5139748b
JH
120
121 const int radius = delta_rect.height() / 2;
122 const QRectF text_rect(delta_rect.intersected(
123 rect).adjusted(radius, 0, -radius, 0));
2ad82c2e 124 if (text_rect.width() >= text_size_.width()) {
5139748b
JH
125 const int highlight_radius = delta_rect.height() / 2 - 2;
126
e9f909b8
JH
127 if (selected()) {
128 p.setBrush(Qt::transparent);
129 p.setPen(highlight_pen());
130 p.drawRoundedRect(delta_rect, radius, radius);
131 }
132
49028d6c
JH
133 p.setBrush(hover ? Cursor::FillColour.lighter() :
134 Cursor::FillColour);
4fabd61a 135 p.setPen(Cursor::FillColour.darker());
5139748b
JH
136 p.drawRoundedRect(delta_rect, radius, radius);
137
138 delta_rect.adjust(1, 1, -1, -1);
4fabd61a 139 p.setPen(Cursor::FillColour.lighter());
5139748b
JH
140 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
141
f106fc3a 142 p.setPen(text_colour);
5139748b 143 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
32045253 144 format_string());
5139748b 145 }
332afa74
JH
146}
147
2ad82c2e
UH
148void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp)
149{
beb897c6
JH
150 if (!enabled())
151 return;
152
0ba172cf 153 p.setPen(Qt::NoPen);
ccee9230 154 p.setBrush(QBrush(ViewportFillColour));
0ba172cf 155
199441e4
JH
156 const pair<float, float> offsets(get_cursor_offsets());
157 const int l = (int)max(min(
158 offsets.first, offsets.second), 0.0f);
159 const int r = (int)min(max(
beb897c6 160 offsets.first, offsets.second), (float)pp.width());
58864c5c 161
beb897c6 162 p.drawRect(l, pp.top(), r - l, pp.height());
0ba172cf
JH
163}
164
32045253
JH
165QString CursorPair::format_string()
166{
d001f416 167 const pv::util::SIPrefix prefix = view_.tick_prefix();
3ccf0f7f
JS
168 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
169
170 const QString s1 = Ruler::format_time_with_distance(
171 diff, diff, prefix, view_.time_unit(), view_.tick_precision(), false);
172 const QString s2 = util::format_time_si(
173 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false);
174
175 return QString("%1 / %2").arg(s1).arg(s2);
32045253
JH
176}
177
178void CursorPair::compute_text_size(QPainter &p)
5139748b 179{
8dbbc7f0
JH
180 assert(first_);
181 assert(second_);
58864c5c 182
32045253 183 text_size_ = p.boundingRect(QRectF(), 0, format_string()).size();
5139748b
JH
184}
185
199441e4
JH
186pair<float, float> CursorPair::get_cursor_offsets() const
187{
8dbbc7f0
JH
188 assert(first_);
189 assert(second_);
58864c5c 190
199441e4 191 return pair<float, float>(
60d9b99a
JS
192 ((first_->time() - view_.offset()) / view_.scale()).convert_to<float>(),
193 ((second_->time() - view_.offset()) / view_.scale()).convert_to<float>());
199441e4
JH
194}
195
f4e57597
SA
196} // namespace TraceView
197} // namespace views
b42d25c4 198} // namespace pv