]> sigrok.org Git - pulseview.git/blob - pv/dialogs/decoder.cpp
Implemented decoder options binding
[pulseview.git] / pv / dialogs / decoder.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 extern "C" {
22 #include <libsigrokdecode/libsigrokdecode.h>
23 }
24
25 #include <utility>
26
27 #include <boost/foreach.hpp>
28
29 #include <QDebug>
30
31 #include "decoder.h"
32
33 #include <pv/view/logicsignal.h>
34 #include <pv/view/signal.h>
35
36 using namespace boost;
37 using namespace std;
38
39 namespace pv {
40 namespace dialogs {
41
42 Decoder::Decoder(QWidget *parent, const srd_decoder *decoder,
43         const vector< shared_ptr<view::Signal> > &sigs, GHashTable *options) :
44         QDialog(parent),
45         _decoder(decoder),
46         _sigs(sigs),
47         _options(options),
48         _binding(decoder, options),
49         _layout(this),
50         _form(this),
51         _form_layout(&_form),
52         _heading(this),
53         _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
54                 Qt::Horizontal, this)
55 {
56         const GSList *probe;
57
58         setWindowTitle(tr("Configure %1").arg(decoder->name));
59
60         _heading.setText(tr("<h2>%1</h2>%2")
61                 .arg(decoder->longname)
62                 .arg(decoder->desc));
63
64         connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
65         connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
66
67         _form.setLayout(&_form_layout);
68
69         setLayout(&_layout);
70         _layout.addWidget(&_heading);
71         _layout.addWidget(&_form);
72         _layout.addWidget(&_button_box);
73
74         _form_layout.addRow(new QLabel(tr("<h3>Probes</h3>"), &_form));
75
76         // Add the mandatory probes
77         for(probe = decoder->probes; probe; probe = probe->next) {
78                 const struct srd_probe *const p =
79                         (struct srd_probe *)probe->data;
80                 QComboBox *const combo = create_probe_selector(
81                         &_form, p->name);
82                 _form_layout.addRow(tr("<b>%1</b> (%2) *")
83                         .arg(p->name).arg(p->desc), combo);
84
85                 _probe_selector_map[p] = combo;
86         }
87
88         // Add the optional probes
89         for(probe = decoder->opt_probes; probe; probe = probe->next) {
90                 const struct srd_probe *const p =
91                         (struct srd_probe *)probe->data;
92                 QComboBox *const combo = create_probe_selector(
93                         &_form, p->name);
94                 _form_layout.addRow(tr("<b>%1</b> (%2)")
95                         .arg(p->name).arg(p->desc), combo);
96
97                 _probe_selector_map[p] = combo;
98         }
99
100         _form_layout.addRow(new QLabel(
101                 tr("<i>* Required Probes</i>"), &_form));
102
103         // Add the options
104         if (!_binding.properties().empty()) {
105                 _form_layout.addRow(new QLabel(tr("<h3>Options</h3>"),
106                         &_form));
107                 _binding.add_properties_to_form(&_form_layout);
108         }
109 }
110
111 void Decoder::accept()
112 {
113         QDialog::accept();
114         _binding.commit();
115 }
116
117 QComboBox* Decoder::create_probe_selector(
118         QWidget *parent, const char *name)
119 {
120         QComboBox *selector = new QComboBox(parent);
121
122         selector->addItem("-", qVariantFromValue(-1));
123         selector->setCurrentIndex(0);
124
125         for(size_t i = 0; i < _sigs.size(); i++) {
126                 const shared_ptr<view::Signal> s(_sigs[i]);
127                 assert(s);
128
129                 if (s->enabled()) {
130                         selector->addItem(s->get_name(), qVariantFromValue(i));
131                         if(s->get_name().toLower().contains(
132                                 QString(name).toLower()))
133                                 selector->setCurrentIndex(i + 1);
134                 }
135         }
136
137         return selector;
138 }
139
140 map<const srd_probe*, shared_ptr<view::Signal> > Decoder::get_probes()
141 {
142         map<const srd_probe*, shared_ptr<view::Signal> > probe_map;
143         for(map<const srd_probe*, QComboBox*>::const_iterator i =
144                 _probe_selector_map.begin();
145                 i != _probe_selector_map.end(); i++)
146         {
147                 const QComboBox *const combo = (*i).second;
148                 const int probe_index =
149                         combo->itemData(combo->currentIndex()).value<int>();
150                 if(probe_index >= 0) {
151                         shared_ptr<view::Signal> sig = _sigs[probe_index];
152                         if(dynamic_cast<pv::view::LogicSignal*>(sig.get()))
153                                 probe_map[(*i).first] = sig;
154                         else
155                                 qDebug() << "Currently only logic signals "
156                                         "are supported for decoding";
157                 }
158         }
159
160         return probe_map;
161 }
162
163 } // namespace dialogs
164 } // namespace pv