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