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