2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "cursorpair.hpp"
25 #include "pv/util.hpp"
33 using std::shared_ptr;
40 const int CursorPair::DeltaPadding = 8;
41 const QColor CursorPair::ViewportFillColour(220, 231, 243);
43 CursorPair::CursorPair(View &view) :
45 first_(new Cursor(view, 0.0)),
46 second_(new Cursor(view, 1.0))
50 bool CursorPair::enabled() const
52 return view_.cursors_shown();
55 shared_ptr<Cursor> CursorPair::first() const
60 shared_ptr<Cursor> CursorPair::second() const
65 void CursorPair::set_time(const pv::util::Timestamp& time)
67 const pv::util::Timestamp delta = second_->time() - first_->time();
68 first_->set_time(time);
69 second_->set_time(time + delta);
72 float CursorPair::get_x() const
74 return (first_->get_x() + second_->get_x()) / 2.0f;
77 QPoint CursorPair::point(const QRect &rect) const
79 return first_->point(rect);
82 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
88 QRectF CursorPair::label_rect(const QRectF &rect) const
90 const QSizeF label_size(text_size_ + LabelPadding * 2);
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));
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);
101 return QRectF(left, rect.height() - label_size.height() -
102 TimeMarker::ArrowSize - 0.5f,
103 right - left, height);
106 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
114 const QColor text_colour =
115 ViewItem::select_text_colour(Cursor::FillColour);
117 p.setPen(text_colour);
118 compute_text_size(p);
119 QRectF delta_rect(label_rect(rect));
121 const int radius = delta_rect.height() / 2;
122 const QRectF text_rect(delta_rect.intersected(
123 rect).adjusted(radius, 0, -radius, 0));
124 if (text_rect.width() >= text_size_.width()) {
125 const int highlight_radius = delta_rect.height() / 2 - 2;
128 p.setBrush(Qt::transparent);
129 p.setPen(highlight_pen());
130 p.drawRoundedRect(delta_rect, radius, radius);
133 p.setBrush(hover ? Cursor::FillColour.lighter() :
135 p.setPen(Cursor::FillColour.darker());
136 p.drawRoundedRect(delta_rect, radius, radius);
138 delta_rect.adjust(1, 1, -1, -1);
139 p.setPen(Cursor::FillColour.lighter());
140 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
142 p.setPen(text_colour);
143 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
148 void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp)
154 p.setBrush(QBrush(ViewportFillColour));
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(
160 offsets.first, offsets.second), (float)pp.width());
162 p.drawRect(l, pp.top(), r - l, pp.height());
165 QString CursorPair::format_string()
167 const pv::util::SIPrefix prefix = view_.tick_prefix();
168 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
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);
175 return QString("%1 / %2").arg(s1).arg(s2);
178 void CursorPair::compute_text_size(QPainter &p)
183 text_size_ = p.boundingRect(QRectF(), 0, format_string()).size();
186 pair<float, float> CursorPair::get_cursor_offsets() const
191 return pair<float, float>(
192 ((first_->time() - view_.offset()) / view_.scale()).convert_to<float>(),
193 ((second_->time() - view_.offset()) / view_.scale()).convert_to<float>());
196 } // namespace TraceView