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