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