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"
24 #include "pv/util.hpp"
32 using std::shared_ptr;
38 const int CursorPair::DeltaPadding = 8;
39 const QColor CursorPair::ViewportFillColour(220, 231, 243);
41 CursorPair::CursorPair(View &view) :
43 first_(new Cursor(view, 0.0)),
44 second_(new Cursor(view, 1.0))
48 bool CursorPair::enabled() const
50 return view_.cursors_shown();
53 shared_ptr<Cursor> CursorPair::first() const
58 shared_ptr<Cursor> CursorPair::second() const
63 void CursorPair::set_time(double time) {
64 const double delta = second_->time() - first_->time();
65 first_->set_time(time);
66 second_->set_time(time + delta);
69 float CursorPair::get_x() const
71 return (first_->get_x() + second_->get_x()) / 2.0f;
74 QPoint CursorPair::point(const QRect &rect) const
76 return first_->point(rect);
79 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
85 QRectF CursorPair::label_rect(const QRectF &rect) const
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));
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);
98 return QRectF(left, rect.height() - label_size.height() -
99 TimeMarker::ArrowSize - 0.5f,
100 right - left, height);
103 void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
111 const unsigned int prefix = view_.tick_prefix();
112 const QColor text_colour =
113 ViewItem::select_text_colour(Cursor::FillColour);
115 p.setPen(text_colour);
116 compute_text_size(p, prefix);
117 QRectF delta_rect(label_rect(rect));
119 const int radius = delta_rect.height() / 2;
120 const QRectF text_rect(delta_rect.intersected(
121 rect).adjusted(radius, 0, -radius, 0));
122 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,
143 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
147 void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) {
152 p.setBrush(QBrush(ViewportFillColour));
154 const pair<float, float> offsets(get_cursor_offsets());
155 const int l = (int)max(min(
156 offsets.first, offsets.second), 0.0f);
157 const int r = (int)min(max(
158 offsets.first, offsets.second), (float)pp.width());
160 p.drawRect(l, pp.top(), r - l, pp.height());
163 void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
168 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
169 second_->time() - first_->time(), prefix, 2)).size();
172 pair<float, float> CursorPair::get_cursor_offsets() const
177 return pair<float, float>(
178 (first_->time() - view_.offset()) / view_.scale(),
179 (second_->time() - view_.offset()) / view_.scale());