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.session(), SIGNAL(signals_changed()),
59 this, SLOT(on_signals_changed()));
61 connect(&_view, SIGNAL(signals_moved()),
62 this, SLOT(on_signals_moved()));
65 QSize Header::sizeHint() const
67 QRectF max_rect(-Padding, 0, Padding, 0);
70 max_rect = max_rect.united(i->label_rect(0));
71 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
74 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
76 const int w = width() - BaselineOffset;
78 if (i->enabled() && i->label_rect(w).contains(pt))
80 return shared_ptr<RowItem>();
83 void Header::clear_selection()
90 void Header::show_popup(const shared_ptr<RowItem> &item)
92 using pv::widgets::Popup;
94 Popup *const p = item->create_popup(&_view);
98 const QPoint pt(width() - BaselineOffset, item->get_y());
99 p->set_position(mapToGlobal(pt), Popup::Right);
103 void Header::paintEvent(QPaintEvent*)
105 // The trace labels are not drawn with the arrows exactly on the
106 // left edge of the widget, because then the selection shadow
107 // would be clipped away.
108 const int w = width() - BaselineOffset;
110 vector< shared_ptr<RowItem> > row_items(
111 _view.begin(), _view.end());
113 stable_sort(row_items.begin(), row_items.end(),
114 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
115 return a->v_offset() < b->v_offset(); });
117 QPainter painter(this);
118 painter.setRenderHint(QPainter::Antialiasing);
120 const bool dragging = !_drag_row_items.empty();
121 for (const shared_ptr<RowItem> r : row_items)
125 const bool highlight = !dragging &&
126 r->label_rect(w).contains(_mouse_point);
127 r->paint_label(painter, w, highlight);
133 void Header::mousePressEvent(QMouseEvent *event)
137 if (event->button() & Qt::LeftButton) {
138 _mouse_down_point = event->pos();
140 // Save the offsets of any signals which will be dragged
141 for (const shared_ptr<RowItem> r : _view)
143 _drag_row_items.push_back(
144 make_pair(r, r->v_offset()));
147 // Select the signal if it has been clicked
148 const shared_ptr<RowItem> mouse_over_row_item =
149 get_mouse_over_row_item(event->pos());
150 if (mouse_over_row_item) {
151 if (mouse_over_row_item->selected())
152 mouse_over_row_item->select(false);
154 mouse_over_row_item->select(true);
156 if (~QApplication::keyboardModifiers() &
158 _drag_row_items.clear();
160 // Add the signal to the drag list
161 if (event->button() & Qt::LeftButton)
162 _drag_row_items.push_back(
163 make_pair(mouse_over_row_item,
164 mouse_over_row_item->v_offset()));
168 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
169 // Unselect all other signals because the Ctrl is not
171 for (const shared_ptr<RowItem> r : _view)
172 if (r != mouse_over_row_item)
180 void Header::mouseReleaseEvent(QMouseEvent *event)
183 if (event->button() == Qt::LeftButton) {
185 _view.normalize_layout();
188 const shared_ptr<RowItem> mouse_over_row_item =
189 get_mouse_over_row_item(event->pos());
190 if (mouse_over_row_item)
191 show_popup(mouse_over_row_item);
195 _drag_row_items.clear();
199 void Header::mouseMoveEvent(QMouseEvent *event)
202 _mouse_point = event->pos();
204 if (!(event->buttons() & Qt::LeftButton))
207 if ((event->pos() - _mouse_down_point).manhattanLength() <
208 QApplication::startDragDistance())
211 // Move the signals if we are dragging
212 if (!_drag_row_items.empty())
216 const int delta = event->pos().y() - _mouse_down_point.y();
218 for (auto i = _drag_row_items.begin();
219 i != _drag_row_items.end(); i++) {
220 const std::shared_ptr<RowItem> row_item((*i).first);
222 const int y = (*i).second + delta;
224 ((y + View::SignalSnapGridSize / 2) /
225 View::SignalSnapGridSize) *
226 View::SignalSnapGridSize;
227 row_item->set_v_offset(y_snap);
229 // Ensure the trace is selected
241 void Header::leaveEvent(QEvent*)
243 _mouse_point = QPoint(-1, -1);
247 void Header::contextMenuEvent(QContextMenuEvent *event)
249 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
253 QMenu *const menu = r->create_context_menu(this);
257 menu->exec(event->globalPos());
260 void Header::keyPressEvent(QKeyEvent *e)
268 for (const shared_ptr<RowItem> r : _view)
276 void Header::on_signals_changed()
278 for (shared_ptr<RowItem> r : _view) {
280 connect(r.get(), SIGNAL(visibility_changed()),
281 this, SLOT(on_trace_changed()));
282 connect(r.get(), SIGNAL(text_changed()),
283 this, SLOT(on_trace_changed()));
284 connect(r.get(), SIGNAL(colour_changed()),
285 this, SLOT(update()));
289 void Header::on_signals_moved()
294 void Header::on_trace_changed()