]> sigrok.org Git - pulseview.git/blame - pv/view/cursor.cpp
Added selection_changed signal to pv::view::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
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
cabce982
JH
36namespace pv {
37namespace view {
38
39const QColor Cursor::LineColour(32, 74, 135);
40const QColor Cursor::FillColour(52, 101, 164);
41const QColor Cursor::HighlightColour(83, 130, 186);
42const QColor Cursor::TextColour(Qt::white);
43
cabce982
JH
44const int Cursor::Offset = 1;
45
34c1ef02
JH
46const int Cursor::ArrowSize = 4;
47
96d3ad83
JH
48Cursor::Cursor(const View &view, double time, Cursor &other) :
49 TimeMarker(view, LineColour, time),
50 _other(other)
cabce982
JH
51{
52}
53
ca4ec3ea 54QRectF Cursor::get_label_rect(const QRect &rect) const
cabce982
JH
55{
56 const float x = (_time - _view.offset()) / _view.scale();
34c1ef02
JH
57
58 const QSizeF label_size(
59 _text_size.width() + View::LabelPadding.width() * 2,
60 _text_size.height() + View::LabelPadding.height() * 2);
199441e4
JH
61 const float top = rect.height() - label_size.height() -
62 Cursor::Offset - Cursor::ArrowSize - 0.5f;
5139748b 63 const float height = label_size.height();
199441e4
JH
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);
ca4ec3ea
JH
70}
71
3a6fe081
JH
72void Cursor::paint_label(QPainter &p, const QRect &rect,
73 unsigned int prefix)
ca4ec3ea 74{
3a6fe081 75 compute_text_size(p, prefix);
ca4ec3ea 76 const QRectF r(get_label_rect(rect));
cabce982 77
f796cf51
JH
78 const QPointF left_points[] = {
79 r.topLeft(),
80 r.topRight(),
81 r.bottomRight(),
82 QPointF(r.left() + ArrowSize, r.bottom()),
83 QPointF(r.left(), rect.bottom()),
84 };
85
86 const QPointF right_points[] = {
87 r.topRight(),
88 r.topLeft(),
89 r.bottomLeft(),
90 QPointF(r.right() - ArrowSize, r.bottom()),
91 QPointF(r.right(), rect.bottom()),
92 };
93
94 const QPointF left_highlight_points[] = {
95 QPointF(r.left() + 1, r.top() + 1),
96 QPointF(r.right() - 1, r.top() + 1),
97 QPointF(r.right() - 1, r.bottom() - 1),
98 QPointF(r.left() + ArrowSize - 1, r.bottom() - 1),
99 QPointF(r.left() + 1, rect.bottom() - 1),
100 };
101
102 const QPointF right_highlight_points[] = {
103 QPointF(r.right() - 1, r.top() + 1),
104 QPointF(r.left() + 1, r.top() + 1),
105 QPointF(r.left() + 1, r.bottom() - 1),
106 QPointF(r.right() - ArrowSize + 1, r.bottom() - 1),
107 QPointF(r.right() - 1, rect.bottom() - 1),
108 };
109
110 const QPointF *const points = (_time > _other.time()) ?
111 left_points : right_points;
112 const QPointF *const highlight_points = (_time > _other.time()) ?
113 left_highlight_points : right_highlight_points;
114
115 if (selected()) {
116 p.setPen(highlight_pen());
96d3ad83 117 p.setBrush(Qt::transparent);
f796cf51 118 p.drawPolygon(points, countof(left_points));
96d3ad83 119 }
96d3ad83 120
f796cf51
JH
121 p.setPen(Qt::transparent);
122 p.setBrush(FillColour);
123 p.drawPolygon(points, countof(left_points));
124
125 p.setPen(HighlightColour);
126 p.setBrush(Qt::transparent);
127 p.drawPolygon(highlight_points, countof(left_highlight_points));
128
129 p.setPen(LineColour);
130 p.setBrush(Qt::transparent);
131 p.drawPolygon(points, countof(left_points));
34c1ef02
JH
132
133 p.setPen(TextColour);
3a6fe081
JH
134 p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter,
135 Ruler::format_time(_time, prefix, 2));
34c1ef02
JH
136}
137
3a6fe081 138void Cursor::compute_text_size(QPainter &p, unsigned int prefix)
34c1ef02 139{
3a6fe081
JH
140 _text_size = p.boundingRect(QRectF(), 0,
141 Ruler::format_time(_time, prefix, 2)).size();
cabce982
JH
142}
143
144} // namespace view
145} // namespace pv