Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //== INCLUDES =================================================================
51 #include <QHBoxLayout>
52 #include <QLineEdit>
53 #include <QLabel>
54 
55 #include "vec3dWidget.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::Vec3DWidget - IMPLEMENTATION
63 //
64 //=============================================================================
65 
67 Vec3DWidget::Vec3DWidget(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 < 3; 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 () == 3)
83  {
84  for (int i = 0; i < 3 && ok; i++)
85  default_[i] = sl[i].toFloat (&ok);
86 
87  if (!ok)
88  for (int i = 0; i < 3; i++)
89  default_[i] = 0.0;
90  }
91  }
92 
93  for (int i = 0; i < 3; 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 
104  for (int i = 0; i < 3; i++)
105  {
106  fields_[i]->setText (QString::number (default_[i]));
107  connect (fields_[i], SIGNAL (editingFinished ()), this, SLOT (editingFinished ()));
108  }
109 
110  setLayout (hL);
111 
112  for (int i = 0; i < 3; i++)
113  current_[i] = default_[i];
114 }
115 
118 {
119 }
120 
121 //------------------------------------------------------------------------------
122 
125 {
126  QString rv = "Vector (";
127  rv += QString::number (current_[0]) + ",";
128  rv += QString::number (current_[1]) + ",";
129  rv += QString::number (current_[2]) + ")";
130  return rv;
131 }
132 
133 //------------------------------------------------------------------------------
134 
136 void Vec3DWidget::fromValue(QString _from)
137 {
138  if (_from.startsWith ("Vector ("))
139  _from.remove (0, 8);
140  if (_from.endsWith (")"))
141  _from.remove (_from.length () - 1, 1);
142 
143  QStringList sl = _from.split (',');
144 
145  float v[3];
146  bool ok = true;
147 
148  if (sl.length () == 3)
149  {
150  for (int i = 0; i < 3 && ok; i++)
151  v[i] = sl[i].toFloat (&ok);
152 
153  if (ok)
154  for (int i = 0; i < 3; i++)
155  {
156  current_[i] = v[i];
157  fields_[i]->setText (QString::number (current_[i]));
158  }
159  }
160 }
161 
162 //------------------------------------------------------------------------------
163 
164 // handle slider changes
165 void Vec3DWidget::editingFinished ()
166 {
167  bool ok;
168 
169  for (int i = 0; i < 3; i++)
170  {
171  const float v = fields_[i]->text ().toFloat (&ok);
172 
173  if (ok)
174  current_[i] = v;
175  else
176  fields_[i]->setText (QString::number (current_[i]));
177  }
178 }
179 
180 
181 
182 //------------------------------------------------------------------------------
183 
186 {
187  for (int i = 0; i < 3; i++)
188  {
189  current_[i] = default_[i];
190  fields_[i]->setText (QString::number (current_[i]));
191  }
192 }
193 
194 //------------------------------------------------------------------------------
195 }
196 
Vec3DWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
Definition: vec3dWidget.cc:67
~Vec3DWidget()
Destructor.
Definition: vec3dWidget.cc:117
void fromValue(QString _from)
Read value from string.
Definition: vec3dWidget.cc:136
QString toValue()
Convert current value to string.
Definition: vec3dWidget.cc:124
void toDefault()
Reset to default.
Definition: vec3dWidget.cc:185