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