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 //== INCLUDES =================================================================
44 #include <QHBoxLayout>
45 #include <QLineEdit>
46 #include <QLabel>
47
48 #include "vec4dWidget.hh"
49
50 //== NAMESPACES ===============================================================
51 namespace VSI {
52
53 //=============================================================================
54 //
55 // CLASS VSI::Vec3DWidget - IMPLEMENTATION
56 //
57 //=============================================================================
58
60 Vec4DWidget::Vec4DWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
61 TypeWidget (_hints, _typeName, _parent)
62 {
63 bool ok;
64
65 QHBoxLayout *hL = new QHBoxLayout;
66
67 for (int i = 0; i < 4; i++)
68 default_[i] = 0.0;
69
70 ok = true;
71 if (_hints.contains ("default"))
72 {
73 QStringList sl = _hints["default"].split (',');
74
75 if (sl.length () == 4)
76 {
77 for (int i = 0; i < 4 && ok; i++)
78 default_[i] = sl[i].toFloat (&ok);
79
80 if (!ok)
81 for (int i = 0; i < 4; i++)
82 default_[i] = 0.0;
83 }
84 }
85
86 for (int i = 0; i < 4; i++)
87 fields_[i] = new QLineEdit;
88
89 hL->addWidget (new QLabel ("("));
90 hL->addWidget (fields_[0]);
91 hL->addWidget (new QLabel (","));
92 hL->addWidget (fields_[1]);
93 hL->addWidget (new QLabel (","));
94 hL->addWidget (fields_[2]);
95 hL->addWidget (new QLabel (","));
96 hL->addWidget (fields_[3]);
97 hL->addWidget (new QLabel (")"));
98
99 for (int i = 0; i < 4; i++)
100 {
101 fields_[i]->setText (QString::number (default_[i]));
102 connect (fields_[i], SIGNAL (editingFinished ()), this, SLOT (editingFinished ()));
103 }
104
105 setLayout (hL);
106
107 for (int i = 0; i < 4; i++)
108 current_[i] = default_[i];
109 }
110
112 Vec4DWidget::~ Vec4DWidget()
113 {
114 }
115
116 //------------------------------------------------------------------------------
117
119 QString Vec4DWidget::toValue()
120 {
121 QString rv = "Vector4 (";
122 rv += QString::number (current_[0]) + ",";
123 rv += QString::number (current_[1]) + ",";
124 rv += QString::number (current_[2]) + ",";
125 rv += QString::number (current_[3]) + ")";
126 return rv;
127 }
128
129 //------------------------------------------------------------------------------
130
132 void Vec4DWidget::fromValue(QString _from)
133 {
134 if (_from.startsWith ("Vector4 ("))
135 _from.remove (0, 8);
136 if (_from.endsWith (")"))
137 _from.remove (_from.length () - 1, 1);
138
139 QStringList sl = _from.split (',');
140
141 float v[4];
142 bool ok = true;
143
144 if (sl.length () == 4)
145 {
146 for (int i = 0; i < 4 && ok; i++)
147 v[i] = sl[i].toFloat (&ok);
148
149 if (ok)
150 for (int i = 0; i < 4; i++)
151 {
152 current_[i] = v[i];
153 fields_[i]->setText (QString::number (current_[i]));
154 }
155 }
156 }
157
158 //------------------------------------------------------------------------------
159
160 // handle slider changes
161 void Vec4DWidget::editingFinished ()
162 {
163 bool ok;
164
165 for (int i = 0; i < 4; i++)
166 {
167 const float v = fields_[i]->text ().toFloat (&ok);
168
169 if (ok)
170 current_[i] = v;
171 else
172 fields_[i]->setText (QString::number (current_[i]));
173 }
174 }
175
176
177
178 //------------------------------------------------------------------------------
179
181 void Vec4DWidget::toDefault()
182 {
183 for (int i = 0; i < 4; i++)
184 {
185 current_[i] = default_[i];
186 fields_[i]->setText (QString::number (current_[i]));
187 }
188 }
189
190 //------------------------------------------------------------------------------
191 }
192
VSI::Vec4DWidget::toValue
QString toValue() override
Convert current value to string.
Definition: vec4dWidget.cc:119
VSI::Vec4DWidget::Vec4DWidget
Vec4DWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
Definition: vec4dWidget.cc:60
VSI::TypeWidget
Definition: typeWidget.hh:59
VSI::Vec4DWidget::fromValue
void fromValue(QString _from) override
Read value from string.
Definition: vec4dWidget.cc:132
VSI
Definition: baseWidget.cc:72
VSI::Vec4DWidget::toDefault
void toDefault() override
Reset to default.
Definition: vec4dWidget.cc:181