Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
matrix4x4Widget.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 <QGridLayout>
52 #include <QLineEdit>
53 #include <QLabel>
54 
55 #include "matrix4x4Widget.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::Matrix4x4Widget - IMPLEMENTATION
63 //
64 //=============================================================================
65 
67 Matrix4x4Widget::Matrix4x4Widget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
68  TypeWidget (_hints, _typeName, _parent)
69 {
70  bool ok;
71 
72  QGridLayout *gL = new QGridLayout;
73 
74  for (int i = 0; i < 16; i++)
75  {
76  default_[i] = 0.0;
77  if (i%5 == 0) //set diagonal to 1, convenient for e.g. transformation matrices
78  default_[i] = 1.0;
79  }
80 
81  ok = true;
82  if (_hints.contains ("default"))
83  {
84  QStringList sl = _hints["default"].split (',');
85 
86  if (sl.length () == 16)
87  {
88  for (int i = 0; i < 16 && ok; i++)
89  default_[i] = sl[i].toFloat (&ok);
90 
91  if (!ok)
92  for (int i = 0; i < 16; i++)
93  default_[i] = 0.0;
94  }
95  }
96 
97  for (int i = 0; i < 16; i++)
98  fields_[i] = new QLineEdit;
99 
100  for (int i = 0; i < 4; i++)
101  {
102  gL->addWidget (new QLabel ("("),i, 0);
103  gL->addWidget (fields_[i], i, 1);
104  gL->addWidget (new QLabel (","), i, 2);
105  gL->addWidget (fields_[i + 4], i, 3);
106  gL->addWidget (new QLabel (","), i, 4);
107  gL->addWidget (fields_[i + 8], i, 5);
108  gL->addWidget (new QLabel (","), i, 6);
109  gL->addWidget (fields_[i + 12], i, 7);
110  gL->addWidget (new QLabel (")"), i, 8);
111  }
112 
113  for (int i = 0; i < 16; i++)
114  {
115  fields_[i]->setText (QString::number (default_[i]));
116  connect (fields_[i], SIGNAL (editingFinished ()), this, SLOT (editingFinished ()));
117  }
118 
119  setLayout (gL);
120 
121  for (int i = 0; i < 16; i++)
122  current_[i] = default_[i];
123 }
124 
127 {
128 }
129 
130 //------------------------------------------------------------------------------
131 
134 {
135  QString rv = "Matrix4x4 (";
136  for (int i = 0; i < 16; i++)
137  rv += QString::number (current_[i]) + ",";
138 
139  rv.remove (rv.length () - 1, 1);
140  rv += ")";
141  return rv;
142 }
143 
144 //------------------------------------------------------------------------------
145 
147 void Matrix4x4Widget::fromValue(QString _from)
148 {
149  if (_from.startsWith ("Matrix4x4 ("))
150  _from.remove (0, 11);
151  if (_from.endsWith (")"))
152  _from.remove (_from.length () - 1, 1);
153 
154  QStringList sl = _from.split (',');
155 
156  float v[16];
157  bool ok = true;
158 
159  if (sl.length () == 16)
160  {
161  for (int i = 0; i < 16 && ok; i++)
162  v[i] = sl[i].toFloat (&ok);
163 
164  if (ok)
165  for (int i = 0; i < 16; i++)
166  {
167  current_[i] = v[i];
168  fields_[i]->setText (QString::number (current_[i]));
169  }
170  }
171 }
172 
173 //------------------------------------------------------------------------------
174 
175 // handle slider changes
176 void Matrix4x4Widget::editingFinished ()
177 {
178  bool ok;
179 
180  for (int i = 0; i < 16; i++)
181  {
182  const float v = fields_[i]->text ().toFloat (&ok);
183 
184  if (ok)
185  current_[i] = v;
186  else
187  fields_[i]->setText (QString::number (current_[i]));
188  }
189 }
190 
191 
192 
193 //------------------------------------------------------------------------------
194 
197 {
198  for (int i = 0; i < 16; i++)
199  {
200  current_[i] = default_[i];
201  fields_[i]->setText (QString::number (current_[i]));
202  }
203 }
204 
205 //------------------------------------------------------------------------------
206 }
207 
void fromValue(QString _from)
Read value from string.
Matrix4x4Widget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
~Matrix4x4Widget()
Destructor.
void toDefault()
Reset to default.
QString toValue()
Convert current value to string.