1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2  * @file   CommandParamsWidget.cpp
3  * @author Sebastien Fourey
4  * @date   Nov 2014
5  * @brief  Definition of the class CommandParamsWidget
6  *
7  * This file is part of the ZArt software's source code.
8  *
9  * Copyright Sebastien Fourey / GREYC Ensicaen (2010-...)
10  *
11  *                    https://foureys.users.greyc.fr/
12  *
13  * This software is a computer program whose purpose is to demonstrate
14  * the possibilities of the GMIC image processing language by offering the
15  * choice of several manipulations on a video stream acquired from a webcam. In
16  * other words, ZArt is a GUI for G'MIC real-time manipulations on the output
17  * of a webcam.
18  *
19  * This software is governed by the CeCILL  license under French law and
20  * abiding by the rules of distribution of free software.  You can  use,
21  * modify and/ or redistribute the software under the terms of the CeCILL
22  * license as circulated by CEA, CNRS and INRIA at the following URL
23  * "http://www.cecill.info". See also the directory "Licence" which comes
24  * with this source code for the full text of the CeCILL license.
25  *
26  * As a counterpart to the access to the source code and  rights to copy,
27  * modify and redistribute granted by the license, users are provided only
28  * with a limited warranty  and the software's author,  the holder of the
29  * economic rights,  and the successive licensors  have only  limited
30  * liability.
31  *
32  * In this respect, the user's attention is drawn to the risks associated
33  * with loading,  using,  modifying and/or developing or reproducing the
34  * software by the user in light of its specific status of free software,
35  * that may mean  that it is complicated to manipulate,  and  that  also
36  * therefore means  that it is reserved for developers  and  experienced
37  * professionals having in-depth computer knowledge. Users are therefore
38  * encouraged to load and test the software's suitability as regards their
39  * requirements in conditions enabling the security of their systems and/or
40  * data to be ensured and,  more generally, to use and operate it in the
41  * same conditions as regards security.
42  *
43  * The fact that you are presently reading this means that you have had
44  * knowledge of the CeCILL license and that you accept its terms.
45  */
46 
47 #include "CommandParamsWidget.h"
48 #include <QGridLayout>
49 #include <QLabel>
50 #include "AbstractParameter.h"
51 #include "Common.h"
52 #include "PointParameter.h"
53 
CommandParamsWidget(QWidget * parent)54 CommandParamsWidget::CommandParamsWidget(QWidget * parent) : QWidget(parent), _valueString(""), _pbReset(0), _labelNoParams(0)
55 {
56   delete layout();
57   QGridLayout * grid = new QGridLayout;
58   grid->setRowStretch(1, 2);
59   setLayout(grid);
60   _labelNoParams = new QLabel("<i>No parameters</i>", this);
61   _labelNoParams->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
62   grid->addWidget(_labelNoParams, 0, 0, 4, 3);
63   _hasKeypoints = false;
64 }
65 
build(QDomNode presetNode)66 void CommandParamsWidget::build(QDomNode presetNode)
67 {
68   clear();
69   delete layout();
70   QGridLayout * grid = new QGridLayout;
71   grid->setRowStretch(1, 2);
72   setLayout(grid);
73   PointParameter::resetDefaultColorIndex();
74 
75   int row = 0;
76   QDomNode child = presetNode.firstChild();
77   while (!child.isNull()) {
78     AbstractParameter * parameter = AbstractParameter::createFromNode(child, this);
79     if (parameter) {
80       _presetParameters.push_back(parameter);
81       if (parameter->isVisible()) {
82         parameter->addTo(this, row++);
83       }
84       connect(parameter, SIGNAL(valueChanged()), this, SLOT(updateValueString()));
85     }
86     child = child.nextSibling();
87   }
88 
89   KeypointList keypoints;
90   QVector<AbstractParameter *>::iterator it = _presetParameters.begin();
91   while (it != _presetParameters.end()) {
92     (*it)->addToKeypointList(keypoints);
93     ++it;
94   }
95   _hasKeypoints = !keypoints.isEmpty();
96 
97   if (row) {
98     _pbReset = new QPushButton("Reset", this);
99     grid->addWidget(_pbReset, row, 0, 1, 3);
100     connect(_pbReset, SIGNAL(clicked()), this, SLOT(reset()));
101     delete _labelNoParams;
102     _labelNoParams = 0;
103   } else {
104     _labelNoParams = new QLabel("<i>No parameters</i>", this);
105     _labelNoParams->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
106     grid->addWidget(_labelNoParams, 0, 0, 4, 3);
107   }
108   updateValueString(false);
109 }
110 
~CommandParamsWidget()111 CommandParamsWidget::~CommandParamsWidget()
112 {
113   clear();
114 }
115 
valueString() const116 const QString & CommandParamsWidget::valueString() const
117 {
118   return _valueString;
119 }
120 
valueStringList() const121 QStringList CommandParamsWidget::valueStringList() const
122 {
123   QStringList list;
124   for (int i = 0; i < _presetParameters.size(); ++i) {
125     list.append(_presetParameters[i]->unquotedTextValue());
126   }
127   return list;
128 }
129 
setValues(const QStringList & list)130 void CommandParamsWidget::setValues(const QStringList & list)
131 {
132   if (_presetParameters.size() != list.size()) {
133     return;
134   }
135   for (int i = 0; i < _presetParameters.size(); ++i) {
136     _presetParameters[i]->setValue(list[i]);
137   }
138   updateValueString(true);
139 }
140 
saveValuesInDOM()141 void CommandParamsWidget::saveValuesInDOM()
142 {
143   for (int i = 0; i < _presetParameters.size(); ++i) {
144     _presetParameters[i]->saveValueInDOM();
145   }
146 }
147 
hasKeypoints() const148 bool CommandParamsWidget::hasKeypoints() const
149 {
150   return _hasKeypoints;
151 }
152 
updateValueString(bool notify)153 void CommandParamsWidget::updateValueString(bool notify)
154 {
155   _valueString.clear();
156   bool firstParameter = true;
157   for (int i = 0; i < _presetParameters.size(); ++i) {
158     QString str = _presetParameters[i]->textValue();
159     if (!str.isNull()) {
160       if (!firstParameter) {
161         _valueString += ",";
162       }
163       _valueString += str;
164       firstParameter = false;
165     }
166   }
167   if (notify)
168     emit valueChanged();
169 }
170 
reset()171 void CommandParamsWidget::reset()
172 {
173   for (int i = 0; i < _presetParameters.size(); ++i) {
174     _presetParameters[i]->reset();
175   }
176   updateValueString(true);
177 }
178 
clear()179 void CommandParamsWidget::clear()
180 {
181   QVector<AbstractParameter *>::iterator it = _presetParameters.begin();
182   while (it != _presetParameters.end()) {
183     delete *it;
184     ++it;
185   }
186   _presetParameters.clear();
187   delete _pbReset;
188   _pbReset = 0;
189   delete _labelNoParams;
190   _labelNoParams = 0;
191 }
192 
keypoints() const193 KeypointList CommandParamsWidget::keypoints() const
194 {
195   KeypointList list;
196   if (!_hasKeypoints) {
197     return list;
198   }
199   QVector<AbstractParameter *>::const_iterator it = _presetParameters.begin();
200   while (it != _presetParameters.end()) {
201     (*it)->addToKeypointList(list);
202     ++it;
203   }
204   return list;
205 }
206 
setKeypoints(KeypointList list,bool notify)207 void CommandParamsWidget::setKeypoints(KeypointList list, bool notify)
208 {
209   Q_ASSERT_X((list.isEmpty() || _hasKeypoints), __PRETTY_FUNCTION__, "Keypoint list mismatch");
210   if (!_hasKeypoints) {
211     return;
212   }
213   QVector<AbstractParameter *>::const_iterator it = _presetParameters.begin();
214   while (it != _presetParameters.end()) {
215     (*it)->extractPositionFromKeypointList(list);
216     ++it;
217   }
218   updateValueString(notify);
219 }
220