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