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