]> sigrok.org Git - pulseview.git/blame - pv/view/cursorpair.cpp
CursorHeader: Paint by the labels by the time item list
[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
2acdb232
JH
23#include "view.hpp"
24#include "pv/util.hpp"
b42d25c4 25
f9abf97e 26#include <cassert>
0ba172cf
JH
27#include <algorithm>
28
819f4c25
JH
29using std::max;
30using std::make_pair;
31using std::min;
f9abf97e 32using std::shared_ptr;
819f4c25 33using std::pair;
0ba172cf 34
b42d25c4
JH
35namespace pv {
36namespace view {
37
5139748b
JH
38const int CursorPair::DeltaPadding = 8;
39
8debe10d 40CursorPair::CursorPair(View &view) :
5a0192d4 41 TimeItem(view),
8dbbc7f0 42 first_(new Cursor(view, 0.0)),
5a0192d4 43 second_(new Cursor(view, 1.0))
b42d25c4
JH
44{
45}
46
5a0192d4
JH
47bool CursorPair::enabled() const
48{
49 return view_.cursors_shown();
50}
51
58864c5c 52shared_ptr<Cursor> CursorPair::first() const
b42d25c4 53{
8dbbc7f0 54 return first_;
b42d25c4
JH
55}
56
58864c5c 57shared_ptr<Cursor> CursorPair::second() const
b42d25c4 58{
8dbbc7f0 59 return second_;
b42d25c4
JH
60}
61
3b9c4a0d
JH
62void CursorPair::set_time(double time) {
63 const double delta = second_->time() - first_->time();
64 first_->set_time(time);
65 second_->set_time(time + delta);
66}
67
5165bb34
JH
68float CursorPair::get_x() const
69{
70 return (first_->get_x() + second_->get_x()) / 2.0f;
71}
72
5a0192d4
JH
73QPoint CursorPair::point() const
74{
75 return first_->point();
76}
77
78pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
79{
80 (void)parent;
81 return nullptr;
82}
83
689dea92 84QRectF CursorPair::label_rect(const QRectF &rect) const
5139748b
JH
85{
86 const QSizeF label_size(
8dbbc7f0
JH
87 text_size_.width() + View::LabelPadding.width() * 2,
88 text_size_.height() + View::LabelPadding.height() * 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() -
4fabd61a 100 TimeMarker::ArrowSize - TimeMarker::Offset - 0.5f,
5139748b
JH
101 right - left, height);
102}
103
2fae5107 104void CursorPair::paint_label(QPainter &p, const QRect &rect)
332afa74 105{
8dbbc7f0
JH
106 assert(first_);
107 assert(second_);
58864c5c 108
9a377493
JH
109 if (!enabled())
110 return;
111
361c560e
JH
112 const unsigned int prefix = view_.tick_prefix();
113
5139748b 114 compute_text_size(p, prefix);
689dea92 115 QRectF delta_rect(label_rect(rect));
5139748b
JH
116
117 const int radius = delta_rect.height() / 2;
118 const QRectF text_rect(delta_rect.intersected(
119 rect).adjusted(radius, 0, -radius, 0));
8dbbc7f0 120 if(text_rect.width() >= text_size_.width())
5139748b
JH
121 {
122 const int highlight_radius = delta_rect.height() / 2 - 2;
123
e9f909b8
JH
124 if (selected()) {
125 p.setBrush(Qt::transparent);
126 p.setPen(highlight_pen());
127 p.drawRoundedRect(delta_rect, radius, radius);
128 }
129
5139748b 130 p.setBrush(Cursor::FillColour);
4fabd61a 131 p.setPen(Cursor::FillColour.darker());
5139748b
JH
132 p.drawRoundedRect(delta_rect, radius, radius);
133
134 delta_rect.adjust(1, 1, -1, -1);
4fabd61a 135 p.setPen(Cursor::FillColour.lighter());
5139748b
JH
136 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
137
4fabd61a
JH
138 p.setPen(SelectableItem::select_text_colour(
139 Cursor::FillColour));
5139748b 140 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
8dbbc7f0 141 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
5139748b 142 }
332afa74
JH
143}
144
0ba172cf
JH
145void CursorPair::draw_viewport_background(QPainter &p,
146 const QRect &rect)
147{
148 p.setPen(Qt::NoPen);
149 p.setBrush(QBrush(View::CursorAreaColour));
150
199441e4
JH
151 const pair<float, float> offsets(get_cursor_offsets());
152 const int l = (int)max(min(
153 offsets.first, offsets.second), 0.0f);
154 const int r = (int)min(max(
155 offsets.first, offsets.second), (float)rect.width());
0ba172cf
JH
156
157 p.drawRect(l, 0, r - l, rect.height());
158}
159
160void CursorPair::draw_viewport_foreground(QPainter &p,
161 const QRect &rect)
162{
8dbbc7f0
JH
163 assert(first_);
164 assert(second_);
58864c5c 165
8dbbc7f0
JH
166 first_->paint(p, rect);
167 second_->paint(p, rect);
0ba172cf
JH
168}
169
5139748b
JH
170void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
171{
8dbbc7f0
JH
172 assert(first_);
173 assert(second_);
58864c5c 174
8dbbc7f0
JH
175 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
176 second_->time() - first_->time(), prefix, 2)).size();
5139748b
JH
177}
178
199441e4
JH
179pair<float, float> CursorPair::get_cursor_offsets() const
180{
8dbbc7f0
JH
181 assert(first_);
182 assert(second_);
58864c5c 183
199441e4 184 return pair<float, float>(
8dbbc7f0
JH
185 (first_->time() - view_.offset()) / view_.scale(),
186 (second_->time() - view_.offset()) / view_.scale());
199441e4
JH
187}
188
b42d25c4
JH
189} // namespace view
190} // namespace pv