]> sigrok.org Git - pulseview.git/blob - pv/prop/string.cpp
Wrapped sr_dev_inst in a class: pv::DevInst
[pulseview.git] / pv / prop / string.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 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
21 #include <assert.h>
22
23 #include <QLineEdit>
24 #include <QSpinBox>
25
26 #include "string.h"
27
28 namespace pv {
29 namespace prop {
30
31 String::String(QString name,
32         Getter getter,
33         Setter setter) :
34         Property(name, getter, setter),
35         _line_edit(NULL)
36 {
37 }
38
39 QWidget* String::get_widget(QWidget *parent, bool auto_commit)
40 {
41         if (_line_edit)
42                 return _line_edit;
43
44         GVariant *const value = _getter ? _getter() : NULL;
45         if (!value)
46                 return NULL;
47
48         _line_edit = new QLineEdit(parent);
49         _line_edit->setText(QString::fromUtf8(
50                 g_variant_get_string(value, NULL)));
51         g_variant_unref(value);
52
53         if (auto_commit)
54                 connect(_line_edit, SIGNAL(textEdited(const QString&)),
55                         this, SLOT(on_text_edited(const QString&)));
56
57         return _line_edit;
58 }
59
60 void String::commit()
61 {
62         assert(_setter);
63
64         if (!_line_edit)
65                 return;
66
67         QByteArray ba = _line_edit->text().toLocal8Bit();
68         _setter(g_variant_new_string(ba.data()));
69 }
70
71 void String::on_text_edited(const QString&)
72 {
73         commit();
74 }
75
76 } // prop
77 } // pv