DecoderSelector: Use proxy model to permit sorting
[pulseview.git] / pv / binding / binding.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 <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_labels_[help_btn] = help_lbl;
86                 }
87
88                 if (p->labeled_widget()) {
89                         layout->addRow(widget);
90                 } else {
91                         auto *lbl = new QLabel(p->name());
92                         lbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
93                         layout->addRow(lbl, widget);
94                 }
95
96                 if (help_lbl)
97                         layout->addRow(help_lbl);
98         }
99 }
100
101 QWidget* Binding::get_property_form(QWidget *parent, bool auto_commit)
102 {
103         QWidget *const form = new QWidget(parent);
104         QFormLayout *const layout = new QFormLayout(form);
105         form->setLayout(layout);
106         add_properties_to_form(layout, auto_commit);
107         return form;
108 }
109
110 void Binding::update_property_widgets()
111 {
112         for (shared_ptr<pv::prop::Property> p : properties_) {
113                 assert(p);
114                 p->update_widget();
115         }
116 }
117
118 QString Binding::print_gvariant(Glib::VariantBase gvar)
119 {
120         QString s;
121
122         if (!gvar.gobj())
123                 s = QString::fromStdString("(null)");
124         else if (gvar.is_of_type(Glib::VariantType("s")))
125                 s = QString::fromStdString(
126                         Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(gvar).get());
127         else
128                 s = QString::fromStdString(gvar.print());
129
130         return s;
131 }
132
133 void Binding::on_help_clicked()
134 {
135         QPushButton *btn = qobject_cast<QPushButton*>(QObject::sender());
136         assert(btn);
137
138         QLabel *lbl = help_labels_.at(btn);
139         lbl->setVisible(!lbl->isVisible());
140 }
141
142 }  // namespace binding
143 }  // namespace pv