]>
Commit | Line | Data |
---|---|---|
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 |
29 | using std::max; |
30 | using std::make_pair; | |
31 | using std::min; | |
f9abf97e | 32 | using std::shared_ptr; |
819f4c25 | 33 | using std::pair; |
0ba172cf | 34 | |
b42d25c4 JH |
35 | namespace pv { |
36 | namespace view { | |
37 | ||
5139748b | 38 | const int CursorPair::DeltaPadding = 8; |
ccee9230 | 39 | const QColor CursorPair::ViewportFillColour(220, 231, 243); |
5139748b | 40 | |
8debe10d | 41 | CursorPair::CursorPair(View &view) : |
5a0192d4 | 42 | TimeItem(view), |
8dbbc7f0 | 43 | first_(new Cursor(view, 0.0)), |
5a0192d4 | 44 | second_(new Cursor(view, 1.0)) |
b42d25c4 JH |
45 | { |
46 | } | |
47 | ||
5a0192d4 JH |
48 | bool CursorPair::enabled() const |
49 | { | |
50 | return view_.cursors_shown(); | |
51 | } | |
52 | ||
58864c5c | 53 | shared_ptr<Cursor> CursorPair::first() const |
b42d25c4 | 54 | { |
8dbbc7f0 | 55 | return first_; |
b42d25c4 JH |
56 | } |
57 | ||
58864c5c | 58 | shared_ptr<Cursor> CursorPair::second() const |
b42d25c4 | 59 | { |
8dbbc7f0 | 60 | return second_; |
b42d25c4 JH |
61 | } |
62 | ||
3b9c4a0d JH |
63 | void CursorPair::set_time(double time) { |
64 | const double delta = second_->time() - first_->time(); | |
65 | first_->set_time(time); | |
66 | second_->set_time(time + delta); | |
67 | } | |
68 | ||
5165bb34 JH |
69 | float CursorPair::get_x() const |
70 | { | |
71 | return (first_->get_x() + second_->get_x()) / 2.0f; | |
72 | } | |
73 | ||
5a0192d4 JH |
74 | QPoint CursorPair::point() const |
75 | { | |
76 | return first_->point(); | |
77 | } | |
78 | ||
79 | pv::widgets::Popup* CursorPair::create_popup(QWidget *parent) | |
80 | { | |
81 | (void)parent; | |
82 | return nullptr; | |
83 | } | |
84 | ||
689dea92 | 85 | QRectF CursorPair::label_rect(const QRectF &rect) const |
5139748b JH |
86 | { |
87 | const QSizeF label_size( | |
ec39632d JH |
88 | text_size_.width() + LabelPadding.width() * 2, |
89 | text_size_.height() + LabelPadding.height() * 2); | |
5139748b JH |
90 | const pair<float, float> offsets(get_cursor_offsets()); |
91 | const pair<float, float> normal_offsets( | |
92 | (offsets.first < offsets.second) ? offsets : | |
93 | make_pair(offsets.second, offsets.first)); | |
94 | ||
95 | const float height = label_size.height(); | |
96 | const float left = max(normal_offsets.first + DeltaPadding, -height); | |
97 | const float right = min(normal_offsets.second - DeltaPadding, | |
98 | (float)rect.width() + height); | |
99 | ||
100 | return QRectF(left, rect.height() - label_size.height() - | |
6abffa97 | 101 | TimeMarker::ArrowSize - 0.5f, |
5139748b JH |
102 | right - left, height); |
103 | } | |
104 | ||
49028d6c | 105 | void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) |
332afa74 | 106 | { |
8dbbc7f0 JH |
107 | assert(first_); |
108 | assert(second_); | |
58864c5c | 109 | |
9a377493 JH |
110 | if (!enabled()) |
111 | return; | |
112 | ||
361c560e | 113 | const unsigned int prefix = view_.tick_prefix(); |
f106fc3a JH |
114 | const QColor text_colour = |
115 | ViewItem::select_text_colour(Cursor::FillColour); | |
361c560e | 116 | |
f106fc3a | 117 | p.setPen(text_colour); |
5139748b | 118 | compute_text_size(p, prefix); |
689dea92 | 119 | QRectF delta_rect(label_rect(rect)); |
5139748b JH |
120 | |
121 | const int radius = delta_rect.height() / 2; | |
122 | const QRectF text_rect(delta_rect.intersected( | |
123 | rect).adjusted(radius, 0, -radius, 0)); | |
8dbbc7f0 | 124 | if(text_rect.width() >= text_size_.width()) |
5139748b JH |
125 | { |
126 | const int highlight_radius = delta_rect.height() / 2 - 2; | |
127 | ||
e9f909b8 JH |
128 | if (selected()) { |
129 | p.setBrush(Qt::transparent); | |
130 | p.setPen(highlight_pen()); | |
131 | p.drawRoundedRect(delta_rect, radius, radius); | |
132 | } | |
133 | ||
49028d6c JH |
134 | p.setBrush(hover ? Cursor::FillColour.lighter() : |
135 | Cursor::FillColour); | |
4fabd61a | 136 | p.setPen(Cursor::FillColour.darker()); |
5139748b JH |
137 | p.drawRoundedRect(delta_rect, radius, radius); |
138 | ||
139 | delta_rect.adjust(1, 1, -1, -1); | |
4fabd61a | 140 | p.setPen(Cursor::FillColour.lighter()); |
5139748b JH |
141 | p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); |
142 | ||
f106fc3a | 143 | p.setPen(text_colour); |
5139748b | 144 | p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, |
8dbbc7f0 | 145 | pv::util::format_time(second_->time() - first_->time(), prefix, 2)); |
5139748b | 146 | } |
332afa74 JH |
147 | } |
148 | ||
beb897c6 JH |
149 | void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) { |
150 | if (!enabled()) | |
151 | return; | |
152 | ||
0ba172cf | 153 | p.setPen(Qt::NoPen); |
ccee9230 | 154 | p.setBrush(QBrush(ViewportFillColour)); |
0ba172cf | 155 | |
199441e4 JH |
156 | const pair<float, float> offsets(get_cursor_offsets()); |
157 | const int l = (int)max(min( | |
158 | offsets.first, offsets.second), 0.0f); | |
159 | const int r = (int)min(max( | |
beb897c6 | 160 | offsets.first, offsets.second), (float)pp.width()); |
58864c5c | 161 | |
beb897c6 | 162 | p.drawRect(l, pp.top(), r - l, pp.height()); |
0ba172cf JH |
163 | } |
164 | ||
5139748b JH |
165 | void CursorPair::compute_text_size(QPainter &p, unsigned int prefix) |
166 | { | |
8dbbc7f0 JH |
167 | assert(first_); |
168 | assert(second_); | |
58864c5c | 169 | |
8dbbc7f0 JH |
170 | text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time( |
171 | second_->time() - first_->time(), prefix, 2)).size(); | |
5139748b JH |
172 | } |
173 | ||
199441e4 JH |
174 | pair<float, float> CursorPair::get_cursor_offsets() const |
175 | { | |
8dbbc7f0 JH |
176 | assert(first_); |
177 | assert(second_); | |
58864c5c | 178 | |
199441e4 | 179 | return pair<float, float>( |
8dbbc7f0 JH |
180 | (first_->time() - view_.offset()) / view_.scale(), |
181 | (second_->time() - view_.offset()) / view_.scale()); | |
199441e4 JH |
182 | } |
183 | ||
b42d25c4 JH |
184 | } // namespace view |
185 | } // namespace pv |