]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/cursorpair.cpp
TimeMarker: Removed Offset
[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;
39
40CursorPair::CursorPair(View &view) :
41 TimeItem(view),
42 first_(new Cursor(view, 0.0)),
43 second_(new Cursor(view, 1.0))
44{
45}
46
47bool CursorPair::enabled() const
48{
49 return view_.cursors_shown();
50}
51
52shared_ptr<Cursor> CursorPair::first() const
53{
54 return first_;
55}
56
57shared_ptr<Cursor> CursorPair::second() const
58{
59 return second_;
60}
61
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
68float CursorPair::get_x() const
69{
70 return (first_->get_x() + second_->get_x()) / 2.0f;
71}
72
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
84QRectF CursorPair::label_rect(const QRectF &rect) const
85{
86 const QSizeF label_size(
87 text_size_.width() + View::LabelPadding.width() * 2,
88 text_size_.height() + View::LabelPadding.height() * 2);
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() -
100 TimeMarker::ArrowSize - 0.5f,
101 right - left, height);
102}
103
104void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
105{
106 assert(first_);
107 assert(second_);
108
109 if (!enabled())
110 return;
111
112 const unsigned int prefix = view_.tick_prefix();
113 const QColor text_colour =
114 ViewItem::select_text_colour(Cursor::FillColour);
115
116 p.setPen(text_colour);
117 compute_text_size(p, prefix);
118 QRectF delta_rect(label_rect(rect));
119
120 const int radius = delta_rect.height() / 2;
121 const QRectF text_rect(delta_rect.intersected(
122 rect).adjusted(radius, 0, -radius, 0));
123 if(text_rect.width() >= text_size_.width())
124 {
125 const int highlight_radius = delta_rect.height() / 2 - 2;
126
127 if (selected()) {
128 p.setBrush(Qt::transparent);
129 p.setPen(highlight_pen());
130 p.drawRoundedRect(delta_rect, radius, radius);
131 }
132
133 p.setBrush(hover ? Cursor::FillColour.lighter() :
134 Cursor::FillColour);
135 p.setPen(Cursor::FillColour.darker());
136 p.drawRoundedRect(delta_rect, radius, radius);
137
138 delta_rect.adjust(1, 1, -1, -1);
139 p.setPen(Cursor::FillColour.lighter());
140 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
141
142 p.setPen(text_colour);
143 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
144 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
145 }
146}
147
148void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) {
149 if (!enabled())
150 return;
151
152 p.setPen(Qt::NoPen);
153 p.setBrush(QBrush(View::CursorAreaColour));
154
155 const pair<float, float> offsets(get_cursor_offsets());
156 const int l = (int)max(min(
157 offsets.first, offsets.second), 0.0f);
158 const int r = (int)min(max(
159 offsets.first, offsets.second), (float)pp.width());
160
161 p.drawRect(l, pp.top(), r - l, pp.height());
162}
163
164void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
165{
166 assert(first_);
167 assert(second_);
168
169 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
170 second_->time() - first_->time(), prefix, 2)).size();
171}
172
173pair<float, float> CursorPair::get_cursor_offsets() const
174{
175 assert(first_);
176 assert(second_);
177
178 return pair<float, float>(
179 (first_->time() - view_.offset()) / view_.scale(),
180 (second_->time() - view_.offset()) / view_.scale());
181}
182
183} // namespace view
184} // namespace pv