]>
Commit | Line | Data |
---|---|---|
1d8dca91 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
1d8dca91 JH |
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 | ||
8d634081 | 24 | #include "signal.h" |
51e77110 | 25 | #include "../sigsession.h" |
1d8dca91 JH |
26 | |
27 | #include <assert.h> | |
28 | ||
29 | #include <boost/foreach.hpp> | |
30 | ||
e3374498 | 31 | #include <QApplication> |
49f8ff3f | 32 | #include <QMenu> |
a29bb7fb | 33 | #include <QMouseEvent> |
1d8dca91 JH |
34 | #include <QPainter> |
35 | #include <QRect> | |
36 | ||
569d1e41 JH |
37 | #include <pv/widgets/popup.h> |
38 | ||
1d8dca91 JH |
39 | using namespace boost; |
40 | using namespace std; | |
41 | ||
42 | namespace pv { | |
43 | namespace view { | |
44 | ||
45 | Header::Header(View &parent) : | |
728fcafc JH |
46 | MarginWidget(parent), |
47 | _dragging(false) | |
1d8dca91 | 48 | { |
5ed1adf5 | 49 | setFocusPolicy(Qt::ClickFocus); |
a29bb7fb | 50 | setMouseTracking(true); |
49f8ff3f | 51 | |
9e40e83d JH |
52 | connect(&_view.session(), SIGNAL(signals_changed()), |
53 | this, SLOT(on_signals_changed())); | |
54 | ||
07204819 JH |
55 | connect(&_view, SIGNAL(signals_moved()), |
56 | this, SLOT(on_signals_moved())); | |
1d8dca91 JH |
57 | } |
58 | ||
38eeddea | 59 | shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt) |
e3374498 JH |
60 | { |
61 | const int w = width(); | |
38eeddea | 62 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 63 | |
38eeddea | 64 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
e3374498 | 65 | { |
38eeddea | 66 | assert(t); |
01fd3263 | 67 | if (t->pt_in_label_rect(0, w, pt)) |
38eeddea | 68 | return t; |
e3374498 JH |
69 | } |
70 | ||
38eeddea | 71 | return shared_ptr<Trace>(); |
e3374498 JH |
72 | } |
73 | ||
a2ae0205 JH |
74 | void Header::clear_selection() |
75 | { | |
38eeddea JH |
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); | |
a2ae0205 JH |
80 | } |
81 | ||
82 | update(); | |
83 | } | |
84 | ||
e314eca4 | 85 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 JH |
86 | { |
87 | const int w = width(); | |
38eeddea | 88 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
1d8dca91 JH |
89 | |
90 | QPainter painter(this); | |
91 | painter.setRenderHint(QPainter::Antialiasing); | |
92 | ||
38eeddea JH |
93 | const bool dragging = !_drag_traces.empty(); |
94 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
1d8dca91 | 95 | { |
38eeddea | 96 | assert(t); |
1d8dca91 | 97 | |
38eeddea | 98 | const bool highlight = !dragging && t->pt_in_label_rect( |
01fd3263 JH |
99 | 0, w, _mouse_point); |
100 | t->paint_label(painter, w, highlight); | |
1d8dca91 JH |
101 | } |
102 | ||
103 | painter.end(); | |
104 | } | |
105 | ||
e3374498 JH |
106 | void Header::mousePressEvent(QMouseEvent *event) |
107 | { | |
108 | assert(event); | |
109 | ||
38eeddea | 110 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 111 | |
333d5bbc | 112 | if (event->button() & Qt::LeftButton) { |
54401bbb JH |
113 | _mouse_down_point = event->pos(); |
114 | ||
da2bebfb | 115 | // Save the offsets of any signals which will be dragged |
38eeddea JH |
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())); | |
da2bebfb JH |
120 | } |
121 | ||
122 | // Select the signal if it has been clicked | |
38eeddea JH |
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); | |
da2bebfb | 128 | else { |
38eeddea | 129 | mouse_over_trace->select(true); |
da2bebfb | 130 | |
333d5bbc | 131 | if (~QApplication::keyboardModifiers() & |
07204819 | 132 | Qt::ControlModifier) |
38eeddea | 133 | _drag_traces.clear(); |
07204819 | 134 | |
da2bebfb | 135 | // Add the signal to the drag list |
333d5bbc | 136 | if (event->button() & Qt::LeftButton) |
38eeddea JH |
137 | _drag_traces.push_back( |
138 | make_pair(mouse_over_trace, | |
139 | mouse_over_trace->get_v_offset())); | |
da2bebfb | 140 | } |
54401bbb JH |
141 | } |
142 | ||
333d5bbc | 143 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { |
07204819 JH |
144 | // Unselect all other signals because the Ctrl is not |
145 | // pressed | |
38eeddea JH |
146 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
147 | if (t != mouse_over_trace) | |
148 | t->select(false); | |
07204819 JH |
149 | } |
150 | ||
b2a53645 | 151 | selection_changed(); |
e3374498 JH |
152 | update(); |
153 | } | |
154 | ||
54401bbb JH |
155 | void Header::mouseReleaseEvent(QMouseEvent *event) |
156 | { | |
569d1e41 JH |
157 | using pv::widgets::Popup; |
158 | ||
54401bbb | 159 | assert(event); |
333d5bbc | 160 | if (event->button() == Qt::LeftButton) { |
569d1e41 JH |
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 | ||
728fcafc | 177 | _dragging = false; |
38eeddea | 178 | _drag_traces.clear(); |
4b192962 | 179 | } |
54401bbb JH |
180 | } |
181 | ||
a29bb7fb JH |
182 | void Header::mouseMoveEvent(QMouseEvent *event) |
183 | { | |
184 | assert(event); | |
185 | _mouse_point = event->pos(); | |
54401bbb | 186 | |
728fcafc JH |
187 | if (!(event->buttons() & Qt::LeftButton)) |
188 | return; | |
189 | ||
190 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
191 | QApplication::startDragDistance()) | |
192 | return; | |
193 | ||
54401bbb | 194 | // Move the signals if we are dragging |
728fcafc JH |
195 | if (!_drag_traces.empty()) |
196 | { | |
197 | _dragging = true; | |
198 | ||
54401bbb JH |
199 | const int delta = event->pos().y() - _mouse_down_point.y(); |
200 | ||
38eeddea JH |
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) { | |
da2bebfb | 206 | const int y = (*i).second + delta; |
49153683 JH |
207 | const int y_snap = |
208 | ((y + View::SignalSnapGridSize / 2) / | |
209 | View::SignalSnapGridSize) * | |
210 | View::SignalSnapGridSize; | |
38eeddea | 211 | trace->set_v_offset(y_snap); |
da2bebfb | 212 | |
38eeddea JH |
213 | // Ensure the trace is selected |
214 | trace->select(); | |
49153683 | 215 | } |
da2bebfb JH |
216 | |
217 | } | |
54401bbb JH |
218 | |
219 | signals_moved(); | |
220 | } | |
221 | ||
a29bb7fb JH |
222 | update(); |
223 | } | |
224 | ||
e314eca4 | 225 | void Header::leaveEvent(QEvent*) |
a29bb7fb JH |
226 | { |
227 | _mouse_point = QPoint(-1, -1); | |
228 | update(); | |
229 | } | |
230 | ||
49f8ff3f JH |
231 | void Header::contextMenuEvent(QContextMenuEvent *event) |
232 | { | |
38eeddea | 233 | const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point); |
49f8ff3f | 234 | |
9b6378f1 JH |
235 | if (t) |
236 | t->create_context_menu(this)->exec(event->globalPos()); | |
b3b57abc JH |
237 | } |
238 | ||
5ed1adf5 JH |
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 | ||
9e40e83d JH |
256 | void Header::on_signals_changed() |
257 | { | |
38eeddea JH |
258 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
259 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
260 | assert(t); | |
aca00b1e JH |
261 | connect(t.get(), SIGNAL(visibility_changed()), |
262 | this, SLOT(update())); | |
91e8bf08 JH |
263 | connect(t.get(), SIGNAL(text_changed()), |
264 | this, SLOT(update())); | |
265 | connect(t.get(), SIGNAL(colour_changed()), | |
266 | this, SLOT(update())); | |
9e40e83d JH |
267 | } |
268 | } | |
269 | ||
07204819 JH |
270 | void Header::on_signals_moved() |
271 | { | |
272 | update(); | |
273 | } | |
274 | ||
275 | ||
1d8dca91 JH |
276 | } // namespace view |
277 | } // namespace pv |