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