]> sigrok.org Git - pulseview.git/blame - pv/binding/binding.cpp
Binding: Add help icons for entries with descriptions
[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);
85 help_labels_[help_btn] = help_lbl;
86 }
87
894c4b52 88 if (p->labeled_widget()) {
e7e57b25 89 layout->addRow(widget);
894c4b52
UH
90 } else {
91 auto *lbl = new QLabel(p->name());
4e2195c8 92 lbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
894c4b52
UH
93 layout->addRow(lbl, widget);
94 }
4e2195c8
SA
95
96 if (help_lbl)
97 layout->addRow(help_lbl);
8481fbdf 98 }
4206fe26 99}
8481fbdf 100
4e2195c8 101QWidget* Binding::get_property_form(QWidget *parent, bool auto_commit)
4206fe26
JH
102{
103 QWidget *const form = new QWidget(parent);
104 QFormLayout *const layout = new QFormLayout(form);
105 form->setLayout(layout);
f904b412 106 add_properties_to_form(layout, auto_commit);
8481fbdf
JH
107 return form;
108}
109
dbed5609
SA
110void Binding::update_property_widgets()
111{
112 for (shared_ptr<pv::prop::Property> p : properties_) {
113 assert(p);
114 p->update_widget();
115 }
116}
117
e8d00928 118QString Binding::print_gvariant(Glib::VariantBase gvar)
5b1994c4
JH
119{
120 QString s;
121
e8d00928
ML
122 if (!gvar.gobj())
123 s = QString::fromStdString("(null)");
124 else if (gvar.is_of_type(Glib::VariantType("s")))
125 s = QString::fromStdString(
dbed5609 126 Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(gvar).get());
5b1994c4 127 else
e8d00928 128 s = QString::fromStdString(gvar.print());
5b1994c4
JH
129
130 return s;
131}
132
4e2195c8
SA
133void 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
870ea3db
UH
142} // namespace binding
143} // namespace pv