]> sigrok.org Git - pulseview.git/blame - pv/view/cursorpair.cpp
TimeMarker: Added get_text
[pulseview.git] / pv / view / cursorpair.cpp
CommitLineData
b42d25c4
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 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 "cursorpair.hpp"
b42d25c4 22
2acdb232
JH
23#include "view.hpp"
24#include "pv/util.hpp"
b42d25c4 25
f9abf97e 26#include <cassert>
0ba172cf
JH
27#include <algorithm>
28
819f4c25
JH
29using std::max;
30using std::make_pair;
31using std::min;
f9abf97e 32using std::shared_ptr;
819f4c25 33using std::pair;
0ba172cf 34
b42d25c4
JH
35namespace pv {
36namespace view {
37
5139748b
JH
38const int CursorPair::DeltaPadding = 8;
39
8debe10d 40CursorPair::CursorPair(View &view) :
8dbbc7f0
JH
41 first_(new Cursor(view, 0.0)),
42 second_(new Cursor(view, 1.0)),
43 view_(view)
b42d25c4
JH
44{
45}
46
58864c5c 47shared_ptr<Cursor> CursorPair::first() const
b42d25c4 48{
8dbbc7f0 49 return first_;
b42d25c4
JH
50}
51
58864c5c 52shared_ptr<Cursor> CursorPair::second() const
b42d25c4 53{
8dbbc7f0 54 return second_;
b42d25c4
JH
55}
56
5139748b
JH
57QRectF CursorPair::get_label_rect(const QRect &rect) const
58{
59 const QSizeF label_size(
8dbbc7f0
JH
60 text_size_.width() + View::LabelPadding.width() * 2,
61 text_size_.height() + View::LabelPadding.height() * 2);
5139748b
JH
62 const pair<float, float> offsets(get_cursor_offsets());
63 const pair<float, float> normal_offsets(
64 (offsets.first < offsets.second) ? offsets :
65 make_pair(offsets.second, offsets.first));
66
67 const float height = label_size.height();
68 const float left = max(normal_offsets.first + DeltaPadding, -height);
69 const float right = min(normal_offsets.second - DeltaPadding,
70 (float)rect.width() + height);
71
72 return QRectF(left, rect.height() - label_size.height() -
4fabd61a 73 TimeMarker::ArrowSize - TimeMarker::Offset - 0.5f,
5139748b
JH
74 right - left, height);
75}
76
361c560e 77void CursorPair::draw_markers(QPainter &p, const QRect &rect)
332afa74 78{
8dbbc7f0
JH
79 assert(first_);
80 assert(second_);
58864c5c 81
361c560e
JH
82 const unsigned int prefix = view_.tick_prefix();
83
5139748b
JH
84 compute_text_size(p, prefix);
85 QRectF delta_rect(get_label_rect(rect));
86
87 const int radius = delta_rect.height() / 2;
88 const QRectF text_rect(delta_rect.intersected(
89 rect).adjusted(radius, 0, -radius, 0));
8dbbc7f0 90 if(text_rect.width() >= text_size_.width())
5139748b
JH
91 {
92 const int highlight_radius = delta_rect.height() / 2 - 2;
93
94 p.setBrush(Cursor::FillColour);
4fabd61a 95 p.setPen(Cursor::FillColour.darker());
5139748b
JH
96 p.drawRoundedRect(delta_rect, radius, radius);
97
98 delta_rect.adjust(1, 1, -1, -1);
4fabd61a 99 p.setPen(Cursor::FillColour.lighter());
5139748b
JH
100 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
101
4fabd61a
JH
102 p.setPen(SelectableItem::select_text_colour(
103 Cursor::FillColour));
5139748b 104 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
8dbbc7f0 105 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
5139748b
JH
106 }
107
108 // Paint the cursor markers
361c560e
JH
109 first_->paint_label(p, rect);
110 second_->paint_label(p, rect);
332afa74
JH
111}
112
0ba172cf
JH
113void CursorPair::draw_viewport_background(QPainter &p,
114 const QRect &rect)
115{
116 p.setPen(Qt::NoPen);
117 p.setBrush(QBrush(View::CursorAreaColour));
118
199441e4
JH
119 const pair<float, float> offsets(get_cursor_offsets());
120 const int l = (int)max(min(
121 offsets.first, offsets.second), 0.0f);
122 const int r = (int)min(max(
123 offsets.first, offsets.second), (float)rect.width());
0ba172cf
JH
124
125 p.drawRect(l, 0, r - l, rect.height());
126}
127
128void CursorPair::draw_viewport_foreground(QPainter &p,
129 const QRect &rect)
130{
8dbbc7f0
JH
131 assert(first_);
132 assert(second_);
58864c5c 133
8dbbc7f0
JH
134 first_->paint(p, rect);
135 second_->paint(p, rect);
0ba172cf
JH
136}
137
5139748b
JH
138void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
139{
8dbbc7f0
JH
140 assert(first_);
141 assert(second_);
58864c5c 142
8dbbc7f0
JH
143 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
144 second_->time() - first_->time(), prefix, 2)).size();
5139748b
JH
145}
146
199441e4
JH
147pair<float, float> CursorPair::get_cursor_offsets() const
148{
8dbbc7f0
JH
149 assert(first_);
150 assert(second_);
58864c5c 151
199441e4 152 return pair<float, float>(
8dbbc7f0
JH
153 (first_->time() - view_.offset()) / view_.scale(),
154 (second_->time() - view_.offset()) / view_.scale());
199441e4
JH
155}
156
b42d25c4
JH
157} // namespace view
158} // namespace pv