]>
Commit | Line | Data |
---|---|---|
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 <QFormLayout> | |
23 | #include <QHBoxLayout> | |
24 | #include <QIcon> | |
25 | #include <QLabel> | |
26 | #include <QPushButton> | |
27 | ||
28 | #include <pv/prop/property.hpp> | |
29 | ||
30 | #include "binding.hpp" | |
31 | ||
32 | using std::shared_ptr; | |
33 | using std::string; | |
34 | using std::vector; | |
35 | ||
36 | namespace pv { | |
37 | namespace binding { | |
38 | ||
39 | const vector< shared_ptr<prop::Property> >& Binding::properties() | |
40 | { | |
41 | return properties_; | |
42 | } | |
43 | ||
44 | void Binding::commit() | |
45 | { | |
46 | for (shared_ptr<pv::prop::Property> p : properties_) { | |
47 | assert(p); | |
48 | p->commit(); | |
49 | } | |
50 | } | |
51 | ||
52 | void Binding::add_properties_to_form(QFormLayout *layout, bool auto_commit) | |
53 | { | |
54 | assert(layout); | |
55 | ||
56 | help_labels_.clear(); | |
57 | ||
58 | for (shared_ptr<pv::prop::Property> p : properties_) { | |
59 | assert(p); | |
60 | ||
61 | QWidget *widget; | |
62 | QLabel *help_lbl = nullptr; | |
63 | ||
64 | if (p->desc().isEmpty()) { | |
65 | widget = p->get_widget(layout->parentWidget(), auto_commit); | |
66 | } else { | |
67 | QPushButton *help_btn = new QPushButton(); | |
68 | help_btn->setFlat(true); | |
69 | help_btn->setIcon(QIcon(":/icons/help-browser.png")); | |
70 | help_btn->setToolTip(p->desc()); | |
71 | connect(help_btn, SIGNAL(clicked(bool)), | |
72 | this, SLOT(on_help_clicked())); | |
73 | ||
74 | QHBoxLayout *layout = new QHBoxLayout(); | |
75 | layout->setContentsMargins(0, 0, 0, 0); | |
76 | layout->addWidget(p->get_widget(layout->parentWidget(), auto_commit)); | |
77 | layout->addWidget(help_btn, 0, Qt::AlignRight); | |
78 | ||
79 | widget = new QWidget(); | |
80 | widget->setLayout(layout); | |
81 | ||
82 | help_lbl = new QLabel(p->desc()); | |
83 | help_lbl->setVisible(false); | |
84 | help_lbl->setWordWrap(true); | |
85 | help_lbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | |
86 | help_labels_[help_btn] = help_lbl; | |
87 | } | |
88 | ||
89 | if (p->labeled_widget()) { | |
90 | layout->addRow(widget); | |
91 | } else { | |
92 | auto *lbl = new QLabel(p->name()); | |
93 | lbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
94 | layout->addRow(lbl, widget); | |
95 | } | |
96 | ||
97 | if (help_lbl) | |
98 | layout->addRow(help_lbl); | |
99 | } | |
100 | } | |
101 | ||
102 | QWidget* Binding::get_property_form(QWidget *parent, bool auto_commit) | |
103 | { | |
104 | QWidget *const form = new QWidget(parent); | |
105 | QFormLayout *const layout = new QFormLayout(form); | |
106 | form->setLayout(layout); | |
107 | add_properties_to_form(layout, auto_commit); | |
108 | return form; | |
109 | } | |
110 | ||
111 | void Binding::update_property_widgets() | |
112 | { | |
113 | for (shared_ptr<pv::prop::Property> p : properties_) { | |
114 | assert(p); | |
115 | p->update_widget(); | |
116 | } | |
117 | } | |
118 | ||
119 | QString Binding::print_gvariant(Glib::VariantBase gvar) | |
120 | { | |
121 | QString s; | |
122 | ||
123 | if (!gvar.gobj()) | |
124 | s = QString::fromStdString("(null)"); | |
125 | else if (gvar.is_of_type(Glib::VariantType("s"))) | |
126 | s = QString::fromStdString( | |
127 | Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(gvar).get()); | |
128 | else | |
129 | s = QString::fromStdString(gvar.print()); | |
130 | ||
131 | return s; | |
132 | } | |
133 | ||
134 | void Binding::on_help_clicked() | |
135 | { | |
136 | QPushButton *btn = qobject_cast<QPushButton*>(QObject::sender()); | |
137 | assert(btn); | |
138 | ||
139 | QLabel *lbl = help_labels_.at(btn); | |
140 | lbl->setVisible(!lbl->isVisible()); | |
141 | } | |
142 | ||
143 | } // namespace binding | |
144 | } // namespace pv |