]> sigrok.org Git - pulseview.git/blob - pv/popups/channels.cpp
Consistenty use auto-generated namespace comments.
[pulseview.git] / pv / popups / channels.cpp
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
43 using namespace Qt;
44
45 using std::map;
46 using std::shared_ptr;
47 using std::make_shared;
48 using std::unordered_set;
49 using std::vector;
50
51 using pv::data::SignalBase;
52
53 using sigrok::Channel;
54 using sigrok::ChannelGroup;
55 using sigrok::Device;
56
57 namespace pv {
58 namespace popups {
59
60 Channels::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
135 void 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
152 void 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 = create_channel_group_grid(sigs);
172         layout_.addRow(channel_grid);
173
174         // Create the channel group options
175         if (binding) {
176                 binding->add_properties_to_form(&layout_, true);
177                 group_bindings_.push_back(binding);
178         }
179 }
180
181 QGridLayout* Channels::create_channel_group_grid(
182         const vector< shared_ptr<SignalBase> > sigs)
183 {
184         int row = 0, col = 0;
185         QGridLayout *const grid = new QGridLayout();
186
187         for (const shared_ptr<SignalBase>& sig : sigs) {
188                 assert(sig);
189
190                 QCheckBox *const checkbox = new QCheckBox(sig->name());
191                 check_box_mapper_.setMapping(checkbox, checkbox);
192                 connect(checkbox, SIGNAL(toggled(bool)),
193                         &check_box_mapper_, SLOT(map()));
194
195                 grid->addWidget(checkbox, row, col);
196
197                 check_box_signal_map_[checkbox] = sig;
198
199                 if (++col >= 8)
200                         col = 0, row++;
201         }
202
203         return grid;
204 }
205
206 void Channels::showEvent(QShowEvent *event)
207 {
208         pv::widgets::Popup::showEvent(event);
209
210         updating_channels_ = true;
211
212         for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
213                         check_box_signal_map_.begin();
214                         i != check_box_signal_map_.end(); i++) {
215                 const shared_ptr<SignalBase> sig = (*i).second;
216                 assert(sig);
217
218                 (*i).first->setChecked(sig->enabled());
219         }
220
221         updating_channels_ = false;
222 }
223
224 void Channels::on_channel_checked(QWidget *widget)
225 {
226         if (updating_channels_)
227                 return;
228
229         QCheckBox *const check_box = (QCheckBox*)widget;
230         assert(check_box);
231
232         // Look up the signal of this check-box
233         map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
234                 check_box_signal_map_.find((QCheckBox*)check_box);
235         assert(iter != check_box_signal_map_.end());
236
237         const shared_ptr<SignalBase> s = (*iter).second;
238         assert(s);
239
240         s->set_enabled(check_box->isChecked());
241 }
242
243 void Channels::enable_all_channels()
244 {
245         set_all_channels(true);
246 }
247
248 void Channels::disable_all_channels()
249 {
250         set_all_channels(false);
251 }
252
253 }  // namespace popups
254 }  // namespace pv