Developer Documentation
vec4dWidget.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision: 10745 $ *
45 * $LastChangedBy: moebius $ *
46 * $Date: 2011-01-26 10:23:50 +0100 (Mi, 26. Jan 2011) $ *
47 * *
48 \*===========================================================================*/
49 
50 //== INCLUDES =================================================================
51 #include <QHBoxLayout>
52 #include <QLineEdit>
53 #include <QLabel>
54 
55 #include "vec4dWidget.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::Vec3DWidget - IMPLEMENTATION
63 //
64 //=============================================================================
65 
67 Vec4DWidget::Vec4DWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
68  TypeWidget (_hints, _typeName, _parent)
69 {
70  bool ok;
71 
72  QHBoxLayout *hL = new QHBoxLayout;
73 
74  for (int i = 0; i < 4; i++)
75  default_[i] = 0.0;
76 
77  ok = true;
78  if (_hints.contains ("default"))
79  {
80  QStringList sl = _hints["default"].split (',');
81 
82  if (sl.length () == 4)
83  {
84  for (int i = 0; i < 4 && ok; i++)
85  default_[i] = sl[i].toFloat (&ok);
86 
87  if (!ok)
88  for (int i = 0; i < 4; i++)
89  default_[i] = 0.0;
90  }
91  }
92 
93  for (int i = 0; i < 4; i++)
94  fields_[i] = new QLineEdit;
95 
96  hL->addWidget (new QLabel ("("));
97  hL->addWidget (fields_[0]);
98  hL->addWidget (new QLabel (","));
99  hL->addWidget (fields_[1]);
100  hL->addWidget (new QLabel (","));
101  hL->addWidget (fields_[2]);
102  hL->addWidget (new QLabel (","));
103  hL->addWidget (fields_[3]);
104  hL->addWidget (new QLabel (")"));
105 
106  for (int i = 0; i < 4; i++)
107  {
108  fields_[i]->setText (QString::number (default_[i]));
109  connect (fields_[i], SIGNAL (editingFinished ()), this, SLOT (editingFinished ()));
110  }
111 
112  setLayout (hL);
113 
114  for (int i = 0; i < 4; i++)
115  current_[i] = default_[i];
116 }
117 
120 {
121 }
122 
123 //------------------------------------------------------------------------------
124 
127 {
128  QString rv = "Vector4 (";
129  rv += QString::number (current_[0]) + ",";
130  rv += QString::number (current_[1]) + ",";
131  rv += QString::number (current_[2]) + ",";
132  rv += QString::number (current_[3]) + ")";
133  return rv;
134 }
135 
136 //------------------------------------------------------------------------------
137 
139 void Vec4DWidget::fromValue(QString _from)
140 {
141  if (_from.startsWith ("Vector4 ("))
142  _from.remove (0, 8);
143  if (_from.endsWith (")"))
144  _from.remove (_from.length () - 1, 1);
145 
146  QStringList sl = _from.split (',');
147 
148  float v[4];
149  bool ok = true;
150 
151  if (sl.length () == 4)
152  {
153  for (int i = 0; i < 4 && ok; i++)
154  v[i] = sl[i].toFloat (&ok);
155 
156  if (ok)
157  for (int i = 0; i < 4; i++)
158  {
159  current_[i] = v[i];
160  fields_[i]->setText (QString::number (current_[i]));
161  }
162  }
163 }
164 
165 //------------------------------------------------------------------------------
166 
167 // handle slider changes
168 void Vec4DWidget::editingFinished ()
169 {
170  bool ok;
171 
172  for (int i = 0; i < 4; i++)
173  {
174  const float v = fields_[i]->text ().toFloat (&ok);
175 
176  if (ok)
177  current_[i] = v;
178  else
179  fields_[i]->setText (QString::number (current_[i]));
180  }
181 }
182 
183 
184 
185 //------------------------------------------------------------------------------
186 
189 {
190  for (int i = 0; i < 4; i++)
191  {
192  current_[i] = default_[i];
193  fields_[i]->setText (QString::number (current_[i]));
194  }
195 }
196 
197 //------------------------------------------------------------------------------
198 }
199 
~Vec4DWidget()
Destructor.
Definition: vec4dWidget.cc:119
void toDefault()
Reset to default.
Definition: vec4dWidget.cc:188
void fromValue(QString _from)
Read value from string.
Definition: vec4dWidget.cc:139
QString toValue()
Convert current value to string.
Definition: vec4dWidget.cc:126
Vec4DWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
Definition: vec4dWidget.cc:67