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