]> sigrok.org Git - pulseview.git/blob - pv/view/cursorheader.cpp
6592acd4d06d43490688f4484dd12c6cbe282dfe
[pulseview.git] / pv / view / cursorheader.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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "cursorheader.h"
22
23 #include "ruler.h"
24 #include "view.h"
25
26 #include <QApplication>
27 #include <QFontMetrics>
28 #include <QMouseEvent>
29
30 #include <pv/widgets/popup.h>
31
32 using std::shared_ptr;
33
34 namespace pv {
35 namespace view {
36
37 const int CursorHeader::Padding = 20;
38
39 int CursorHeader::calculateTextHeight()
40 {
41         QFontMetrics fm(font());
42         return fm.boundingRect(0, 0, INT_MAX, INT_MAX,
43                 Qt::AlignLeft | Qt::AlignTop, "8").height();
44 }
45
46 CursorHeader::CursorHeader(View &parent) :
47         MarginWidget(parent),
48         _dragging(false),
49         _textHeight(calculateTextHeight())
50 {
51         setMouseTracking(true);
52 }
53
54 QSize CursorHeader::sizeHint() const
55 {
56         return QSize(0, _textHeight + Padding);
57 }
58
59 void CursorHeader::clear_selection()
60 {
61         CursorPair &cursors = _view.cursors();
62         cursors.first()->select(false);
63         cursors.second()->select(false);
64         update();
65 }
66
67 void CursorHeader::paintEvent(QPaintEvent*)
68 {
69         QPainter p(this);
70         p.setRenderHint(QPainter::Antialiasing);
71
72         unsigned int prefix = pv::view::Ruler::calculate_tick_spacing(
73                 p, _view.scale(), _view.offset()).second;
74
75         // Draw the cursors
76         if (_view.cursors_shown()) {
77                 _view.cursors().draw_markers(p, rect(), prefix);
78         }
79 }
80
81 void CursorHeader::mouseMoveEvent(QMouseEvent *e)
82 {
83         if (!(e->buttons() & Qt::LeftButton))
84                 return;
85
86         if ((e->pos() - _mouse_down_point).manhattanLength() <
87                 QApplication::startDragDistance())
88                 return;
89
90         _dragging = true;
91
92         if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
93                 m->set_time(_view.offset() +
94                         ((double)e->x() + 0.5) * _view.scale());
95 }
96
97 void CursorHeader::mousePressEvent(QMouseEvent *e)
98 {
99         if (e->buttons() & Qt::LeftButton) {
100                 _mouse_down_point = e->pos();
101
102                 _grabbed_marker.reset();
103
104                 clear_selection();
105
106                 if (_view.cursors_shown()) {
107                         CursorPair &cursors = _view.cursors();
108                         if (cursors.first()->get_label_rect(
109                                 rect()).contains(e->pos()))
110                                 _grabbed_marker = cursors.first();
111                         else if (cursors.second()->get_label_rect(
112                                 rect()).contains(e->pos()))
113                                 _grabbed_marker = cursors.second();
114                 }
115
116                 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
117                         m->select();
118
119                 selection_changed();
120         }
121 }
122
123 void CursorHeader::mouseReleaseEvent(QMouseEvent *)
124 {
125         using pv::widgets::Popup;
126
127         if (!_dragging)
128                 if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
129                         Popup *const p = m->create_popup(&_view);
130                         p->set_position(mapToGlobal(QPoint(m->get_x(),
131                                 height())), Popup::Bottom);
132                         p->show();
133                 }
134
135         _dragging = false;
136         _grabbed_marker.reset();
137 }
138
139 } // namespace view
140 } // namespace pv