]> sigrok.org Git - pulseview.git/blame - pv/prop/enum.cpp
Use libsigrok C++ bindings (patch version 7).
[pulseview.git] / pv / prop / enum.cpp
CommitLineData
820c3dea
JH
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
24bb0bd1
JH
21#include <assert.h>
22
820c3dea
JH
23#include <QComboBox>
24
25#include "enum.h"
26
819f4c25
JH
27using std::pair;
28using std::vector;
820c3dea
JH
29
30namespace pv {
31namespace prop {
32
24bb0bd1 33Enum::Enum(QString name,
e8d00928 34 vector<pair<Glib::VariantBase, QString> > values,
9b258927
JH
35 Getter getter, Setter setter) :
36 Property(name, getter, setter),
820c3dea
JH
37 _values(values),
38 _selector(NULL)
39{
40}
41
9b258927
JH
42Enum::~Enum()
43{
9b258927
JH
44}
45
b1fe148e 46QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
820c3dea 47{
9ba4ca35 48 if (_selector)
820c3dea
JH
49 return _selector;
50
e8d00928
ML
51 if (!_getter)
52 return NULL;
53
54 Glib::VariantBase variant = _getter();
55 if (!variant.gobj())
f352b9bb 56 return NULL;
d191508b 57
820c3dea 58 _selector = new QComboBox(parent);
793f8096 59 for (unsigned int i = 0; i < _values.size(); i++) {
e8d00928
ML
60 const pair<Glib::VariantBase, QString> &v = _values[i];
61 _selector->addItem(v.second, qVariantFromValue(v.first));
62 if (v.first.equal(variant))
d191508b
JH
63 _selector->setCurrentIndex(i);
64 }
820c3dea 65
b1fe148e
JH
66 if (auto_commit)
67 connect(_selector, SIGNAL(currentIndexChanged(int)),
68 this, SLOT(on_current_item_changed(int)));
69
820c3dea
JH
70 return _selector;
71}
72
24bb0bd1
JH
73void Enum::commit()
74{
75 assert(_setter);
76
9ba4ca35 77 if (!_selector)
24bb0bd1
JH
78 return;
79
80 const int index = _selector->currentIndex();
81 if (index < 0)
82 return;
83
e8d00928 84 _setter(_selector->itemData(index).value<Glib::VariantBase>());
24bb0bd1
JH
85}
86
b1fe148e
JH
87void Enum::on_current_item_changed(int)
88{
89 commit();
90}
91
820c3dea
JH
92} // prop
93} // pv