]>
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 |
bf0edd2b | 103 | vector< shared_ptr<SignalBase> > global_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); |
488f068c JH |
107 | if (iter != signal_map.end()) |
108 | global_sigs.push_back((*iter).second); | |
109 | } | |
110 | ||
111 | // Create a group | |
4c60462b | 112 | populate_group(nullptr, global_sigs); |
488f068c JH |
113 | |
114 | // Create the enable/disable all buttons | |
5b35d18c SA |
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())); | |
ed773982 | 122 | |
b887c4d8 SA |
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); | |
ed773982 | 130 | |
8dbbc7f0 | 131 | layout_.addRow(&buttons_bar_); |
0740907f | 132 | |
488f068c | 133 | // Connect the check-box signal mapper |
8dbbc7f0 | 134 | connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)), |
6ac6242b | 135 | this, SLOT(on_channel_checked(QWidget*))); |
b7b659aa JH |
136 | } |
137 | ||
6ac6242b | 138 | void Channels::set_all_channels(bool set) |
b7b659aa | 139 | { |
8dbbc7f0 | 140 | updating_channels_ = true; |
b7b659aa | 141 | |
b5d20c6d SA |
142 | for (auto entry : check_box_signal_map_) { |
143 | QCheckBox *cb = entry.first; | |
144 | const shared_ptr<SignalBase> sig = entry.second; | |
488f068c | 145 | assert(sig); |
b7b659aa | 146 | |
bf0edd2b | 147 | sig->set_enabled(set); |
b5d20c6d | 148 | cb->setChecked(set); |
0740907f | 149 | } |
51d4a9ab | 150 | |
8dbbc7f0 | 151 | updating_channels_ = false; |
0740907f JH |
152 | } |
153 | ||
5b35d18c SA |
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 | ||
e8d00928 | 172 | void Channels::populate_group(shared_ptr<ChannelGroup> group, |
bf0edd2b | 173 | const vector< shared_ptr<SignalBase> > sigs) |
488f068c | 174 | { |
3cc9ad7b | 175 | using pv::binding::Device; |
488f068c JH |
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. | |
3cc9ad7b | 180 | shared_ptr<Device> binding; |
488f068c | 181 | if (group) |
067bb624 | 182 | binding = make_shared<Device>(group); |
488f068c JH |
183 | |
184 | // Create a title if the group is going to have any content | |
b5d20c6d SA |
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 | } | |
488f068c | 192 | |
2445160a | 193 | // Create the channel group grid |
c063290a | 194 | QGridLayout *const channel_grid = create_channel_group_grid(sigs); |
8dbbc7f0 | 195 | layout_.addRow(channel_grid); |
488f068c | 196 | |
2445160a | 197 | // Create the channel group options |
c063290a | 198 | if (binding) { |
8dbbc7f0 JH |
199 | binding->add_properties_to_form(&layout_, true); |
200 | group_bindings_.push_back(binding); | |
488f068c JH |
201 | } |
202 | } | |
203 | ||
6ac6242b | 204 | QGridLayout* Channels::create_channel_group_grid( |
bf0edd2b | 205 | const vector< shared_ptr<SignalBase> > sigs) |
488f068c JH |
206 | { |
207 | int row = 0, col = 0; | |
208 | QGridLayout *const grid = new QGridLayout(); | |
209 | ||
bf0edd2b | 210 | for (const shared_ptr<SignalBase>& sig : sigs) { |
488f068c JH |
211 | assert(sig); |
212 | ||
b5d20c6d | 213 | QCheckBox *const checkbox = new QCheckBox(sig->display_name()); |
8dbbc7f0 | 214 | check_box_mapper_.setMapping(checkbox, checkbox); |
488f068c | 215 | connect(checkbox, SIGNAL(toggled(bool)), |
8dbbc7f0 | 216 | &check_box_mapper_, SLOT(map())); |
488f068c JH |
217 | |
218 | grid->addWidget(checkbox, row, col); | |
219 | ||
8dbbc7f0 | 220 | check_box_signal_map_[checkbox] = sig; |
488f068c | 221 | |
f3290553 | 222 | if (++col >= 8) |
488f068c JH |
223 | col = 0, row++; |
224 | } | |
225 | ||
226 | return grid; | |
227 | } | |
228 | ||
d9ea9628 | 229 | void Channels::showEvent(QShowEvent *event) |
ed773982 | 230 | { |
d9ea9628 | 231 | pv::widgets::Popup::showEvent(event); |
b7b659aa | 232 | |
b5d20c6d SA |
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())); | |
30677c13 | 243 | } catch (out_of_range&) { |
b5d20c6d SA |
244 | // Do nothing |
245 | } | |
246 | } | |
247 | ||
8dbbc7f0 | 248 | updating_channels_ = true; |
b7b659aa | 249 | |
b5d20c6d SA |
250 | for (auto entry : check_box_signal_map_) { |
251 | QCheckBox *cb = entry.first; | |
252 | const shared_ptr<SignalBase> sig = entry.second; | |
488f068c JH |
253 | assert(sig); |
254 | ||
b5d20c6d SA |
255 | // Update the check box |
256 | cb->setChecked(sig->enabled()); | |
257 | cb->setText(sig->display_name()); | |
ed773982 | 258 | } |
b7b659aa | 259 | |
8dbbc7f0 | 260 | updating_channels_ = false; |
ed773982 JH |
261 | } |
262 | ||
6ac6242b | 263 | void Channels::on_channel_checked(QWidget *widget) |
51d4a9ab | 264 | { |
8dbbc7f0 | 265 | if (updating_channels_) |
b7b659aa JH |
266 | return; |
267 | ||
488f068c JH |
268 | QCheckBox *const check_box = (QCheckBox*)widget; |
269 | assert(check_box); | |
270 | ||
271 | // Look up the signal of this check-box | |
bf0edd2b | 272 | map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter = |
8dbbc7f0 JH |
273 | check_box_signal_map_.find((QCheckBox*)check_box); |
274 | assert(iter != check_box_signal_map_.end()); | |
488f068c | 275 | |
bf0edd2b | 276 | const shared_ptr<SignalBase> s = (*iter).second; |
aca00b1e | 277 | assert(s); |
488f068c | 278 | |
bf0edd2b | 279 | s->set_enabled(check_box->isChecked()); |
51d4a9ab JH |
280 | } |
281 | ||
6ac6242b | 282 | void Channels::enable_all_channels() |
ed773982 | 283 | { |
6ac6242b | 284 | set_all_channels(true); |
ed773982 JH |
285 | } |
286 | ||
6ac6242b | 287 | void Channels::disable_all_channels() |
ed773982 | 288 | { |
6ac6242b | 289 | set_all_channels(false); |
ed773982 JH |
290 | } |
291 | ||
5b35d18c SA |
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 | { | |
d13d95b3 SA |
314 | // Never enable channels without sample data |
315 | if (!signal->has_samples()) | |
316 | return false; | |
317 | ||
5b35d18c SA |
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 | ||
870ea3db UH |
343 | } // namespace popups |
344 | } // namespace pv |