1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2  * @file   ChoiceParameter.cpp
3  * @author Sebastien Fourey
4  * @date   Nov 2014
5  *
6  * @brief  Declaration of the class AbstractParameter
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 "ChoiceParameter.h"
48 #include <QComboBox>
49 #include <QDomNamedNodeMap>
50 #include <QGridLayout>
51 #include <QLabel>
52 #include <QWidget>
53 #include "Common.h"
54 
ChoiceParameter(QDomNode node,QObject * parent)55 ChoiceParameter::ChoiceParameter(QDomNode node, QObject * parent) : AbstractParameter(parent), _node(node), _label(0), _comboBox(0)
56 {
57   _name = node.attributes().namedItem("name").nodeValue();
58 
59   QString def = node.toElement().attribute("default", "0");
60   QString value = node.toElement().attribute("savedValue", def);
61   _default = def.toInt();
62   _value = value.toInt();
63 }
64 
~ChoiceParameter()65 ChoiceParameter::~ChoiceParameter()
66 {
67   delete _comboBox;
68   delete _label;
69 }
70 
addTo(QWidget * widget,int row)71 void ChoiceParameter::addTo(QWidget * widget, int row)
72 {
73   QGridLayout * grid = dynamic_cast<QGridLayout *>(widget->layout());
74   if (!grid)
75     return;
76   delete _comboBox;
77   delete _label;
78 
79   _comboBox = new QComboBox(widget);
80   QDomNamedNodeMap attributes = _node.attributes();
81   bool done = false;
82   for (int i = 0; i < 50 && !done; ++i) {
83     QDomAttr attr = attributes.namedItem(QString("choice%1").arg(i)).toAttr();
84     if (attr.isNull()) {
85       done = true;
86     } else {
87       _comboBox->addItem(attr.nodeValue(), QVariant(i));
88     }
89   }
90   _comboBox->setCurrentIndex(_value);
91 
92   grid->addWidget(_label = new QLabel(_name, widget), row, 0, 1, 1);
93   grid->addWidget(_comboBox, row, 1, 1, 2);
94   connect(_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxIndexChanged(int)));
95 }
96 
textValue() const97 QString ChoiceParameter::textValue() const
98 {
99   return QString("%1").arg(_comboBox->currentIndex());
100 }
101 
setValue(const QString & value)102 void ChoiceParameter::setValue(const QString & value)
103 {
104   _value = value.toInt();
105   if (_comboBox) {
106     _comboBox->setCurrentIndex(_value);
107   }
108 }
109 
reset()110 void ChoiceParameter::reset()
111 {
112   _comboBox->setCurrentIndex(_default);
113   _value = _default;
114 }
115 
saveValueInDOM()116 void ChoiceParameter::saveValueInDOM()
117 {
118   _node.toElement().setAttribute("savedValue", _comboBox->currentIndex());
119 }
120 
onComboBoxIndexChanged(int i)121 void ChoiceParameter::onComboBoxIndexChanged(int i)
122 {
123   _value = i;
124   emit valueChanged();
125 }
126