]> sigrok.org Git - pulseview.git/blob - pv/view/cursorpair.cpp
0370793d0407b9a9d8db11dc55e592f50ef53732
[pulseview.git] / pv / view / cursorpair.cpp
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
29 using std::max;
30 using std::make_pair;
31 using std::min;
32 using std::shared_ptr;
33 using std::pair;
34
35 namespace pv {
36 namespace view {
37
38 const int CursorPair::DeltaPadding = 8;
39
40 CursorPair::CursorPair(View &view) :
41         TimeItem(view),
42         first_(new Cursor(view, 0.0)),
43         second_(new Cursor(view, 1.0))
44 {
45 }
46
47 bool CursorPair::enabled() const
48 {
49         return view_.cursors_shown();
50 }
51
52 shared_ptr<Cursor> CursorPair::first() const
53 {
54         return first_;
55 }
56
57 shared_ptr<Cursor> CursorPair::second() const
58 {
59         return second_;
60 }
61
62 QPoint CursorPair::point() const
63 {
64         return first_->point();
65 }
66
67 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
68 {
69         (void)parent;
70         return nullptr;
71 }
72
73 QRectF CursorPair::get_label_rect(const QRect &rect) const
74 {
75         const QSizeF label_size(
76                 text_size_.width() + View::LabelPadding.width() * 2,
77                 text_size_.height() + View::LabelPadding.height() * 2);
78         const pair<float, float> offsets(get_cursor_offsets());
79         const pair<float, float> normal_offsets(
80                 (offsets.first < offsets.second) ? offsets :
81                 make_pair(offsets.second, offsets.first));
82
83         const float height = label_size.height();
84         const float left = max(normal_offsets.first + DeltaPadding, -height);
85         const float right = min(normal_offsets.second - DeltaPadding,
86                 (float)rect.width() + height);
87
88         return QRectF(left, rect.height() - label_size.height() -
89                 TimeMarker::ArrowSize - TimeMarker::Offset - 0.5f,
90                 right - left, height);
91 }
92
93 void CursorPair::draw_markers(QPainter &p, const QRect &rect)
94 {
95         assert(first_);
96         assert(second_);
97
98         const unsigned int prefix = view_.tick_prefix();
99
100         compute_text_size(p, prefix);
101         QRectF delta_rect(get_label_rect(rect));
102
103         const int radius = delta_rect.height() / 2;
104         const QRectF text_rect(delta_rect.intersected(
105                 rect).adjusted(radius, 0, -radius, 0));
106         if(text_rect.width() >= text_size_.width())
107         {
108                 const int highlight_radius = delta_rect.height() / 2 - 2;
109
110                 p.setBrush(Cursor::FillColour);
111                 p.setPen(Cursor::FillColour.darker());
112                 p.drawRoundedRect(delta_rect, radius, radius);
113
114                 delta_rect.adjust(1, 1, -1, -1);
115                 p.setPen(Cursor::FillColour.lighter());
116                 p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
117
118                 p.setPen(SelectableItem::select_text_colour(
119                         Cursor::FillColour));
120                 p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
121                         pv::util::format_time(second_->time() - first_->time(), prefix, 2));
122         }
123
124         // Paint the cursor markers
125         first_->paint_label(p, rect);
126         second_->paint_label(p, rect);
127 }
128
129 void CursorPair::draw_viewport_background(QPainter &p,
130         const QRect &rect)
131 {
132         p.setPen(Qt::NoPen);
133         p.setBrush(QBrush(View::CursorAreaColour));
134
135         const pair<float, float> offsets(get_cursor_offsets());
136         const int l = (int)max(min(
137                 offsets.first, offsets.second), 0.0f);
138         const int r = (int)min(max(
139                 offsets.first, offsets.second), (float)rect.width());
140
141         p.drawRect(l, 0, r - l, rect.height());
142 }
143
144 void CursorPair::draw_viewport_foreground(QPainter &p,
145         const QRect &rect)
146 {
147         assert(first_);
148         assert(second_);
149
150         first_->paint(p, rect);
151         second_->paint(p, rect);
152 }
153
154 void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
155 {
156         assert(first_);
157         assert(second_);
158
159         text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
160                 second_->time() - first_->time(), prefix, 2)).size();
161 }
162
163 pair<float, float> CursorPair::get_cursor_offsets() const
164 {
165         assert(first_);
166         assert(second_);
167
168         return pair<float, float>(
169                 (first_->time() - view_.offset()) / view_.scale(),
170                 (second_->time() - view_.offset()) / view_.scale());
171 }
172
173 } // namespace view
174 } // namespace pv