Application::print_version_info(): Minor output whitespace fixes.
[pulseview.git] / pv / views / trace / timemarker.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <algorithm>
21 #include <cmath>
22
23 #include <extdef.h>
24
25 #include "timemarker.hpp"
26
27 #include "pv/widgets/timestampspinbox.hpp"
28 #include "view.hpp"
29
30 #include <QApplication>
31 #include <QFontMetrics>
32 #include <QFormLayout>
33 #include <QPainter>
34
35 #include <pv/widgets/popup.hpp>
36
37 using std::max;
38 using std::min;
39
40 namespace pv {
41 namespace views {
42 namespace trace {
43
44 const int TimeMarker::ArrowSize = 4;
45
46 TimeMarker::TimeMarker(
47         View &view, const QColor &color, const pv::util::Timestamp& time) :
48         TimeItem(view),
49         color_(color),
50         time_(time),
51         value_action_(nullptr),
52         value_widget_(nullptr),
53         updating_value_widget_(false)
54 {
55 }
56
57 const pv::util::Timestamp& TimeMarker::time() const
58 {
59         return time_;
60 }
61
62 void TimeMarker::set_time(const pv::util::Timestamp& time)
63 {
64         time_ = time;
65
66         if (value_widget_) {
67                 updating_value_widget_ = true;
68                 value_widget_->setValue(time);
69                 updating_value_widget_ = false;
70         }
71
72         view_.time_item_appearance_changed(true, true);
73 }
74
75 float TimeMarker::get_x() const
76 {
77         // Use roundf() from cmath, std::roundf() causes Android issues (see #945).
78         return roundf(((time_ - view_.offset()) / view_.scale()).convert_to<float>()) + 0.5f;
79 }
80
81 QPoint TimeMarker::drag_point(const QRect &rect) const
82 {
83         (void)rect;
84
85         return QPoint(get_x(), view_.mapFromGlobal(QCursor::pos()).y());
86 }
87
88 QRectF TimeMarker::label_rect(const QRectF &rect) const
89 {
90         QFontMetrics m(QApplication::font());
91         const QSizeF text_size(
92                 max(m.boundingRect(get_text()).size().width(), ArrowSize),
93                 m.height());
94         const QSizeF label_size(text_size + LabelPadding * 2);
95         const float top = rect.height() - label_size.height() -
96                 TimeMarker::ArrowSize - 0.5f;
97         const float x = get_x();
98
99         return QRectF(QPointF(x - label_size.width() / 2, top), label_size);
100 }
101
102 QRectF TimeMarker::hit_box_rect(const ViewItemPaintParams &pp) const
103 {
104         const float x = get_x();
105         const float h = QFontMetrics(QApplication::font()).height();
106         return QRectF(x - h / 2.0f, pp.top(), h, pp.height());
107 }
108
109 void TimeMarker::paint_label(QPainter &p, const QRect &rect, bool hover)
110 {
111         if (!enabled())
112                 return;
113
114         const qreal x = get_x();
115         const QRectF r(label_rect(rect));
116
117         const QPointF points[] = {
118                 r.topLeft(),
119                 r.bottomLeft(),
120                 QPointF(max(r.left(), x - ArrowSize), r.bottom()),
121                 QPointF(x, rect.bottom()),
122                 QPointF(min(r.right(), x + ArrowSize), r.bottom()),
123                 r.bottomRight(),
124                 r.topRight()
125         };
126
127         const QPointF highlight_points[] = {
128                 QPointF(r.left() + 1, r.top() + 1),
129                 QPointF(r.left() + 1, r.bottom() - 1),
130                 QPointF(max(r.left() + 1, x - ArrowSize), r.bottom() - 1),
131                 QPointF(min(max(r.left() + 1, x), r.right() - 1),
132                         rect.bottom() - 1),
133                 QPointF(min(r.right() - 1, x + ArrowSize), r.bottom() - 1),
134                 QPointF(r.right() - 1, r.bottom() - 1),
135                 QPointF(r.right() - 1, r.top() + 1),
136         };
137
138         if (selected()) {
139                 p.setPen(highlight_pen());
140                 p.setBrush(Qt::transparent);
141                 p.drawPolygon(points, countof(points));
142         }
143
144         p.setPen(Qt::transparent);
145         p.setBrush(hover ? color_.lighter() : color_);
146         p.drawPolygon(points, countof(points));
147
148         p.setPen(color_.lighter());
149         p.setBrush(Qt::transparent);
150         p.drawPolygon(highlight_points, countof(highlight_points));
151
152         p.setPen(color_.darker());
153         p.setBrush(Qt::transparent);
154         p.drawPolygon(points, countof(points));
155
156         p.setPen(select_text_color(color_));
157         p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, get_text());
158 }
159
160 void TimeMarker::paint_fore(QPainter &p, ViewItemPaintParams &pp)
161 {
162         if (!enabled())
163                 return;
164
165         const float x = get_x();
166         p.setPen(color_.darker());
167         p.drawLine(QPointF(x, pp.top()), QPointF(x, pp.bottom()));
168 }
169
170 pv::widgets::Popup* TimeMarker::create_popup(QWidget *parent)
171 {
172         using pv::widgets::Popup;
173
174         Popup *const popup = new Popup(parent);
175         popup->set_position(parent->mapToGlobal(
176                 drag_point(parent->rect())), Popup::Bottom);
177
178         QFormLayout *const form = new QFormLayout(popup);
179         popup->setLayout(form);
180
181         value_widget_ = new pv::widgets::TimestampSpinBox(parent);
182         value_widget_->setValue(time_);
183
184         connect(value_widget_, SIGNAL(valueChanged(const pv::util::Timestamp&)),
185                 this, SLOT(on_value_changed(const pv::util::Timestamp&)));
186
187         form->addRow(tr("Time"), value_widget_);
188
189         return popup;
190 }
191
192 void TimeMarker::on_value_changed(const pv::util::Timestamp& value)
193 {
194         if (!updating_value_widget_)
195                 set_time(value);
196 }
197
198 } // namespace trace
199 } // namespace views
200 } // namespace pv