]> sigrok.org Git - pulseview.git/blame_incremental - pv/prop/enum.cpp
Introduce DecodeSignal class
[pulseview.git] / pv / prop / enum.cpp
... / ...
CommitLineData
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <cassert>
21
22#include <QComboBox>
23
24#include "enum.hpp"
25
26using std::pair;
27using std::vector;
28
29namespace pv {
30namespace prop {
31
32Enum::Enum(QString name, QString desc,
33 vector<pair<Glib::VariantBase, QString> > values,
34 Getter getter, Setter setter) :
35 Property(name, desc, getter, setter),
36 values_(values),
37 selector_(nullptr)
38{
39}
40
41QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
42{
43 if (selector_)
44 return selector_;
45
46 if (!getter_)
47 return nullptr;
48
49 Glib::VariantBase variant = getter_();
50 if (!variant.gobj())
51 return nullptr;
52
53 selector_ = new QComboBox(parent);
54 for (unsigned int i = 0; i < values_.size(); i++) {
55 const pair<Glib::VariantBase, QString> &v = values_[i];
56 selector_->addItem(v.second, qVariantFromValue(v.first));
57 if (v.first.equal(variant))
58 selector_->setCurrentIndex(i);
59 }
60
61 if (auto_commit)
62 connect(selector_, SIGNAL(currentIndexChanged(int)),
63 this, SLOT(on_current_item_changed(int)));
64
65 return selector_;
66}
67
68void Enum::commit()
69{
70 assert(setter_);
71
72 if (!selector_)
73 return;
74
75 const int index = selector_->currentIndex();
76 if (index < 0)
77 return;
78
79 setter_(selector_->itemData(index).value<Glib::VariantBase>());
80}
81
82void Enum::on_current_item_changed(int)
83{
84 commit();
85}
86
87} // namespace prop
88} // namespace pv