]> sigrok.org Git - pulseview.git/blame - pv/view/cursorpair.cpp
Don't allow disabled probes to be selected
[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
5139748b 23#include "ruler.h"
b42d25c4
JH
24#include "view.h"
25
0ba172cf
JH
26#include <algorithm>
27
819f4c25
JH
28using boost::shared_ptr;
29using std::max;
30using std::make_pair;
31using std::min;
32using std::pair;
0ba172cf 33
b42d25c4
JH
34namespace pv {
35namespace view {
36
5139748b
JH
37const int CursorPair::DeltaPadding = 8;
38
8debe10d 39CursorPair::CursorPair(View &view) :
58864c5c
JH
40 _first(new Cursor(view, 0.0)),
41 _second(new Cursor(view, 1.0)),
b42d25c4
JH
42 _view(view)
43{
44}
45
58864c5c 46shared_ptr<Cursor> CursorPair::first() const
b42d25c4
JH
47{
48 return _first;
49}
50
58864c5c 51shared_ptr<Cursor> CursorPair::second() const
b42d25c4
JH
52{
53 return _second;
54}
55
5139748b
JH
56QRectF CursorPair::get_label_rect(const QRect &rect) const
57{
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));
65
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);
70
71 return QRectF(left, rect.height() - label_size.height() -
72 Cursor::ArrowSize - Cursor::Offset - 0.5f,
73 right - left, height);
74}
75
332afa74
JH
76void CursorPair::draw_markers(QPainter &p,
77 const QRect &rect, unsigned int prefix)
78{
58864c5c
JH
79 assert(_first);
80 assert(_second);
81
5139748b
JH
82 compute_text_size(p, prefix);
83 QRectF delta_rect(get_label_rect(rect));
84
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())
89 {
90 const int highlight_radius = delta_rect.height() / 2 - 2;
91
92 p.setBrush(Cursor::FillColour);
93 p.setPen(Cursor::LineColour);
94 p.drawRoundedRect(delta_rect, radius, radius);
95
96 delta_rect.adjust(1, 1, -1, -1);
97 p.setPen(Cursor::HighlightColour);
98 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
99
100 p.setPen(Cursor::TextColour);
101 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
58864c5c 102 Ruler::format_time(_second->time() - _first->time(), prefix, 2));
5139748b
JH
103 }
104
105 // Paint the cursor markers
58864c5c
JH
106 _first->paint_label(p, rect, prefix);
107 _second->paint_label(p, rect, prefix);
332afa74
JH
108}
109
0ba172cf
JH
110void CursorPair::draw_viewport_background(QPainter &p,
111 const QRect &rect)
112{
113 p.setPen(Qt::NoPen);
114 p.setBrush(QBrush(View::CursorAreaColour));
115
199441e4
JH
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());
0ba172cf
JH
121
122 p.drawRect(l, 0, r - l, rect.height());
123}
124
125void CursorPair::draw_viewport_foreground(QPainter &p,
126 const QRect &rect)
127{
58864c5c
JH
128 assert(_first);
129 assert(_second);
130
131 _first->paint(p, rect);
132 _second->paint(p, rect);
0ba172cf
JH
133}
134
5139748b
JH
135void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
136{
58864c5c
JH
137 assert(_first);
138 assert(_second);
139
5139748b 140 _text_size = p.boundingRect(QRectF(), 0, Ruler::format_time(
58864c5c 141 _second->time() - _first->time(), prefix, 2)).size();
5139748b
JH
142}
143
199441e4
JH
144pair<float, float> CursorPair::get_cursor_offsets() const
145{
58864c5c
JH
146 assert(_first);
147 assert(_second);
148
199441e4 149 return pair<float, float>(
58864c5c
JH
150 (_first->time() - _view.offset()) / _view.scale(),
151 (_second->time() - _view.offset()) / _view.scale());
199441e4
JH
152}
153
b42d25c4
JH
154} // namespace view
155} // namespace pv