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