]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Header, Viewport: Don't manually invoke on_signals_changed on construction
[pulseview.git] / pv / view / header.cpp
CommitLineData
1d8dca91 1/*
b3f22de0 2 * This file is part of the PulseView project.
1d8dca91
JH
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
8d634081 24#include "signal.h"
51e77110 25#include "../sigsession.h"
1d8dca91 26
68b21a71
JH
27#include <cassert>
28#include <algorithm>
1d8dca91 29
e3374498 30#include <QApplication>
49f8ff3f 31#include <QMenu>
a29bb7fb 32#include <QMouseEvent>
1d8dca91
JH
33#include <QPainter>
34#include <QRect>
35
569d1e41
JH
36#include <pv/widgets/popup.h>
37
819f4c25
JH
38using std::max;
39using std::make_pair;
40using std::pair;
f9abf97e 41using std::shared_ptr;
68b21a71 42using std::stable_sort;
819f4c25 43using std::vector;
1d8dca91
JH
44
45namespace pv {
46namespace view {
47
d7c0ca4a 48const int Header::Padding = 12;
512bfc56 49const int Header::BaselineOffset = 5;
d7c0ca4a 50
1d8dca91 51Header::Header(View &parent) :
728fcafc
JH
52 MarginWidget(parent),
53 _dragging(false)
1d8dca91 54{
5ed1adf5 55 setFocusPolicy(Qt::ClickFocus);
a29bb7fb 56 setMouseTracking(true);
49f8ff3f 57
9e40e83d
JH
58 connect(&_view.session(), SIGNAL(signals_changed()),
59 this, SLOT(on_signals_changed()));
60
07204819
JH
61 connect(&_view, SIGNAL(signals_moved()),
62 this, SLOT(on_signals_moved()));
1d8dca91
JH
63}
64
d7c0ca4a
JH
65QSize Header::sizeHint() const
66{
67 int max_width = 0;
68
aa59d5c2
JH
69 for (auto &i : _view)
70 if (i->enabled())
71 max_width = max(max_width, (int)i->label_rect(0).width());
d7c0ca4a 72
512bfc56 73 return QSize(max_width + Padding + BaselineOffset, 0);
d7c0ca4a
JH
74}
75
eae6e30a 76shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
e3374498 77{
de0b46de 78 const int w = width() - BaselineOffset;
aa59d5c2
JH
79 for (auto &i : _view)
80 if (i->enabled() && i->label_rect(w).contains(pt))
81 return i;
eae6e30a 82 return shared_ptr<RowItem>();
e3374498
JH
83}
84
a2ae0205
JH
85void Header::clear_selection()
86{
aa59d5c2
JH
87 for (auto &i : _view)
88 i->select(false);
a2ae0205
JH
89 update();
90}
91
8b4802fb
JH
92void Header::show_popup(const shared_ptr<RowItem> &item)
93{
94 using pv::widgets::Popup;
95
96 Popup *const p = item->create_popup(&_view);
97 if (!p)
98 return;
99
100 const QPoint pt(width() - BaselineOffset, item->get_y());
101 p->set_position(mapToGlobal(pt), Popup::Right);
102 p->show();
103}
104
e314eca4 105void Header::paintEvent(QPaintEvent*)
1d8dca91 106{
512bfc56
JS
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;
68b21a71 111
aa59d5c2
JH
112 vector< shared_ptr<RowItem> > row_items(
113 _view.begin(), _view.end());
114
68b21a71
JH
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(); });
1d8dca91
JH
118
119 QPainter painter(this);
120 painter.setRenderHint(QPainter::Antialiasing);
121
eae6e30a
JH
122 const bool dragging = !_drag_row_items.empty();
123 for (const shared_ptr<RowItem> r : row_items)
1d8dca91 124 {
eae6e30a 125 assert(r);
1d8dca91 126
25201e88 127 const bool highlight = !dragging &&
eae6e30a
JH
128 r->label_rect(w).contains(_mouse_point);
129 r->paint_label(painter, w, highlight);
1d8dca91
JH
130 }
131
132 painter.end();
133}
134
e3374498
JH
135void Header::mousePressEvent(QMouseEvent *event)
136{
137 assert(event);
138
333d5bbc 139 if (event->button() & Qt::LeftButton) {
54401bbb
JH
140 _mouse_down_point = event->pos();
141
da2bebfb 142 // Save the offsets of any signals which will be dragged
aa59d5c2 143 for (const shared_ptr<RowItem> r : _view)
eae6e30a
JH
144 if (r->selected())
145 _drag_row_items.push_back(
146 make_pair(r, r->v_offset()));
da2bebfb
JH
147 }
148
149 // Select the signal if it has been clicked
eae6e30a
JH
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);
da2bebfb 155 else {
eae6e30a 156 mouse_over_row_item->select(true);
da2bebfb 157
333d5bbc 158 if (~QApplication::keyboardModifiers() &
07204819 159 Qt::ControlModifier)
eae6e30a 160 _drag_row_items.clear();
07204819 161
da2bebfb 162 // Add the signal to the drag list
333d5bbc 163 if (event->button() & Qt::LeftButton)
eae6e30a
JH
164 _drag_row_items.push_back(
165 make_pair(mouse_over_row_item,
166 mouse_over_row_item->v_offset()));
da2bebfb 167 }
54401bbb
JH
168 }
169
333d5bbc 170 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
171 // Unselect all other signals because the Ctrl is not
172 // pressed
aa59d5c2 173 for (const shared_ptr<RowItem> r : _view)
eae6e30a
JH
174 if (r != mouse_over_row_item)
175 r->select(false);
07204819
JH
176 }
177
b2a53645 178 selection_changed();
e3374498
JH
179 update();
180}
181
54401bbb
JH
182void Header::mouseReleaseEvent(QMouseEvent *event)
183{
184 assert(event);
333d5bbc 185 if (event->button() == Qt::LeftButton) {
569d1e41
JH
186 if (_dragging)
187 _view.normalize_layout();
188 else
189 {
eae6e30a
JH
190 const shared_ptr<RowItem> mouse_over_row_item =
191 get_mouse_over_row_item(event->pos());
8b4802fb
JH
192 if (mouse_over_row_item)
193 show_popup(mouse_over_row_item);
569d1e41
JH
194 }
195
728fcafc 196 _dragging = false;
eae6e30a 197 _drag_row_items.clear();
4b192962 198 }
54401bbb
JH
199}
200
a29bb7fb
JH
201void Header::mouseMoveEvent(QMouseEvent *event)
202{
203 assert(event);
204 _mouse_point = event->pos();
54401bbb 205
728fcafc
JH
206 if (!(event->buttons() & Qt::LeftButton))
207 return;
208
209 if ((event->pos() - _mouse_down_point).manhattanLength() <
210 QApplication::startDragDistance())
211 return;
212
54401bbb 213 // Move the signals if we are dragging
eae6e30a 214 if (!_drag_row_items.empty())
728fcafc
JH
215 {
216 _dragging = true;
217
54401bbb
JH
218 const int delta = event->pos().y() - _mouse_down_point.y();
219
eae6e30a
JH
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);
223 if (row_item) {
da2bebfb 224 const int y = (*i).second + delta;
49153683
JH
225 const int y_snap =
226 ((y + View::SignalSnapGridSize / 2) /
227 View::SignalSnapGridSize) *
228 View::SignalSnapGridSize;
eae6e30a 229 row_item->set_v_offset(y_snap);
da2bebfb 230
38eeddea 231 // Ensure the trace is selected
eae6e30a 232 row_item->select();
49153683 233 }
da2bebfb
JH
234
235 }
54401bbb
JH
236
237 signals_moved();
238 }
239
a29bb7fb
JH
240 update();
241}
242
e314eca4 243void Header::leaveEvent(QEvent*)
a29bb7fb
JH
244{
245 _mouse_point = QPoint(-1, -1);
246 update();
247}
248
49f8ff3f
JH
249void Header::contextMenuEvent(QContextMenuEvent *event)
250{
eae6e30a 251 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
a28878f4
JH
252 if (!r)
253 return;
254
255 QMenu *const menu = r->create_context_menu(this);
256 if (!menu)
257 return;
49f8ff3f 258
a28878f4 259 menu->exec(event->globalPos());
b3b57abc
JH
260}
261
5ed1adf5
JH
262void Header::keyPressEvent(QKeyEvent *e)
263{
264 assert(e);
265
266 switch (e->key())
267 {
268 case Qt::Key_Delete:
269 {
aa59d5c2 270 for (const shared_ptr<RowItem> r : _view)
eae6e30a
JH
271 if (r->selected())
272 r->delete_pressed();
5ed1adf5
JH
273 break;
274 }
275 }
276}
277
9e40e83d
JH
278void Header::on_signals_changed()
279{
aa59d5c2 280 for (shared_ptr<RowItem> r : _view) {
eae6e30a
JH
281 assert(r);
282 connect(r.get(), SIGNAL(visibility_changed()),
6f98ca4c 283 this, SLOT(on_trace_changed()));
eae6e30a 284 connect(r.get(), SIGNAL(text_changed()),
6f98ca4c 285 this, SLOT(on_trace_changed()));
eae6e30a 286 connect(r.get(), SIGNAL(colour_changed()),
91e8bf08 287 this, SLOT(update()));
9e40e83d
JH
288 }
289}
290
07204819
JH
291void Header::on_signals_moved()
292{
293 update();
294}
295
6f98ca4c 296void Header::on_trace_changed()
d7c0ca4a
JH
297{
298 update();
299 geometry_updated();
300}
07204819 301
1d8dca91
JH
302} // namespace view
303} // namespace pv