Commit d6739f57 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - added several new constraints

[ADD] - added filename property widget
parent 848b4312
//---------------------------------------------------------------------------
//
// 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 "WPropertyConstraintIsDirectory.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 WPROPERTYCONSTRAINTISDIRECTORY_H
#define WPROPERTYCONSTRAINTISDIRECTORY_H
#include "WPropertyTypes.h"
#include "WPropertyConstraintTypes.h"
template< typename T >
class WPropertyVariable;
/**
* This class allows constraining properties to be existing filenames. This is especially useful for WPropFilename.
*/
template < typename T >
class WPropertyConstraintIsDirectory: public WPropertyVariable< T >::PropertyConstraint
{
public:
/**
* Constructor.
*/
explicit WPropertyConstraintIsDirectory();
/**
* Destructor.
*/
virtual ~WPropertyConstraintIsDirectory();
/**
* Checks whether the specified value is a directory or not.
*
* \param property the property whose new value should be set.
* \param value the new value to check
*
* \return true if the file/path is a directory
*/
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 >
WPropertyConstraintIsDirectory< T >::WPropertyConstraintIsDirectory()
{
}
template < typename T >
WPropertyConstraintIsDirectory< T >::~WPropertyConstraintIsDirectory()
{
}
template < typename T >
bool WPropertyConstraintIsDirectory< T >::accept( boost::shared_ptr< WPropertyVariable< T > > /* property */, T value )
{
return boost::filesystem::is_directory( value );
}
template < typename T >
PROPERTYCONSTRAINT_TYPE WPropertyConstraintIsDirectory< T >::getType()
{
return PC_ISDIRECTORY;
}
#endif // WPROPERTYCONSTRAINTISDIRECTORY_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 "WPropertyConstraintPathExists.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 WPROPERTYCONSTRAINTPATHEXISTS_H
#define WPROPERTYCONSTRAINTPATHEXISTS_H
#include "WPropertyTypes.h"
#include "WPropertyConstraintTypes.h"
template< typename T >
class WPropertyVariable;
/**
* This class allows constraining properties to be existing filenames. This is especially useful for WPropFilename.
*/
template < typename T >
class WPropertyConstraintPathExists: public WPropertyVariable< T >::PropertyConstraint
{
public:
/**
* Constructor.
*/
explicit WPropertyConstraintPathExists();
/**
* Destructor.
*/
virtual ~WPropertyConstraintPathExists();
/**
* 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 the file/path exists
*/
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 >
WPropertyConstraintPathExists< T >::WPropertyConstraintPathExists()
{
}
template < typename T >
WPropertyConstraintPathExists< T >::~WPropertyConstraintPathExists()
{
}
template < typename T >
bool WPropertyConstraintPathExists< T >::accept( boost::shared_ptr< WPropertyVariable< T > > /* property */, T value )
{
return boost::filesystem::exists( value );
}
template < typename T >
PROPERTYCONSTRAINT_TYPE WPropertyConstraintPathExists< T >::getType()
{
return PC_PATHEXISTS;
}
#endif // WPROPERTYCONSTRAINTPATHEXISTS_H
......@@ -26,14 +26,17 @@
#define WPROPERTYCONSTRAINTTYPES_H
/**
* IDs for simple identification of the real constraint type
* IDs for simple identification of the real constraint type.
* \note If you modify this list, also adopt WPropertyVariable::WPropertyConstraint::create() in an appropriate way.
*/
typedef enum
{
PC_UNKNOWN, // type ID for arbitrary WPropertyConstraint
PC_MIN, // type ID for WPropertyConstraintMin
PC_MAX, // type ID for WPropertyConstraintMax
PC_NOTEMPTY // type ID for WPropertyConstraintNotEmpty
PC_NOTEMPTY, // type ID for WPropertyConstraintNotEmpty
PC_PATHEXISTS, // type ID for WPropertyConstraintPathExists
PC_ISDIRECTORY // type ID for WPropertyConstraintIsDirectory
}
PROPERTYCONSTRAINT_TYPE;
......
......@@ -36,11 +36,18 @@
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include "WLogger.h"
#include "WFlag.h"
#include "WPropertyBase.h"
#include "WPropertyConstraintTypes.h"
#include "WPropertyConstraintMin.h"
#include "WPropertyConstraintMax.h"
#include "WPropertyConstraintNotEmpty.h"
#include "WPropertyConstraintPathExists.h"
#include "WPropertyConstraintIsDirectory.h"
#include "WCondition.h"
/**
......@@ -151,6 +158,16 @@ public:
* \return the type
*/
virtual PROPERTYCONSTRAINT_TYPE getType();
/**
* This method creates a constraint using the specified type. This is a useful convenience class for easily adding
* constraints.
*
* \param type the type of the constraint to create
*
* \return NULL if the type is unknown or an constraint instance
*/
static boost::shared_ptr< PropertyConstraint > create( PROPERTYCONSTRAINT_TYPE type );
};
/**
......@@ -221,6 +238,24 @@ public:
*/
PropertyConstraintMax getMax();
/**
* Method searching the first appearance of a constrained with the specified type.
*
* \param type the type of the searched constraint
*
* \return the constraint, or NULL if none.
*/
boost::shared_ptr< PropertyConstraint > getFirstConstraint( PROPERTYCONSTRAINT_TYPE type );
/**
* Method searching the first appearance of a constrained with the specified type.
*
* \param type the type of the searched constraint
*
* \return the constraint, or NULL if none.
*/
int countConstraint( PROPERTYCONSTRAINT_TYPE type );
protected:
/**
......@@ -239,7 +274,7 @@ protected:
std::set< boost::shared_ptr< PropertyConstraint > > m_constraints;
/**
* The iterator used togo through the set
* The iterator used to go through the set
*/
typedef typename std::set< boost::shared_ptr< PropertyConstraint > >::const_iterator constraintIterator;
......@@ -256,15 +291,6 @@ protected:
*/
void removeConstraints( PROPERTYCONSTRAINT_TYPE type );
/**
* Method searching the first appearance of a constrained with the specified type.
*
* \param type the type of the searched constraint
*
* \return the constraint, or NULL if none.
*/
boost::shared_ptr< PropertyConstraint > getFirstConstraint( PROPERTYCONSTRAINT_TYPE type );
private:
};
......@@ -400,6 +426,26 @@ WPropertyVariable< T >::getFirstConstraint( PROPERTYCONSTRAINT_TYPE type )
return boost::shared_ptr< PropertyConstraint >();
}
template < typename T >
int WPropertyVariable< T >::countConstraint( PROPERTYCONSTRAINT_TYPE type )
{
// lock
boost::shared_lock< boost::shared_mutex > lock = boost::shared_lock< boost::shared_mutex >( m_constraintsLock );
int i = 0;
// search first appearance of a constraint of the specified type
for ( constraintIterator it = m_constraints.begin(); it != m_constraints.end(); ++it )
{
if ( ( *it )->getType() == type )
{
i++;
}
}
lock.unlock();
return i;
}
template < typename T >
boost::shared_ptr< WPropertyConstraintMin< T > > WPropertyVariable< T >::getMin()
{
......@@ -460,5 +506,38 @@ PROPERTYCONSTRAINT_TYPE WPropertyVariable< T >::PropertyConstraint::getType()
return PC_UNKNOWN;
}
template < typename T >
boost::shared_ptr< typename WPropertyVariable< T >::PropertyConstraint >
WPropertyVariable< T >::PropertyConstraint::create( PROPERTYCONSTRAINT_TYPE type )
{
WPropertyVariable< T >::PropertyConstraint* c = NULL;
// simply create a new instance for all those constraints.
switch( type )
{
case PC_MIN: // min and max constraints can't be created this way since they need a construction parameter
WLogger::getLogger()->addLogMessage( "Minimum property constraints can't be created this way. Use setMin() instead.",
"PropertyConstraint::create", LL_WARNING );
break;
case PC_MAX:
WLogger::getLogger()->addLogMessage( "Maximum property constraints can't be created this way. Use setMax() instead.",
"PropertyConstraint::create", LL_WARNING );
break;
case PC_NOTEMPTY:
c = new WPropertyConstraintNotEmpty< T >();
break;
case PC_PATHEXISTS:
c = new WPropertyConstraintPathExists< T >();
break;
case PC_ISDIRECTORY:
c = new WPropertyConstraintIsDirectory< T >();
break;
default:
WLogger::getLogger()->addLogMessage( "The property constraint is unknown.", "PropertyConstraint::create", LL_WARNING );
break;
}
return boost::shared_ptr< WPropertyVariable< T >::PropertyConstraint >( c );
}
#endif // WPROPERTYVARIABLE_H
......@@ -21,6 +21,7 @@ SET(GUI_QT4_MOC_HDRS
datasetbrowser/WPropertyDoubleWidget.h
datasetbrowser/WPropertyStringWidget.h
datasetbrowser/WPropertyColorWidget.h
datasetbrowser/WPropertyFilenameWidget.h
datasetbrowser/WQtDatasetBrowser.h
datasetbrowser/WQtDSBWidget.h
datasetbrowser/WQtLineEdit.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 <cmath>
#include <string>
#include <boost/lexical_cast.hpp>
#include <QtGui/QFileDialog>
#include "../../../common/WLogger.h"
#include "../../../common/WPropertyVariable.h"
#include "../../../common/WPropertyConstraintTypes.h"
#include "WPropertyFilenameWidget.h"
WPropertyFilenameWidget::WPropertyFilenameWidget( WPropFilename property, QGridLayout* propertyGrid, QWidget* parent ):
WPropertyWidget( property, propertyGrid, parent ),
m_fnProperty( property ),
m_button( this ),
m_layout()
{
// initialize members
setLayout( &m_layout );
// layout both against each other
m_layout.addWidget( &m_button );
// this prevents the button from resizing when a long path is selected
m_button.setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
// set the initial values
m_button.setText( QString::fromStdString( m_fnProperty->get().file_string() ) );
// connect the modification signal of the edit and slider with our callback
connect( &m_button, SIGNAL( released() ), this, SLOT( buttonReleased() ) );
}
WPropertyFilenameWidget::~WPropertyFilenameWidget()
{
// cleanup
}
void WPropertyFilenameWidget::buttonReleased()
{
QString path;
// if there is a "IsDirectory" constraint -> set a special option in the dialog
if ( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 )
{
// OK there should only be directories allowed
path = QFileDialog::getExistingDirectory( this,
QString::fromStdString( "Select directory for " + m_fnProperty->getName() ),
QString::fromStdString( m_fnProperty->get().file_string() ),
QFileDialog::DontConfirmOverwrite );
}
else
{
path = QFileDialog::getOpenFileName( this,
QString::fromStdString( "Select file for " + m_fnProperty->getName() ),
QString::fromStdString( m_fnProperty->get().file_string() ), QString(), 0,
QFileDialog::DontConfirmOverwrite );
}
// convert it back to a WColor
invalidate( !m_fnProperty->set( boost::filesystem::path( path.toStdString() ) ) ); // NOTE: set automatically checks the validity of the value
// set button
m_button.setText( path );
}
//---------------------------------------------------------------------------
//
// 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 WPROPERTYFILENAMEWIDGET_H
#define WPROPERTYFILENAMEWIDGET_H
#include <string>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include "WPropertyWidget.h"
/**
* Implements a property widget for WPropColor.
*/
class WPropertyFilenameWidget: public WPropertyWidget
{
Q_OBJECT
public:
/**
* Constructor. Creates a new widget appropriate for the specified property.
*
* \param property the property to handle
* \param parent the parent widget.
* \param propertyGrid the grid used to layout the labels and property widgets
*/
WPropertyFilenameWidget( WPropFilename property, QGridLayout* propertyGrid, QWidget* parent = 0 );
/**
* Destructor.
*/
virtual ~WPropertyFilenameWidget();
protected:
/**
* The filename property represented by this widget.
*/
WPropFilename m_fnProperty;
/**
* The button field showing the value
*/
QPushButton m_button;
/**
* Layout used to position the label and the checkbox
*/
QHBoxLayout m_layout;
private:
public slots:
/**
* Called when the m_button was pressed.
*/
void buttonReleased();
};
#endif // WPROPERTYFILENAMEWIDGET_H
......@@ -144,7 +144,7 @@ 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 );
int value = text.toInt( &valid );
if ( !valid )
{
invalidate();
......
......@@ -67,7 +67,7 @@ void WPropertyStringWidget::editChanged()
void WPropertyStringWidget::textEdited( const QString& text )
{