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