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