]> sigrok.org Git - pulseview.git/blame - pv/view/cursorheader.cpp
CursorHeader: Handle all TimeItems instead of TimeMarkers
[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
JH
50 dragging_(false),
51 textHeight_(calculateTextHeight())
84a0d458
JS
52{
53 setMouseTracking(true);
54}
55
56QSize CursorHeader::sizeHint() const
57{
8dbbc7f0 58 return QSize(0, textHeight_ + Padding + BaselineOffset);
84a0d458
JS
59}
60
61void CursorHeader::clear_selection()
62{
2496bf45
JH
63 const vector< shared_ptr<TimeItem> > items(view_.time_items());
64 for (auto &i : items)
65 i->select(false);
84a0d458
JS
66 update();
67}
68
69void CursorHeader::paintEvent(QPaintEvent*)
70{
71 QPainter p(this);
72 p.setRenderHint(QPainter::Antialiasing);
73
5c5ce757
JH
74 // The cursor labels are not drawn with the arrows exactly on the
75 // bottom line of the widget, because then the selection shadow
76 // would be clipped away.
77 const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset);
78
84a0d458 79 // Draw the cursors
5c5ce757
JH
80 if (view_.cursors_shown())
81 view_.cursors()->draw_markers(p, r);
84a0d458
JS
82}
83
84void CursorHeader::mouseMoveEvent(QMouseEvent *e)
85{
86 if (!(e->buttons() & Qt::LeftButton))
87 return;
88
8dbbc7f0 89 if ((e->pos() - mouse_down_point_).manhattanLength() <
84a0d458
JS
90 QApplication::startDragDistance())
91 return;
92
e95fb8e9 93 // Do the drag
8dbbc7f0 94 dragging_ = true;
84a0d458 95
e95fb8e9
JH
96 const int delta = e->pos().x() - mouse_down_point_.x();
97 const vector< shared_ptr<TimeItem> > items(view_.time_items());
98 for (auto &i : items)
99 if (i->dragging())
100 i->set_time(view_.offset() +
101 (i->drag_point().x() + delta) * view_.scale());
84a0d458
JS
102}
103
104void CursorHeader::mousePressEvent(QMouseEvent *e)
105{
106 if (e->buttons() & Qt::LeftButton) {
8dbbc7f0 107 mouse_down_point_ = e->pos();
84a0d458 108
e95fb8e9 109 mouse_down_item_.reset();
84a0d458
JS
110
111 clear_selection();
112
e95fb8e9
JH
113 const vector< shared_ptr<TimeItem> > items(view_.time_items());
114 for (auto &i : items)
115 if (i && i->label_rect(rect()).contains(e->pos())) {
116 mouse_down_item_ = i;
117 break;
118 }
84a0d458 119
e95fb8e9
JH
120 if (mouse_down_item_) {
121 mouse_down_item_->select();
122 mouse_down_item_->drag();
123 }
84a0d458
JS
124
125 selection_changed();
126 }
127}
128
129void CursorHeader::mouseReleaseEvent(QMouseEvent *)
130{
131 using pv::widgets::Popup;
132
e95fb8e9
JH
133 if (!dragging_ && mouse_down_item_) {
134 Popup *const p = mouse_down_item_->create_popup(&view_);
135 const QPoint arrpos(mouse_down_item_->get_x(),
136 height() - BaselineOffset);
137 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
138 p->show();
139 }
84a0d458 140
8dbbc7f0 141 dragging_ = false;
e95fb8e9
JH
142 mouse_down_item_.reset();
143
144 const vector< shared_ptr<TimeItem> > items(view_.time_items());
145 for (auto &i : items)
146 i->drag_release();
84a0d458
JS
147}
148
149} // namespace view
150} // namespace pv