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, see <http://www.gnu.org/licenses/>.
20 #include "cursorpair.hpp"
22 #include "pv/util.hpp"
32 using std::shared_ptr;
39 const int CursorPair::DeltaPadding = 8;
40 const QColor CursorPair::ViewportFillColour(220, 231, 243);
42 CursorPair::CursorPair(View &view) :
44 first_(new Cursor(view, 0.0)),
45 second_(new Cursor(view, 1.0))
49 bool CursorPair::enabled() const
51 return view_.cursors_shown();
54 shared_ptr<Cursor> CursorPair::first() const
59 shared_ptr<Cursor> CursorPair::second() const
64 void CursorPair::set_time(const pv::util::Timestamp& time)
66 const pv::util::Timestamp delta = second_->time() - first_->time();
67 first_->set_time(time);
68 second_->set_time(time + delta);
71 float CursorPair::get_x() const
73 return (first_->get_x() + second_->get_x()) / 2.0f;
76 QPoint CursorPair::point(const QRect &rect) const
78 return first_->point(rect);
81 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
87 QRectF CursorPair::label_rect(const QRectF &rect) const
89 const QSizeF label_size(text_size_ + LabelPadding * 2);
90 const pair<float, float> offsets(get_cursor_offsets());
91 const pair<float, float> normal_offsets(
92 (offsets.first < offsets.second) ? offsets :
93 make_pair(offsets.second, offsets.first));
95 const float height = label_size.height();
96 const float left = max(normal_offsets.first + DeltaPadding, -height);
97 const float right = min(normal_offsets.second - DeltaPadding,
98 (float)rect.width() + height);
100 return QRectF(left, rect.height() - label_size.height() -
101 TimeMarker::ArrowSize - 0.5f,
102 right - left, height);
105 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
113 const QColor text_colour =
114 ViewItem::select_text_colour(Cursor::FillColour);
116 p.setPen(text_colour);
117 compute_text_size(p);
118 QRectF delta_rect(label_rect(rect));
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 const int highlight_radius = delta_rect.height() / 2 - 2;
127 p.setBrush(Qt::transparent);
128 p.setPen(highlight_pen());
129 p.drawRoundedRect(delta_rect, radius, radius);
132 p.setBrush(hover ? Cursor::FillColour.lighter() :
134 p.setPen(Cursor::FillColour.darker());
135 p.drawRoundedRect(delta_rect, radius, radius);
137 delta_rect.adjust(1, 1, -1, -1);
138 p.setPen(Cursor::FillColour.lighter());
139 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
141 p.setPen(text_colour);
142 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
147 void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
153 p.setBrush(QBrush(ViewportFillColour));
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());
161 p.drawRect(l, pp.top(), r - l, pp.height());
164 QString CursorPair::format_string()
166 const pv::util::SIPrefix prefix = view_.tick_prefix();
167 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
169 const QString s1 = Ruler::format_time_with_distance(
170 diff, diff, prefix, view_.time_unit(), view_.tick_precision(), false);
171 const QString s2 = util::format_time_si(
172 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false);
174 return QString("%1 / %2").arg(s1).arg(s2);
177 void CursorPair::compute_text_size(QPainter &p)
182 text_size_ = p.boundingRect(QRectF(), 0, format_string()).size();
185 pair<float, float> CursorPair::get_cursor_offsets() const
190 return pair<float, float>(first_->get_x(), second_->get_x());
193 } // namespace TraceView