]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/cursorheader.cpp
CursorHeader: Paint by the labels by the time item list
[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;
33using std::vector;
34
35namespace pv {
36namespace view {
37
38const int CursorHeader::Padding = 20;
39const int CursorHeader::BaselineOffset = 5;
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}
47
48CursorHeader::CursorHeader(View &parent) :
49 MarginWidget(parent),
50 dragging_(false),
51 textHeight_(calculateTextHeight())
52{
53 setMouseTracking(true);
54}
55
56QSize CursorHeader::sizeHint() const
57{
58 return QSize(0, textHeight_ + Padding + BaselineOffset);
59}
60
61void CursorHeader::clear_selection()
62{
63 const vector< shared_ptr<TimeItem> > items(view_.time_items());
64 for (auto &i : items)
65 i->select(false);
66 update();
67}
68
69void CursorHeader::paintEvent(QPaintEvent*)
70{
71 QPainter p(this);
72 p.setRenderHint(QPainter::Antialiasing);
73
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
79 // Draw the items
80 const vector< shared_ptr<TimeItem> > items(view_.time_items());
81 for (auto &m : items)
82 m->paint_label(p, r);
83}
84
85void CursorHeader::mouseMoveEvent(QMouseEvent *e)
86{
87 if (!(e->buttons() & Qt::LeftButton))
88 return;
89
90 if ((e->pos() - mouse_down_point_).manhattanLength() <
91 QApplication::startDragDistance())
92 return;
93
94 // Do the drag
95 dragging_ = true;
96
97 const int delta = e->pos().x() - mouse_down_point_.x();
98 const vector< shared_ptr<TimeItem> > items(view_.time_items());
99 for (auto &i : items)
100 if (i->dragging())
101 i->set_time(view_.offset() +
102 (i->drag_point().x() + delta) * view_.scale());
103}
104
105void CursorHeader::mousePressEvent(QMouseEvent *e)
106{
107 if (e->buttons() & Qt::LeftButton) {
108 mouse_down_point_ = e->pos();
109
110 mouse_down_item_.reset();
111
112 clear_selection();
113
114 const vector< shared_ptr<TimeItem> > items(view_.time_items());
115 for (auto &i : items)
116 if (i && i->label_rect(rect()).contains(e->pos())) {
117 mouse_down_item_ = i;
118 break;
119 }
120
121 if (mouse_down_item_) {
122 mouse_down_item_->select();
123 mouse_down_item_->drag();
124 }
125
126 selection_changed();
127 }
128}
129
130void CursorHeader::mouseReleaseEvent(QMouseEvent *)
131{
132 using pv::widgets::Popup;
133
134 if (!dragging_ && mouse_down_item_) {
135 Popup *const p = mouse_down_item_->create_popup(&view_);
136 if (p) {
137 const QPoint arrpos(mouse_down_item_->get_x(),
138 height() - BaselineOffset);
139 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
140 p->show();
141 }
142 }
143
144 dragging_ = false;
145 mouse_down_item_.reset();
146
147 const vector< shared_ptr<TimeItem> > items(view_.time_items());
148 for (auto &i : items)
149 i->drag_release();
150}
151
152} // namespace view
153} // namespace pv