]> sigrok.org Git - pulseview.git/blob - pv/prop/enum.cpp
Update property widgets before showing device config popup
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21
22 #include <QComboBox>
23
24 #include "enum.hpp"
25
26 using std::pair;
27 using std::vector;
28
29 namespace pv {
30 namespace prop {
31
32 Enum::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
41 QWidget* 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         }
58
59         update_widget();
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
68 void Enum::update_widget()
69 {
70         if (!selector_)
71                 return;
72
73         Glib::VariantBase variant = getter_();
74         assert(variant.gobj());
75
76         for (unsigned int i = 0; i < values_.size(); i++) {
77                 const pair<Glib::VariantBase, QString> &v = values_[i];
78                 if (v.first.equal(variant))
79                         selector_->setCurrentIndex(i);
80         }
81 }
82
83 void Enum::commit()
84 {
85         assert(setter_);
86
87         if (!selector_)
88                 return;
89
90         const int index = selector_->currentIndex();
91         if (index < 0)
92                 return;
93
94         setter_(selector_->itemData(index).value<Glib::VariantBase>());
95 }
96
97 void Enum::on_current_item_changed(int)
98 {
99         commit();
100 }
101
102 }  // namespace prop
103 }  // namespace pv