1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2  * @file   TextParameter.cpp
3  * @author Sebastien Fourey
4  * @date   Nov 2014
5  *
6  * @brief  Declaration of the class TextParameter
7  *
8  * This file is part of the ZArt software's source code.
9  *
10  * Copyright Sebastien Fourey / GREYC Ensicaen (2010-...)
11  *
12  *                    https://foureys.users.greyc.fr/
13  *
14  * This software is a computer program whose purpose is to demonstrate
15  * the possibilities of the GMIC image processing language by offering the
16  * choice of several manipulations on a video stream acquired from a webcam. In
17  * other words, ZArt is a GUI for G'MIC real-time manipulations on the output
18  * of a webcam.
19  *
20  * This software is governed by the CeCILL  license under French law and
21  * abiding by the rules of distribution of free software.  You can  use,
22  * modify and/ or redistribute the software under the terms of the CeCILL
23  * license as circulated by CEA, CNRS and INRIA at the following URL
24  * "http://www.cecill.info". See also the directory "Licence" which comes
25  * with this source code for the full text of the CeCILL license.
26  *
27  * As a counterpart to the access to the source code and  rights to copy,
28  * modify and redistribute granted by the license, users are provided only
29  * with a limited warranty  and the software's author,  the holder of the
30  * economic rights,  and the successive licensors  have only  limited
31  * liability.
32  *
33  * In this respect, the user's attention is drawn to the risks associated
34  * with loading,  using,  modifying and/or developing or reproducing the
35  * software by the user in light of its specific status of free software,
36  * that may mean  that it is complicated to manipulate,  and  that  also
37  * therefore means  that it is reserved for developers  and  experienced
38  * professionals having in-depth computer knowledge. Users are therefore
39  * encouraged to load and test the software's suitability as regards their
40  * requirements in conditions enabling the security of their systems and/or
41  * data to be ensured and,  more generally, to use and operate it in the
42  * same conditions as regards security.
43  *
44  * The fact that you are presently reading this means that you have had
45  * knowledge of the CeCILL license and that you accept its terms.
46  */
47 #include "TextParameter.h"
48 #include <QGridLayout>
49 #include <QLabel>
50 #include <QLineEdit>
51 #include <QWidget>
52 #include "Common.h"
53 
TextParameter(QDomNode node,QObject * parent)54 TextParameter::TextParameter(QDomNode node, QObject * parent) : AbstractParameter(parent), _node(node), _label(0), _lineEdit(0)
55 {
56   _name = node.attributes().namedItem("name").nodeValue();
57   QString def = node.attributes().namedItem("default").nodeValue();
58   _default = node.toElement().attribute("default", QString());
59   _value = node.toElement().attribute("savedValue", _default);
60 }
61 
~TextParameter()62 TextParameter::~TextParameter()
63 {
64   delete _lineEdit;
65   delete _label;
66 }
67 
addTo(QWidget * widget,int row)68 void TextParameter::addTo(QWidget * widget, int row)
69 {
70   QGridLayout * grid = dynamic_cast<QGridLayout *>(widget->layout());
71   if (!grid)
72     return;
73   delete _label;
74   delete _lineEdit;
75   _lineEdit = new QLineEdit(_value, widget);
76   grid->addWidget(_label = new QLabel(_name, widget), row, 0, 1, 1);
77   grid->addWidget(_lineEdit, row, 1, 1, 2);
78   connect(_lineEdit, SIGNAL(editingFinished()), this, SIGNAL(valueChanged()));
79 }
80 
textValue() const81 QString TextParameter::textValue() const
82 {
83   QString text = _lineEdit->text();
84   text.replace(QChar('"'), QString("\\\""));
85   if (_lineEdit->text().isEmpty())
86     return QString("\"\\\"\\\"\"");
87   else
88     return QString("\"%1\"").arg(text);
89 }
90 
unquotedTextValue() const91 QString TextParameter::unquotedTextValue() const
92 {
93   return _lineEdit->text();
94 }
95 
setValue(const QString & value)96 void TextParameter::setValue(const QString & value)
97 {
98   _value = value;
99   if (_lineEdit) {
100     _lineEdit->setText(_value);
101   }
102 }
103 
reset()104 void TextParameter::reset()
105 {
106   _lineEdit->setText(_default);
107   _value = _default;
108 }
109 
saveValueInDOM()110 void TextParameter::saveValueInDOM()
111 {
112   _node.toElement().setAttribute("savedValue", _lineEdit->text());
113 }
114