]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/cursorpair.cpp
Ruler: Moved calculate_tick_spacing into View
[pulseview.git] / pv / view / cursorpair.cpp
... / ...
CommitLineData
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
21#include "cursorpair.hpp"
22
23#include "view.hpp"
24#include "pv/util.hpp"
25
26#include <cassert>
27#include <algorithm>
28
29using std::max;
30using std::make_pair;
31using std::min;
32using std::shared_ptr;
33using std::pair;
34
35namespace pv {
36namespace view {
37
38const int CursorPair::DeltaPadding = 8;
39
40CursorPair::CursorPair(View &view) :
41 first_(new Cursor(view, 0.0)),
42 second_(new Cursor(view, 1.0)),
43 view_(view)
44{
45}
46
47shared_ptr<Cursor> CursorPair::first() const
48{
49 return first_;
50}
51
52shared_ptr<Cursor> CursorPair::second() const
53{
54 return second_;
55}
56
57QRectF CursorPair::get_label_rect(const QRect &rect) const
58{
59 const QSizeF label_size(
60 text_size_.width() + View::LabelPadding.width() * 2,
61 text_size_.height() + View::LabelPadding.height() * 2);
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() -
73 Cursor::ArrowSize - Cursor::Offset - 0.5f,
74 right - left, height);
75}
76
77void CursorPair::draw_markers(QPainter &p, const QRect &rect)
78{
79 assert(first_);
80 assert(second_);
81
82 const unsigned int prefix = view_.tick_prefix();
83
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));
90 if(text_rect.width() >= text_size_.width())
91 {
92 const int highlight_radius = delta_rect.height() / 2 - 2;
93
94 p.setBrush(Cursor::FillColour);
95 p.setPen(Cursor::LineColour);
96 p.drawRoundedRect(delta_rect, radius, radius);
97
98 delta_rect.adjust(1, 1, -1, -1);
99 p.setPen(Cursor::HighlightColour);
100 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
101
102 p.setPen(Cursor::TextColour);
103 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
104 pv::util::format_time(second_->time() - first_->time(), prefix, 2));
105 }
106
107 // Paint the cursor markers
108 first_->paint_label(p, rect);
109 second_->paint_label(p, rect);
110}
111
112void CursorPair::draw_viewport_background(QPainter &p,
113 const QRect &rect)
114{
115 p.setPen(Qt::NoPen);
116 p.setBrush(QBrush(View::CursorAreaColour));
117
118 const pair<float, float> offsets(get_cursor_offsets());
119 const int l = (int)max(min(
120 offsets.first, offsets.second), 0.0f);
121 const int r = (int)min(max(
122 offsets.first, offsets.second), (float)rect.width());
123
124 p.drawRect(l, 0, r - l, rect.height());
125}
126
127void CursorPair::draw_viewport_foreground(QPainter &p,
128 const QRect &rect)
129{
130 assert(first_);
131 assert(second_);
132
133 first_->paint(p, rect);
134 second_->paint(p, rect);
135}
136
137void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
138{
139 assert(first_);
140 assert(second_);
141
142 text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
143 second_->time() - first_->time(), prefix, 2)).size();
144}
145
146pair<float, float> CursorPair::get_cursor_offsets() const
147{
148 assert(first_);
149 assert(second_);
150
151 return pair<float, float>(
152 (first_->time() - view_.offset()) / view_.scale(),
153 (second_->time() - view_.offset()) / view_.scale());
154}
155
156} // namespace view
157} // namespace pv