OpenWalnut  1.5.0dev
WPropertyPositionWidget.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 #include "core/common/WLogger.h"
30 #include "core/common/WPropertyVariable.h"
31 #include "../WGuiConsts.h"
32 
33 #include "WPropertyPositionWidget.h"
34 
35 WPropertyPositionWidget::WPropertyPositionWidget( WPropPosition property, QGridLayout* propertyGrid, QWidget* parent ):
36  WPropertyWidget( property, propertyGrid, parent ),
37  m_posProperty( property ),
38  m_editX( &m_parameterWidgets ),
39  m_editY( &m_parameterWidgets ),
40  m_editZ( &m_parameterWidgets ),
41  m_layout( &m_parameterWidgets ),
42  m_asText( &m_informationWidgets ),
43  m_infoLayout( &m_informationWidgets )
44 {
45  m_editX.setMinimumHeight( WMIN_WIDGET_HEIGHT );
46  m_editY.setMinimumHeight( WMIN_WIDGET_HEIGHT );
47  m_editZ.setMinimumHeight( WMIN_WIDGET_HEIGHT );
48 
49  // initialize members
50  m_editX.resize( m_editX.minimumSizeHint().width() * 1.0, m_editX.size().height() );
51  //m_editX.setMaximumWidth( m_editX.minimumSizeHint().width() * 5.0 );
52  m_editY.resize( m_editY.minimumSizeHint().width() * 1.0, m_editY.size().height() );
53  //m_editY.setMaximumWidth( m_editY.minimumSizeHint().width() * 5.0 );
54  m_editZ.resize( m_editZ.minimumSizeHint().width() * 1.0, m_editZ.size().height() );
55  //m_editZ.setMaximumWidth( m_editZ.minimumSizeHint().width() * 5.0 );
56 
57  // layout both against each other
58  m_layout.addWidget( &m_editX );
59  m_layout.addWidget( &m_editY );
60  m_layout.addWidget( &m_editZ );
61  m_layout.setMargin( WGLOBAL_MARGIN );
62  m_layout.setSpacing( WGLOBAL_SPACING );
63 
64  m_parameterWidgets.setLayout( &m_layout );
65 
66  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
67  m_infoLayout.addWidget( &m_asText );
68  m_infoLayout.setMargin( WGLOBAL_MARGIN );
69  m_infoLayout.setSpacing( WGLOBAL_SPACING );
70  m_informationWidgets.setLayout( &m_infoLayout );
71 
72  update();
73 
74  // connect the modification signal of the edit and slider with our callback
75  connect( &m_editX, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
76  connect( &m_editX, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
77  connect( &m_editY, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
78  connect( &m_editY, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
79  connect( &m_editZ, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
80  connect( &m_editZ, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
81 }
82 
84 {
85  // cleanup
86 }
87 
88 std::string WPropertyPositionWidget::toString( const WPosition& value )
89 {
90  std::ostringstream o;
91  // we output it manually as the << opersator of WValue does not set a nice precision
92  o.precision( 5 );
93  o << "(" << value[0] << ", " << value[1] << ", " << value[2] << ")";
94  return o.str();
95 }
96 
97 std::string WPropertyPositionWidget::toString( double value )
98 {
99  std::ostringstream o;
100  o.precision( 5 );
101  o << value;
102  return o.str();
103 }
104 
106 {
107  // set the values
108  m_editX.setText( QString::fromStdString( toString( m_posProperty->get()[0] ) ) );
109  m_editY.setText( QString::fromStdString( toString( m_posProperty->get()[1] ) ) );
110  m_editZ.setText( QString::fromStdString( toString( m_posProperty->get()[2] ) ) );
111 
112  // do not forget to update the label
113  m_asText.setText( QString::fromStdString( toString( m_posProperty->get() ) ) );
114 }
115 
117 {
119 }
120 
121 void WPropertyPositionWidget::textEdited( const QString& /*text*/ )
122 {
123  setPropertyFromWidgets( true );
124 }
125 
127 {
128  // grab all the values
129  bool valid;
130  double valueX = m_editX.text().toDouble( &valid );
131  if( !valid )
132  {
133  invalidate();
134  return;
135  }
136  double valueY = m_editY.text().toDouble( &valid );
137  if( !valid )
138  {
139  invalidate();
140  return;
141  }
142  double valueZ = m_editZ.text().toDouble( &valid );
143  if( !valid )
144  {
145  invalidate();
146  return;
147  }
148 
149  // create a new position
150  WPosition p = WPosition( valueX, valueY, valueZ );
151 
152  // set/validate to the property
153  if( validateOnly )
154  {
155  invalidate( !m_posProperty->accept( p ) );
156  }
157  else
158  {
159  invalidate( !m_posProperty->set( p ) ); // NOTE: set automatically checks the validity of the value
160  }
161 }
162 
This only is a 3d double vector.
virtual void update()
Called whenever the widget should update.
std::string toString(const WPosition &value)
Helper function converting a position into a nice formatted string.
WScaleLabel m_asText
Used to show the property as text.
void setPropertyFromWidgets(bool validateOnly=false)
Updates the property using the edit X Y and Z widgets.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
void textEdited(const QString &text)
Called when the text in m_edit changes.
virtual ~WPropertyPositionWidget()
Destructor.
QLineEdit m_editY
The edit field showing the value of the slider.
WPropertyPositionWidget(WPropPosition property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
WPropPosition m_posProperty
The integer property represented by this widget.
QLineEdit m_editX
The edit field showing the value of the slider.
void editChanged()
Called whenever the edit field changes.
QLineEdit m_editZ
The edit field showing the value of the slider.
QHBoxLayout m_layout
Layout used to position the label and the widgets.
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