OpenWalnut  1.5.0dev
WPropertyTransferFunctionWidget.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 <cmath>
26 #include <sstream>
27 #include <string>
28 // #include <iostream>
29 
30 
31 #include "core/common/WLogger.h"
32 #include "core/common/WPropertyVariable.h"
33 #include "core/common/WTransferFunction.h"
34 #include "../WGuiConsts.h"
35 
36 #include "WPropertyTransferFunctionWidget.h"
37 
38 WPropertyTransferFunctionWidget::WPropertyTransferFunctionWidget( WPropTransferFunction property, QGridLayout* propertyGrid, QWidget* parent ):
39  WPropertyWidget( property, propertyGrid, parent ),
40  m_transferFunctionProperty( property ),
41  m_layout( &m_parameterWidgets ),
42  m_infoLayout( &m_informationWidgets ),
43  m_transferFunction( &m_parameterWidgets, this ),
44  modifying( false )
45 {
46  setAttribute( Qt::WA_DeleteOnClose );
47  m_layout.addWidget( &m_transferFunction );
48  m_layout.setMargin( WGLOBAL_MARGIN );
49  m_layout.setSpacing( WGLOBAL_SPACING );
50 
51  m_parameterWidgets.setLayout( &m_layout );
52 
53  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
54  m_infoLayout.setMargin( WGLOBAL_MARGIN );
55  m_infoLayout.setSpacing( WGLOBAL_SPACING );
56  m_informationWidgets.setLayout( &m_infoLayout );
57 
58  update();
59 
60  //connect the modification signal of the edit and slider with our callback
61  //connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
62  //connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
63  //connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
64 }
65 
67 {
68  // cleanup
69  // std::cout << "cleanup." << std::endl;
70 }
71 
72 namespace
73 {
74  QColor toQColor( const WColor &color )
75  {
76  QColor tmp;
77  tmp.setRgbF( color[0],
78  color[1],
79  color[2],
80  color[3] );
81 
82  return tmp;
83  }
84 }
85 
86 // this function is more complex than it should be because of the way
87 // the widget handles mouse input which interferes with adding points the same way
88 // because points are updated immediately and I don't know whether and how I can
89 // block the update routine while still inserting points into the scene
91 {
92  modifying = true;
93  // std::cout << "Widget update" << std::endl;
95 
96  // check whether the values changed. If they did, propagate to the GUI
97  if( tf != lastTransferFunction )
98  {
100  //m_transferFunction.externalDataChange( tf );
102 
103  // initialize the widget with the new transfer function
104  size_t nbColors = tf.numColors();
105  QColor c;
106  for( size_t i = 0; i< nbColors; ++i )
107  {
108  WColor cc = tf.getColor( i );
109  c = toQColor( cc );
110  double iso = tf.getColorIsovalue( i );
111  m_transferFunction.insertColorNormalized( QPointF( iso, 0 ), &c );
112  }
113  size_t nbAlphas = tf.numAlphas();
114 
115  // add first alpha first
116  if( nbAlphas > 0 )
117  {
118  double alpha = tf.getAlpha( 0 );
119  double iso = tf.getAlphaIsovalue( 0 );
120  m_transferFunction.insertPointNormalized( QPointF( iso, alpha ) );
121  }
122 
123  // add last alpha second to prevent the widget moving mid points to the right
124  if( nbAlphas > 1 )
125  {
126  double alpha = tf.getAlpha( nbAlphas-1 );
127  double iso = tf.getAlphaIsovalue( nbAlphas-1 );
128  m_transferFunction.insertPointNormalized( QPointF( iso, alpha ) );
129  }
130 
131  for( size_t i = 1; i< nbAlphas-1; ++i )
132  {
133  double alpha = tf.getAlpha( i );
134  double iso = tf.getAlphaIsovalue( i );
135  m_transferFunction.insertPointNormalized( QPointF( iso, alpha ) );
136  }
137  }
138  else
139  {
140  // std::cout << "update blocked because nothing changed" << std::endl;
141  }
142  modifying = false;
143 }
144 
146 {
147  // store old setup in lastTransferFunction because we have to avoid
148  // repainting because of conversion errors between the widget's storage in
149  // screen space and WTransferFunction's storage in normalized space
151  if( !modifying )
152  {
153  // std::cout << "guiUpdate(...)" << std::endl;
154  m_transferFunctionProperty->set( tf );
155  // std::cout << "end guiUpdate(...)" << std::endl;
156  }
157  else
158  {
159  // std::cout << "guiUpdate(...) blocked" << std::endl;
160  }
161 }
162 
virtual void update()
Called whenever the widget should update.
virtual void guiUpdate(const WTransferFunction &tf)
Called when the gui has new data.
bool modifying
internal synchronization flag
QHBoxLayout m_infoLayout
Used to show the property as text.
WTransferFunctionWidget m_transferFunction
the current transfer function
WTransferFunction lastTransferFunction
the previously used transfer function
WPropTransferFunction m_transferFunctionProperty
The integer property represented by this widget.
WPropertyTransferFunctionWidget(WPropTransferFunction property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
QHBoxLayout m_layout
The slider allowing modification of the integer value.
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.
void insertColorNormalized(const QPointF &pos, QColor const *const color=0)
Same as insertColor but in normalized coordinates, i.e., [ 0...1 ] along x.
void setHistogram(const std::vector< double > &histogram)
Called from external to set histogram.
void insertPointNormalized(const QPointF &position)
Same as insertPoint but in normalized coordinates and not in screen space.
void clearTransferFunction()
Remove all points from the transfer function widget to be able to insert new points.
A class that stores a 1D transfer function which consists of a linear interpolation of alpha and colo...
double getColorIsovalue(size_t i) const
The isovalue of the color at a given index.
const std::vector< double > & getHistogram() const
Returns the histogram.
size_t numAlphas() const
Get the number of alphas.
double getAlphaIsovalue(size_t i) const
Get the isovalue at a given index in the alpha values.
size_t numColors() const
Get the number of colors.
double getAlpha(size_t i) const
Get alpha at given index.
const WColor & getColor(size_t i) const
Get color at given index.