]> sigrok.org Git - pulseview.git/blame_incremental - pv/prop/enum.cpp
Move file loading from MainBar to Session
[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, 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 <QComboBox>
24
25#include "enum.hpp"
26
27using std::pair;
28using std::vector;
29
30namespace pv {
31namespace prop {
32
33Enum::Enum(QString name,
34 vector<pair<Glib::VariantBase, QString> > values,
35 Getter getter, Setter setter) :
36 Property(name, getter, setter),
37 values_(values),
38 selector_(nullptr)
39{
40}
41
42QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
43{
44 if (selector_)
45 return selector_;
46
47 if (!getter_)
48 return nullptr;
49
50 Glib::VariantBase variant = getter_();
51 if (!variant.gobj())
52 return nullptr;
53
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));
58 if (v.first.equal(variant))
59 selector_->setCurrentIndex(i);
60 }
61
62 if (auto_commit)
63 connect(selector_, SIGNAL(currentIndexChanged(int)),
64 this, SLOT(on_current_item_changed(int)));
65
66 return selector_;
67}
68
69void Enum::commit()
70{
71 assert(setter_);
72
73 if (!selector_)
74 return;
75
76 const int index = selector_->currentIndex();
77 if (index < 0)
78 return;
79
80 setter_(selector_->itemData(index).value<Glib::VariantBase>());
81}
82
83void Enum::on_current_item_changed(int)
84{
85 commit();
86}
87
88} // prop
89} // pv