]> sigrok.org Git - pulseview.git/blame_incremental - pv/popups/channels.cpp
Session: Fix trigger handling
[pulseview.git] / pv / popups / channels.cpp
... / ...
CommitLineData
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 <QCheckBox>
23#include <QFormLayout>
24#include <QGridLayout>
25#include <QLabel>
26
27#include "channels.hpp"
28
29#include <pv/session.hpp>
30#include <pv/binding/device.hpp>
31#include <pv/data/signalbase.hpp>
32#include <pv/devices/device.hpp>
33
34using std::make_shared;
35using std::map;
36using std::out_of_range;
37using std::shared_ptr;
38using std::unordered_set;
39using std::vector;
40
41using pv::data::SignalBase;
42
43using sigrok::Channel;
44using sigrok::ChannelGroup;
45using sigrok::Device;
46
47namespace pv {
48namespace popups {
49
50Channels::Channels(Session &session, QWidget *parent) :
51 Popup(parent),
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)
57{
58 // Create the layout
59 setLayout(&layout_);
60
61 const shared_ptr<sigrok::Device> device = session_.device()->device();
62 assert(device);
63
64 // Collect a set of signals
65 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
66
67 unordered_set< shared_ptr<SignalBase> > sigs;
68 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
69 sigs.insert(b);
70
71 for (const shared_ptr<SignalBase> &sig : sigs)
72 signal_map[sig->channel()] = sig;
73
74 // Populate channel groups
75 for (auto entry : device->channel_groups()) {
76 const shared_ptr<ChannelGroup> group = entry.second;
77 // Make a set of signals and remove these signals from the signal map
78 vector< shared_ptr<SignalBase> > group_sigs;
79 for (auto channel : group->channels()) {
80 const auto iter = signal_map.find(channel);
81
82 if (iter == signal_map.end())
83 break;
84
85 group_sigs.push_back((*iter).second);
86 signal_map.erase(iter);
87 }
88
89 populate_group(group, group_sigs);
90 }
91
92 // Make a vector of the remaining channels
93 vector< shared_ptr<SignalBase> > global_sigs;
94 for (auto channel : device->channels()) {
95 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
96 const_iterator iter = signal_map.find(channel);
97 if (iter != signal_map.end())
98 global_sigs.push_back((*iter).second);
99 }
100
101 // Create a group
102 populate_group(nullptr, global_sigs);
103
104 // Create the enable/disable all buttons
105 connect(&enable_all_channels_, SIGNAL(clicked()),
106 this, SLOT(enable_all_channels()));
107 connect(&disable_all_channels_, SIGNAL(clicked()),
108 this, SLOT(disable_all_channels()));
109
110 enable_all_channels_.setFlat(true);
111 disable_all_channels_.setFlat(true);
112
113 buttons_bar_.addWidget(&enable_all_channels_);
114 buttons_bar_.addWidget(&disable_all_channels_);
115 buttons_bar_.addStretch(1);
116
117 layout_.addRow(&buttons_bar_);
118
119 // Connect the check-box signal mapper
120 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
121 this, SLOT(on_channel_checked(QWidget*)));
122}
123
124void Channels::set_all_channels(bool set)
125{
126 updating_channels_ = true;
127
128 for (auto entry : check_box_signal_map_) {
129 QCheckBox *cb = entry.first;
130 const shared_ptr<SignalBase> sig = entry.second;
131 assert(sig);
132
133 sig->set_enabled(set);
134 cb->setChecked(set);
135 }
136
137 updating_channels_ = false;
138}
139
140void Channels::populate_group(shared_ptr<ChannelGroup> group,
141 const vector< shared_ptr<SignalBase> > sigs)
142{
143 using pv::binding::Device;
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.
148 shared_ptr<Device> binding;
149 if (group)
150 binding = make_shared<Device>(group);
151
152 // Create a title if the group is going to have any content
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 }
160
161 // Create the channel group grid
162 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
163 layout_.addRow(channel_grid);
164
165 // Create the channel group options
166 if (binding) {
167 binding->add_properties_to_form(&layout_, true);
168 group_bindings_.push_back(binding);
169 }
170}
171
172QGridLayout* Channels::create_channel_group_grid(
173 const vector< shared_ptr<SignalBase> > sigs)
174{
175 int row = 0, col = 0;
176 QGridLayout *const grid = new QGridLayout();
177
178 for (const shared_ptr<SignalBase>& sig : sigs) {
179 assert(sig);
180
181 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
182 check_box_mapper_.setMapping(checkbox, checkbox);
183 connect(checkbox, SIGNAL(toggled(bool)),
184 &check_box_mapper_, SLOT(map()));
185
186 grid->addWidget(checkbox, row, col);
187
188 check_box_signal_map_[checkbox] = sig;
189
190 if (++col >= 8)
191 col = 0, row++;
192 }
193
194 return grid;
195}
196
197void Channels::showEvent(QShowEvent *event)
198{
199 pv::widgets::Popup::showEvent(event);
200
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
216 updating_channels_ = true;
217
218 for (auto entry : check_box_signal_map_) {
219 QCheckBox *cb = entry.first;
220 const shared_ptr<SignalBase> sig = entry.second;
221 assert(sig);
222
223 // Update the check box
224 cb->setChecked(sig->enabled());
225 cb->setText(sig->display_name());
226 }
227
228 updating_channels_ = false;
229}
230
231void Channels::on_channel_checked(QWidget *widget)
232{
233 if (updating_channels_)
234 return;
235
236 QCheckBox *const check_box = (QCheckBox*)widget;
237 assert(check_box);
238
239 // Look up the signal of this check-box
240 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
241 check_box_signal_map_.find((QCheckBox*)check_box);
242 assert(iter != check_box_signal_map_.end());
243
244 const shared_ptr<SignalBase> s = (*iter).second;
245 assert(s);
246
247 s->set_enabled(check_box->isChecked());
248}
249
250void Channels::enable_all_channels()
251{
252 set_all_channels(true);
253}
254
255void Channels::disable_all_channels()
256{
257 set_all_channels(false);
258}
259
260} // namespace popups
261} // namespace pv