]>
Commit | Line | Data |
---|---|---|
cabce982 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 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 "cursor.h" | |
22 | ||
3a6fe081 | 23 | #include "ruler.h" |
cabce982 JH |
24 | #include "view.h" |
25 | ||
26 | #include <QBrush> | |
27 | #include <QPainter> | |
34c1ef02 | 28 | #include <QPointF> |
cabce982 JH |
29 | #include <QRect> |
30 | #include <QRectF> | |
31 | ||
34c1ef02 JH |
32 | #include <stdio.h> |
33 | ||
34 | #include <extdef.h> | |
35 | ||
58864c5c JH |
36 | using namespace boost; |
37 | ||
cabce982 JH |
38 | namespace pv { |
39 | namespace view { | |
40 | ||
41 | const QColor Cursor::LineColour(32, 74, 135); | |
42 | const QColor Cursor::FillColour(52, 101, 164); | |
43 | const QColor Cursor::HighlightColour(83, 130, 186); | |
44 | const QColor Cursor::TextColour(Qt::white); | |
45 | ||
cabce982 JH |
46 | const int Cursor::Offset = 1; |
47 | ||
34c1ef02 JH |
48 | const int Cursor::ArrowSize = 4; |
49 | ||
8debe10d | 50 | Cursor::Cursor(View &view, double time) : |
58864c5c | 51 | TimeMarker(view, LineColour, time) |
cabce982 JH |
52 | { |
53 | } | |
54 | ||
ca4ec3ea | 55 | QRectF Cursor::get_label_rect(const QRect &rect) const |
cabce982 | 56 | { |
58864c5c JH |
57 | const shared_ptr<Cursor> other(get_other_cursor()); |
58 | assert(other); | |
59 | ||
cabce982 | 60 | const float x = (_time - _view.offset()) / _view.scale(); |
34c1ef02 JH |
61 | |
62 | const QSizeF label_size( | |
63 | _text_size.width() + View::LabelPadding.width() * 2, | |
64 | _text_size.height() + View::LabelPadding.height() * 2); | |
199441e4 JH |
65 | const float top = rect.height() - label_size.height() - |
66 | Cursor::Offset - Cursor::ArrowSize - 0.5f; | |
5139748b | 67 | const float height = label_size.height(); |
199441e4 | 68 | |
58864c5c | 69 | if (_time > other->time()) |
199441e4 JH |
70 | return QRectF(x, top, label_size.width(), height); |
71 | else | |
72 | return QRectF(x - label_size.width(), top, | |
73 | label_size.width(), height); | |
ca4ec3ea JH |
74 | } |
75 | ||
3a6fe081 JH |
76 | void Cursor::paint_label(QPainter &p, const QRect &rect, |
77 | unsigned int prefix) | |
ca4ec3ea | 78 | { |
58864c5c JH |
79 | const shared_ptr<Cursor> other(get_other_cursor()); |
80 | assert(other); | |
81 | ||
3a6fe081 | 82 | compute_text_size(p, prefix); |
ca4ec3ea | 83 | const QRectF r(get_label_rect(rect)); |
cabce982 | 84 | |
f796cf51 JH |
85 | const QPointF left_points[] = { |
86 | r.topLeft(), | |
87 | r.topRight(), | |
88 | r.bottomRight(), | |
89 | QPointF(r.left() + ArrowSize, r.bottom()), | |
90 | QPointF(r.left(), rect.bottom()), | |
91 | }; | |
92 | ||
93 | const QPointF right_points[] = { | |
94 | r.topRight(), | |
95 | r.topLeft(), | |
96 | r.bottomLeft(), | |
97 | QPointF(r.right() - ArrowSize, r.bottom()), | |
98 | QPointF(r.right(), rect.bottom()), | |
99 | }; | |
100 | ||
101 | const QPointF left_highlight_points[] = { | |
102 | QPointF(r.left() + 1, r.top() + 1), | |
103 | QPointF(r.right() - 1, r.top() + 1), | |
104 | QPointF(r.right() - 1, r.bottom() - 1), | |
105 | QPointF(r.left() + ArrowSize - 1, r.bottom() - 1), | |
106 | QPointF(r.left() + 1, rect.bottom() - 1), | |
107 | }; | |
108 | ||
109 | const QPointF right_highlight_points[] = { | |
110 | QPointF(r.right() - 1, r.top() + 1), | |
111 | QPointF(r.left() + 1, r.top() + 1), | |
112 | QPointF(r.left() + 1, r.bottom() - 1), | |
113 | QPointF(r.right() - ArrowSize + 1, r.bottom() - 1), | |
114 | QPointF(r.right() - 1, rect.bottom() - 1), | |
115 | }; | |
116 | ||
58864c5c | 117 | const QPointF *const points = (_time > other->time()) ? |
f796cf51 | 118 | left_points : right_points; |
58864c5c | 119 | const QPointF *const highlight_points = (_time > other->time()) ? |
f796cf51 JH |
120 | left_highlight_points : right_highlight_points; |
121 | ||
122 | if (selected()) { | |
123 | p.setPen(highlight_pen()); | |
96d3ad83 | 124 | p.setBrush(Qt::transparent); |
f796cf51 | 125 | p.drawPolygon(points, countof(left_points)); |
96d3ad83 | 126 | } |
96d3ad83 | 127 | |
f796cf51 JH |
128 | p.setPen(Qt::transparent); |
129 | p.setBrush(FillColour); | |
130 | p.drawPolygon(points, countof(left_points)); | |
131 | ||
132 | p.setPen(HighlightColour); | |
133 | p.setBrush(Qt::transparent); | |
134 | p.drawPolygon(highlight_points, countof(left_highlight_points)); | |
135 | ||
136 | p.setPen(LineColour); | |
137 | p.setBrush(Qt::transparent); | |
138 | p.drawPolygon(points, countof(left_points)); | |
34c1ef02 JH |
139 | |
140 | p.setPen(TextColour); | |
3a6fe081 JH |
141 | p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, |
142 | Ruler::format_time(_time, prefix, 2)); | |
34c1ef02 JH |
143 | } |
144 | ||
3a6fe081 | 145 | void Cursor::compute_text_size(QPainter &p, unsigned int prefix) |
34c1ef02 | 146 | { |
3a6fe081 JH |
147 | _text_size = p.boundingRect(QRectF(), 0, |
148 | Ruler::format_time(_time, prefix, 2)).size(); | |
cabce982 JH |
149 | } |
150 | ||
58864c5c JH |
151 | shared_ptr<Cursor> Cursor::get_other_cursor() const |
152 | { | |
153 | const CursorPair &cursors = _view.cursors(); | |
154 | return (cursors.first().get() == this) ? | |
155 | cursors.second() : cursors.first(); | |
156 | } | |
157 | ||
cabce982 JH |
158 | } // namespace view |
159 | } // namespace pv |