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