]> sigrok.org Git - pulseview.git/blame - pv/view/cursorheader.cpp
CursorHeader: Made drag position pixel perfect
[pulseview.git] / pv / view / cursorheader.cpp
CommitLineData
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
32using std::shared_ptr;
2496bf45 33using std::vector;
84a0d458
JS
34
35namespace pv {
36namespace view {
37
4707a23d 38const int CursorHeader::Padding = 20;
4d95ef60 39const int CursorHeader::BaselineOffset = 5;
4707a23d
JS
40
41int CursorHeader::calculateTextHeight()
42{
43 QFontMetrics fm(font());
44 return fm.boundingRect(0, 0, INT_MAX, INT_MAX,
45 Qt::AlignLeft | Qt::AlignTop, "8").height();
46}
84a0d458
JS
47
48CursorHeader::CursorHeader(View &parent) :
49 MarginWidget(parent),
8dbbc7f0 50 textHeight_(calculateTextHeight())
84a0d458
JS
51{
52 setMouseTracking(true);
53}
54
55QSize CursorHeader::sizeHint() const
56{
8dbbc7f0 57 return QSize(0, textHeight_ + Padding + BaselineOffset);
84a0d458
JS
58}
59
60void CursorHeader::clear_selection()
61{
2496bf45
JH
62 const vector< shared_ptr<TimeItem> > items(view_.time_items());
63 for (auto &i : items)
64 i->select(false);
84a0d458
JS
65 update();
66}
67
68void CursorHeader::paintEvent(QPaintEvent*)
69{
70 QPainter p(this);
71 p.setRenderHint(QPainter::Antialiasing);
72
5c5ce757
JH
73 // The cursor labels are not drawn with the arrows exactly on the
74 // bottom line of the widget, because then the selection shadow
75 // would be clipped away.
76 const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset);
77
9a377493
JH
78 // Draw the items
79 const vector< shared_ptr<TimeItem> > items(view_.time_items());
80 for (auto &m : items)
81 m->paint_label(p, r);
84a0d458
JS
82}
83
84void CursorHeader::mouseMoveEvent(QMouseEvent *e)
85{
f4433aa9
JH
86 mouse_point_ = e->pos();
87
84a0d458
JS
88 if (!(e->buttons() & Qt::LeftButton))
89 return;
90
8dbbc7f0 91 if ((e->pos() - mouse_down_point_).manhattanLength() <
84a0d458
JS
92 QApplication::startDragDistance())
93 return;
94
e95fb8e9 95 // Do the drag
8dbbc7f0 96 dragging_ = true;
84a0d458 97
e95fb8e9
JH
98 const int delta = e->pos().x() - mouse_down_point_.x();
99 const vector< shared_ptr<TimeItem> > items(view_.time_items());
100 for (auto &i : items)
101 if (i->dragging())
102 i->set_time(view_.offset() +
650e2e5f
JH
103 (i->drag_point().x() + delta - 0.5) *
104 view_.scale());
84a0d458
JS
105}
106
107void CursorHeader::mousePressEvent(QMouseEvent *e)
108{
109 if (e->buttons() & Qt::LeftButton) {
8dbbc7f0 110 mouse_down_point_ = e->pos();
84a0d458 111
e95fb8e9 112 mouse_down_item_.reset();
84a0d458
JS
113
114 clear_selection();
115
e95fb8e9
JH
116 const vector< shared_ptr<TimeItem> > items(view_.time_items());
117 for (auto &i : items)
118 if (i && i->label_rect(rect()).contains(e->pos())) {
119 mouse_down_item_ = i;
120 break;
121 }
84a0d458 122
e95fb8e9
JH
123 if (mouse_down_item_) {
124 mouse_down_item_->select();
125 mouse_down_item_->drag();
126 }
84a0d458
JS
127
128 selection_changed();
129 }
130}
131
132void CursorHeader::mouseReleaseEvent(QMouseEvent *)
133{
134 using pv::widgets::Popup;
135
e95fb8e9
JH
136 if (!dragging_ && mouse_down_item_) {
137 Popup *const p = mouse_down_item_->create_popup(&view_);
faeb1a29
JH
138 if (p) {
139 const QPoint arrpos(mouse_down_item_->get_x(),
140 height() - BaselineOffset);
141 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
142 p->show();
143 }
e95fb8e9 144 }
84a0d458 145
8dbbc7f0 146 dragging_ = false;
e95fb8e9
JH
147 mouse_down_item_.reset();
148
149 const vector< shared_ptr<TimeItem> > items(view_.time_items());
150 for (auto &i : items)
151 i->drag_release();
84a0d458
JS
152}
153
f4433aa9
JH
154void CursorHeader::leaveEvent(QEvent*)
155{
156 mouse_point_ = QPoint(-1, -1);
157 update();
158}
159
84a0d458
JS
160} // namespace view
161} // namespace pv