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