OpenWalnut  1.5.0dev
WPropertyMatrix4X4Widget.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <sstream>
26 #include <cmath>
27 #include <string>
28 
29 
30 #include "core/common/WStringUtils.h"
31 #include "core/common/WLogger.h"
32 #include "core/common/WPropertyVariable.h"
33 #include "../WGuiConsts.h"
34 #include "../guiElements/WScaleLabel.h"
35 
36 #include "WPropertyMatrix4X4Widget.h"
37 
38 WPropertyMatrix4X4Widget::WPropertyMatrix4X4Widget( WPropMatrix4X4 property, QGridLayout* propertyGrid, QWidget* parent ):
39  WPropertyWidget( property, propertyGrid, parent ),
40  m_matrixProperty( property ),
41  m_layout( &m_parameterWidgets ),
42  m_asText( &m_informationWidgets ),
43  m_infoLayout( &m_informationWidgets )
44 {
45  // initialize members
46  for( size_t row = 0; row < 4; ++row )
47  {
48  QHBoxLayout* h = new QHBoxLayout();
49 
50  for( size_t col = 0; col < 4; ++col )
51  {
52  size_t i = row * 4 + col;
53  m_edits[ i ].setParent( &m_parameterWidgets );
54  m_edits[ i ].setMinimumHeight( WMIN_WIDGET_HEIGHT );
55  m_edits[ i ].resize( m_edits[ i ].minimumSizeHint().width() * 2.0, m_edits[ i ].size().height() );
56 
57  connect( &m_edits[ i ], SIGNAL( returnPressed() ), this, SLOT( editChanged() ) );
58  connect( &m_edits[ i ], SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
59 
60  h->addWidget( &m_edits[ i ] );
61  }
62 
63  m_layout.addLayout( h );
64  }
65 
66  m_parameterWidgets.setLayout( &m_layout );
67  m_layout.setMargin( WGLOBAL_MARGIN );
68  m_layout.setSpacing( WGLOBAL_SPACING );
69 
70  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
71  m_infoLayout.addWidget( &m_asText );
72  m_infoLayout.setMargin( WGLOBAL_MARGIN );
73  m_infoLayout.setSpacing( WGLOBAL_SPACING );
74  m_informationWidgets.setLayout( &m_infoLayout );
75 
76  update();
77 }
78 
80 {
81  // cleanup
82 }
83 
85 {
86  // set the values
88 
89  for( size_t row = 0; row < 4; ++row )
90  {
91  for( size_t col = 0; col < 4; ++col )
92  {
93  size_t i = row * 4 + col;
94  m_edits[ i ].setText( QString::fromStdString( string_utils::toString( m( row, col ) ) ) );
95  }
96  }
97 
98  // do not forget to update the label
99  m_asText.setText( QString::fromStdString( string_utils::toString( m_matrixProperty->get() ) ) );
100 }
101 
103 {
105 }
106 
107 void WPropertyMatrix4X4Widget::textEdited( const QString& /*text*/ )
108 {
109  setPropertyFromWidgets( true );
110 }
111 
113 {
114  // grab all the values
115  bool valid = true;
116  // create a new matrix
118 
119  for( size_t row = 0; row < 4; ++row )
120  {
121  for( size_t col = 0; col < 4; ++col )
122  {
123  size_t i = row * 4 + col;
124 
125  // check validity
126  bool tmp;
127  double value = m_edits[ i ].text().toDouble( &tmp );
128  valid = valid && tmp;
129  m( row, col ) = value;
130  }
131  }
132 
133  if( !valid )
134  {
135  invalidate();
136  return;
137  }
138 
139  // set/validate to the property
140  if( validateOnly )
141  {
142  invalidate( !m_matrixProperty->accept( m ) );
143  }
144  else
145  {
146  invalidate( !m_matrixProperty->set( m ) ); // NOTE: set automatically checks the validity of the value
147  }
148 }
149 
WPropMatrix4X4 m_matrixProperty
The integer property represented by this widget.
QVBoxLayout m_layout
Layout used to position the label and the widgets.
void editChanged()
Called whenever the edit field changes.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
void setPropertyFromWidgets(bool validateOnly=false)
Updates the property using the edit X Y and Z widgets.
WScaleLabel m_asText
Used to show the property as text.
void textEdited(const QString &text)
Called when the text in m_edit changes.
virtual void update()
Called whenever the widget should update.
WPropertyMatrix4X4Widget(WPropMatrix4X4 property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
virtual ~WPropertyMatrix4X4Widget()
Destructor.
QLineEdit m_edits[16]
The edit field showing the value of the slider.
Class building the base for all widgets representing properties.
QWidget m_informationWidgets
The widget containing a layout and provides the widgets for showing information properties.
QWidget m_parameterWidgets
The widget containing a layout and provides the edit widgets for the property.
virtual void invalidate(bool invalid=true)
This method marks this widget as invalid.
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120