2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
25 #include "../sigsession.h"
30 #include <QApplication>
32 #include <QMouseEvent>
36 #include <pv/widgets/popup.h>
41 using std::shared_ptr;
42 using std::stable_sort;
48 const int Header::Padding = 12;
49 const int Header::BaselineOffset = 5;
51 Header::Header(View &parent) :
55 setFocusPolicy(Qt::ClickFocus);
56 setMouseTracking(true);
58 connect(&_view, SIGNAL(signals_moved()),
59 this, SLOT(on_signals_moved()));
62 QSize Header::sizeHint() const
64 QRectF max_rect(-Padding, 0, Padding, 0);
67 max_rect = max_rect.united(i->label_rect(0));
68 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
71 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
73 const int w = width() - BaselineOffset;
75 if (i->enabled() && i->label_rect(w).contains(pt))
77 return shared_ptr<RowItem>();
80 void Header::clear_selection()
87 void Header::signals_updated()
89 for (shared_ptr<RowItem> r : _view) {
91 connect(r.get(), SIGNAL(appearance_changed()),
92 this, SLOT(on_trace_changed()));
96 void Header::show_popup(const shared_ptr<RowItem> &item)
98 using pv::widgets::Popup;
100 Popup *const p = item->create_popup(&_view);
104 const QPoint pt(width() - BaselineOffset, item->get_y());
105 p->set_position(mapToGlobal(pt), Popup::Right);
109 void Header::paintEvent(QPaintEvent*)
111 // The trace labels are not drawn with the arrows exactly on the
112 // left edge of the widget, because then the selection shadow
113 // would be clipped away.
114 const int w = width() - BaselineOffset;
116 vector< shared_ptr<RowItem> > row_items(
117 _view.begin(), _view.end());
119 stable_sort(row_items.begin(), row_items.end(),
120 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
121 return a->v_offset() < b->v_offset(); });
123 QPainter painter(this);
124 painter.setRenderHint(QPainter::Antialiasing);
126 const bool dragging = !_drag_row_items.empty();
127 for (const shared_ptr<RowItem> r : row_items)
131 const bool highlight = !dragging &&
132 r->label_rect(w).contains(_mouse_point);
133 r->paint_label(painter, w, highlight);
139 void Header::mouseLeftPressEvent(QMouseEvent *event)
141 const bool ctrl_pressed =
142 QApplication::keyboardModifiers() & Qt::ControlModifier;
144 // Clear selection if control is not pressed and this item is unselected
145 const shared_ptr<RowItem> mouse_over =
146 get_mouse_over_row_item(event->pos());
147 if (!ctrl_pressed && (!mouse_over || !mouse_over->selected()))
148 for (shared_ptr<RowItem> r : _view)
151 // Set the signal selection state if the item has been clicked
154 mouse_over->select(!mouse_over->selected());
156 mouse_over->select(true);
159 // Save the offsets of any signals which will be dragged
160 _mouse_down_point = event->pos();
161 for (const shared_ptr<RowItem> r : _view)
163 _drag_row_items.push_back(
164 make_pair(r, r->v_offset()));
170 void Header::mousePressEvent(QMouseEvent *event)
173 if (event->button() & Qt::LeftButton)
174 mouseLeftPressEvent(event);
177 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
181 const bool ctrl_pressed =
182 QApplication::keyboardModifiers() & Qt::ControlModifier;
184 // Unselect everything if control is not pressed
185 const shared_ptr<RowItem> mouse_over =
186 get_mouse_over_row_item(event->pos());
189 _view.normalize_layout();
193 for (shared_ptr<RowItem> r : _view)
198 show_popup(mouse_over);
203 _drag_row_items.clear();
206 void Header::mouseReleaseEvent(QMouseEvent *event)
209 if (event->button() & Qt::LeftButton)
210 mouseLeftReleaseEvent(event);
213 void Header::mouseMoveEvent(QMouseEvent *event)
216 _mouse_point = event->pos();
218 if (!(event->buttons() & Qt::LeftButton))
221 if ((event->pos() - _mouse_down_point).manhattanLength() <
222 QApplication::startDragDistance())
225 // Check the list of dragging items is not empty
226 if (_drag_row_items.empty())
229 // Check all the drag items share a common owner
230 const shared_ptr<RowItem> first_row_item(
231 _drag_row_items.front().first);
232 for (const auto &r : _drag_row_items) {
233 const shared_ptr<RowItem> row_item(r.first);
236 if (row_item->owner() != first_row_item->owner())
243 const int delta = event->pos().y() - _mouse_down_point.y();
245 for (auto i = _drag_row_items.begin();
246 i != _drag_row_items.end(); i++) {
247 const std::shared_ptr<RowItem> row_item((*i).first);
249 const int y = (*i).second + delta;
250 row_item->set_v_offset(y);
252 // Ensure the trace is selected
262 void Header::leaveEvent(QEvent*)
264 _mouse_point = QPoint(-1, -1);
268 void Header::contextMenuEvent(QContextMenuEvent *event)
270 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
274 QMenu *const menu = r->create_context_menu(this);
278 menu->exec(event->globalPos());
281 void Header::keyPressEvent(QKeyEvent *e)
289 for (const shared_ptr<RowItem> r : _view)
297 void Header::on_signals_moved()
302 void Header::on_trace_changed()