]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <map> | |
21 | ||
22 | #include <QApplication> | |
23 | #include <QCheckBox> | |
24 | #include <QFontMetrics> | |
25 | #include <QFormLayout> | |
26 | #include <QGridLayout> | |
27 | #include <QLabel> | |
28 | ||
29 | #include "channels.hpp" | |
30 | ||
31 | #include <pv/session.hpp> | |
32 | #include <pv/binding/device.hpp> | |
33 | #include <pv/data/logic.hpp> | |
34 | #include <pv/data/logicsegment.hpp> | |
35 | #include <pv/data/signalbase.hpp> | |
36 | #include <pv/devices/device.hpp> | |
37 | ||
38 | using std::make_shared; | |
39 | using std::map; | |
40 | using std::out_of_range; | |
41 | using std::shared_ptr; | |
42 | using std::unordered_set; | |
43 | using std::vector; | |
44 | ||
45 | using pv::data::SignalBase; | |
46 | using pv::data::Logic; | |
47 | using pv::data::LogicSegment; | |
48 | ||
49 | using sigrok::Channel; | |
50 | using sigrok::ChannelGroup; | |
51 | using sigrok::Device; | |
52 | ||
53 | namespace pv { | |
54 | namespace popups { | |
55 | ||
56 | Channels::Channels(Session &session, QWidget *parent) : | |
57 | Popup(parent), | |
58 | session_(session), | |
59 | updating_channels_(false), | |
60 | enable_all_channels_(tr("Enable All"), this), | |
61 | disable_all_channels_(tr("Disable All"), this), | |
62 | enable_all_logic_channels_(tr("Enable only logic"), this), | |
63 | enable_all_analog_channels_(tr("Enable only analog"), this), | |
64 | enable_all_named_channels_(tr("Enable only named"), this), | |
65 | enable_all_changing_channels_(tr("Enable only changing"), this), | |
66 | check_box_mapper_(this) | |
67 | { | |
68 | // Create the layout | |
69 | setLayout(&layout_); | |
70 | ||
71 | const shared_ptr<sigrok::Device> device = session_.device()->device(); | |
72 | assert(device); | |
73 | ||
74 | // Collect a set of signals | |
75 | map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map; | |
76 | ||
77 | unordered_set< shared_ptr<SignalBase> > sigs; | |
78 | for (const shared_ptr<data::SignalBase> b : session_.signalbases()) | |
79 | sigs.insert(b); | |
80 | ||
81 | for (const shared_ptr<SignalBase> &sig : sigs) | |
82 | signal_map[sig->channel()] = sig; | |
83 | ||
84 | // Populate channel groups | |
85 | for (auto entry : device->channel_groups()) { | |
86 | const shared_ptr<ChannelGroup> group = entry.second; | |
87 | // Make a set of signals and remove these signals from the signal map | |
88 | vector< shared_ptr<SignalBase> > group_sigs; | |
89 | for (auto channel : group->channels()) { | |
90 | const auto iter = signal_map.find(channel); | |
91 | ||
92 | if (iter == signal_map.end()) | |
93 | break; | |
94 | ||
95 | group_sigs.push_back((*iter).second); | |
96 | signal_map.erase(iter); | |
97 | } | |
98 | ||
99 | populate_group(group, group_sigs); | |
100 | } | |
101 | ||
102 | // Make a vector of the remaining channels | |
103 | vector< shared_ptr<SignalBase> > global_sigs; | |
104 | for (auto channel : device->channels()) { | |
105 | const map<shared_ptr<Channel>, shared_ptr<SignalBase> >:: | |
106 | const_iterator iter = signal_map.find(channel); | |
107 | if (iter != signal_map.end()) | |
108 | global_sigs.push_back((*iter).second); | |
109 | } | |
110 | ||
111 | // Create a group | |
112 | populate_group(nullptr, global_sigs); | |
113 | ||
114 | // Create the enable/disable all buttons | |
115 | connect(&enable_all_channels_, SIGNAL(clicked()), this, SLOT(enable_all_channels())); | |
116 | connect(&disable_all_channels_, SIGNAL(clicked()), this, SLOT(disable_all_channels())); | |
117 | connect(&enable_all_logic_channels_, SIGNAL(clicked()), this, SLOT(enable_all_logic_channels())); | |
118 | connect(&enable_all_analog_channels_, SIGNAL(clicked()), this, SLOT(enable_all_analog_channels())); | |
119 | connect(&enable_all_named_channels_, SIGNAL(clicked()), this, SLOT(enable_all_named_channels())); | |
120 | connect(&enable_all_changing_channels_, SIGNAL(clicked()), | |
121 | this, SLOT(enable_all_changing_channels())); | |
122 | ||
123 | buttons_bar_.setRowMinimumHeight(0, 2 * QFontMetrics(QApplication::font()).height()); | |
124 | buttons_bar_.addWidget(&enable_all_channels_, 1, 0); | |
125 | buttons_bar_.addWidget(&disable_all_channels_, 1, 1); | |
126 | buttons_bar_.addWidget(&enable_all_logic_channels_, 2, 0); | |
127 | buttons_bar_.addWidget(&enable_all_analog_channels_, 2, 1); | |
128 | buttons_bar_.addWidget(&enable_all_named_channels_, 2, 2); | |
129 | buttons_bar_.addWidget(&enable_all_changing_channels_, 2, 3); | |
130 | ||
131 | layout_.addRow(&buttons_bar_); | |
132 | ||
133 | // Connect the check-box signal mapper | |
134 | connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)), | |
135 | this, SLOT(on_channel_checked(QWidget*))); | |
136 | } | |
137 | ||
138 | void Channels::set_all_channels(bool set) | |
139 | { | |
140 | updating_channels_ = true; | |
141 | ||
142 | for (auto entry : check_box_signal_map_) { | |
143 | QCheckBox *cb = entry.first; | |
144 | const shared_ptr<SignalBase> sig = entry.second; | |
145 | assert(sig); | |
146 | ||
147 | sig->set_enabled(set); | |
148 | cb->setChecked(set); | |
149 | } | |
150 | ||
151 | updating_channels_ = false; | |
152 | } | |
153 | ||
154 | void Channels::set_all_channels_conditionally( | |
155 | function<bool (const shared_ptr<data::SignalBase>)> cond_func) | |
156 | { | |
157 | updating_channels_ = true; | |
158 | ||
159 | for (auto entry : check_box_signal_map_) { | |
160 | QCheckBox *cb = entry.first; | |
161 | const shared_ptr<SignalBase> sig = entry.second; | |
162 | assert(sig); | |
163 | ||
164 | const bool state = cond_func(sig); | |
165 | sig->set_enabled(state); | |
166 | cb->setChecked(state); | |
167 | } | |
168 | ||
169 | updating_channels_ = false; | |
170 | } | |
171 | ||
172 | void Channels::populate_group(shared_ptr<ChannelGroup> group, | |
173 | const vector< shared_ptr<SignalBase> > sigs) | |
174 | { | |
175 | using pv::binding::Device; | |
176 | ||
177 | // Only bind options if this is a group. We don't do it for general | |
178 | // options, because these properties are shown in the device config | |
179 | // popup. | |
180 | shared_ptr<Device> binding; | |
181 | if (group) | |
182 | binding = make_shared<Device>(group); | |
183 | ||
184 | // Create a title if the group is going to have any content | |
185 | if ((!sigs.empty() || (binding && !binding->properties().empty())) && group) | |
186 | { | |
187 | QLabel *label = new QLabel( | |
188 | QString("<h3>%1</h3>").arg(group->name().c_str())); | |
189 | layout_.addRow(label); | |
190 | group_label_map_[group] = label; | |
191 | } | |
192 | ||
193 | // Create the channel group grid | |
194 | QGridLayout *const channel_grid = create_channel_group_grid(sigs); | |
195 | layout_.addRow(channel_grid); | |
196 | ||
197 | // Create the channel group options | |
198 | if (binding) { | |
199 | binding->add_properties_to_form(&layout_, true); | |
200 | group_bindings_.push_back(binding); | |
201 | } | |
202 | } | |
203 | ||
204 | QGridLayout* Channels::create_channel_group_grid( | |
205 | const vector< shared_ptr<SignalBase> > sigs) | |
206 | { | |
207 | int row = 0, col = 0; | |
208 | QGridLayout *const grid = new QGridLayout(); | |
209 | ||
210 | for (const shared_ptr<SignalBase>& sig : sigs) { | |
211 | assert(sig); | |
212 | ||
213 | QCheckBox *const checkbox = new QCheckBox(sig->display_name()); | |
214 | check_box_mapper_.setMapping(checkbox, checkbox); | |
215 | connect(checkbox, SIGNAL(toggled(bool)), | |
216 | &check_box_mapper_, SLOT(map())); | |
217 | ||
218 | grid->addWidget(checkbox, row, col); | |
219 | ||
220 | check_box_signal_map_[checkbox] = sig; | |
221 | ||
222 | if (++col >= 8) | |
223 | col = 0, row++; | |
224 | } | |
225 | ||
226 | return grid; | |
227 | } | |
228 | ||
229 | void Channels::showEvent(QShowEvent *event) | |
230 | { | |
231 | pv::widgets::Popup::showEvent(event); | |
232 | ||
233 | const shared_ptr<sigrok::Device> device = session_.device()->device(); | |
234 | assert(device); | |
235 | ||
236 | // Update group labels | |
237 | for (auto entry : device->channel_groups()) { | |
238 | const shared_ptr<ChannelGroup> group = entry.second; | |
239 | ||
240 | try { | |
241 | QLabel* label = group_label_map_.at(group); | |
242 | label->setText(QString("<h3>%1</h3>").arg(group->name().c_str())); | |
243 | } catch (out_of_range&) { | |
244 | // Do nothing | |
245 | } | |
246 | } | |
247 | ||
248 | updating_channels_ = true; | |
249 | ||
250 | for (auto entry : check_box_signal_map_) { | |
251 | QCheckBox *cb = entry.first; | |
252 | const shared_ptr<SignalBase> sig = entry.second; | |
253 | assert(sig); | |
254 | ||
255 | // Update the check box | |
256 | cb->setChecked(sig->enabled()); | |
257 | cb->setText(sig->display_name()); | |
258 | } | |
259 | ||
260 | updating_channels_ = false; | |
261 | } | |
262 | ||
263 | void Channels::on_channel_checked(QWidget *widget) | |
264 | { | |
265 | if (updating_channels_) | |
266 | return; | |
267 | ||
268 | QCheckBox *const check_box = (QCheckBox*)widget; | |
269 | assert(check_box); | |
270 | ||
271 | // Look up the signal of this check-box | |
272 | map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter = | |
273 | check_box_signal_map_.find((QCheckBox*)check_box); | |
274 | assert(iter != check_box_signal_map_.end()); | |
275 | ||
276 | const shared_ptr<SignalBase> s = (*iter).second; | |
277 | assert(s); | |
278 | ||
279 | s->set_enabled(check_box->isChecked()); | |
280 | } | |
281 | ||
282 | void Channels::enable_all_channels() | |
283 | { | |
284 | set_all_channels(true); | |
285 | } | |
286 | ||
287 | void Channels::disable_all_channels() | |
288 | { | |
289 | set_all_channels(false); | |
290 | } | |
291 | ||
292 | void Channels::enable_all_logic_channels() | |
293 | { | |
294 | set_all_channels_conditionally([](const shared_ptr<SignalBase> signal) | |
295 | { return signal->type() == SignalBase::LogicChannel; }); | |
296 | } | |
297 | ||
298 | void Channels::enable_all_analog_channels() | |
299 | { | |
300 | set_all_channels_conditionally([](const shared_ptr<SignalBase> signal) | |
301 | { return signal->type() == SignalBase::AnalogChannel; }); | |
302 | } | |
303 | ||
304 | void Channels::enable_all_named_channels() | |
305 | { | |
306 | set_all_channels_conditionally([](const shared_ptr<SignalBase> signal) | |
307 | { return signal->name() != signal->internal_name(); }); | |
308 | } | |
309 | ||
310 | void Channels::enable_all_changing_channels() | |
311 | { | |
312 | set_all_channels_conditionally([](const shared_ptr<SignalBase> signal) | |
313 | { | |
314 | // Never enable channels without sample data | |
315 | if (!signal->has_samples()) | |
316 | return false; | |
317 | ||
318 | // Non-logic channels are considered to always have a signal | |
319 | if (signal->type() != SignalBase::LogicChannel) | |
320 | return true; | |
321 | ||
322 | const shared_ptr<Logic> logic = signal->logic_data(); | |
323 | assert(logic); | |
324 | ||
325 | // If any of the segments has edges, enable this channel | |
326 | for (shared_ptr<LogicSegment> segment : logic->logic_segments()) { | |
327 | vector<LogicSegment::EdgePair> edges; | |
328 | ||
329 | segment->get_subsampled_edges(edges, | |
330 | 0, segment->get_sample_count() - 1, | |
331 | LogicSegment::MipMapScaleFactor, | |
332 | signal->index()); | |
333 | ||
334 | if (edges.size() > 2) | |
335 | return true; | |
336 | } | |
337 | ||
338 | // No edges detected in any of the segments | |
339 | return false; | |
340 | }); | |
341 | } | |
342 | ||
343 | } // namespace popups | |
344 | } // namespace pv |