]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/cursorheader.cpp
Ruler: Moved calculate_tick_spacing into View
[pulseview.git] / pv / view / cursorheader.cpp
... / ...
CommitLineData
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.hpp"
22
23#include "ruler.hpp"
24#include "view.hpp"
25
26#include <QApplication>
27#include <QFontMetrics>
28#include <QMouseEvent>
29
30#include <pv/widgets/popup.hpp>
31
32using std::shared_ptr;
33
34namespace pv {
35namespace view {
36
37const int CursorHeader::Padding = 20;
38const int CursorHeader::BaselineOffset = 5;
39
40int 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}
46
47CursorHeader::CursorHeader(View &parent) :
48 MarginWidget(parent),
49 dragging_(false),
50 textHeight_(calculateTextHeight())
51{
52 setMouseTracking(true);
53}
54
55QSize CursorHeader::sizeHint() const
56{
57 return QSize(0, textHeight_ + Padding + BaselineOffset);
58}
59
60void CursorHeader::clear_selection()
61{
62 CursorPair &cursors = view_.cursors();
63 cursors.first()->select(false);
64 cursors.second()->select(false);
65 update();
66}
67
68void CursorHeader::paintEvent(QPaintEvent*)
69{
70 QPainter p(this);
71 p.setRenderHint(QPainter::Antialiasing);
72
73 // Draw the cursors
74 if (view_.cursors_shown()) {
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);
79 view_.cursors().draw_markers(p, r);
80 }
81}
82
83void CursorHeader::mouseMoveEvent(QMouseEvent *e)
84{
85 if (!(e->buttons() & Qt::LeftButton))
86 return;
87
88 if ((e->pos() - mouse_down_point_).manhattanLength() <
89 QApplication::startDragDistance())
90 return;
91
92 dragging_ = true;
93
94 if (shared_ptr<TimeMarker> m = grabbed_marker_.lock())
95 m->set_time(view_.offset() +
96 ((double)e->x() + 0.5) * view_.scale());
97}
98
99void CursorHeader::mousePressEvent(QMouseEvent *e)
100{
101 if (e->buttons() & Qt::LeftButton) {
102 mouse_down_point_ = e->pos();
103
104 grabbed_marker_.reset();
105
106 clear_selection();
107
108 if (view_.cursors_shown()) {
109 CursorPair &cursors = view_.cursors();
110 if (cursors.first()->get_label_rect(
111 rect()).contains(e->pos()))
112 grabbed_marker_ = cursors.first();
113 else if (cursors.second()->get_label_rect(
114 rect()).contains(e->pos()))
115 grabbed_marker_ = cursors.second();
116 }
117
118 if (shared_ptr<TimeMarker> m = grabbed_marker_.lock())
119 m->select();
120
121 selection_changed();
122 }
123}
124
125void CursorHeader::mouseReleaseEvent(QMouseEvent *)
126{
127 using pv::widgets::Popup;
128
129 if (!dragging_)
130 if (shared_ptr<TimeMarker> m = grabbed_marker_.lock()) {
131 Popup *const p = m->create_popup(&view_);
132 const QPoint arrpos(m->get_x(), height() - BaselineOffset);
133 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
134 p->show();
135 }
136
137 dragging_ = false;
138 grabbed_marker_.reset();
139}
140
141} // namespace view
142} // namespace pv