]>
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 | |
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 |
27 | using std::pair; |
28 | using std::vector; | |
820c3dea JH |
29 | |
30 | namespace pv { | |
31 | namespace prop { | |
32 | ||
24bb0bd1 | 33 | Enum::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 | ||
9b258927 JH |
42 | Enum::~Enum() |
43 | { | |
9b258927 JH |
44 | } |
45 | ||
b1fe148e | 46 | QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) |
820c3dea | 47 | { |
8dbbc7f0 JH |
48 | if (selector_) |
49 | return selector_; | |
820c3dea | 50 | |
8dbbc7f0 | 51 | if (!getter_) |
4c60462b | 52 | return nullptr; |
e8d00928 | 53 | |
8dbbc7f0 | 54 | Glib::VariantBase variant = getter_(); |
e8d00928 | 55 | if (!variant.gobj()) |
4c60462b | 56 | return nullptr; |
d191508b | 57 | |
8dbbc7f0 JH |
58 | selector_ = new QComboBox(parent); |
59 | for (unsigned int i = 0; i < values_.size(); i++) { | |
60 | const pair<Glib::VariantBase, QString> &v = values_[i]; | |
61 | selector_->addItem(v.second, qVariantFromValue(v.first)); | |
e8d00928 | 62 | if (v.first.equal(variant)) |
8dbbc7f0 | 63 | selector_->setCurrentIndex(i); |
d191508b | 64 | } |
820c3dea | 65 | |
b1fe148e | 66 | if (auto_commit) |
8dbbc7f0 | 67 | connect(selector_, SIGNAL(currentIndexChanged(int)), |
b1fe148e JH |
68 | this, SLOT(on_current_item_changed(int))); |
69 | ||
8dbbc7f0 | 70 | return selector_; |
820c3dea JH |
71 | } |
72 | ||
24bb0bd1 JH |
73 | void Enum::commit() |
74 | { | |
8dbbc7f0 | 75 | assert(setter_); |
24bb0bd1 | 76 | |
8dbbc7f0 | 77 | if (!selector_) |
24bb0bd1 JH |
78 | return; |
79 | ||
8dbbc7f0 | 80 | const int index = selector_->currentIndex(); |
24bb0bd1 JH |
81 | if (index < 0) |
82 | return; | |
83 | ||
8dbbc7f0 | 84 | setter_(selector_->itemData(index).value<Glib::VariantBase>()); |
24bb0bd1 JH |
85 | } |
86 | ||
b1fe148e JH |
87 | void Enum::on_current_item_changed(int) |
88 | { | |
89 | commit(); | |
90 | } | |
91 | ||
820c3dea JH |
92 | } // prop |
93 | } // pv |