Simplify Qt5 setup, require cmake >= 2.8.12.
[pulseview.git] / pv / popups / channels.cpp
blob49784bd7ae40630f3f5b3ccc4e125a9f228053e4
1 /*
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, see <http://www.gnu.org/licenses/>.
20 #include <map>
22 #include <QCheckBox>
23 #include <QFormLayout>
24 #include <QGridLayout>
25 #include <QLabel>
27 #include "channels.hpp"
29 #include <pv/binding/device.hpp>
30 #include <pv/data/signalbase.hpp>
31 #include <pv/devices/device.hpp>
32 #include <pv/session.hpp>
33 #include <pv/view/signal.hpp>
35 #include <libsigrokcxx/libsigrokcxx.hpp>
37 using namespace Qt;
39 using std::map;
40 using std::shared_ptr;
41 using std::make_shared;
42 using std::unordered_set;
43 using std::vector;
45 using pv::data::SignalBase;
47 using sigrok::Channel;
48 using sigrok::ChannelGroup;
49 using sigrok::Device;
51 namespace pv {
52 namespace popups {
54 Channels::Channels(Session &session, QWidget *parent) :
55 Popup(parent),
56 session_(session),
57 updating_channels_(false),
58 enable_all_channels_(tr("Enable All"), this),
59 disable_all_channels_(tr("Disable All"), this),
60 check_box_mapper_(this)
62 // Create the layout
63 setLayout(&layout_);
65 const shared_ptr<sigrok::Device> device = session_.device()->device();
66 assert(device);
68 // Collect a set of signals
69 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
71 unordered_set< shared_ptr<SignalBase> > sigs;
72 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
73 sigs.insert(b);
75 for (const shared_ptr<SignalBase> &sig : sigs)
76 signal_map[sig->channel()] = sig;
78 // Populate channel groups
79 for (auto entry : device->channel_groups()) {
80 shared_ptr<ChannelGroup> group = entry.second;
81 // Make a set of signals, and removed this signals from the
82 // signal map.
83 vector< shared_ptr<SignalBase> > group_sigs;
84 for (auto channel : group->channels()) {
85 const auto iter = signal_map.find(channel);
87 if (iter == signal_map.end())
88 break;
90 group_sigs.push_back((*iter).second);
91 signal_map.erase(iter);
94 populate_group(group, group_sigs);
97 // Make a vector of the remaining channels
98 vector< shared_ptr<SignalBase> > global_sigs;
99 for (auto channel : device->channels()) {
100 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
101 const_iterator iter = signal_map.find(channel);
102 if (iter != signal_map.end())
103 global_sigs.push_back((*iter).second);
106 // Create a group
107 populate_group(nullptr, global_sigs);
109 // Create the enable/disable all buttons
110 connect(&enable_all_channels_, SIGNAL(clicked()),
111 this, SLOT(enable_all_channels()));
112 connect(&disable_all_channels_, SIGNAL(clicked()),
113 this, SLOT(disable_all_channels()));
115 enable_all_channels_.setFlat(true);
116 disable_all_channels_.setFlat(true);
118 buttons_bar_.addWidget(&enable_all_channels_);
119 buttons_bar_.addWidget(&disable_all_channels_);
120 buttons_bar_.addStretch(1);
122 layout_.addRow(&buttons_bar_);
124 // Connect the check-box signal mapper
125 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
126 this, SLOT(on_channel_checked(QWidget*)));
129 void Channels::set_all_channels(bool set)
131 updating_channels_ = true;
133 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
134 check_box_signal_map_.begin();
135 i != check_box_signal_map_.end(); i++) {
136 const shared_ptr<SignalBase> sig = (*i).second;
137 assert(sig);
139 sig->set_enabled(set);
140 (*i).first->setChecked(set);
143 updating_channels_ = false;
146 void Channels::populate_group(shared_ptr<ChannelGroup> group,
147 const vector< shared_ptr<SignalBase> > sigs)
149 using pv::binding::Device;
151 // Only bind options if this is a group. We don't do it for general
152 // options, because these properties are shown in the device config
153 // popup.
154 shared_ptr<Device> binding;
155 if (group)
156 binding = make_shared<Device>(group);
158 // Create a title if the group is going to have any content
159 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
160 group)
161 layout_.addRow(new QLabel(
162 QString("<h3>%1</h3>").arg(group->name().c_str())));
164 // Create the channel group grid
165 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
166 layout_.addRow(channel_grid);
168 // Create the channel group options
169 if (binding) {
170 binding->add_properties_to_form(&layout_, true);
171 group_bindings_.push_back(binding);
175 QGridLayout* Channels::create_channel_group_grid(
176 const vector< shared_ptr<SignalBase> > sigs)
178 int row = 0, col = 0;
179 QGridLayout *const grid = new QGridLayout();
181 for (const shared_ptr<SignalBase>& sig : sigs) {
182 assert(sig);
184 QCheckBox *const checkbox = new QCheckBox(sig->name());
185 check_box_mapper_.setMapping(checkbox, checkbox);
186 connect(checkbox, SIGNAL(toggled(bool)),
187 &check_box_mapper_, SLOT(map()));
189 grid->addWidget(checkbox, row, col);
191 check_box_signal_map_[checkbox] = sig;
193 if (++col >= 8)
194 col = 0, row++;
197 return grid;
200 void Channels::showEvent(QShowEvent *event)
202 pv::widgets::Popup::showEvent(event);
204 updating_channels_ = true;
206 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
207 check_box_signal_map_.begin();
208 i != check_box_signal_map_.end(); i++) {
209 const shared_ptr<SignalBase> sig = (*i).second;
210 assert(sig);
212 (*i).first->setChecked(sig->enabled());
215 updating_channels_ = false;
218 void Channels::on_channel_checked(QWidget *widget)
220 if (updating_channels_)
221 return;
223 QCheckBox *const check_box = (QCheckBox*)widget;
224 assert(check_box);
226 // Look up the signal of this check-box
227 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
228 check_box_signal_map_.find((QCheckBox*)check_box);
229 assert(iter != check_box_signal_map_.end());
231 const shared_ptr<SignalBase> s = (*iter).second;
232 assert(s);
234 s->set_enabled(check_box->isChecked());
237 void Channels::enable_all_channels()
239 set_all_channels(true);
242 void Channels::disable_all_channels()
244 set_all_channels(false);
247 } // namespace popups
248 } // namespace pv