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