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::show_popup(const shared_ptr<RowItem> &item)
89 using pv::widgets::Popup;
91 Popup *const p = item->create_popup(&_view);
95 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
96 p->set_position(mapToGlobal(pt), Popup::Right);
100 void Header::paintEvent(QPaintEvent*)
102 // The trace labels are not drawn with the arrows exactly on the
103 // left edge of the widget, because then the selection shadow
104 // would be clipped away.
105 const int w = width() - BaselineOffset;
107 vector< shared_ptr<RowItem> > row_items(
108 _view.begin(), _view.end());
110 stable_sort(row_items.begin(), row_items.end(),
111 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
112 return a->visual_v_offset() < b->visual_v_offset(); });
114 QPainter painter(this);
115 painter.setRenderHint(QPainter::Antialiasing);
117 for (const shared_ptr<RowItem> r : row_items)
121 const bool highlight = !_dragging &&
122 r->label_rect(w).contains(_mouse_point);
123 r->paint_label(painter, w, highlight);
129 void Header::mouseLeftPressEvent(QMouseEvent *event)
133 const bool ctrl_pressed =
134 QApplication::keyboardModifiers() & Qt::ControlModifier;
136 // Clear selection if control is not pressed and this item is unselected
137 if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
139 for (shared_ptr<RowItem> r : _view)
142 // Set the signal selection state if the item has been clicked
143 if (_mouse_down_item) {
145 _mouse_down_item->select(!_mouse_down_item->selected());
147 _mouse_down_item->select(true);
150 // Save the offsets of any signals which will be dragged
151 for (const shared_ptr<RowItem> r : _view)
159 void Header::mousePressEvent(QMouseEvent *event)
163 _mouse_down_point = event->pos();
164 _mouse_down_item = get_mouse_over_row_item(event->pos());
166 if (event->button() & Qt::LeftButton)
167 mouseLeftPressEvent(event);
170 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
174 const bool ctrl_pressed =
175 QApplication::keyboardModifiers() & Qt::ControlModifier;
177 // Unselect everything if control is not pressed
178 const shared_ptr<RowItem> mouse_over =
179 get_mouse_over_row_item(event->pos());
181 for (auto &r : _view)
185 _view.restack_all_row_items();
189 for (shared_ptr<RowItem> r : _view)
190 if (_mouse_down_item != r)
193 if (_mouse_down_item)
194 show_popup(_mouse_down_item);
201 void Header::mouseReleaseEvent(QMouseEvent *event)
204 if (event->button() & Qt::LeftButton)
205 mouseLeftReleaseEvent(event);
207 _mouse_down_item = nullptr;
210 void Header::mouseMoveEvent(QMouseEvent *event)
213 _mouse_point = event->pos();
215 if (!(event->buttons() & Qt::LeftButton))
218 if ((event->pos() - _mouse_down_point).manhattanLength() <
219 QApplication::startDragDistance())
222 // Check all the drag items share a common owner
223 RowItemOwner *item_owner = nullptr;
224 for (shared_ptr<RowItem> r : _view)
227 item_owner = r->owner();
228 else if(item_owner != r->owner())
238 const int delta = event->pos().y() - _mouse_down_point.y();
240 for (std::shared_ptr<RowItem> r : _view)
242 r->force_to_v_offset(r->drag_point().y() + delta);
244 // Ensure the trace is selected
248 item_owner->restack_items();
249 for (const auto &r : *item_owner)
250 r->animate_to_layout_v_offset();
256 void Header::leaveEvent(QEvent*)
258 _mouse_point = QPoint(-1, -1);
262 void Header::contextMenuEvent(QContextMenuEvent *event)
264 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
268 QMenu *const menu = r->create_context_menu(this);
272 menu->exec(event->globalPos());
275 void Header::keyPressEvent(QKeyEvent *e)
283 for (const shared_ptr<RowItem> r : _view)
291 void Header::on_signals_moved()