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