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