]>
Commit | Line | Data |
---|---|---|
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 |
26 | using std::pair; |
27 | using std::vector; | |
820c3dea JH |
28 | |
29 | namespace pv { | |
30 | namespace prop { | |
31 | ||
9a267f8d | 32 | Enum::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 | 41 | QWidget* 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)); | |
d191508b | 57 | } |
820c3dea | 58 | |
dbed5609 SA |
59 | update_widget(); |
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 | ||
dbed5609 SA |
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 | ||
24bb0bd1 JH |
83 | void Enum::commit() |
84 | { | |
8dbbc7f0 | 85 | assert(setter_); |
24bb0bd1 | 86 | |
8dbbc7f0 | 87 | if (!selector_) |
24bb0bd1 JH |
88 | return; |
89 | ||
8dbbc7f0 | 90 | const int index = selector_->currentIndex(); |
24bb0bd1 JH |
91 | if (index < 0) |
92 | return; | |
93 | ||
8dbbc7f0 | 94 | setter_(selector_->itemData(index).value<Glib::VariantBase>()); |
24bb0bd1 JH |
95 | } |
96 | ||
b1fe148e JH |
97 | void Enum::on_current_item_changed(int) |
98 | { | |
99 | commit(); | |
100 | } | |
101 | ||
870ea3db UH |
102 | } // namespace prop |
103 | } // namespace pv |