]> sigrok.org Git - pulseview.git/blame - pv/prop/enum.cpp
Channels: Don't use flat buttons and add vertical spacer
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
820c3dea
JH
18 */
19
eb8269e3 20#include <cassert>
24bb0bd1 21
820c3dea
JH
22#include <QComboBox>
23
2acdb232 24#include "enum.hpp"
820c3dea 25
819f4c25
JH
26using std::pair;
27using std::vector;
820c3dea
JH
28
29namespace pv {
30namespace prop {
31
9a267f8d 32Enum::Enum(QString name, QString desc,
e8d00928 33 vector<pair<Glib::VariantBase, QString> > values,
9b258927 34 Getter getter, Setter setter) :
9a267f8d 35 Property(name, desc, getter, setter),
8dbbc7f0 36 values_(values),
4c60462b 37 selector_(nullptr)
820c3dea
JH
38{
39}
40
b1fe148e 41QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
820c3dea 42{
8dbbc7f0
JH
43 if (selector_)
44 return selector_;
820c3dea 45
8dbbc7f0 46 if (!getter_)
4c60462b 47 return nullptr;
e8d00928 48
8dbbc7f0 49 Glib::VariantBase variant = getter_();
e8d00928 50 if (!variant.gobj())
4c60462b 51 return nullptr;
d191508b 52
8dbbc7f0
JH
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));
e8d00928 57 if (v.first.equal(variant))
8dbbc7f0 58 selector_->setCurrentIndex(i);
d191508b 59 }
820c3dea 60
b1fe148e 61 if (auto_commit)
8dbbc7f0 62 connect(selector_, SIGNAL(currentIndexChanged(int)),
b1fe148e
JH
63 this, SLOT(on_current_item_changed(int)));
64
8dbbc7f0 65 return selector_;
820c3dea
JH
66}
67
24bb0bd1
JH
68void Enum::commit()
69{
8dbbc7f0 70 assert(setter_);
24bb0bd1 71
8dbbc7f0 72 if (!selector_)
24bb0bd1
JH
73 return;
74
8dbbc7f0 75 const int index = selector_->currentIndex();
24bb0bd1
JH
76 if (index < 0)
77 return;
78
8dbbc7f0 79 setter_(selector_->itemData(index).value<Glib::VariantBase>());
24bb0bd1
JH
80}
81
b1fe148e
JH
82void Enum::on_current_item_changed(int)
83{
84 commit();
85}
86
870ea3db
UH
87} // namespace prop
88} // namespace pv