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