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