Commit 02f04ac5 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - now widgets immediately update validity status

parent 8102fd2b
......@@ -84,8 +84,12 @@ public:
* Sets the new value for this flag. Also notifies waiting threads.
*
* \param value the new value
*
* \return true if the value has been set successfully.
*
* \note set( get() ) == true
*/
virtual void set( T value );
virtual bool set( T value );
/**
* Sets the new value for this flag. Also notifies waiting threads.
......@@ -101,6 +105,16 @@ public:
*/
boost::shared_ptr< WCondition > getCondition();
/**
* Determines whether the specified value is acceptable. In WFlags, this always returns true. To modify the behaviour,
* implement this function in an appropriate way.
*
* \param newValue the new value.
*
* \return true if it is a valid/acceptable value.
*/
virtual bool accept( T newValue );
protected:
/**
......@@ -165,13 +179,24 @@ void WFlag< T >::operator()( T value )
}
template < typename T >
void WFlag< T >::set( T value )
bool WFlag< T >::set( T value )
{
// if the value is the same as the current one -> do not notify but let the caller know "all ok"
if ( m_flag == value )
return;
{
return true;
}
// let the caller know whether the value was acceptable.
if ( !accept( value ) )
{
return false;
}
m_flag = value;
m_condition->notify();
return true;
}
template < typename T >
......@@ -180,5 +205,12 @@ boost::shared_ptr< WCondition > WFlag< T >::getCondition()
return m_condition;
}
template < typename T >
bool WFlag< T >::accept( T /* newValue */ )
{
// please implement this method in your class to modify the behaviour.
return true;
}
#endif // WFLAG_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WPropertyConstraintNotEmpty.h"
#include "WPropertyVariable.h"
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPROPERTYCONSTRAINTNOTEMPTY_H
#define WPROPERTYCONSTRAINTNOTEMPTY_H
#include "WPropertyTypes.h"
#include "WPropertyConstraintTypes.h"
template< typename T >
class WPropertyVariable;
/**
* This class allows constraining properties to be not empty. This is especially useful for strings. This works on all types
* providing an empty() member function (as std::string and boost::filesystem::path do).
*/
template< typename T >
class WPropertyConstraintNotEmpty: public WPropertyVariable< T >::PropertyConstraint
{
public:
/**
* Constructor.
*/
explicit WPropertyConstraintNotEmpty();
/**
* Destructor.
*/
virtual ~WPropertyConstraintNotEmpty();
/**
* Checks whether the specified new value is larger or equal to the specified min value.
*
* \param property the property whose new value should be set.
* \param value the new value to check
*
* \return true if value >= m_min
*/
virtual bool accept( boost::shared_ptr< WPropertyVariable< T > > property, T value );
/**
* Allows simple identification of the real constraint type.
*
* \return the type
*/
virtual PROPERTYCONSTRAINT_TYPE getType();
private:
};
template < typename T >
WPropertyConstraintNotEmpty< T >::WPropertyConstraintNotEmpty()
{
}
template < typename T >
WPropertyConstraintNotEmpty< T >::~WPropertyConstraintNotEmpty()
{
}
template < typename T >
bool WPropertyConstraintNotEmpty< T >::accept( boost::shared_ptr< WPropertyVariable< T > > /* property */, T value )
{
return !value.empty();
}
template < typename T >
PROPERTYCONSTRAINT_TYPE WPropertyConstraintNotEmpty< T >::getType()
{
return PC_NOTEMPTY;
}
#endif // WPROPERTYCONSTRAINTNOTEMPTY_H
......@@ -32,7 +32,8 @@ typedef enum
{
PC_UNKNOWN, // type ID for arbitrary WPropertyConstraint
PC_MIN, // type ID for WPropertyConstraintMin
PC_MAX // type ID for WPropertyConstraintMax
PC_MAX, // type ID for WPropertyConstraintMax
PC_NOTEMPTY // type ID for WPropertyConstraintNotEmpty
} PROPERTYCONSTRAINT_TYPE;
#endif // WPROPERTYCONSTRAINTTYPES_H
......@@ -115,10 +115,8 @@ public:
* \param newValue the new value.
*
* \return true if it is a valid/acceptable value.
*
* \note this currently is a dummy.
*/
bool accept( T newValue );
virtual bool accept( T newValue );
/**
* Class building the base for user defined constraints on a property instance.
......@@ -323,7 +321,7 @@ bool WPropertyVariable< T >::accept( T newValue )
boost::shared_lock< boost::shared_mutex > lock = boost::shared_lock< boost::shared_mutex >( m_constraintsLock );
// iterate through the set
bool acceptable = true;
bool acceptable = WFlag< T >::accept( newValue );
for ( constraintIterator it = m_constraints.begin(); it != m_constraints.end(); ++it )
{
acceptable &= ( *it )->accept( boost::shared_static_cast< WPropertyVariable< T > >( shared_from_this() ), newValue );
......
......@@ -51,6 +51,6 @@ WPropertyBoolWidget::~WPropertyBoolWidget()
void WPropertyBoolWidget::changed()
{
// set the value
m_boolProperty->set( m_checkbox.isChecked() );
invalidate( !m_boolProperty->set( m_checkbox.isChecked() ) );
}
......@@ -93,15 +93,6 @@ WPropertyDoubleWidget::~WPropertyDoubleWidget()
void WPropertyDoubleWidget::spinChanged( double value )
{
// set to the property
if ( !m_doubleProperty->accept( value ) )
{
// this is not a valid value!
invalidate();
}
else
{
invalidate( false );
m_doubleProperty->set( value );
}
invalidate( !m_doubleProperty->set( value ) );
}
......@@ -103,6 +103,7 @@ WPropertyIntWidget::WPropertyIntWidget( WPropInt property, QGridLayout* property
// connect the modification signal of the edit and slider with our callback
connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
connect( &m_edit, SIGNAL( returnPressed() ), this, SLOT( editChanged() ) );
connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
}
WPropertyIntWidget::~WPropertyIntWidget()
......@@ -113,21 +114,10 @@ WPropertyIntWidget::~WPropertyIntWidget()
void WPropertyIntWidget::sliderChanged( int value )
{
// set the value in the line edit
std::ostringstream s;
s << value;
m_edit.setText( QString( s.str().c_str() ) );
m_edit.setText( QString( boost::lexical_cast< std::string >( value ).c_str() ) );
// set to the property
if ( !m_intProperty->accept( value ) )
{
// this is not a valid value!
invalidate();
}
else
{
invalidate( false );
m_intProperty->set( value );
}
invalidate( !m_intProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
}
void WPropertyIntWidget::editChanged()
......@@ -144,15 +134,22 @@ void WPropertyIntWidget::editChanged()
// update slider
m_slider.setValue( value );
// now: is the value acceptable by the property?
if ( !m_intProperty->accept( value ) )
// set to the property
invalidate( !m_intProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
}
void WPropertyIntWidget::textEdited( const QString& text )
{
// this method does NOT set the property actually, but tries to validate it
bool valid;
int value = m_edit.text().toInt( &valid );
if ( !valid )
{
invalidate();
return;
}
else
{
invalidate( false );
m_intProperty->set( value );
}
// simply check validity
invalidate( !m_intProperty->accept( value ) );
}
......@@ -92,6 +92,13 @@ public slots:
* Called whenever the edit field changes
*/
void editChanged();
/**
* Called when the text in m_edit changes.
*
* \param text
*/
void textEdited( const QString& text );
};
#endif // WPROPERTYINTWIDGET_H
......
......@@ -49,6 +49,7 @@ WPropertyStringWidget::WPropertyStringWidget( WPropString property, QGridLayout*
// connect the modification signal of the edit and slider with our callback
connect( &m_edit, SIGNAL( returnPressed() ), this, SLOT( editChanged() ) );
connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
}
WPropertyStringWidget::~WPropertyStringWidget()
......@@ -60,14 +61,13 @@ void WPropertyStringWidget::editChanged()
{
std::string value = m_edit.text().toStdString();
// now: is the value acceptable by the property?
if ( !m_stringProperty->accept( value ) )
{
invalidate();
}
else
{
invalidate( false );
m_stringProperty->set( value );
}
invalidate( !m_stringProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
}
void WPropertyStringWidget::textEdited( const QString& text )
{
// this method does NOT set the property actually, but tries to validate it
std::string value = m_edit.text().toStdString();
invalidate( !m_stringProperty->accept( value ) );
}
......@@ -80,6 +80,13 @@ public slots:
* Called whenever the edit field changes
*/
void editChanged();
/**
* Called when the text in m_edit changes.
*
* \param text
*/
void textEdited( const QString& text );
};
#endif // WPROPERTYSTRINGWIDGET_H
......
......@@ -47,6 +47,7 @@
#include "../data/WMData.h"
#include "WMNavSlices.h"
#include "navslices.xpm"
#include "../../common/WPropertyConstraintNotEmpty.h"
WMNavSlices::WMNavSlices():
WModule()
......@@ -147,6 +148,7 @@ void WMNavSlices::properties()
d->setMax( 10.2 );
WPropString s = m_properties2->addProperty( "hallo2", "Position of axial slice.", std::string( "hallo du" ) );
s->addConstraint( boost::shared_ptr< WPropertyConstraintNotEmpty< std::string > >(new WPropertyConstraintNotEmpty< std::string >() ) );
}
void WMNavSlices::notifyDataChange( boost::shared_ptr<WModuleConnector> input,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment