]> sigrok.org Git - pulseview.git/blame - pv/prop/enum.cpp
Adjust pv:prop::Enum to GVariant-based sr_config_* functions
[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
2db7704a 23#include <glib-2.0/glib.h>
820c3dea
JH
24#include <QComboBox>
25
26#include "enum.h"
27
8a017a8b 28using namespace boost;
820c3dea
JH
29using namespace std;
30
31namespace pv {
32namespace prop {
33
24bb0bd1 34Enum::Enum(QString name,
8a017a8b 35 vector<pair<const void*, QString> > values,
2db7704a
BV
36 function<GVariant* ()> getter,
37 function<void (GVariant*)> setter) :
820c3dea
JH
38 Property(name),
39 _values(values),
24bb0bd1
JH
40 _getter(getter),
41 _setter(setter),
820c3dea
JH
42 _selector(NULL)
43{
44}
45
46QWidget* Enum::get_widget(QWidget *parent)
47{
9ba4ca35 48 if (_selector)
820c3dea
JH
49 return _selector;
50
d191508b 51 const void *value = NULL;
793f8096 52 if (_getter)
d191508b
JH
53 value = _getter();
54
820c3dea 55 _selector = new QComboBox(parent);
793f8096 56 for (unsigned int i = 0; i < _values.size(); i++) {
d191508b
JH
57 const pair<const void*, QString> &v = _values[i];
58 _selector->addItem(v.second,
59 qVariantFromValue((void*)v.first));
793f8096 60 if (v.first == value)
d191508b
JH
61 _selector->setCurrentIndex(i);
62 }
820c3dea
JH
63
64 return _selector;
65}
66
24bb0bd1
JH
67void Enum::commit()
68{
69 assert(_setter);
70
9ba4ca35 71 if (!_selector)
24bb0bd1
JH
72 return;
73
74 const int index = _selector->currentIndex();
75 if (index < 0)
76 return;
77
2db7704a 78 _setter(_selector->itemData(index).value<GVariant*>());
24bb0bd1
JH
79}
80
820c3dea
JH
81} // prop
82} // pv