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