]> sigrok.org Git - pulseview.git/blob - pv/popups/channels.cpp
Session: Store signals_ in an unordered_set
[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.hpp"
29
30 #include <pv/binding/device.hpp>
31 #include <pv/session.hpp>
32 #include <pv/view/signal.hpp>
33
34 #include <libsigrokcxx/libsigrokcxx.hpp>
35
36 using namespace Qt;
37
38 using boost::shared_lock;
39 using boost::shared_mutex;
40 using std::lock_guard;
41 using std::map;
42 using std::mutex;
43 using std::set;
44 using std::shared_ptr;
45 using std::unordered_set;
46 using std::vector;
47
48 using sigrok::Channel;
49 using sigrok::ChannelGroup;
50 using sigrok::Device;
51
52 using pv::view::Signal;
53
54 namespace pv {
55 namespace popups {
56
57 Channels::Channels(Session &session, QWidget *parent) :
58         Popup(parent),
59         session_(session),
60         updating_channels_(false),
61         enable_all_channels_(tr("Enable All"), this),
62         disable_all_channels_(tr("Disable All"), this),
63         check_box_mapper_(this)
64 {
65         // Create the layout
66         setLayout(&layout_);
67
68         shared_ptr<sigrok::Device> device = session_.device();
69         assert(device);
70
71         // Collect a set of signals
72         map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map;
73
74         shared_lock<shared_mutex> lock(session_.signals_mutex());
75         const unordered_set< shared_ptr<Signal> > &sigs(session_.signals());
76
77         for (const shared_ptr<Signal> &sig : sigs)
78                 signal_map[sig->channel()] = sig;
79
80         // Populate channel groups
81         for (auto entry : device->channel_groups())
82         {
83                 shared_ptr<ChannelGroup> group = entry.second;
84                 // Make a set of signals, and removed this signals from the
85                 // signal map.
86                 vector< shared_ptr<Signal> > group_sigs;
87                 for (auto channel : group->channels())
88                 {
89                         const auto iter = signal_map.find(channel);
90
91                         if (iter == signal_map.end())
92                                 break;
93
94                         group_sigs.push_back((*iter).second);
95                         signal_map.erase(iter);
96                 }
97
98                 populate_group(group, group_sigs);
99         }
100
101         // Make a vector of the remaining channels
102         vector< shared_ptr<Signal> > global_sigs;
103         for (auto channel : device->channels())
104         {
105                 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
106                         const_iterator iter = signal_map.find(channel);
107                 if (iter != signal_map.end())
108                         global_sigs.push_back((*iter).second);
109         }
110
111         // Create a group
112         populate_group(NULL, global_sigs);
113
114         // Create the enable/disable all buttons
115         connect(&enable_all_channels_, SIGNAL(clicked()),
116                 this, SLOT(enable_all_channels()));
117         connect(&disable_all_channels_, SIGNAL(clicked()),
118                 this, SLOT(disable_all_channels()));
119
120         enable_all_channels_.setFlat(true);
121         disable_all_channels_.setFlat(true);
122
123         buttons_bar_.addWidget(&enable_all_channels_);
124         buttons_bar_.addWidget(&disable_all_channels_);
125         buttons_bar_.addStretch(1);
126
127         layout_.addRow(&buttons_bar_);
128
129         // Connect the check-box signal mapper
130         connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
131                 this, SLOT(on_channel_checked(QWidget*)));
132 }
133
134 void Channels::set_all_channels(bool set)
135 {
136         updating_channels_ = true;
137
138         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
139                 check_box_signal_map_.begin();
140                 i != check_box_signal_map_.end(); i++)
141         {
142                 const shared_ptr<Signal> sig = (*i).second;
143                 assert(sig);
144
145                 sig->enable(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<pv::view::Signal> > 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 = shared_ptr<Device>(new 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
183 QGridLayout* Channels::create_channel_group_grid(
184         const vector< shared_ptr<pv::view::Signal> > sigs)
185 {
186         int row = 0, col = 0;
187         QGridLayout *const grid = new QGridLayout();
188
189         for (const shared_ptr<pv::view::Signal>& sig : sigs)
190         {
191                 assert(sig);
192
193                 QCheckBox *const checkbox = new QCheckBox(sig->name());
194                 check_box_mapper_.setMapping(checkbox, checkbox);
195                 connect(checkbox, SIGNAL(toggled(bool)),
196                         &check_box_mapper_, SLOT(map()));
197
198                 grid->addWidget(checkbox, row, col);
199
200                 check_box_signal_map_[checkbox] = sig;
201
202                 if(++col >= 8)
203                         col = 0, row++;
204         }
205
206         return grid;
207 }
208
209 void Channels::showEvent(QShowEvent *e)
210 {
211         pv::widgets::Popup::showEvent(e);
212
213         updating_channels_ = true;
214
215         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
216                 check_box_signal_map_.begin();
217                 i != check_box_signal_map_.end(); i++)
218         {
219                 const shared_ptr<Signal> sig = (*i).second;
220                 assert(sig);
221
222                 (*i).first->setChecked(sig->enabled());
223         }
224
225         updating_channels_ = false;
226 }
227
228 void Channels::on_channel_checked(QWidget *widget)
229 {
230         if (updating_channels_)
231                 return;
232
233         QCheckBox *const check_box = (QCheckBox*)widget;
234         assert(check_box);
235
236         // Look up the signal of this check-box
237         map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
238                 check_box_signal_map_.find((QCheckBox*)check_box);
239         assert(iter != check_box_signal_map_.end());
240
241         const shared_ptr<pv::view::Signal> s = (*iter).second;
242         assert(s);
243
244         s->enable(check_box->isChecked());
245 }
246
247 void Channels::enable_all_channels()
248 {
249         set_all_channels(true);
250 }
251
252 void Channels::disable_all_channels()
253 {
254         set_all_channels(false);
255 }
256
257 } // popups
258 } // pv