]>
Commit | Line | Data |
---|---|---|
84a0d458 JS |
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 | ||
2acdb232 | 21 | #include "cursorheader.hpp" |
84a0d458 | 22 | |
2acdb232 JH |
23 | #include "ruler.hpp" |
24 | #include "view.hpp" | |
84a0d458 JS |
25 | |
26 | #include <QApplication> | |
4707a23d | 27 | #include <QFontMetrics> |
84a0d458 JS |
28 | #include <QMouseEvent> |
29 | ||
2acdb232 | 30 | #include <pv/widgets/popup.hpp> |
84a0d458 JS |
31 | |
32 | using std::shared_ptr; | |
33 | ||
34 | namespace pv { | |
35 | namespace view { | |
36 | ||
4707a23d | 37 | const int CursorHeader::Padding = 20; |
4d95ef60 | 38 | const int CursorHeader::BaselineOffset = 5; |
4707a23d JS |
39 | |
40 | int CursorHeader::calculateTextHeight() | |
41 | { | |
42 | QFontMetrics fm(font()); | |
43 | return fm.boundingRect(0, 0, INT_MAX, INT_MAX, | |
44 | Qt::AlignLeft | Qt::AlignTop, "8").height(); | |
45 | } | |
84a0d458 JS |
46 | |
47 | CursorHeader::CursorHeader(View &parent) : | |
48 | MarginWidget(parent), | |
8dbbc7f0 JH |
49 | dragging_(false), |
50 | textHeight_(calculateTextHeight()) | |
84a0d458 JS |
51 | { |
52 | setMouseTracking(true); | |
53 | } | |
54 | ||
55 | QSize CursorHeader::sizeHint() const | |
56 | { | |
8dbbc7f0 | 57 | return QSize(0, textHeight_ + Padding + BaselineOffset); |
84a0d458 JS |
58 | } |
59 | ||
60 | void CursorHeader::clear_selection() | |
61 | { | |
8dbbc7f0 | 62 | CursorPair &cursors = view_.cursors(); |
84a0d458 JS |
63 | cursors.first()->select(false); |
64 | cursors.second()->select(false); | |
65 | update(); | |
66 | } | |
67 | ||
68 | void CursorHeader::paintEvent(QPaintEvent*) | |
69 | { | |
70 | QPainter p(this); | |
71 | p.setRenderHint(QPainter::Antialiasing); | |
72 | ||
73 | // Draw the cursors | |
8dbbc7f0 | 74 | if (view_.cursors_shown()) { |
4d95ef60 JS |
75 | // The cursor labels are not drawn with the arrows exactly on the |
76 | // bottom line of the widget, because then the selection shadow | |
77 | // would be clipped away. | |
78 | const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset); | |
361c560e | 79 | view_.cursors().draw_markers(p, r); |
84a0d458 JS |
80 | } |
81 | } | |
82 | ||
83 | void CursorHeader::mouseMoveEvent(QMouseEvent *e) | |
84 | { | |
85 | if (!(e->buttons() & Qt::LeftButton)) | |
86 | return; | |
87 | ||
8dbbc7f0 | 88 | if ((e->pos() - mouse_down_point_).manhattanLength() < |
84a0d458 JS |
89 | QApplication::startDragDistance()) |
90 | return; | |
91 | ||
8dbbc7f0 | 92 | dragging_ = true; |
84a0d458 | 93 | |
8dbbc7f0 JH |
94 | if (shared_ptr<TimeMarker> m = grabbed_marker_.lock()) |
95 | m->set_time(view_.offset() + | |
96 | ((double)e->x() + 0.5) * view_.scale()); | |
84a0d458 JS |
97 | } |
98 | ||
99 | void CursorHeader::mousePressEvent(QMouseEvent *e) | |
100 | { | |
101 | if (e->buttons() & Qt::LeftButton) { | |
8dbbc7f0 | 102 | mouse_down_point_ = e->pos(); |
84a0d458 | 103 | |
8dbbc7f0 | 104 | grabbed_marker_.reset(); |
84a0d458 JS |
105 | |
106 | clear_selection(); | |
107 | ||
8dbbc7f0 JH |
108 | if (view_.cursors_shown()) { |
109 | CursorPair &cursors = view_.cursors(); | |
84a0d458 JS |
110 | if (cursors.first()->get_label_rect( |
111 | rect()).contains(e->pos())) | |
8dbbc7f0 | 112 | grabbed_marker_ = cursors.first(); |
84a0d458 JS |
113 | else if (cursors.second()->get_label_rect( |
114 | rect()).contains(e->pos())) | |
8dbbc7f0 | 115 | grabbed_marker_ = cursors.second(); |
84a0d458 JS |
116 | } |
117 | ||
8dbbc7f0 | 118 | if (shared_ptr<TimeMarker> m = grabbed_marker_.lock()) |
84a0d458 JS |
119 | m->select(); |
120 | ||
121 | selection_changed(); | |
122 | } | |
123 | } | |
124 | ||
125 | void CursorHeader::mouseReleaseEvent(QMouseEvent *) | |
126 | { | |
127 | using pv::widgets::Popup; | |
128 | ||
8dbbc7f0 JH |
129 | if (!dragging_) |
130 | if (shared_ptr<TimeMarker> m = grabbed_marker_.lock()) { | |
131 | Popup *const p = m->create_popup(&view_); | |
4d95ef60 JS |
132 | const QPoint arrpos(m->get_x(), height() - BaselineOffset); |
133 | p->set_position(mapToGlobal(arrpos), Popup::Bottom); | |
84a0d458 JS |
134 | p->show(); |
135 | } | |
136 | ||
8dbbc7f0 JH |
137 | dragging_ = false; |
138 | grabbed_marker_.reset(); | |
84a0d458 JS |
139 | } |
140 | ||
141 | } // namespace view | |
142 | } // namespace pv |