]> sigrok.org Git - pulseview.git/blob - pv/view/header.cpp
b9261f9753d060bdaffbb48ab7c52a35d4a5210b
[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, SIGNAL(signals_moved()),
59                 this, SLOT(on_signals_moved()));
60 }
61
62 QSize 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
71 shared_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
80 void Header::clear_selection()
81 {
82         for (auto &i : _view)
83                 i->select(false);
84         update();
85 }
86
87 void Header::signals_updated()
88 {
89         for (shared_ptr<RowItem> r : _view) {
90                 assert(r);
91                 connect(r.get(), SIGNAL(appearance_changed()),
92                         this, SLOT(on_trace_changed()));
93         }
94 }
95
96 void Header::show_popup(const shared_ptr<RowItem> &item)
97 {
98         using pv::widgets::Popup;
99
100         Popup *const p = item->create_popup(&_view);
101         if (!p)
102                 return;
103
104         const QPoint pt(width() - BaselineOffset, item->get_y());
105         p->set_position(mapToGlobal(pt), Popup::Right);
106         p->show();
107 }
108
109 void 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
116         vector< shared_ptr<RowItem> > row_items(
117                 _view.begin(), _view.end());
118
119         stable_sort(row_items.begin(), row_items.end(),
120                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
121                         return a->v_offset() < b->v_offset(); });
122
123         QPainter painter(this);
124         painter.setRenderHint(QPainter::Antialiasing);
125
126         for (const shared_ptr<RowItem> r : row_items)
127         {
128                 assert(r);
129
130                 const bool highlight = !_dragging &&
131                         r->label_rect(w).contains(_mouse_point);
132                 r->paint_label(painter, w, highlight);
133         }
134
135         painter.end();
136 }
137
138 void Header::mouseLeftPressEvent(QMouseEvent *event)
139 {
140         const bool ctrl_pressed =
141                 QApplication::keyboardModifiers() & Qt::ControlModifier;
142
143         // Clear selection if control is not pressed and this item is unselected
144         const shared_ptr<RowItem> mouse_over =
145                 get_mouse_over_row_item(event->pos());
146         if (!ctrl_pressed && (!mouse_over || !mouse_over->selected()))
147                 for (shared_ptr<RowItem> r : _view)
148                         r->select(false);
149
150         // Set the signal selection state if the item has been clicked
151         if (mouse_over) {
152                 if (ctrl_pressed)
153                         mouse_over->select(!mouse_over->selected());
154                 else
155                         mouse_over->select(true);
156         }
157
158         // Save the offsets of any signals which will be dragged
159         _mouse_down_point = event->pos();
160         for (const shared_ptr<RowItem> r : _view)
161                 if (r->selected())
162                         r->drag();
163
164         selection_changed();
165         update();
166 }
167
168 void Header::mousePressEvent(QMouseEvent *event)
169 {
170         assert(event);
171         if (event->button() & Qt::LeftButton)
172                 mouseLeftPressEvent(event);
173 }
174
175 void Header::mouseLeftReleaseEvent(QMouseEvent *event)
176 {
177         assert(event);
178
179         const bool ctrl_pressed =
180                 QApplication::keyboardModifiers() & Qt::ControlModifier;
181
182         // Unselect everything if control is not pressed
183         const shared_ptr<RowItem> mouse_over =
184                 get_mouse_over_row_item(event->pos());
185
186         for (auto &r : _view)
187                 r->drag_release();
188
189         if (_dragging)
190                 _view.normalize_layout();
191         else
192         {
193                 if (!ctrl_pressed) {
194                         for (shared_ptr<RowItem> r : _view)
195                                 if (mouse_over != r)
196                                         r->select(false);
197
198                         if (mouse_over)
199                                 show_popup(mouse_over);
200                 }
201         }
202
203         _dragging = false;
204 }
205
206 void Header::mouseReleaseEvent(QMouseEvent *event)
207 {
208         assert(event);
209         if (event->button() & Qt::LeftButton)
210                 mouseLeftReleaseEvent(event);
211 }
212
213 void Header::mouseMoveEvent(QMouseEvent *event)
214 {
215         assert(event);
216         _mouse_point = event->pos();
217
218         if (!(event->buttons() & Qt::LeftButton))
219                 return;
220
221         if ((event->pos() - _mouse_down_point).manhattanLength() <
222                 QApplication::startDragDistance())
223                 return;
224
225         // Check all the drag items share a common owner
226         RowItemOwner *item_owner = nullptr;
227         for (shared_ptr<RowItem> r : _view)
228                 if (r->dragging()) {
229                         if (!item_owner)
230                                 item_owner = r->owner();
231                         else if(item_owner != r->owner())
232                                 return;
233                 }
234
235         if (!item_owner)
236                 return;
237
238         // Do the drag
239         _dragging = true;
240
241         const int delta = event->pos().y() - _mouse_down_point.y();
242
243         for (std::shared_ptr<RowItem> r : _view)
244                 if (r->dragging()) {
245                         r->set_v_offset(r->drag_point().y() + delta);
246
247                         // Ensure the trace is selected
248                         r->select();
249                 }
250
251         signals_moved();
252
253         update();
254 }
255
256 void Header::leaveEvent(QEvent*)
257 {
258         _mouse_point = QPoint(-1, -1);
259         update();
260 }
261
262 void 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
275 void 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
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