]> sigrok.org Git - pulseview.git/blob - pv/view/header.cpp
TraceGroup: Implemented label_rect
[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         QRectF max_rect(-Padding, 0, Padding, 0);
68         for (auto &i : _view)
69                 if (i->enabled())
70                         max_rect = max_rect.united(i->label_rect(0));
71         return QSize(max_rect.width() + Padding + BaselineOffset, 0);
72 }
73
74 shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
75 {
76         const int w = width() - BaselineOffset;
77         for (auto &i : _view)
78                 if (i->enabled() && i->label_rect(w).contains(pt))
79                         return i;
80         return shared_ptr<RowItem>();
81 }
82
83 void Header::clear_selection()
84 {
85         for (auto &i : _view)
86                 i->select(false);
87         update();
88 }
89
90 void Header::show_popup(const shared_ptr<RowItem> &item)
91 {
92         using pv::widgets::Popup;
93
94         Popup *const p = item->create_popup(&_view);
95         if (!p)
96                 return;
97
98         const QPoint pt(width() - BaselineOffset, item->get_y());
99         p->set_position(mapToGlobal(pt), Popup::Right);
100         p->show();
101 }
102
103 void Header::paintEvent(QPaintEvent*)
104 {
105         // The trace labels are not drawn with the arrows exactly on the
106         // left edge of the widget, because then the selection shadow
107         // would be clipped away.
108         const int w = width() - BaselineOffset;
109
110         vector< shared_ptr<RowItem> > row_items(
111                 _view.begin(), _view.end());
112
113         stable_sort(row_items.begin(), row_items.end(),
114                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
115                         return a->v_offset() < b->v_offset(); });
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
133 void Header::mousePressEvent(QMouseEvent *event)
134 {
135         assert(event);
136
137         if (event->button() & Qt::LeftButton) {
138                 _mouse_down_point = event->pos();
139
140                 // Save the offsets of any signals which will be dragged
141                 for (const shared_ptr<RowItem> r : _view)
142                         if (r->selected())
143                                 _drag_row_items.push_back(
144                                         make_pair(r, r->v_offset()));
145         }
146
147         // Select the signal if it has been clicked
148         const shared_ptr<RowItem> mouse_over_row_item =
149                 get_mouse_over_row_item(event->pos());
150         if (mouse_over_row_item) {
151                 if (mouse_over_row_item->selected())
152                         mouse_over_row_item->select(false);
153                 else {
154                         mouse_over_row_item->select(true);
155
156                         if (~QApplication::keyboardModifiers() &
157                                 Qt::ControlModifier)
158                                 _drag_row_items.clear();
159
160                         // Add the signal to the drag list
161                         if (event->button() & Qt::LeftButton)
162                                 _drag_row_items.push_back(
163                                         make_pair(mouse_over_row_item,
164                                         mouse_over_row_item->v_offset()));
165                 }
166         }
167
168         if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
169                 // Unselect all other signals because the Ctrl is not
170                 // pressed
171                 for (const shared_ptr<RowItem> r : _view)
172                         if (r != mouse_over_row_item)
173                                 r->select(false);
174         }
175
176         selection_changed();
177         update();
178 }
179
180 void Header::mouseReleaseEvent(QMouseEvent *event)
181 {
182         assert(event);
183         if (event->button() == Qt::LeftButton) {
184                 if (_dragging)
185                         _view.normalize_layout();
186                 else
187                 {
188                         const shared_ptr<RowItem> mouse_over_row_item =
189                                 get_mouse_over_row_item(event->pos());
190                         if (mouse_over_row_item)
191                                 show_popup(mouse_over_row_item);
192                 }
193
194                 _dragging = false;
195                 _drag_row_items.clear();
196         }
197 }
198
199 void Header::mouseMoveEvent(QMouseEvent *event)
200 {
201         assert(event);
202         _mouse_point = event->pos();
203
204         if (!(event->buttons() & Qt::LeftButton))
205                 return;
206
207         if ((event->pos() - _mouse_down_point).manhattanLength() <
208                 QApplication::startDragDistance())
209                 return;
210
211         // Move the signals if we are dragging
212         if (!_drag_row_items.empty())
213         {
214                 _dragging = true;
215
216                 const int delta = event->pos().y() - _mouse_down_point.y();
217
218                 for (auto i = _drag_row_items.begin();
219                         i != _drag_row_items.end(); i++) {
220                         const std::shared_ptr<RowItem> row_item((*i).first);
221                         if (row_item) {
222                                 const int y = (*i).second + delta;
223                                 const int y_snap =
224                                         ((y + View::SignalSnapGridSize / 2) /
225                                                 View::SignalSnapGridSize) *
226                                                 View::SignalSnapGridSize;
227                                 row_item->set_v_offset(y_snap);
228
229                                 // Ensure the trace is selected
230                                 row_item->select();
231                         }
232                         
233                 }
234
235                 signals_moved();
236         }
237
238         update();
239 }
240
241 void Header::leaveEvent(QEvent*)
242 {
243         _mouse_point = QPoint(-1, -1);
244         update();
245 }
246
247 void Header::contextMenuEvent(QContextMenuEvent *event)
248 {
249         const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
250         if (!r)
251                 return;
252
253         QMenu *const menu = r->create_context_menu(this);
254         if (!menu)
255                 return;
256
257         menu->exec(event->globalPos());
258 }
259
260 void Header::keyPressEvent(QKeyEvent *e)
261 {
262         assert(e);
263
264         switch (e->key())
265         {
266         case Qt::Key_Delete:
267         {
268                 for (const shared_ptr<RowItem> r : _view)
269                         if (r->selected())
270                                 r->delete_pressed();
271                 break;
272         }
273         }
274 }
275
276 void Header::on_signals_changed()
277 {
278         for (shared_ptr<RowItem> r : _view) {
279                 assert(r);
280                 connect(r.get(), SIGNAL(visibility_changed()),
281                         this, SLOT(on_trace_changed()));
282                 connect(r.get(), SIGNAL(text_changed()),
283                         this, SLOT(on_trace_changed()));
284                 connect(r.get(), SIGNAL(colour_changed()),
285                         this, SLOT(update()));
286         }
287 }
288
289 void Header::on_signals_moved()
290 {
291         update();
292 }
293
294 void Header::on_trace_changed()
295 {
296         update();
297         geometry_updated();
298 }
299
300 } // namespace view
301 } // namespace pv