desktop file: Rename to org.sigrok.PulseView.desktop.
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <map>
21
22 #include <QCheckBox>
23 #include <QFormLayout>
24 #include <QGridLayout>
25 #include <QLabel>
26
27 #include "channels.hpp"
28
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>
34
35 #include <libsigrokcxx/libsigrokcxx.hpp>
36
37 using namespace Qt;
38
39 using std::map;
40 using std::shared_ptr;
41 using std::make_shared;
42 using std::unordered_set;
43 using std::vector;
44
45 using pv::data::SignalBase;
46
47 using sigrok::Channel;
48 using sigrok::ChannelGroup;
49 using sigrok::Device;
50
51 namespace pv {
52 namespace popups {
53
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)
61 {
62         // Create the layout
63         setLayout(&layout_);
64
65         const shared_ptr<sigrok::Device> device = session_.device()->device();
66         assert(device);
67
68         // Collect a set of signals
69         map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
70
71         unordered_set< shared_ptr<SignalBase> > sigs;
72         for (const shared_ptr<data::SignalBase> b : session_.signalbases())
73                 sigs.insert(b);
74
75         for (const shared_ptr<SignalBase> &sig : sigs)
76                 signal_map[sig->channel()] = sig;
77
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);
86
87                         if (iter == signal_map.end())
88                                 break;
89
90                         group_sigs.push_back((*iter).second);
91                         signal_map.erase(iter);
92                 }
93
94                 populate_group(group, group_sigs);
95         }
96
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);
104         }
105
106         // Create a group
107         populate_group(nullptr, global_sigs);
108
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()));
114
115         enable_all_channels_.setFlat(true);
116         disable_all_channels_.setFlat(true);
117
118         buttons_bar_.addWidget(&enable_all_channels_);
119         buttons_bar_.addWidget(&disable_all_channels_);
120         buttons_bar_.addStretch(1);
121
122         layout_.addRow(&buttons_bar_);
123
124         // Connect the check-box signal mapper
125         connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
126                 this, SLOT(on_channel_checked(QWidget*)));
127 }
128
129 void Channels::set_all_channels(bool set)
130 {
131         updating_channels_ = true;
132
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);
138
139                 sig->set_enabled(set);
140                 (*i).first->setChecked(set);
141         }
142
143         updating_channels_ = false;
144 }
145
146 void Channels::populate_group(shared_ptr<ChannelGroup> group,
147         const vector< shared_ptr<SignalBase> > sigs)
148 {
149         using pv::binding::Device;
150
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);
157
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())));
163
164         // Create the channel group grid
165         QGridLayout *const channel_grid = create_channel_group_grid(sigs);
166         layout_.addRow(channel_grid);
167
168         // Create the channel group options
169         if (binding) {
170                 binding->add_properties_to_form(&layout_, true);
171                 group_bindings_.push_back(binding);
172         }
173 }
174
175 QGridLayout* Channels::create_channel_group_grid(
176         const vector< shared_ptr<SignalBase> > sigs)
177 {
178         int row = 0, col = 0;
179         QGridLayout *const grid = new QGridLayout();
180
181         for (const shared_ptr<SignalBase>& sig : sigs) {
182                 assert(sig);
183
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()));
188
189                 grid->addWidget(checkbox, row, col);
190
191                 check_box_signal_map_[checkbox] = sig;
192
193                 if (++col >= 8)
194                         col = 0, row++;
195         }
196
197         return grid;
198 }
199
200 void Channels::showEvent(QShowEvent *event)
201 {
202         pv::widgets::Popup::showEvent(event);
203
204         updating_channels_ = true;
205
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);
211
212                 (*i).first->setChecked(sig->enabled());
213         }
214
215         updating_channels_ = false;
216 }
217
218 void Channels::on_channel_checked(QWidget *widget)
219 {
220         if (updating_channels_)
221                 return;
222
223         QCheckBox *const check_box = (QCheckBox*)widget;
224         assert(check_box);
225
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());
230
231         const shared_ptr<SignalBase> s = (*iter).second;
232         assert(s);
233
234         s->set_enabled(check_box->isChecked());
235 }
236
237 void Channels::enable_all_channels()
238 {
239         set_all_channels(true);
240 }
241
242 void Channels::disable_all_channels()
243 {
244         set_all_channels(false);
245 }
246
247 }  // namespace popups
248 }  // namespace pv