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;
40 CursorPair::CursorPair(View &view) :
42 first_(new Cursor(view, 0.0)),
43 second_(new Cursor(view, 1.0))
47 bool CursorPair::enabled() const
49 return view_.cursors_shown();
52 shared_ptr<Cursor> CursorPair::first() const
57 shared_ptr<Cursor> CursorPair::second() const
62 void CursorPair::set_time(double time) {
63 const double delta = second_->time() - first_->time();
64 first_->set_time(time);
65 second_->set_time(time + delta);
68 float CursorPair::get_x() const
70 return (first_->get_x() + second_->get_x()) / 2.0f;
73 QPoint CursorPair::point() const
75 return first_->point();
78 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
84 QRectF CursorPair::label_rect(const QRectF &rect) const
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));
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);
99 return QRectF(left, rect.height() - label_size.height() -
100 TimeMarker::ArrowSize - TimeMarker::Offset - 0.5f,
101 right - left, height);
104 void CursorPair::draw_markers(QPainter &p, const QRect &rect)
109 const unsigned int prefix = view_.tick_prefix();
111 compute_text_size(p, prefix);
112 QRectF delta_rect(label_rect(rect));
114 const int radius = delta_rect.height() / 2;
115 const QRectF text_rect(delta_rect.intersected(
116 rect).adjusted(radius, 0, -radius, 0));
117 if(text_rect.width() >= text_size_.width())
119 const int highlight_radius = delta_rect.height() / 2 - 2;
121 p.setBrush(Cursor::FillColour);
122 p.setPen(Cursor::FillColour.darker());
123 p.drawRoundedRect(delta_rect, radius, radius);
125 delta_rect.adjust(1, 1, -1, -1);
126 p.setPen(Cursor::FillColour.lighter());
127 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
129 p.setPen(SelectableItem::select_text_colour(
130 Cursor::FillColour));
131 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
132 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
135 // Paint the cursor markers
136 first_->paint_label(p, rect);
137 second_->paint_label(p, rect);
140 void CursorPair::draw_viewport_background(QPainter &p,
144 p.setBrush(QBrush(View::CursorAreaColour));
146 const pair<float, float> offsets(get_cursor_offsets());
147 const int l = (int)max(min(
148 offsets.first, offsets.second), 0.0f);
149 const int r = (int)min(max(
150 offsets.first, offsets.second), (float)rect.width());
152 p.drawRect(l, 0, r - l, rect.height());
155 void CursorPair::draw_viewport_foreground(QPainter &p,
161 first_->paint(p, rect);
162 second_->paint(p, rect);
165 void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
170 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
171 second_->time() - first_->time(), prefix, 2)).size();
174 pair<float, float> CursorPair::get_cursor_offsets() const
179 return pair<float, float>(
180 (first_->time() - view_.offset()) / view_.scale(),
181 (second_->time() - view_.offset()) / view_.scale());