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/>.
26 #include "cursorpair.hpp"
28 #include "pv/util.hpp"
35 using std::shared_ptr;
42 const int CursorPair::DeltaPadding = 8;
43 const QColor CursorPair::ViewportFillColor(220, 231, 243);
45 CursorPair::CursorPair(View &view) :
47 first_(new Cursor(view, 0.0)),
48 second_(new Cursor(view, 1.0))
50 connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
51 this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
54 bool CursorPair::enabled() const
56 return view_.cursors_shown();
59 shared_ptr<Cursor> CursorPair::first() const
64 shared_ptr<Cursor> CursorPair::second() const
69 void CursorPair::set_time(const pv::util::Timestamp& time)
71 const pv::util::Timestamp delta = second_->time() - first_->time();
72 first_->set_time(time);
73 second_->set_time(time + delta);
76 float CursorPair::get_x() const
78 return (first_->get_x() + second_->get_x()) / 2.0f;
81 QPoint CursorPair::drag_point(const QRect &rect) const
83 return first_->drag_point(rect);
86 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
92 QRectF CursorPair::label_rect(const QRectF &rect) const
94 const QSizeF label_size(text_size_ + LabelPadding * 2);
95 const pair<float, float> offsets(get_cursor_offsets());
96 const pair<float, float> normal_offsets(
97 (offsets.first < offsets.second) ? offsets :
98 make_pair(offsets.second, offsets.first));
100 const float height = label_size.height();
101 const float left = max(normal_offsets.first + DeltaPadding, -height);
102 const float right = min(normal_offsets.second - DeltaPadding,
103 (float)rect.width() + height);
105 return QRectF(left, rect.height() - label_size.height() -
106 TimeMarker::ArrowSize - 0.5f,
107 right - left, height);
110 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
118 const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
119 p.setPen(text_color);
121 QString text = format_string();
122 text_size_ = p.boundingRect(QRectF(), 0, text).size();
124 QRectF delta_rect(label_rect(rect));
125 const int radius = delta_rect.height() / 2;
126 QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
128 if (text_rect.width() < text_size_.width()) {
130 text_size_ = p.boundingRect(QRectF(), 0, text).size();
131 label_incomplete_ = true;
133 label_incomplete_ = false;
136 p.setBrush(Qt::transparent);
137 p.setPen(highlight_pen());
138 p.drawRoundedRect(delta_rect, radius, radius);
141 p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
142 p.setPen(Cursor::FillColor.darker());
143 p.drawRoundedRect(delta_rect, radius, radius);
145 delta_rect.adjust(1, 1, -1, -1);
146 p.setPen(Cursor::FillColor.lighter());
147 const int highlight_radius = delta_rect.height() / 2 - 2;
148 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
149 label_area_ = delta_rect;
151 p.setPen(text_color);
152 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
155 void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
161 p.setBrush(QBrush(ViewportFillColor));
163 const pair<float, float> offsets(get_cursor_offsets());
164 const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
165 const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
167 p.drawRect(l, pp.top(), r - l, pp.height());
170 QString CursorPair::format_string()
172 const pv::util::SIPrefix prefix = view_.tick_prefix();
173 const pv::util::Timestamp diff = abs(second_->time() - first_->time());
175 const QString s1 = Ruler::format_time_with_distance(
176 diff, diff, prefix, view_.time_unit(), 12, false); /* Always use 12 precision digits */
177 const QString s2 = util::format_time_si(
178 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false);
180 return QString("%1 / %2").arg(s1, s2);
183 pair<float, float> CursorPair::get_cursor_offsets() const
188 return pair<float, float>(first_->get_x(), second_->get_x());
191 void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
193 if (widget != view_.ruler())
196 if (!label_incomplete_)
199 if (label_area_.contains(hp))
200 QToolTip::showText(view_.mapToGlobal(hp), format_string());
202 QToolTip::hideText(); // TODO Will break other tooltips when there can be others