]> sigrok.org Git - pulseview.git/blob - pv/view/header.cpp
50eefc43cd765b1d0a300fb7fb622549b94044c8
[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
65 QSize Header::sizeHint() const
66 {
67         int max_width = 0;
68
69         for (auto &i : _view)
70                 if (i->enabled())
71                         max_width = max(max_width, (int)i->label_rect(0).width());
72
73         return QSize(max_width + Padding + BaselineOffset, 0);
74 }
75
76 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
77 {
78         const int w = width() - BaselineOffset;
79         for (auto &i : _view)
80                 if (i->enabled() && i->label_rect(w).contains(pt))
81                         return i;
82         return shared_ptr<RowItem>();
83 }
84
85 void Header::clear_selection()
86 {
87         for (auto &i : _view)
88                 i->select(false);
89         update();
90 }
91
92 void 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
105 void Header::paintEvent(QPaintEvent*)
106 {
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;
111
112         vector< shared_ptr<RowItem> > row_items(
113                 _view.begin(), _view.end());
114
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(); });
118
119         QPainter painter(this);
120         painter.setRenderHint(QPainter::Antialiasing);
121
122         const bool dragging = !_drag_row_items.empty();
123         for (const shared_ptr<RowItem> r : row_items)
124         {
125                 assert(r);
126
127                 const bool highlight = !dragging &&
128                         r->label_rect(w).contains(_mouse_point);
129                 r->paint_label(painter, w, highlight);
130         }
131
132         painter.end();
133 }
134
135 void Header::mousePressEvent(QMouseEvent *event)
136 {
137         assert(event);
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 : _view)
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 : _view)
174                         if (r != mouse_over_row_item)
175                                 r->select(false);
176         }
177
178         selection_changed();
179         update();
180 }
181
182 void Header::mouseReleaseEvent(QMouseEvent *event)
183 {
184         assert(event);
185         if (event->button() == Qt::LeftButton) {
186                 if (_dragging)
187                         _view.normalize_layout();
188                 else
189                 {
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);
194                 }
195
196                 _dragging = false;
197                 _drag_row_items.clear();
198         }
199 }
200
201 void Header::mouseMoveEvent(QMouseEvent *event)
202 {
203         assert(event);
204         _mouse_point = event->pos();
205
206         if (!(event->buttons() & Qt::LeftButton))
207                 return;
208
209         if ((event->pos() - _mouse_down_point).manhattanLength() <
210                 QApplication::startDragDistance())
211                 return;
212
213         // Move the signals if we are dragging
214         if (!_drag_row_items.empty())
215         {
216                 _dragging = true;
217
218                 const int delta = event->pos().y() - _mouse_down_point.y();
219
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) {
224                                 const int y = (*i).second + delta;
225                                 const int y_snap =
226                                         ((y + View::SignalSnapGridSize / 2) /
227                                                 View::SignalSnapGridSize) *
228                                                 View::SignalSnapGridSize;
229                                 row_item->set_v_offset(y_snap);
230
231                                 // Ensure the trace is selected
232                                 row_item->select();
233                         }
234                         
235                 }
236
237                 signals_moved();
238         }
239
240         update();
241 }
242
243 void Header::leaveEvent(QEvent*)
244 {
245         _mouse_point = QPoint(-1, -1);
246         update();
247 }
248
249 void Header::contextMenuEvent(QContextMenuEvent *event)
250 {
251         const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
252         if (!r)
253                 return;
254
255         QMenu *const menu = r->create_context_menu(this);
256         if (!menu)
257                 return;
258
259         menu->exec(event->globalPos());
260 }
261
262 void Header::keyPressEvent(QKeyEvent *e)
263 {
264         assert(e);
265
266         switch (e->key())
267         {
268         case Qt::Key_Delete:
269         {
270                 for (const shared_ptr<RowItem> r : _view)
271                         if (r->selected())
272                                 r->delete_pressed();
273                 break;
274         }
275         }
276 }
277
278 void Header::on_signals_changed()
279 {
280         for (shared_ptr<RowItem> r : _view) {
281                 assert(r);
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()));
288         }
289 }
290
291 void Header::on_signals_moved()
292 {
293         update();
294 }
295
296 void Header::on_trace_changed()
297 {
298         update();
299         geometry_updated();
300 }
301
302 } // namespace view
303 } // namespace pv