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
71 max_width = max(max_width, (int)i->label_rect(0).width());
73 return QSize(max_width + Padding + BaselineOffset, 0);
76 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
78 const int w = width() - BaselineOffset;
80 if (i->enabled() && i->label_rect(w).contains(pt))
82 return shared_ptr<RowItem>();
85 void Header::clear_selection()
92 void Header::show_popup(const shared_ptr<RowItem> &item)
94 using pv::widgets::Popup;
96 Popup *const p = item->create_popup(&_view);
100 const QPoint pt(width() - BaselineOffset, item->get_y());
101 p->set_position(mapToGlobal(pt), Popup::Right);
105 void Header::paintEvent(QPaintEvent*)
107 // The trace labels are not drawn with the arrows exactly on the
108 // left edge of the widget, because then the selection shadow
109 // would be clipped away.
110 const int w = width() - BaselineOffset;
112 vector< shared_ptr<RowItem> > row_items(
113 _view.begin(), _view.end());
115 stable_sort(row_items.begin(), row_items.end(),
116 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
117 return a->v_offset() < b->v_offset(); });
119 QPainter painter(this);
120 painter.setRenderHint(QPainter::Antialiasing);
122 const bool dragging = !_drag_row_items.empty();
123 for (const shared_ptr<RowItem> r : row_items)
127 const bool highlight = !dragging &&
128 r->label_rect(w).contains(_mouse_point);
129 r->paint_label(painter, w, highlight);
135 void Header::mousePressEvent(QMouseEvent *event)
139 if (event->button() & Qt::LeftButton) {
140 _mouse_down_point = event->pos();
142 // Save the offsets of any signals which will be dragged
143 for (const shared_ptr<RowItem> r : _view)
145 _drag_row_items.push_back(
146 make_pair(r, r->v_offset()));
149 // Select the signal if it has been clicked
150 const shared_ptr<RowItem> mouse_over_row_item =
151 get_mouse_over_row_item(event->pos());
152 if (mouse_over_row_item) {
153 if (mouse_over_row_item->selected())
154 mouse_over_row_item->select(false);
156 mouse_over_row_item->select(true);
158 if (~QApplication::keyboardModifiers() &
160 _drag_row_items.clear();
162 // Add the signal to the drag list
163 if (event->button() & Qt::LeftButton)
164 _drag_row_items.push_back(
165 make_pair(mouse_over_row_item,
166 mouse_over_row_item->v_offset()));
170 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
171 // Unselect all other signals because the Ctrl is not
173 for (const shared_ptr<RowItem> r : _view)
174 if (r != mouse_over_row_item)
182 void Header::mouseReleaseEvent(QMouseEvent *event)
185 if (event->button() == Qt::LeftButton) {
187 _view.normalize_layout();
190 const shared_ptr<RowItem> mouse_over_row_item =
191 get_mouse_over_row_item(event->pos());
192 if (mouse_over_row_item)
193 show_popup(mouse_over_row_item);
197 _drag_row_items.clear();
201 void Header::mouseMoveEvent(QMouseEvent *event)
204 _mouse_point = event->pos();
206 if (!(event->buttons() & Qt::LeftButton))
209 if ((event->pos() - _mouse_down_point).manhattanLength() <
210 QApplication::startDragDistance())
213 // Move the signals if we are dragging
214 if (!_drag_row_items.empty())
218 const int delta = event->pos().y() - _mouse_down_point.y();
220 for (auto i = _drag_row_items.begin();
221 i != _drag_row_items.end(); i++) {
222 const std::shared_ptr<RowItem> row_item((*i).first);
224 const int y = (*i).second + delta;
226 ((y + View::SignalSnapGridSize / 2) /
227 View::SignalSnapGridSize) *
228 View::SignalSnapGridSize;
229 row_item->set_v_offset(y_snap);
231 // Ensure the trace is selected
243 void Header::leaveEvent(QEvent*)
245 _mouse_point = QPoint(-1, -1);
249 void Header::contextMenuEvent(QContextMenuEvent *event)
251 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
255 QMenu *const menu = r->create_context_menu(this);
259 menu->exec(event->globalPos());
262 void Header::keyPressEvent(QKeyEvent *e)
270 for (const shared_ptr<RowItem> r : _view)
278 void Header::on_signals_changed()
280 for (shared_ptr<RowItem> r : _view) {
282 connect(r.get(), SIGNAL(visibility_changed()),
283 this, SLOT(on_trace_changed()));
284 connect(r.get(), SIGNAL(text_changed()),
285 this, SLOT(on_trace_changed()));
286 connect(r.get(), SIGNAL(colour_changed()),
287 this, SLOT(update()));
291 void Header::on_signals_moved()
296 void Header::on_trace_changed()