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