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