]> sigrok.org Git - pulseview.git/blob - pv/view/signalscalehandle.cpp
074bfb125fbadb1d6c67a37343c63e4e2596dd3f
[pulseview.git] / pv / view / signalscalehandle.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 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 <algorithm>
22
23 #include <QRadialGradient>
24
25 #include "signal.hpp"
26 #include "signalscalehandle.hpp"
27 #include "tracetreeitemowner.hpp"
28
29 using std::max;
30 using std::min;
31
32 namespace pv {
33 namespace views {
34 namespace TraceView {
35
36 SignalScaleHandle::SignalScaleHandle(Signal &owner) :
37         owner_(owner)
38 {
39 }
40
41 bool SignalScaleHandle::enabled() const
42 {
43         return selected() || owner_.selected();
44 }
45
46 void SignalScaleHandle::select(bool select)
47 {
48         ViewItem::select(select);
49         owner_.owner()->row_item_appearance_changed(true, true);
50 }
51
52 void SignalScaleHandle::drag_release()
53 {
54         RowItem::drag_release();
55         owner_.scale_handle_released();
56         owner_.owner()->row_item_appearance_changed(true, true);
57 }
58
59 void SignalScaleHandle::drag_by(const QPoint &delta)
60 {
61         owner_.scale_handle_dragged(
62                 drag_point_.y() + delta.y() - owner_.get_visual_y());
63         owner_.owner()->row_item_appearance_changed(true, true);
64 }
65
66 QPoint SignalScaleHandle::point(const QRect &rect) const
67 {
68         return owner_.point(rect) + QPoint(0, owner_.scale_handle_offset());
69 }
70
71 QRectF SignalScaleHandle::hit_box_rect(const ViewItemPaintParams &pp) const
72 {
73         const int text_height = ViewItemPaintParams::text_height();
74         const double x = -pp.pixels_offset() - text_height / 2;
75         const double min_x = pp.left() + text_height;
76         const double max_x = pp.right() - text_height * 2;
77         return QRectF(min(max(x, min_x), max_x),
78                 owner_.get_visual_y() + owner_.scale_handle_offset() -
79                         text_height / 2,
80                 text_height, text_height);
81 }
82
83 void SignalScaleHandle::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
84 {
85         if (!enabled())
86                 return;
87
88         const QRectF r(hit_box_rect(pp));
89         const QPointF c = (r.topLeft() + 2 * r.center()) / 3;
90         QRadialGradient gradient(c, r.width(), c);
91
92         if (selected()) {
93                 gradient.setColorAt(0.0, QColor(255, 255, 255));
94                 gradient.setColorAt(0.75, QColor(192, 192, 192));
95                 gradient.setColorAt(1.0, QColor(128, 128, 128));
96         } else {
97                 gradient.setColorAt(0.0, QColor(192, 192, 192));
98                 gradient.setColorAt(0.75, QColor(128, 128, 128));
99                 gradient.setColorAt(1.0, QColor(128, 128, 128));
100         }
101
102         p.setBrush(QBrush(gradient));
103         p.setPen(QColor(128, 128, 128));
104         p.drawEllipse(r);
105 }
106
107 } // namespace TraceView
108 } // namespace views
109 } // namespace pv