]> sigrok.org Git - pulseview.git/blob - pv/view/cursor.cpp
809a0501b20da1a4be759451fb0d51b717a4dff6
[pulseview.git] / pv / view / cursor.cpp
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
23 #include "ruler.h"
24 #include "view.h"
25
26 #include <QBrush>
27 #include <QPainter>
28 #include <QPointF>
29 #include <QRect>
30 #include <QRectF>
31
32 #include <stdio.h>
33
34 #include <extdef.h>
35
36 namespace pv {
37 namespace view {
38
39 const QColor Cursor::LineColour(32, 74, 135);
40 const QColor Cursor::FillColour(52, 101, 164);
41 const QColor Cursor::HighlightColour(83, 130, 186);
42 const QColor Cursor::TextColour(Qt::white);
43
44 const int Cursor::Offset = 1;
45
46 const int Cursor::ArrowSize = 4;
47
48 Cursor::Cursor(const View &view, double time, Cursor &other) :
49         TimeMarker(view, LineColour, time),
50         _other(other)
51 {
52 }
53
54 QRectF Cursor::get_label_rect(const QRect &rect) const
55 {
56         const float x = (_time - _view.offset()) / _view.scale();
57
58         const QSizeF label_size(
59                 _text_size.width() + View::LabelPadding.width() * 2,
60                 _text_size.height() + View::LabelPadding.height() * 2);
61         const float top = rect.height() - label_size.height() -
62                 Cursor::Offset - Cursor::ArrowSize - 0.5f;
63         const float height = label_size.height() + 1;
64
65         if (_time > _other.time())
66                 return QRectF(x, top, label_size.width(), height);
67         else
68                 return QRectF(x - label_size.width(), top,
69                         label_size.width(), height);
70 }
71
72 void Cursor::paint_label(QPainter &p, const QRect &rect,
73         unsigned int prefix)
74 {
75         compute_text_size(p, prefix);
76         const QRectF r(get_label_rect(rect));
77
78         if (_time > _other.time())
79         {
80                 const QPointF points[] = {
81                         r.topLeft(),
82                         r.topRight(),
83                         r.bottomRight(),
84                         QPointF(r.left() + ArrowSize, r.bottom()),
85                         QPointF(r.left(), rect.bottom()),
86                 };
87
88                 const QPointF highlight_points[] = {
89                         QPointF(r.left() + 1, r.top() + 1),
90                         QPointF(r.right() - 1, r.top() + 1),
91                         QPointF(r.right() - 1, r.bottom() - 1),
92                         QPointF(r.left() + ArrowSize - 1, r.bottom() - 1),
93                         QPointF(r.left() + 1, rect.bottom() - 1),
94                 };
95
96                 p.setPen(Qt::transparent);
97                 p.setBrush(FillColour);
98                 p.drawPolygon(points, countof(points));
99
100                 p.setPen(HighlightColour);
101                 p.setBrush(Qt::transparent);
102                 p.drawPolygon(highlight_points, countof(highlight_points));
103
104                 p.setPen(LineColour);
105                 p.setBrush(Qt::transparent);
106                 p.drawPolygon(points, countof(points));
107         }
108         else
109         {
110                 const QPointF points[] = {
111                         r.topRight(),
112                         r.topLeft(),
113                         r.bottomLeft(),
114                         QPointF(r.right() - ArrowSize, r.bottom()),
115                         QPointF(r.right(), rect.bottom()),
116                 };
117
118                 const QPointF highlight_points[] = {
119                         QPointF(r.right() - 1, r.top() + 1),
120                         QPointF(r.left() + 1, r.top() + 1),
121                         QPointF(r.left() + 1, r.bottom() - 1),
122                         QPointF(r.right() - ArrowSize + 1, r.bottom() - 1),
123                         QPointF(r.right() - 1, rect.bottom() - 1),
124                 };
125
126                 p.setPen(Qt::transparent);
127                 p.setBrush(FillColour);
128                 p.drawPolygon(points, countof(points));
129
130                 p.setPen(HighlightColour);
131                 p.setBrush(Qt::transparent);
132                 p.drawPolygon(highlight_points, countof(highlight_points));
133
134                 p.setPen(LineColour);
135                 p.setBrush(Qt::transparent);
136                 p.drawPolygon(points, countof(points));
137         }
138
139         p.setPen(TextColour);
140         p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter,
141                 Ruler::format_time(_time, prefix, 2));
142 }
143
144 void Cursor::compute_text_size(QPainter &p, unsigned int prefix)
145 {
146         _text_size = p.boundingRect(QRectF(), 0,
147                 Ruler::format_time(_time, prefix, 2)).size();
148 }
149
150 } // namespace view
151 } // namespace pv