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