]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/header.cpp
Replace View ownership of traces with RowItemOwner
[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 <assert.h>
28
29#include <QApplication>
30#include <QMenu>
31#include <QMouseEvent>
32#include <QPainter>
33#include <QRect>
34
35#include <pv/widgets/popup.h>
36
37using std::max;
38using std::make_pair;
39using std::pair;
40using std::shared_ptr;
41using std::vector;
42
43namespace pv {
44namespace view {
45
46const int Header::Padding = 12;
47const int Header::BaselineOffset = 5;
48
49Header::Header(View &parent) :
50 MarginWidget(parent),
51 _dragging(false)
52{
53 setFocusPolicy(Qt::ClickFocus);
54 setMouseTracking(true);
55
56 connect(&_view.session(), SIGNAL(signals_changed()),
57 this, SLOT(on_signals_changed()));
58
59 connect(&_view, SIGNAL(signals_moved()),
60 this, SLOT(on_signals_moved()));
61
62 // Trigger the initial event manually. The default device has signals
63 // which were created before this object came into being
64 on_signals_changed();
65}
66
67QSize Header::sizeHint() const
68{
69 int max_width = 0;
70
71 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
72 for (shared_ptr<RowItem> r : row_items) {
73 assert(r);
74
75 if (r->enabled()) {
76 max_width = max(max_width, (int)r->label_rect(0).width());
77 }
78 }
79
80 return QSize(max_width + Padding + BaselineOffset, 0);
81}
82
83shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
84{
85 const int w = width() - BaselineOffset;
86 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
87
88 for (const shared_ptr<RowItem> r : row_items)
89 {
90 assert(r);
91 if (r->enabled() && r->label_rect(w).contains(pt))
92 return r;
93 }
94
95 return shared_ptr<RowItem>();
96}
97
98void Header::clear_selection()
99{
100 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
101 for (const shared_ptr<RowItem> r : row_items) {
102 assert(r);
103 r->select(false);
104 }
105
106 update();
107}
108
109void Header::paintEvent(QPaintEvent*)
110{
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;
115 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
116
117 QPainter painter(this);
118 painter.setRenderHint(QPainter::Antialiasing);
119
120 const bool dragging = !_drag_row_items.empty();
121 for (const shared_ptr<RowItem> r : row_items)
122 {
123 assert(r);
124
125 const bool highlight = !dragging &&
126 r->label_rect(w).contains(_mouse_point);
127 r->paint_label(painter, w, highlight);
128 }
129
130 painter.end();
131}
132
133void Header::mousePressEvent(QMouseEvent *event)
134{
135 assert(event);
136
137 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
138
139 if (event->button() & Qt::LeftButton) {
140 _mouse_down_point = event->pos();
141
142 // Save the offsets of any signals which will be dragged
143 for (const shared_ptr<RowItem> r : row_items)
144 if (r->selected())
145 _drag_row_items.push_back(
146 make_pair(r, r->v_offset()));
147 }
148
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);
155 else {
156 mouse_over_row_item->select(true);
157
158 if (~QApplication::keyboardModifiers() &
159 Qt::ControlModifier)
160 _drag_row_items.clear();
161
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()));
167 }
168 }
169
170 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
171 // Unselect all other signals because the Ctrl is not
172 // pressed
173 for (const shared_ptr<RowItem> r : row_items)
174 if (r != mouse_over_row_item)
175 r->select(false);
176 }
177
178 selection_changed();
179 update();
180}
181
182void Header::mouseReleaseEvent(QMouseEvent *event)
183{
184 using pv::widgets::Popup;
185
186 assert(event);
187 if (event->button() == Qt::LeftButton) {
188 if (_dragging)
189 _view.normalize_layout();
190 else
191 {
192 const shared_ptr<RowItem> mouse_over_row_item =
193 get_mouse_over_row_item(event->pos());
194 if (mouse_over_row_item) {
195 const int w = width() - BaselineOffset;
196 Popup *const p =
197 mouse_over_row_item->create_popup(&_view);
198 p->set_position(mapToGlobal(QPoint(w,
199 mouse_over_row_item->get_y())),
200 Popup::Right);
201 p->show();
202 }
203 }
204
205 _dragging = false;
206 _drag_row_items.clear();
207 }
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 // Move the signals if we are dragging
223 if (!_drag_row_items.empty())
224 {
225 _dragging = true;
226
227 const int delta = event->pos().y() - _mouse_down_point.y();
228
229 for (auto i = _drag_row_items.begin();
230 i != _drag_row_items.end(); i++) {
231 const std::shared_ptr<RowItem> row_item((*i).first);
232 if (row_item) {
233 const int y = (*i).second + delta;
234 const int y_snap =
235 ((y + View::SignalSnapGridSize / 2) /
236 View::SignalSnapGridSize) *
237 View::SignalSnapGridSize;
238 row_item->set_v_offset(y_snap);
239
240 // Ensure the trace is selected
241 row_item->select();
242 }
243
244 }
245
246 signals_moved();
247 }
248
249 update();
250}
251
252void Header::leaveEvent(QEvent*)
253{
254 _mouse_point = QPoint(-1, -1);
255 update();
256}
257
258void Header::contextMenuEvent(QContextMenuEvent *event)
259{
260 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
261
262 if (r)
263 r->create_context_menu(this)->exec(event->globalPos());
264}
265
266void Header::keyPressEvent(QKeyEvent *e)
267{
268 assert(e);
269
270 switch (e->key())
271 {
272 case Qt::Key_Delete:
273 {
274 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
275 for (const shared_ptr<RowItem> r : row_items)
276 if (r->selected())
277 r->delete_pressed();
278 break;
279 }
280 }
281}
282
283void Header::on_signals_changed()
284{
285 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
286 for (shared_ptr<RowItem> r : row_items) {
287 assert(r);
288 connect(r.get(), SIGNAL(visibility_changed()),
289 this, SLOT(on_trace_changed()));
290 connect(r.get(), SIGNAL(text_changed()),
291 this, SLOT(on_trace_changed()));
292 connect(r.get(), SIGNAL(colour_changed()),
293 this, SLOT(update()));
294 }
295}
296
297void Header::on_signals_moved()
298{
299 update();
300}
301
302void Header::on_trace_changed()
303{
304 update();
305 geometry_updated();
306}
307
308} // namespace view
309} // namespace pv