]>
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> |
b3b57abc | 32 | #include <QColorDialog> |
49f8ff3f JH |
33 | #include <QInputDialog> |
34 | #include <QMenu> | |
a29bb7fb | 35 | #include <QMouseEvent> |
1d8dca91 JH |
36 | #include <QPainter> |
37 | #include <QRect> | |
38 | ||
39 | using namespace boost; | |
40 | using namespace std; | |
41 | ||
42 | namespace pv { | |
43 | namespace view { | |
44 | ||
45 | Header::Header(View &parent) : | |
c23b29d6 | 46 | MarginWidget(parent), |
b3b57abc JH |
47 | _action_set_name(new QAction(tr("Set &Name..."), this)), |
48 | _action_set_colour(new QAction(tr("Set &Colour..."), this)) | |
1d8dca91 | 49 | { |
a29bb7fb | 50 | setMouseTracking(true); |
49f8ff3f JH |
51 | |
52 | connect(_action_set_name, SIGNAL(triggered()), | |
53 | this, SLOT(on_action_set_name_triggered())); | |
b3b57abc JH |
54 | connect(_action_set_colour, SIGNAL(triggered()), |
55 | this, SLOT(on_action_set_colour_triggered())); | |
07204819 | 56 | |
9e40e83d JH |
57 | connect(&_view.session(), SIGNAL(signals_changed()), |
58 | this, SLOT(on_signals_changed())); | |
59 | ||
07204819 JH |
60 | connect(&_view, SIGNAL(signals_moved()), |
61 | this, SLOT(on_signals_moved())); | |
1d8dca91 JH |
62 | } |
63 | ||
38eeddea | 64 | shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt) |
e3374498 JH |
65 | { |
66 | const int w = width(); | |
38eeddea | 67 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 68 | |
38eeddea | 69 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
e3374498 | 70 | { |
38eeddea | 71 | assert(t); |
01fd3263 | 72 | if (t->pt_in_label_rect(0, w, pt)) |
38eeddea | 73 | return t; |
e3374498 JH |
74 | } |
75 | ||
38eeddea | 76 | return shared_ptr<Trace>(); |
e3374498 JH |
77 | } |
78 | ||
a2ae0205 JH |
79 | void Header::clear_selection() |
80 | { | |
38eeddea JH |
81 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
82 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) { | |
83 | assert(t); | |
84 | t->select(false); | |
a2ae0205 JH |
85 | } |
86 | ||
87 | update(); | |
88 | } | |
89 | ||
e314eca4 | 90 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 JH |
91 | { |
92 | const int w = width(); | |
38eeddea | 93 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
1d8dca91 JH |
94 | |
95 | QPainter painter(this); | |
96 | painter.setRenderHint(QPainter::Antialiasing); | |
97 | ||
38eeddea JH |
98 | const bool dragging = !_drag_traces.empty(); |
99 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
1d8dca91 | 100 | { |
38eeddea | 101 | assert(t); |
1d8dca91 | 102 | |
38eeddea | 103 | const bool highlight = !dragging && t->pt_in_label_rect( |
01fd3263 JH |
104 | 0, w, _mouse_point); |
105 | t->paint_label(painter, w, highlight); | |
1d8dca91 JH |
106 | } |
107 | ||
108 | painter.end(); | |
109 | } | |
110 | ||
e3374498 JH |
111 | void Header::mousePressEvent(QMouseEvent *event) |
112 | { | |
113 | assert(event); | |
114 | ||
38eeddea | 115 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 116 | |
333d5bbc | 117 | if (event->button() & Qt::LeftButton) { |
54401bbb JH |
118 | _mouse_down_point = event->pos(); |
119 | ||
da2bebfb | 120 | // Save the offsets of any signals which will be dragged |
38eeddea JH |
121 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
122 | if (t->selected()) | |
123 | _drag_traces.push_back( | |
124 | make_pair(t, t->get_v_offset())); | |
da2bebfb JH |
125 | } |
126 | ||
127 | // Select the signal if it has been clicked | |
38eeddea JH |
128 | const shared_ptr<Trace> mouse_over_trace = |
129 | get_mouse_over_trace(event->pos()); | |
130 | if (mouse_over_trace) { | |
131 | if (mouse_over_trace->selected()) | |
132 | mouse_over_trace->select(false); | |
da2bebfb | 133 | else { |
38eeddea | 134 | mouse_over_trace->select(true); |
da2bebfb | 135 | |
333d5bbc | 136 | if (~QApplication::keyboardModifiers() & |
07204819 | 137 | Qt::ControlModifier) |
38eeddea | 138 | _drag_traces.clear(); |
07204819 | 139 | |
da2bebfb | 140 | // Add the signal to the drag list |
333d5bbc | 141 | if (event->button() & Qt::LeftButton) |
38eeddea JH |
142 | _drag_traces.push_back( |
143 | make_pair(mouse_over_trace, | |
144 | mouse_over_trace->get_v_offset())); | |
da2bebfb | 145 | } |
54401bbb JH |
146 | } |
147 | ||
333d5bbc | 148 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { |
07204819 JH |
149 | // Unselect all other signals because the Ctrl is not |
150 | // pressed | |
38eeddea JH |
151 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
152 | if (t != mouse_over_trace) | |
153 | t->select(false); | |
07204819 JH |
154 | } |
155 | ||
b2a53645 | 156 | selection_changed(); |
e3374498 JH |
157 | update(); |
158 | } | |
159 | ||
54401bbb JH |
160 | void Header::mouseReleaseEvent(QMouseEvent *event) |
161 | { | |
162 | assert(event); | |
333d5bbc | 163 | if (event->button() == Qt::LeftButton) { |
38eeddea | 164 | _drag_traces.clear(); |
4b192962 JH |
165 | _view.normalize_layout(); |
166 | } | |
54401bbb JH |
167 | } |
168 | ||
a29bb7fb JH |
169 | void Header::mouseMoveEvent(QMouseEvent *event) |
170 | { | |
171 | assert(event); | |
172 | _mouse_point = event->pos(); | |
54401bbb JH |
173 | |
174 | // Move the signals if we are dragging | |
38eeddea | 175 | if (!_drag_traces.empty()) { |
54401bbb JH |
176 | const int delta = event->pos().y() - _mouse_down_point.y(); |
177 | ||
38eeddea JH |
178 | for (std::list<std::pair<boost::weak_ptr<Trace>, |
179 | int> >::iterator i = _drag_traces.begin(); | |
180 | i != _drag_traces.end(); i++) { | |
181 | const boost::shared_ptr<Trace> trace((*i).first); | |
182 | if (trace) { | |
da2bebfb | 183 | const int y = (*i).second + delta; |
49153683 JH |
184 | const int y_snap = |
185 | ((y + View::SignalSnapGridSize / 2) / | |
186 | View::SignalSnapGridSize) * | |
187 | View::SignalSnapGridSize; | |
38eeddea | 188 | trace->set_v_offset(y_snap); |
da2bebfb | 189 | |
38eeddea JH |
190 | // Ensure the trace is selected |
191 | trace->select(); | |
49153683 | 192 | } |
da2bebfb JH |
193 | |
194 | } | |
54401bbb JH |
195 | |
196 | signals_moved(); | |
197 | } | |
198 | ||
a29bb7fb JH |
199 | update(); |
200 | } | |
201 | ||
e314eca4 | 202 | void Header::leaveEvent(QEvent*) |
a29bb7fb JH |
203 | { |
204 | _mouse_point = QPoint(-1, -1); | |
205 | update(); | |
206 | } | |
207 | ||
49f8ff3f JH |
208 | void Header::contextMenuEvent(QContextMenuEvent *event) |
209 | { | |
38eeddea | 210 | const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point); |
49f8ff3f | 211 | |
38eeddea | 212 | if (!t) |
e3374498 | 213 | return; |
49f8ff3f | 214 | |
e3374498 JH |
215 | QMenu menu(this); |
216 | menu.addAction(_action_set_name); | |
217 | menu.addAction(_action_set_colour); | |
49f8ff3f | 218 | |
38eeddea | 219 | _context_trace = t; |
e3374498 | 220 | menu.exec(event->globalPos()); |
38eeddea | 221 | _context_trace.reset(); |
49f8ff3f JH |
222 | } |
223 | ||
224 | void Header::on_action_set_name_triggered() | |
225 | { | |
2c982fc3 JH |
226 | bool ok = false; |
227 | ||
38eeddea JH |
228 | shared_ptr<view::Trace> context_trace = _context_trace; |
229 | if (!context_trace) | |
49f8ff3f JH |
230 | return; |
231 | ||
232 | const QString new_label = QInputDialog::getText(this, tr("Set Name"), | |
38eeddea | 233 | tr("Name"), QLineEdit::Normal, context_trace->get_name(), &ok); |
49f8ff3f | 234 | |
2c982fc3 | 235 | if (ok) |
38eeddea | 236 | context_trace->set_name(new_label); |
49f8ff3f JH |
237 | } |
238 | ||
b3b57abc JH |
239 | void Header::on_action_set_colour_triggered() |
240 | { | |
38eeddea JH |
241 | shared_ptr<view::Trace> context_trace = _context_trace; |
242 | if (!context_trace) | |
b3b57abc JH |
243 | return; |
244 | ||
245 | const QColor new_colour = QColorDialog::getColor( | |
38eeddea | 246 | context_trace->get_colour(), this, tr("Set Colour")); |
b3b57abc | 247 | |
333d5bbc | 248 | if (new_colour.isValid()) |
38eeddea | 249 | context_trace->set_colour(new_colour); |
b3b57abc JH |
250 | } |
251 | ||
9e40e83d JH |
252 | void Header::on_signals_changed() |
253 | { | |
38eeddea JH |
254 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
255 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
256 | assert(t); | |
257 | connect(t.get(), SIGNAL(text_changed()), this, SLOT(update())); | |
9e40e83d JH |
258 | } |
259 | } | |
260 | ||
07204819 JH |
261 | void Header::on_signals_moved() |
262 | { | |
263 | update(); | |
264 | } | |
265 | ||
266 | ||
1d8dca91 JH |
267 | } // namespace view |
268 | } // namespace pv |