]> sigrok.org Git - pulseview.git/blame - pv/view/cursorpair.cpp
Added RowItemOwner
[pulseview.git] / pv / view / cursorpair.cpp
CommitLineData
b42d25c4
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "cursorpair.h"
22
23#include "view.h"
f0c9f81c 24#include "pv/util.h"
b42d25c4 25
f9abf97e 26#include <cassert>
0ba172cf
JH
27#include <algorithm>
28
819f4c25
JH
29using std::max;
30using std::make_pair;
31using std::min;
f9abf97e 32using std::shared_ptr;
819f4c25 33using std::pair;
0ba172cf 34
b42d25c4
JH
35namespace pv {
36namespace view {
37
5139748b
JH
38const int CursorPair::DeltaPadding = 8;
39
8debe10d 40CursorPair::CursorPair(View &view) :
58864c5c
JH
41 _first(new Cursor(view, 0.0)),
42 _second(new Cursor(view, 1.0)),
b42d25c4
JH
43 _view(view)
44{
45}
46
58864c5c 47shared_ptr<Cursor> CursorPair::first() const
b42d25c4
JH
48{
49 return _first;
50}
51
58864c5c 52shared_ptr<Cursor> CursorPair::second() const
b42d25c4
JH
53{
54 return _second;
55}
56
5139748b
JH
57QRectF CursorPair::get_label_rect(const QRect &rect) const
58{
59 const QSizeF label_size(
60 _text_size.width() + View::LabelPadding.width() * 2,
61 _text_size.height() + View::LabelPadding.height() * 2);
62 const pair<float, float> offsets(get_cursor_offsets());
63 const pair<float, float> normal_offsets(
64 (offsets.first < offsets.second) ? offsets :
65 make_pair(offsets.second, offsets.first));
66
67 const float height = label_size.height();
68 const float left = max(normal_offsets.first + DeltaPadding, -height);
69 const float right = min(normal_offsets.second - DeltaPadding,
70 (float)rect.width() + height);
71
72 return QRectF(left, rect.height() - label_size.height() -
73 Cursor::ArrowSize - Cursor::Offset - 0.5f,
74 right - left, height);
75}
76
332afa74
JH
77void CursorPair::draw_markers(QPainter &p,
78 const QRect &rect, unsigned int prefix)
79{
58864c5c
JH
80 assert(_first);
81 assert(_second);
82
5139748b
JH
83 compute_text_size(p, prefix);
84 QRectF delta_rect(get_label_rect(rect));
85
86 const int radius = delta_rect.height() / 2;
87 const QRectF text_rect(delta_rect.intersected(
88 rect).adjusted(radius, 0, -radius, 0));
89 if(text_rect.width() >= _text_size.width())
90 {
91 const int highlight_radius = delta_rect.height() / 2 - 2;
92
93 p.setBrush(Cursor::FillColour);
94 p.setPen(Cursor::LineColour);
95 p.drawRoundedRect(delta_rect, radius, radius);
96
97 delta_rect.adjust(1, 1, -1, -1);
98 p.setPen(Cursor::HighlightColour);
99 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
100
101 p.setPen(Cursor::TextColour);
102 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
f0c9f81c 103 pv::util::format_time(_second->time() - _first->time(), prefix, 2));
5139748b
JH
104 }
105
106 // Paint the cursor markers
58864c5c
JH
107 _first->paint_label(p, rect, prefix);
108 _second->paint_label(p, rect, prefix);
332afa74
JH
109}
110
0ba172cf
JH
111void CursorPair::draw_viewport_background(QPainter &p,
112 const QRect &rect)
113{
114 p.setPen(Qt::NoPen);
115 p.setBrush(QBrush(View::CursorAreaColour));
116
199441e4
JH
117 const pair<float, float> offsets(get_cursor_offsets());
118 const int l = (int)max(min(
119 offsets.first, offsets.second), 0.0f);
120 const int r = (int)min(max(
121 offsets.first, offsets.second), (float)rect.width());
0ba172cf
JH
122
123 p.drawRect(l, 0, r - l, rect.height());
124}
125
126void CursorPair::draw_viewport_foreground(QPainter &p,
127 const QRect &rect)
128{
58864c5c
JH
129 assert(_first);
130 assert(_second);
131
132 _first->paint(p, rect);
133 _second->paint(p, rect);
0ba172cf
JH
134}
135
5139748b
JH
136void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
137{
58864c5c
JH
138 assert(_first);
139 assert(_second);
140
f0c9f81c 141 _text_size = p.boundingRect(QRectF(), 0, pv::util::format_time(
58864c5c 142 _second->time() - _first->time(), prefix, 2)).size();
5139748b
JH
143}
144
199441e4
JH
145pair<float, float> CursorPair::get_cursor_offsets() const
146{
58864c5c
JH
147 assert(_first);
148 assert(_second);
149
199441e4 150 return pair<float, float>(
58864c5c
JH
151 (_first->time() - _view.offset()) / _view.scale(),
152 (_second->time() - _view.offset()) / _view.scale());
199441e4
JH
153}
154
b42d25c4
JH
155} // namespace view
156} // namespace pv