2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 #include <QFormLayout>
25 #include <QGridLayout>
28 #include "channels.hpp"
30 #include <pv/binding/device.hpp>
31 #include <pv/devices/device.hpp>
32 #include <pv/session.hpp>
33 #include <pv/view/signal.hpp>
35 #include <libsigrokcxx/libsigrokcxx.hpp>
39 using boost::shared_lock;
40 using boost::shared_mutex;
41 using std::lock_guard;
45 using std::shared_ptr;
46 using std::unordered_set;
49 using sigrok::Channel;
50 using sigrok::ChannelGroup;
53 using pv::view::Signal;
58 Channels::Channels(Session &session, QWidget *parent) :
61 updating_channels_(false),
62 enable_all_channels_(tr("Enable All"), this),
63 disable_all_channels_(tr("Disable All"), this),
64 check_box_mapper_(this)
69 const shared_ptr<sigrok::Device> device = session_.device()->device();
72 // Collect a set of signals
73 map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map;
75 shared_lock<shared_mutex> lock(session_.signals_mutex());
76 const unordered_set< shared_ptr<Signal> > &sigs(session_.signals());
78 for (const shared_ptr<Signal> &sig : sigs)
79 signal_map[sig->channel()] = sig;
81 // Populate channel groups
82 for (auto entry : device->channel_groups())
84 shared_ptr<ChannelGroup> group = entry.second;
85 // Make a set of signals, and removed this signals from the
87 vector< shared_ptr<Signal> > group_sigs;
88 for (auto channel : group->channels())
90 const auto iter = signal_map.find(channel);
92 if (iter == signal_map.end())
95 group_sigs.push_back((*iter).second);
96 signal_map.erase(iter);
99 populate_group(group, group_sigs);
102 // Make a vector of the remaining channels
103 vector< shared_ptr<Signal> > global_sigs;
104 for (auto channel : device->channels())
106 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
107 const_iterator iter = signal_map.find(channel);
108 if (iter != signal_map.end())
109 global_sigs.push_back((*iter).second);
113 populate_group(NULL, global_sigs);
115 // Create the enable/disable all buttons
116 connect(&enable_all_channels_, SIGNAL(clicked()),
117 this, SLOT(enable_all_channels()));
118 connect(&disable_all_channels_, SIGNAL(clicked()),
119 this, SLOT(disable_all_channels()));
121 enable_all_channels_.setFlat(true);
122 disable_all_channels_.setFlat(true);
124 buttons_bar_.addWidget(&enable_all_channels_);
125 buttons_bar_.addWidget(&disable_all_channels_);
126 buttons_bar_.addStretch(1);
128 layout_.addRow(&buttons_bar_);
130 // Connect the check-box signal mapper
131 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
132 this, SLOT(on_channel_checked(QWidget*)));
135 void Channels::set_all_channels(bool set)
137 updating_channels_ = true;
139 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
140 check_box_signal_map_.begin();
141 i != check_box_signal_map_.end(); i++)
143 const shared_ptr<Signal> sig = (*i).second;
147 (*i).first->setChecked(set);
150 updating_channels_ = false;
153 void Channels::populate_group(shared_ptr<ChannelGroup> group,
154 const vector< shared_ptr<pv::view::Signal> > sigs)
156 using pv::binding::Device;
158 // Only bind options if this is a group. We don't do it for general
159 // options, because these properties are shown in the device config
161 shared_ptr<Device> binding;
163 binding = shared_ptr<Device>(new Device(group));
165 // Create a title if the group is going to have any content
166 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
168 layout_.addRow(new QLabel(
169 QString("<h3>%1</h3>").arg(group->name().c_str())));
171 // Create the channel group grid
172 QGridLayout *const channel_grid =
173 create_channel_group_grid(sigs);
174 layout_.addRow(channel_grid);
176 // Create the channel group options
179 binding->add_properties_to_form(&layout_, true);
180 group_bindings_.push_back(binding);
184 QGridLayout* Channels::create_channel_group_grid(
185 const vector< shared_ptr<pv::view::Signal> > sigs)
187 int row = 0, col = 0;
188 QGridLayout *const grid = new QGridLayout();
190 for (const shared_ptr<pv::view::Signal>& sig : sigs)
194 QCheckBox *const checkbox = new QCheckBox(sig->name());
195 check_box_mapper_.setMapping(checkbox, checkbox);
196 connect(checkbox, SIGNAL(toggled(bool)),
197 &check_box_mapper_, SLOT(map()));
199 grid->addWidget(checkbox, row, col);
201 check_box_signal_map_[checkbox] = sig;
210 void Channels::showEvent(QShowEvent *e)
212 pv::widgets::Popup::showEvent(e);
214 updating_channels_ = true;
216 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
217 check_box_signal_map_.begin();
218 i != check_box_signal_map_.end(); i++)
220 const shared_ptr<Signal> sig = (*i).second;
223 (*i).first->setChecked(sig->enabled());
226 updating_channels_ = false;
229 void Channels::on_channel_checked(QWidget *widget)
231 if (updating_channels_)
234 QCheckBox *const check_box = (QCheckBox*)widget;
237 // Look up the signal of this check-box
238 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
239 check_box_signal_map_.find((QCheckBox*)check_box);
240 assert(iter != check_box_signal_map_.end());
242 const shared_ptr<pv::view::Signal> s = (*iter).second;
245 s->enable(check_box->isChecked());
248 void Channels::enable_all_channels()
250 set_all_channels(true);
253 void Channels::disable_all_channels()
255 set_all_channels(false);