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