]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/cursorpair.cpp
SamplingBar: Handle a failure to list the LIMIT_SAMPLES key
[pulseview.git] / pv / view / cursorpair.cpp
... / ...
CommitLineData
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
21#include "cursorpair.hpp"
22
23#include "view.hpp"
24#include "pv/util.hpp"
25
26#include <cassert>
27#include <algorithm>
28
29using std::max;
30using std::make_pair;
31using std::min;
32using std::shared_ptr;
33using std::pair;
34
35namespace pv {
36namespace view {
37
38const int CursorPair::DeltaPadding = 8;
39const QColor CursorPair::ViewportFillColour(220, 231, 243);
40
41CursorPair::CursorPair(View &view) :
42 TimeItem(view),
43 first_(new Cursor(view, 0.0)),
44 second_(new Cursor(view, 1.0))
45{
46}
47
48bool CursorPair::enabled() const
49{
50 return view_.cursors_shown();
51}
52
53shared_ptr<Cursor> CursorPair::first() const
54{
55 return first_;
56}
57
58shared_ptr<Cursor> CursorPair::second() const
59{
60 return second_;
61}
62
63void CursorPair::set_time(double time) {
64 const double delta = second_->time() - first_->time();
65 first_->set_time(time);
66 second_->set_time(time + delta);
67}
68
69float CursorPair::get_x() const
70{
71 return (first_->get_x() + second_->get_x()) / 2.0f;
72}
73
74QPoint CursorPair::point(const QRect &rect) const
75{
76 return first_->point(rect);
77}
78
79pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
80{
81 (void)parent;
82 return nullptr;
83}
84
85QRectF CursorPair::label_rect(const QRectF &rect) const
86{
87 const QSizeF label_size(text_size_ + LabelPadding * 2);
88 const pair<float, float> offsets(get_cursor_offsets());
89 const pair<float, float> normal_offsets(
90 (offsets.first < offsets.second) ? offsets :
91 make_pair(offsets.second, offsets.first));
92
93 const float height = label_size.height();
94 const float left = max(normal_offsets.first + DeltaPadding, -height);
95 const float right = min(normal_offsets.second - DeltaPadding,
96 (float)rect.width() + height);
97
98 return QRectF(left, rect.height() - label_size.height() -
99 TimeMarker::ArrowSize - 0.5f,
100 right - left, height);
101}
102
103void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
104{
105 assert(first_);
106 assert(second_);
107
108 if (!enabled())
109 return;
110
111 const QColor text_colour =
112 ViewItem::select_text_colour(Cursor::FillColour);
113
114 p.setPen(text_colour);
115 compute_text_size(p);
116 QRectF delta_rect(label_rect(rect));
117
118 const int radius = delta_rect.height() / 2;
119 const QRectF text_rect(delta_rect.intersected(
120 rect).adjusted(radius, 0, -radius, 0));
121 if(text_rect.width() >= text_size_.width())
122 {
123 const int highlight_radius = delta_rect.height() / 2 - 2;
124
125 if (selected()) {
126 p.setBrush(Qt::transparent);
127 p.setPen(highlight_pen());
128 p.drawRoundedRect(delta_rect, radius, radius);
129 }
130
131 p.setBrush(hover ? Cursor::FillColour.lighter() :
132 Cursor::FillColour);
133 p.setPen(Cursor::FillColour.darker());
134 p.drawRoundedRect(delta_rect, radius, radius);
135
136 delta_rect.adjust(1, 1, -1, -1);
137 p.setPen(Cursor::FillColour.lighter());
138 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
139
140 p.setPen(text_colour);
141 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
142 format_string());
143 }
144}
145
146void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) {
147 if (!enabled())
148 return;
149
150 p.setPen(Qt::NoPen);
151 p.setBrush(QBrush(ViewportFillColour));
152
153 const pair<float, float> offsets(get_cursor_offsets());
154 const int l = (int)max(min(
155 offsets.first, offsets.second), 0.0f);
156 const int r = (int)min(max(
157 offsets.first, offsets.second), (float)pp.width());
158
159 p.drawRect(l, pp.top(), r - l, pp.height());
160}
161
162QString CursorPair::format_string()
163{
164 const unsigned int prefix = view_.tick_prefix();
165 const double delta = second_->time() - first_->time();
166 return QString("%1 / %2").
167 arg(util::format_time(delta, prefix, 2)).
168 arg(util::format_si_value(1.0 / fabs(delta), "Hz", -1, 4));
169}
170
171void CursorPair::compute_text_size(QPainter &p)
172{
173 assert(first_);
174 assert(second_);
175
176 text_size_ = p.boundingRect(QRectF(), 0, format_string()).size();
177}
178
179pair<float, float> CursorPair::get_cursor_offsets() const
180{
181 assert(first_);
182 assert(second_);
183
184 return pair<float, float>(
185 (first_->time() - view_.offset()) / view_.scale(),
186 (second_->time() - view_.offset()) / view_.scale());
187}
188
189} // namespace view
190} // namespace pv