]> sigrok.org Git - pulseview.git/blame - pv/prop/enum.cpp
Session: Keep track of signal data locally
[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
2acdb232 25#include "enum.hpp"
820c3dea 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),
8dbbc7f0 37 values_(values),
4c60462b 38 selector_(nullptr)
820c3dea
JH
39{
40}
41
b1fe148e 42QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
820c3dea 43{
8dbbc7f0
JH
44 if (selector_)
45 return selector_;
820c3dea 46
8dbbc7f0 47 if (!getter_)
4c60462b 48 return nullptr;
e8d00928 49
8dbbc7f0 50 Glib::VariantBase variant = getter_();
e8d00928 51 if (!variant.gobj())
4c60462b 52 return nullptr;
d191508b 53
8dbbc7f0
JH
54 selector_ = new QComboBox(parent);
55 for (unsigned int i = 0; i < values_.size(); i++) {
56 const pair<Glib::VariantBase, QString> &v = values_[i];
57 selector_->addItem(v.second, qVariantFromValue(v.first));
e8d00928 58 if (v.first.equal(variant))
8dbbc7f0 59 selector_->setCurrentIndex(i);
d191508b 60 }
820c3dea 61
b1fe148e 62 if (auto_commit)
8dbbc7f0 63 connect(selector_, SIGNAL(currentIndexChanged(int)),
b1fe148e
JH
64 this, SLOT(on_current_item_changed(int)));
65
8dbbc7f0 66 return selector_;
820c3dea
JH
67}
68
24bb0bd1
JH
69void Enum::commit()
70{
8dbbc7f0 71 assert(setter_);
24bb0bd1 72
8dbbc7f0 73 if (!selector_)
24bb0bd1
JH
74 return;
75
8dbbc7f0 76 const int index = selector_->currentIndex();
24bb0bd1
JH
77 if (index < 0)
78 return;
79
8dbbc7f0 80 setter_(selector_->itemData(index).value<Glib::VariantBase>());
24bb0bd1
JH
81}
82
b1fe148e
JH
83void Enum::on_current_item_changed(int)
84{
85 commit();
86}
87
820c3dea
JH
88} // prop
89} // pv