]> sigrok.org Git - pulseview.git/blame - pv/binding/binding.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / binding / binding.cpp
CommitLineData
c8df6005
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/>.
c8df6005
JH
18 */
19
f9abf97e
JH
20#include <cassert>
21
8481fbdf 22#include <QFormLayout>
4e2195c8
SA
23#include <QHBoxLayout>
24#include <QIcon>
894c4b52 25#include <QLabel>
4e2195c8 26#include <QPushButton>
8481fbdf 27
2acdb232 28#include <pv/prop/property.hpp>
8481fbdf 29
2acdb232 30#include "binding.hpp"
c8df6005 31
f9abf97e 32using std::shared_ptr;
6f925ba9
UH
33using std::string;
34using std::vector;
8481fbdf 35
c8df6005 36namespace pv {
c8df6005
JH
37namespace binding {
38
6f925ba9 39const vector< shared_ptr<prop::Property> >& Binding::properties()
3a69ef6b 40{
8dbbc7f0 41 return properties_;
3a69ef6b
JH
42}
43
8481fbdf
JH
44void Binding::commit()
45{
8dbbc7f0 46 for (shared_ptr<pv::prop::Property> p : properties_) {
8481fbdf
JH
47 assert(p);
48 p->commit();
49 }
50}
51
4e2195c8 52void Binding::add_properties_to_form(QFormLayout *layout, bool auto_commit)
8481fbdf 53{
4206fe26 54 assert(layout);
8481fbdf 55
4e2195c8
SA
56 help_labels_.clear();
57
2ad82c2e 58 for (shared_ptr<pv::prop::Property> p : properties_) {
8481fbdf 59 assert(p);
e7e57b25 60
4e2195c8
SA
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);
fa2f1db5 85 help_lbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
4e2195c8
SA
86 help_labels_[help_btn] = help_lbl;
87 }
88
894c4b52 89 if (p->labeled_widget()) {
e7e57b25 90 layout->addRow(widget);
894c4b52
UH
91 } else {
92 auto *lbl = new QLabel(p->name());
4e2195c8 93 lbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
894c4b52
UH
94 layout->addRow(lbl, widget);
95 }
4e2195c8
SA
96
97 if (help_lbl)
98 layout->addRow(help_lbl);
8481fbdf 99 }
4206fe26 100}
8481fbdf 101
4e2195c8 102QWidget* Binding::get_property_form(QWidget *parent, bool auto_commit)
4206fe26
JH
103{
104 QWidget *const form = new QWidget(parent);
105 QFormLayout *const layout = new QFormLayout(form);
106 form->setLayout(layout);
f904b412 107 add_properties_to_form(layout, auto_commit);
8481fbdf
JH
108 return form;
109}
110
dbed5609
SA
111void Binding::update_property_widgets()
112{
113 for (shared_ptr<pv::prop::Property> p : properties_) {
114 assert(p);
115 p->update_widget();
116 }
117}
118
e8d00928 119QString Binding::print_gvariant(Glib::VariantBase gvar)
5b1994c4
JH
120{
121 QString s;
122
e8d00928
ML
123 if (!gvar.gobj())
124 s = QString::fromStdString("(null)");
125 else if (gvar.is_of_type(Glib::VariantType("s")))
126 s = QString::fromStdString(
dbed5609 127 Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(gvar).get());
5b1994c4 128 else
e8d00928 129 s = QString::fromStdString(gvar.print());
5b1994c4
JH
130
131 return s;
132}
133
4e2195c8
SA
134void 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
870ea3db
UH
143} // namespace binding
144} // namespace pv