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.h"
28 using boost::shared_ptr;
37 const int CursorPair::DeltaPadding = 8;
39 CursorPair::CursorPair(View &view) :
40 _first(new Cursor(view, 0.0)),
41 _second(new Cursor(view, 1.0)),
46 shared_ptr<Cursor> CursorPair::first() const
51 shared_ptr<Cursor> CursorPair::second() const
56 QRectF CursorPair::get_label_rect(const QRect &rect) const
58 const QSizeF label_size(
59 _text_size.width() + View::LabelPadding.width() * 2,
60 _text_size.height() + View::LabelPadding.height() * 2);
61 const pair<float, float> offsets(get_cursor_offsets());
62 const pair<float, float> normal_offsets(
63 (offsets.first < offsets.second) ? offsets :
64 make_pair(offsets.second, offsets.first));
66 const float height = label_size.height();
67 const float left = max(normal_offsets.first + DeltaPadding, -height);
68 const float right = min(normal_offsets.second - DeltaPadding,
69 (float)rect.width() + height);
71 return QRectF(left, rect.height() - label_size.height() -
72 Cursor::ArrowSize - Cursor::Offset - 0.5f,
73 right - left, height);
76 void CursorPair::draw_markers(QPainter &p,
77 const QRect &rect, unsigned int prefix)
82 compute_text_size(p, prefix);
83 QRectF delta_rect(get_label_rect(rect));
85 const int radius = delta_rect.height() / 2;
86 const QRectF text_rect(delta_rect.intersected(
87 rect).adjusted(radius, 0, -radius, 0));
88 if(text_rect.width() >= _text_size.width())
90 const int highlight_radius = delta_rect.height() / 2 - 2;
92 p.setBrush(Cursor::FillColour);
93 p.setPen(Cursor::LineColour);
94 p.drawRoundedRect(delta_rect, radius, radius);
96 delta_rect.adjust(1, 1, -1, -1);
97 p.setPen(Cursor::HighlightColour);
98 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
100 p.setPen(Cursor::TextColour);
101 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
102 pv::util::format_time(_second->time() - _first->time(), prefix, 2));
105 // Paint the cursor markers
106 _first->paint_label(p, rect, prefix);
107 _second->paint_label(p, rect, prefix);
110 void CursorPair::draw_viewport_background(QPainter &p,
114 p.setBrush(QBrush(View::CursorAreaColour));
116 const pair<float, float> offsets(get_cursor_offsets());
117 const int l = (int)max(min(
118 offsets.first, offsets.second), 0.0f);
119 const int r = (int)min(max(
120 offsets.first, offsets.second), (float)rect.width());
122 p.drawRect(l, 0, r - l, rect.height());
125 void CursorPair::draw_viewport_foreground(QPainter &p,
131 _first->paint(p, rect);
132 _second->paint(p, rect);
135 void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
140 _text_size = p.boundingRect(QRectF(), 0, pv::util::format_time(
141 _second->time() - _first->time(), prefix, 2)).size();
144 pair<float, float> CursorPair::get_cursor_offsets() const
149 return pair<float, float>(
150 (_first->time() - _view.offset()) / _view.scale(),
151 (_second->time() - _view.offset()) / _view.scale());