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