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