]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/header.cpp
View: Create trace groups from channel groups
[pulseview.git] / pv / view / header.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 "header.h"
22#include "view.h"
23
24#include "signal.h"
25#include "../sigsession.h"
26
27#include <cassert>
28#include <algorithm>
29
30#include <QApplication>
31#include <QMenu>
32#include <QMouseEvent>
33#include <QPainter>
34#include <QRect>
35
36#include <pv/widgets/popup.h>
37
38using std::max;
39using std::make_pair;
40using std::pair;
41using std::shared_ptr;
42using std::stable_sort;
43using std::vector;
44
45namespace pv {
46namespace view {
47
48const int Header::Padding = 12;
49const int Header::BaselineOffset = 5;
50
51Header::Header(View &parent) :
52 MarginWidget(parent),
53 _dragging(false)
54{
55 setFocusPolicy(Qt::ClickFocus);
56 setMouseTracking(true);
57
58 connect(&_view, SIGNAL(signals_moved()),
59 this, SLOT(on_signals_moved()));
60}
61
62QSize Header::sizeHint() const
63{
64 QRectF max_rect(-Padding, 0, Padding, 0);
65 for (auto &i : _view)
66 if (i->enabled())
67 max_rect = max_rect.united(i->label_rect(0));
68 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
69}
70
71shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
72{
73 const int w = width() - BaselineOffset;
74 for (auto &i : _view)
75 if (i->enabled() && i->label_rect(w).contains(pt))
76 return i;
77 return shared_ptr<RowItem>();
78}
79
80void Header::clear_selection()
81{
82 for (auto &i : _view)
83 i->select(false);
84 update();
85}
86
87void Header::show_popup(const shared_ptr<RowItem> &item)
88{
89 using pv::widgets::Popup;
90
91 Popup *const p = item->create_popup(&_view);
92 if (!p)
93 return;
94
95 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
96 p->set_position(mapToGlobal(pt), Popup::Right);
97 p->show();
98}
99
100void Header::paintEvent(QPaintEvent*)
101{
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;
106
107 vector< shared_ptr<RowItem> > row_items(
108 _view.begin(), _view.end());
109
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(); });
113
114 QPainter painter(this);
115 painter.setRenderHint(QPainter::Antialiasing);
116
117 for (const shared_ptr<RowItem> r : row_items)
118 {
119 assert(r);
120
121 const bool highlight = !_dragging &&
122 r->label_rect(w).contains(_mouse_point);
123 r->paint_label(painter, w, highlight);
124 }
125
126 painter.end();
127}
128
129void Header::mouseLeftPressEvent(QMouseEvent *event)
130{
131 (void)event;
132
133 const bool ctrl_pressed =
134 QApplication::keyboardModifiers() & Qt::ControlModifier;
135
136 // Clear selection if control is not pressed and this item is unselected
137 if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
138 !ctrl_pressed)
139 for (shared_ptr<RowItem> r : _view)
140 r->select(false);
141
142 // Set the signal selection state if the item has been clicked
143 if (_mouse_down_item) {
144 if (ctrl_pressed)
145 _mouse_down_item->select(!_mouse_down_item->selected());
146 else
147 _mouse_down_item->select(true);
148 }
149
150 // Save the offsets of any signals which will be dragged
151 for (const shared_ptr<RowItem> r : _view)
152 if (r->selected())
153 r->drag();
154
155 selection_changed();
156 update();
157}
158
159void Header::mousePressEvent(QMouseEvent *event)
160{
161 assert(event);
162
163 _mouse_down_point = event->pos();
164 _mouse_down_item = get_mouse_over_row_item(event->pos());
165
166 if (event->button() & Qt::LeftButton)
167 mouseLeftPressEvent(event);
168}
169
170void Header::mouseLeftReleaseEvent(QMouseEvent *event)
171{
172 assert(event);
173
174 const bool ctrl_pressed =
175 QApplication::keyboardModifiers() & Qt::ControlModifier;
176
177 // Unselect everything if control is not pressed
178 const shared_ptr<RowItem> mouse_over =
179 get_mouse_over_row_item(event->pos());
180
181 for (auto &r : _view)
182 r->drag_release();
183
184 if (_dragging)
185 _view.restack_all_row_items();
186 else
187 {
188 if (!ctrl_pressed) {
189 for (shared_ptr<RowItem> r : _view)
190 if (_mouse_down_item != r)
191 r->select(false);
192
193 if (_mouse_down_item)
194 show_popup(_mouse_down_item);
195 }
196 }
197
198 _dragging = false;
199}
200
201void Header::mouseReleaseEvent(QMouseEvent *event)
202{
203 assert(event);
204 if (event->button() & Qt::LeftButton)
205 mouseLeftReleaseEvent(event);
206
207 _mouse_down_item = nullptr;
208}
209
210void Header::mouseMoveEvent(QMouseEvent *event)
211{
212 assert(event);
213 _mouse_point = event->pos();
214
215 if (!(event->buttons() & Qt::LeftButton))
216 return;
217
218 if ((event->pos() - _mouse_down_point).manhattanLength() <
219 QApplication::startDragDistance())
220 return;
221
222 // Check all the drag items share a common owner
223 RowItemOwner *item_owner = nullptr;
224 for (shared_ptr<RowItem> r : _view)
225 if (r->dragging()) {
226 if (!item_owner)
227 item_owner = r->owner();
228 else if(item_owner != r->owner())
229 return;
230 }
231
232 if (!item_owner)
233 return;
234
235 // Do the drag
236 _dragging = true;
237
238 const int delta = event->pos().y() - _mouse_down_point.y();
239
240 for (std::shared_ptr<RowItem> r : _view)
241 if (r->dragging()) {
242 r->force_to_v_offset(r->drag_point().y() + delta);
243
244 // Ensure the trace is selected
245 r->select();
246 }
247
248 item_owner->restack_items();
249 for (const auto &r : *item_owner)
250 r->animate_to_layout_v_offset();
251 signals_moved();
252
253 update();
254}
255
256void Header::leaveEvent(QEvent*)
257{
258 _mouse_point = QPoint(-1, -1);
259 update();
260}
261
262void Header::contextMenuEvent(QContextMenuEvent *event)
263{
264 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
265 if (!r)
266 return;
267
268 QMenu *const menu = r->create_context_menu(this);
269 if (!menu)
270 return;
271
272 menu->exec(event->globalPos());
273}
274
275void Header::keyPressEvent(QKeyEvent *e)
276{
277 assert(e);
278
279 switch (e->key())
280 {
281 case Qt::Key_Delete:
282 {
283 for (const shared_ptr<RowItem> r : _view)
284 if (r->selected())
285 r->delete_pressed();
286 break;
287 }
288 }
289}
290
291void Header::on_signals_moved()
292{
293 update();
294}
295
296} // namespace view
297} // namespace pv