]> sigrok.org Git - pulseview.git/blob - pv/view/cursor.cpp
Initial code to render text inside cursor markers
[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 "view.h"
24
25 #include <QBrush>
26 #include <QPainter>
27 #include <QPointF>
28 #include <QRect>
29 #include <QRectF>
30
31 #include <stdio.h>
32
33 #include <extdef.h>
34
35 namespace pv {
36 namespace view {
37
38 const QColor Cursor::LineColour(32, 74, 135);
39 const QColor Cursor::FillColour(52, 101, 164);
40 const QColor Cursor::HighlightColour(83, 130, 186);
41 const QColor Cursor::TextColour(Qt::white);
42
43 const int Cursor::Size = 12;
44 const int Cursor::Offset = 1;
45
46 const int Cursor::ArrowSize = 4;
47
48 Cursor::Cursor(const View &view, double time) :
49         TimeMarker(view, LineColour, time)
50 {
51 }
52
53 QRectF Cursor::get_label_rect(const QRect &rect) const
54 {
55         const float x = (_time - _view.offset()) / _view.scale();
56
57         const QSizeF label_size(
58                 _text_size.width() + View::LabelPadding.width() * 2,
59                 _text_size.height() + View::LabelPadding.height() * 2);
60         return QRectF(x - label_size.width() / 2 - 0.5f,
61                 rect.height() - label_size.height() - Offset - ArrowSize - 0.5f,
62                 label_size.width() + 1, label_size.height() + 1);
63 }
64
65 void Cursor::paint_label(QPainter &p, const QRect &rect)
66 {
67         compute_text_size(p);
68         const QRectF r(get_label_rect(rect));
69
70         const float h_centre = (r.left() + r.right()) / 2;
71         const QPointF points[] = {
72                 r.topRight(),
73                 QPointF(r.right(), r.bottom()),
74                 QPointF(h_centre + ArrowSize, r.bottom()),
75                 QPointF(h_centre, rect.bottom()),
76                 QPointF(h_centre - ArrowSize, r.bottom()),
77                 QPointF(r.left(), r.bottom()),
78                 r.topLeft()
79         };
80
81         const QPointF highlight_points[] = {
82                 QPointF(r.right() - 1, r.top() + 1),
83                 QPointF(r.right() - 1, r.bottom() - 1),
84                 QPointF(h_centre + ArrowSize - 1, r.bottom() - 1),
85                 QPointF(h_centre, rect.bottom() - 1),
86                 QPointF(h_centre - ArrowSize + 1, r.bottom() - 1),
87                 QPointF(r.left() + 1, r.bottom() - 1),
88                 QPointF(r.left() + 1, r.top() + 1),
89         };
90
91         char text[16];
92         format_text(text);
93
94         p.setPen(Qt::transparent);
95         p.setBrush(FillColour);
96         p.drawPolygon(points, countof(points));
97
98         p.setPen(HighlightColour);
99         p.setBrush(Qt::transparent);
100         p.drawPolygon(highlight_points, countof(highlight_points));
101
102         p.setPen(LineColour);
103         p.setBrush(Qt::transparent);
104         p.drawPolygon(points, countof(points));
105
106         p.setPen(TextColour);
107         p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, text);
108 }
109
110 void Cursor::compute_text_size(QPainter &p)
111 {
112         char text[16];
113         format_text(text);
114         _text_size = p.boundingRect(QRectF(), 0, text).size();
115 }
116
117 void Cursor::format_text(char *text)
118 {
119         sprintf(text, "%gs", _time);
120 }
121
122 } // namespace view
123 } // namespace pv