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